blob: d1376accca9002a047772f66569b2b65ad47aba4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000046/// [GNU] asm-statement
47/// [OMP] openmp-construct [TODO]
48///
49/// labeled-statement:
50/// identifier ':' statement
51/// 'case' constant-expression ':' statement
52/// 'default' ':' statement
53///
54/// selection-statement:
55/// if-statement
56/// switch-statement
57///
58/// iteration-statement:
59/// while-statement
60/// do-statement
61/// for-statement
62///
63/// expression-statement:
64/// expression[opt] ';'
65///
66/// jump-statement:
67/// 'goto' identifier ';'
68/// 'continue' ';'
69/// 'break' ';'
70/// 'return' expression[opt] ';'
71/// [GNU] 'goto' '*' expression ';'
72///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000075/// [OBC] '@' 'throw' ';'
76///
John McCall60d7b3a2010-08-24 06:29:42 +000077StmtResult
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +000078Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000079 const char *SemiError = 0;
John McCall60d7b3a2010-08-24 06:29:42 +000080 StmtResult Res;
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000081
82 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000083
John McCall7f040a92010-12-24 02:08:15 +000084 ParsedAttributesWithRange attrs;
85 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 // Cases in this switch statement should fall through if the parser expects
88 // the token to end in a semicolon (in which case SemiError should be set),
89 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000090 tok::TokenKind Kind = Tok.getKind();
91 SourceLocation AtLoc;
92 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000093 case tok::at: // May be a @try or @throw statement
94 {
95 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000096 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000097 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000098
Douglas Gregor791215b2009-09-21 20:51:25 +000099 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000100 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorc8bddde2010-05-28 00:22:41 +0000101 ConsumeCodeCompletionToken();
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000102 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor791215b2009-09-21 20:51:25 +0000103
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000104 case tok::identifier:
105 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
106 // identifier ':' statement
John McCall7f040a92010-12-24 02:08:15 +0000107 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000108 }
109 // PASS THROUGH.
110
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000111 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000112 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000113 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000114 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall7f040a92010-12-24 02:08:15 +0000115 DeclEnd, attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000116 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000117 }
118
119 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000121 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Sean Huntbbd37c62009-11-21 08:43:09 +0000124 // FIXME: Use the attributes
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000125 // expression[opt] ';'
John McCall60d7b3a2010-08-24 06:29:42 +0000126 ExprResult Expr(ParseExpression());
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000127 if (Expr.isInvalid()) {
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000128 // If the expression is invalid, skip ahead to the next semicolon or '}'.
129 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000130 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000131 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
132 if (Tok.is(tok::semi))
133 ConsumeToken();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000134 return StmtError();
135 }
136 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000137 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000138 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000139 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000142 return ParseCaseStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000144 return ParseDefaultStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall7f040a92010-12-24 02:08:15 +0000147 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000148 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
149 bool LeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
150 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacro);
151 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall7f040a92010-12-24 02:08:15 +0000154 return ParseIfStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall7f040a92010-12-24 02:08:15 +0000156 return ParseSwitchStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall7f040a92010-12-24 02:08:15 +0000159 return ParseWhileStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000161 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000162 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 break;
164 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall7f040a92010-12-24 02:08:15 +0000165 return ParseForStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
167 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000168 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000169 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 break;
171 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000172 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000173 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 break;
175 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000176 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000177 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 break;
179 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000180 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000181 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000183
Sebastian Redla0fd8652008-12-21 16:41:36 +0000184 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000185 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000186 bool msAsm = false;
187 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000188 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000189 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000190 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 break;
192 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000193
Sebastian Redla0fd8652008-12-21 16:41:36 +0000194 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000195 return ParseCXXTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000196 }
197
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000199 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000201 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000202 // If the result was valid, then we do want to diagnose this. Use
203 // ExpectAndConsume to emit the diagnostic, even though we know it won't
204 // succeed.
205 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000206 // Skip until we see a } or ;, but don't eat it.
207 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Sebastian Redl61364dd2008-12-11 19:30:53 +0000210 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000213/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000214///
215/// labeled-statement:
216/// identifier ':' statement
217/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000218///
John McCall7f040a92010-12-24 02:08:15 +0000219StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000220 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
221 "Not an identifier!");
222
223 Token IdentTok = Tok; // Save the whole token.
224 ConsumeToken(); // eat the identifier.
225
226 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000227
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000228 // identifier ':' statement
229 SourceLocation ColonLoc = ConsumeToken();
230
231 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000232 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000233
John McCall60d7b3a2010-08-24 06:29:42 +0000234 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000235
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000236 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000237 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000238 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner337e5502011-02-18 01:27:55 +0000239
240 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
241 IdentTok.getLocation());
242 if (AttributeList *Attrs = attrs.getList())
243 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
244
245 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
246 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000247}
Reid Spencer5f016e22007-07-11 17:01:13 +0000248
249/// ParseCaseStatement
250/// labeled-statement:
251/// 'case' constant-expression ':' statement
252/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
253///
John McCall7f040a92010-12-24 02:08:15 +0000254StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000255 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000256 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner24e1e702009-03-04 04:23:07 +0000258 // It is very very common for code to contain many case statements recursively
259 // nested, as in (but usually without indentation):
260 // case 1:
261 // case 2:
262 // case 3:
263 // case 4:
264 // case 5: etc.
265 //
266 // Parsing this naively works, but is both inefficient and can cause us to run
267 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000268 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000269 // but all the grossness is constrained to ParseCaseStatement (and some
270 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner24e1e702009-03-04 04:23:07 +0000272 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
273 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000274 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner24e1e702009-03-04 04:23:07 +0000276 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
277 // gets updated each time a new case is parsed, and whose body is unset so
278 // far. When parsing 'case 4', this is the 'case 3' node.
279 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattner24e1e702009-03-04 04:23:07 +0000281 // While we have case statements, eat and stack them.
282 do {
283 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000285 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000286 Actions.CodeCompleteCase(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000287 ConsumeCodeCompletionToken();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000288 }
289
Chris Lattner6fb09c82009-12-10 00:38:54 +0000290 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
291 /// Disable this form of error recovery while we're parsing the case
292 /// expression.
293 ColonProtectionRAIIObject ColonProtection(*this);
294
John McCall60d7b3a2010-08-24 06:29:42 +0000295 ExprResult LHS(ParseConstantExpression());
Chris Lattner24e1e702009-03-04 04:23:07 +0000296 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000298 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000300
Chris Lattner24e1e702009-03-04 04:23:07 +0000301 // GNU case range extension.
302 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000303 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000304 if (Tok.is(tok::ellipsis)) {
305 Diag(Tok, diag::ext_gnu_case_range);
306 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000307
Chris Lattner24e1e702009-03-04 04:23:07 +0000308 RHS = ParseConstantExpression();
309 if (RHS.isInvalid()) {
310 SkipUntil(tok::colon);
311 return StmtError();
312 }
313 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000314
315 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000316
Douglas Gregor662a4822010-12-23 22:56:40 +0000317 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000318 if (Tok.is(tok::colon)) {
319 ColonLoc = ConsumeToken();
320
321 // Treat "case blah;" as a typo for "case blah:".
322 } else if (Tok.is(tok::semi)) {
323 ColonLoc = ConsumeToken();
324 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
325 << FixItHint::CreateReplacement(ColonLoc, ":");
326 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000327 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
328 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
329 << FixItHint::CreateInsertion(ExpectedLoc, ":");
330 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000331 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000332
John McCall60d7b3a2010-08-24 06:29:42 +0000333 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000334 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
335 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner24e1e702009-03-04 04:23:07 +0000337 // If we had a sema error parsing this case, then just ignore it and
338 // continue parsing the sub-stmt.
339 if (Case.isInvalid()) {
340 if (TopLevelCase.isInvalid()) // No parsed case stmts.
341 return ParseStatement();
342 // Otherwise, just don't add it as a nested case.
343 } else {
344 // If this is the first case statement we parsed, it becomes TopLevelCase.
345 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000346 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000347 if (TopLevelCase.isInvalid())
348 TopLevelCase = move(Case);
349 else
John McCall9ae2f072010-08-23 23:25:46 +0000350 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000351 DeepestParsedCaseStmt = NextDeepest;
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattner24e1e702009-03-04 04:23:07 +0000354 // Handle all case statements.
355 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner24e1e702009-03-04 04:23:07 +0000357 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner24e1e702009-03-04 04:23:07 +0000359 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000360 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattner24e1e702009-03-04 04:23:07 +0000362 if (Tok.isNot(tok::r_brace)) {
363 SubStmt = ParseStatement();
364 } else {
365 // Nicely diagnose the common error "switch (X) { case 4: }", which is
366 // not valid.
367 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000369 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner24e1e702009-03-04 04:23:07 +0000372 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000373 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000374 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattner24e1e702009-03-04 04:23:07 +0000376 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000377 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000378
Chris Lattner24e1e702009-03-04 04:23:07 +0000379 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000380 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381}
382
383/// ParseDefaultStatement
384/// labeled-statement:
385/// 'default' ':' statement
386/// Note that this does not parse the 'statement' at the end.
387///
John McCall7f040a92010-12-24 02:08:15 +0000388StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000389 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000390
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000391 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
393
Douglas Gregor662a4822010-12-23 22:56:40 +0000394 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000395 if (Tok.is(tok::colon)) {
396 ColonLoc = ConsumeToken();
397
398 // Treat "default;" as a typo for "default:".
399 } else if (Tok.is(tok::semi)) {
400 ColonLoc = ConsumeToken();
401 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
402 << FixItHint::CreateReplacement(ColonLoc, ":");
403 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000404 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
405 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
406 << FixItHint::CreateInsertion(ExpectedLoc, ":");
407 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000411 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000413 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 }
415
John McCall60d7b3a2010-08-24 06:29:42 +0000416 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000417 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000418 return StmtError();
419
Sebastian Redl117054a2008-12-28 16:13:43 +0000420 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000421 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000422}
423
424
425/// ParseCompoundStatement - Parse a "{}" block.
426///
427/// compound-statement: [C99 6.8.2]
428/// { block-item-list[opt] }
429/// [GNU] { label-declarations block-item-list } [TODO]
430///
431/// block-item-list:
432/// block-item
433/// block-item-list block-item
434///
435/// block-item:
436/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000437/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000438/// statement
439/// [OMP] openmp-directive [TODO]
440///
441/// [GNU] label-declarations:
442/// [GNU] label-declaration
443/// [GNU] label-declarations label-declaration
444///
445/// [GNU] label-declaration:
446/// [GNU] '__label__' identifier-list ';'
447///
448/// [OMP] openmp-directive: [TODO]
449/// [OMP] barrier-directive
450/// [OMP] flush-directive
451///
John McCall7f040a92010-12-24 02:08:15 +0000452StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Sean Huntbbd37c62009-11-21 08:43:09 +0000453 bool isStmtExpr) {
454 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000455
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000456 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000457
Chris Lattner31e05722007-08-26 06:24:45 +0000458 // Enter a scope to hold everything within the compound stmt. Compound
459 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000460 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000461
462 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000463 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000464}
465
466
467/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000468/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000469/// consume the '}' at the end of the block. It does not manipulate the scope
470/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000471StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000472 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000473 Tok.getLocation(),
474 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000475 InMessageExpressionRAIIObject InMessage(*this, false);
476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
478
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000479 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000480
Chris Lattner4ae493c2011-02-18 02:08:43 +0000481 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
482 // only allowed at the start of a compound stmt regardless of the language.
483 while (Tok.is(tok::kw___label__)) {
484 SourceLocation LabelLoc = ConsumeToken();
485 Diag(LabelLoc, diag::ext_gnu_local_label);
486
487 llvm::SmallVector<Decl *, 8> DeclsInGroup;
488 while (1) {
489 if (Tok.isNot(tok::identifier)) {
490 Diag(Tok, diag::err_expected_ident);
491 break;
492 }
493
494 IdentifierInfo *II = Tok.getIdentifierInfo();
495 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000496 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner4ae493c2011-02-18 02:08:43 +0000497
498 if (!Tok.is(tok::comma))
499 break;
500 ConsumeToken();
501 }
502
503 DeclSpec DS;
504 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
505 DeclsInGroup.data(), DeclsInGroup.size());
506 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
507
508 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
509 if (R.isUsable())
510 Stmts.push_back(R.release());
511 }
512
513 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000514 if (Tok.is(tok::annot_pragma_unused)) {
515 HandlePragmaUnused();
516 continue;
517 }
518
John McCall60d7b3a2010-08-24 06:29:42 +0000519 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000520 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000521 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000522 } else {
523 // __extension__ can start declarations and it can also be a unary
524 // operator for expressions. Consume multiple __extension__ markers here
525 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000526 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000527 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000528 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000529 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000530
John McCall7f040a92010-12-24 02:08:15 +0000531 ParsedAttributesWithRange attrs;
532 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000533
Chris Lattner45a566c2007-08-27 01:01:57 +0000534 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000535 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000536 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000537 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000538 ExtensionRAIIObject O(Diags);
539
Chris Lattner97144fc2009-04-02 04:16:50 +0000540 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000541 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
542 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000543 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000544 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000545 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000546 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000547 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000548
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000549 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000550 SkipUntil(tok::semi);
551 continue;
552 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000553
Sean Huntbbd37c62009-11-21 08:43:09 +0000554 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000555 // Eat the semicolon at the end of stmt and convert the expr into a
556 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000557 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000558 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000559 }
560 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000561
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000562 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000563 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000567 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 Diag(Tok, diag::err_expected_rbrace);
Chris Lattnerf65086b2010-09-01 15:49:26 +0000569 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000570 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000572
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000574 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000575 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000576}
577
Chris Lattner15ff1112008-12-12 06:31:07 +0000578/// ParseParenExprOrCondition:
579/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000580/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000581///
582/// This function parses and performs error recovery on the specified condition
583/// or expression (depending on whether we're in C++ or C mode). This function
584/// goes out of its way to recover well. It returns true if there was a parser
585/// error (the right paren couldn't be found), which indicates that the caller
586/// should try to recover harder. It returns false if the condition is
587/// successfully parsed. Note that a successful parse can still have semantic
588/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000589bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000590 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000591 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000592 bool ConvertToBoolean) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000593 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000594 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000595 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000596 else {
597 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000598 DeclResult = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000599
600 // If required, convert to a boolean value.
601 if (!ExprResult.isInvalid() && ConvertToBoolean)
602 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000603 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Chris Lattner15ff1112008-12-12 06:31:07 +0000606 // If the parser was confused by the condition and we don't have a ')', try to
607 // recover by skipping ahead to a semi and bailing out. If condexp is
608 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000609 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000610 SkipUntil(tok::semi);
611 // Skipping may have stopped if it found the containing ')'. If so, we can
612 // continue parsing the if statement.
613 if (Tok.isNot(tok::r_paren))
614 return true;
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Chris Lattner15ff1112008-12-12 06:31:07 +0000617 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000618 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000619 return false;
620}
621
622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623/// ParseIfStatement
624/// if-statement: [C99 6.8.4.1]
625/// 'if' '(' expression ')' statement
626/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000627/// [C++] 'if' '(' condition ')' statement
628/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000629///
John McCall7f040a92010-12-24 02:08:15 +0000630StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000631 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000632
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000633 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
635
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000636 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000637 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000639 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000641
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000642 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
643
Chris Lattner22153252007-08-26 23:08:06 +0000644 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
645 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000646 //
647 // C++ 6.4p3:
648 // A name introduced by a declaration in a condition is in scope from its
649 // point of declaration until the end of the substatements controlled by the
650 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000651 // C++ 3.3.2p4:
652 // Names declared in the for-init-statement, and in the condition of if,
653 // while, for, and switch statements are local to the if, while, for, or
654 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000655 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000656 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000659 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000660 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000661 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000662 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000663
John McCall9ae2f072010-08-23 23:25:46 +0000664 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Chris Lattner0ecea032007-08-22 05:28:50 +0000666 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000667 // there is no compound stmt. C90 does not have this clause. We only do this
668 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000669 //
670 // C++ 6.4p1:
671 // The substatement in a selection-statement (each substatement, in the else
672 // form of the if statement) implicitly defines a local scope.
673 //
674 // For C++ we create a scope for the condition and a new scope for
675 // substatements because:
676 // -When the 'then' scope exits, we want the condition declaration to still be
677 // active for the 'else' scope too.
678 // -Sema will detect name clashes by considering declarations of a
679 // 'ControlScope' as part of its direct subscope.
680 // -If we wanted the condition and substatement to be in the same scope, we
681 // would have to notify ParseStatement not to create a new scope. It's
682 // simpler to let it create a new scope.
683 //
Mike Stump1eb44332009-09-09 15:08:12 +0000684 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000685 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000686
Chris Lattnerb96728d2007-10-29 05:08:52 +0000687 // Read the 'then' stmt.
688 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000689 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
Chris Lattnera36ce712007-08-22 05:16:28 +0000691 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000692 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // If it has an else, parse it.
695 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000696 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000697 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000698
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000699 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000701 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000702
Chris Lattner0ecea032007-08-22 05:28:50 +0000703 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000704 // there is no compound stmt. C90 does not have this clause. We only do
705 // this if the body isn't a compound statement to avoid push/pop in common
706 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000707 //
708 // C++ 6.4p1:
709 // The substatement in a selection-statement (each substatement, in the else
710 // form of the if statement) implicitly defines a local scope.
711 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000712 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000713 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000714
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000716
Chris Lattnera36ce712007-08-22 05:16:28 +0000717 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000718 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000720
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000721 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattner18914bc2008-12-12 06:19:11 +0000723 // If the condition was invalid, discard the if statement. We could recover
724 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000725 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000726 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000727
Chris Lattnerb96728d2007-10-29 05:08:52 +0000728 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000729 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000730 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000731 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
732 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
733 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000734 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000735 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000736 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000737
Chris Lattnerb96728d2007-10-29 05:08:52 +0000738 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000739 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000740 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000741 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000742 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000743
John McCall9ae2f072010-08-23 23:25:46 +0000744 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000745 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000746}
747
748/// ParseSwitchStatement
749/// switch-statement:
750/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000751/// [C++] 'switch' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000752StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000753 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000754
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000755 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
757
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000758 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000759 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000761 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 }
Chris Lattner22153252007-08-26 23:08:06 +0000763
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000764 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
765
Chris Lattner22153252007-08-26 23:08:06 +0000766 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
767 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000768 //
769 // C++ 6.4p3:
770 // A name introduced by a declaration in a condition is in scope from its
771 // point of declaration until the end of the substatements controlled by the
772 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000773 // C++ 3.3.2p4:
774 // Names declared in the for-init-statement, and in the condition of if,
775 // while, for, and switch statements are local to the if, while, for, or
776 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000777 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000778 unsigned ScopeFlags = Scope::BreakScope;
779 if (C99orCXX)
780 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000781 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000782
783 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000784 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000785 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000786 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +0000787 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000788
John McCall60d7b3a2010-08-24 06:29:42 +0000789 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +0000790 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000791
Douglas Gregor586596f2010-05-06 17:25:47 +0000792 if (Switch.isInvalid()) {
793 // Skip the switch body.
794 // FIXME: This is not optimal recovery, but parsing the body is more
795 // dangerous due to the presence of case and default statements, which
796 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +0000797 if (Tok.is(tok::l_brace)) {
798 ConsumeBrace();
799 SkipUntil(tok::r_brace, false, false);
800 } else
Douglas Gregor586596f2010-05-06 17:25:47 +0000801 SkipUntil(tok::semi);
802 return move(Switch);
803 }
804
Chris Lattner0ecea032007-08-22 05:28:50 +0000805 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000806 // there is no compound stmt. C90 does not have this clause. We only do this
807 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000808 //
809 // C++ 6.4p1:
810 // The substatement in a selection-statement (each substatement, in the else
811 // form of the if statement) implicitly defines a local scope.
812 //
813 // See comments in ParseIfStatement for why we create a scope for the
814 // condition and a new scope for substatement in C++.
815 //
Mike Stump1eb44332009-09-09 15:08:12 +0000816 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000817 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000818
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000820 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000821
Chris Lattner7e52de42010-01-24 01:50:29 +0000822 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000823 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000824 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000825
Chris Lattner7e52de42010-01-24 01:50:29 +0000826 if (Body.isInvalid())
827 // FIXME: Remove the case statement list from the Switch statement.
828 Body = Actions.ActOnNullStmt(Tok.getLocation());
829
John McCall9ae2f072010-08-23 23:25:46 +0000830 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000831}
832
833/// ParseWhileStatement
834/// while-statement: [C99 6.8.5.1]
835/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000836/// [C++] 'while' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000837StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000838 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000839
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000840 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 SourceLocation WhileLoc = Tok.getLocation();
842 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000843
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000844 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000845 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000847 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000849
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000850 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
851
Chris Lattner22153252007-08-26 23:08:06 +0000852 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
853 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000854 //
855 // C++ 6.4p3:
856 // A name introduced by a declaration in a condition is in scope from its
857 // point of declaration until the end of the substatements controlled by the
858 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000859 // C++ 3.3.2p4:
860 // Names declared in the for-init-statement, and in the condition of if,
861 // while, for, and switch statements are local to the if, while, for, or
862 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000863 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000864 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000865 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000866 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
867 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000868 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000869 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
870 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000871
872 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000873 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000874 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000875 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000876 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000877
John McCall9ae2f072010-08-23 23:25:46 +0000878 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Chris Lattner0ecea032007-08-22 05:28:50 +0000880 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000881 // there is no compound stmt. C90 does not have this clause. We only do this
882 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000883 //
884 // C++ 6.5p2:
885 // The substatement in an iteration-statement implicitly defines a local scope
886 // which is entered and exited each time through the loop.
887 //
888 // See comments in ParseIfStatement for why we create a scope for the
889 // condition and a new scope for substatement in C++.
890 //
Mike Stump1eb44332009-09-09 15:08:12 +0000891 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000892 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000893
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000895 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000896
Chris Lattner0ecea032007-08-22 05:28:50 +0000897 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000898 InnerScope.Exit();
899 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000900
John McCalld226f652010-08-21 09:40:31 +0000901 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000902 return StmtError();
903
John McCall9ae2f072010-08-23 23:25:46 +0000904 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000905}
906
907/// ParseDoStatement
908/// do-statement: [C99 6.8.5.2]
909/// 'do' statement 'while' '(' expression ')' ';'
910/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +0000911StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000912 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000913
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000914 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000916
Chris Lattner22153252007-08-26 23:08:06 +0000917 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
918 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000919 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000920 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000921 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000922 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000923 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000924
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000925 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000926
Chris Lattner0ecea032007-08-22 05:28:50 +0000927 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000928 // there is no compound stmt. C90 does not have this clause. We only do this
929 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000930 //
931 // C++ 6.5p2:
932 // The substatement in an iteration-statement implicitly defines a local scope
933 // which is entered and exited each time through the loop.
934 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000935 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000936 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000937 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000938
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000940 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000941
Chris Lattner0ecea032007-08-22 05:28:50 +0000942 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000943 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000944
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000945 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000946 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000947 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000948 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000949 SkipUntil(tok::semi, false, true);
950 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000951 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 }
953 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000954
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000955 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000956 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000957 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000958 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000960
Chris Lattnerff871fb2008-12-12 06:35:28 +0000961 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000962 SourceLocation LPLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +0000963 ExprResult Cond = ParseExpression();
Douglas Gregor04895d32009-11-24 21:34:32 +0000964 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000965 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000966
Sebastian Redl9a920342008-12-11 19:48:14 +0000967 if (Cond.isInvalid() || Body.isInvalid())
968 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000969
John McCall9ae2f072010-08-23 23:25:46 +0000970 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
971 Cond.get(), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000972}
973
974/// ParseForStatement
975/// for-statement: [C99 6.8.5.3]
976/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
977/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000978/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
979/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000980/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
981/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000982///
983/// [C++] for-init-statement:
984/// [C++] expression-statement
985/// [C++] simple-declaration
986///
John McCall7f040a92010-12-24 02:08:15 +0000987StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000988 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000989
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000990 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000992
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000993 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000994 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000996 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000998
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000999 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001000
Chris Lattner22153252007-08-26 23:08:06 +00001001 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1002 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001003 //
1004 // C++ 6.4p3:
1005 // A name introduced by a declaration in a condition is in scope from its
1006 // point of declaration until the end of the substatements controlled by the
1007 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001008 // C++ 3.3.2p4:
1009 // Names declared in the for-init-statement, and in the condition of if,
1010 // while, for, and switch statements are local to the if, while, for, or
1011 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001012 // C++ 6.5.3p1:
1013 // Names declared in the for-init-statement are in the same declarative-region
1014 // as those declared in the condition.
1015 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001016 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001017 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001018 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1019 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001020 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001021 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1022
1023 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001024
1025 SourceLocation LParenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001026 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001027
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001028 bool ForEach = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001029 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001030 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001031 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001032 ExprResult Collection;
Douglas Gregor586596f2010-05-06 17:25:47 +00001033 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001034 Decl *SecondVar = 0;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001035
Douglas Gregor791215b2009-09-21 20:51:25 +00001036 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001037 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001038 C99orCXXorObjC? Sema::PCC_ForInit
1039 : Sema::PCC_Expression);
Douglas Gregordc845342010-05-25 05:58:43 +00001040 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +00001041 }
1042
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001044 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 // no first part, eat the ';'.
1046 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001047 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001049 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001051
John McCall7f040a92010-12-24 02:08:15 +00001052 ParsedAttributesWithRange attrs;
1053 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001054
Chris Lattner97144fc2009-04-02 04:16:50 +00001055 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001056 StmtVector Stmts(Actions);
1057 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
John McCall7f040a92010-12-24 02:08:15 +00001058 DeclEnd, attrs, false);
Chris Lattnercd147752009-03-29 17:27:48 +00001059 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Chris Lattnercd147752009-03-29 17:27:48 +00001061 if (Tok.is(tok::semi)) { // for (int x = 4;
1062 ConsumeToken();
1063 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001064 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001065 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001066 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001067
1068 if (Tok.is(tok::code_completion)) {
1069 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1070 ConsumeCodeCompletionToken();
1071 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001072 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001073 } else {
1074 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001075 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 } else {
1077 Value = ParseExpression();
1078
John McCallf6a16482010-12-04 03:47:34 +00001079 ForEach = isTokIdentifier_in();
1080
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001082 if (!Value.isInvalid()) {
1083 if (ForEach)
1084 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1085 else
1086 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1087 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001088
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001089 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001091 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001092 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001093
1094 if (Tok.is(tok::code_completion)) {
1095 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1096 ConsumeCodeCompletionToken();
1097 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001098 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001099 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001100 if (!Value.isInvalid()) {
1101 Diag(Tok, diag::err_expected_semi_for);
1102 } else {
1103 // Skip until semicolon or rparen, don't consume it.
1104 SkipUntil(tok::r_paren, true, true);
1105 if (Tok.is(tok::semi))
1106 ConsumeToken();
1107 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 }
1109 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001110 if (!ForEach) {
John McCall9ae2f072010-08-23 23:25:46 +00001111 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001112 // Parse the second part of the for specifier.
1113 if (Tok.is(tok::semi)) { // for (...;;
1114 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001115 } else if (Tok.is(tok::r_paren)) {
1116 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001117 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001118 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001119 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001120 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1121 else {
1122 Second = ParseExpression();
1123 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001124 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001125 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001126 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001127 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001128 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001129 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001130
Douglas Gregorb72c7782011-02-17 03:38:46 +00001131 if (Tok.isNot(tok::semi)) {
1132 if (!SecondPartIsInvalid || SecondVar)
1133 Diag(Tok, diag::err_expected_semi_for);
1134 else
1135 // Skip until semicolon or rparen, don't consume it.
1136 SkipUntil(tok::r_paren, true, true);
1137 }
1138
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001139 if (Tok.is(tok::semi)) {
1140 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001141 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001142
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001143 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001144 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001145 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001146 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001147 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 // Match the ')'.
1150 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001151
Chris Lattner0ecea032007-08-22 05:28:50 +00001152 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001153 // there is no compound stmt. C90 does not have this clause. We only do this
1154 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001155 //
1156 // C++ 6.5p2:
1157 // The substatement in an iteration-statement implicitly defines a local scope
1158 // which is entered and exited each time through the loop.
1159 //
1160 // See comments in ParseIfStatement for why we create a scope for
1161 // for-init-statement/condition and a new scope for substatement in C++.
1162 //
Mike Stump1eb44332009-09-09 15:08:12 +00001163 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001164 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001165
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001167 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001168
Chris Lattner0ecea032007-08-22 05:28:50 +00001169 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001170 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001171
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001173 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001174
1175 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001176 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001177
1178 if (!ForEach)
John McCall9ae2f072010-08-23 23:25:46 +00001179 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1180 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor586596f2010-05-06 17:25:47 +00001182 // FIXME: It isn't clear how to communicate the late destruction of
1183 // C++ temporaries used to create the collection.
John McCall9ae2f072010-08-23 23:25:46 +00001184 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1185 Collection.take(), RParenLoc,
1186 Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001187}
1188
1189/// ParseGotoStatement
1190/// jump-statement:
1191/// 'goto' identifier ';'
1192/// [GNU] 'goto' '*' expression ';'
1193///
1194/// Note: this lets the caller parse the end ';'.
1195///
John McCall7f040a92010-12-24 02:08:15 +00001196StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001197 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001198
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001199 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001201
John McCall60d7b3a2010-08-24 06:29:42 +00001202 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001203 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001204 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1205 Tok.getLocation());
1206 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001208 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 // GNU indirect goto extension.
1210 Diag(Tok, diag::ext_gnu_indirect_goto);
1211 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001212 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001213 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001215 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 }
John McCall9ae2f072010-08-23 23:25:46 +00001217 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001218 } else {
1219 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001220 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001222
Sebastian Redl9a920342008-12-11 19:48:14 +00001223 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001224}
1225
1226/// ParseContinueStatement
1227/// jump-statement:
1228/// 'continue' ';'
1229///
1230/// Note: this lets the caller parse the end ';'.
1231///
John McCall7f040a92010-12-24 02:08:15 +00001232StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001233 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001234
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001236 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001237}
1238
1239/// ParseBreakStatement
1240/// jump-statement:
1241/// 'break' ';'
1242///
1243/// Note: this lets the caller parse the end ';'.
1244///
John McCall7f040a92010-12-24 02:08:15 +00001245StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001246 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001247
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001249 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001250}
1251
1252/// ParseReturnStatement
1253/// jump-statement:
1254/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001255StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001256 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001257
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001258 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001260
John McCall60d7b3a2010-08-24 06:29:42 +00001261 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001262 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001263 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001264 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001265 ConsumeCodeCompletionToken();
1266 SkipUntil(tok::semi, false, true);
1267 return StmtError();
1268 }
1269
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001271 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001273 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 }
1275 }
John McCall9ae2f072010-08-23 23:25:46 +00001276 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001277}
1278
Steve Naroff5f8aa692008-02-11 23:15:56 +00001279/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1280/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001281StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1282 SourceLocation EndLoc;
Steve Naroffb746ce82008-02-07 23:24:32 +00001283 if (Tok.is(tok::l_brace)) {
1284 unsigned short savedBraceCount = BraceCount;
1285 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001286 EndLoc = Tok.getLocation();
Steve Naroffb746ce82008-02-07 23:24:32 +00001287 ConsumeAnyToken();
1288 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001289 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001290 // From the MS website: If used without braces, the __asm keyword means
1291 // that the rest of the line is an assembly-language statement.
1292 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001293 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001294 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001295 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001296 EndLoc = TokLoc;
Steve Naroff36280972008-02-08 18:01:27 +00001297 ConsumeAnyToken();
1298 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001299 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1300 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001301 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001302 }
Mike Stump95059b52009-12-11 00:04:56 +00001303 Token t;
1304 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001305 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001306 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001307 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001308 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001309 ExprVector Constraints(Actions);
1310 ExprVector Exprs(Actions);
1311 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001312 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001313 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001314 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001315 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001316}
1317
Reid Spencer5f016e22007-07-11 17:01:13 +00001318/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001319/// asm-statement:
1320/// gnu-asm-statement
1321/// ms-asm-statement
1322///
1323/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001324/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1325///
1326/// [GNU] asm-argument:
1327/// asm-string-literal
1328/// asm-string-literal ':' asm-operands[opt]
1329/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1330/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1331/// ':' asm-clobbers
1332///
1333/// [GNU] asm-clobbers:
1334/// asm-string-literal
1335/// asm-clobbers ',' asm-string-literal
1336///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001337/// [MS] ms-asm-statement:
1338/// '__asm' assembly-instruction ';'[opt]
1339/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1340///
1341/// [MS] assembly-instruction-list:
1342/// assembly-instruction ';'[opt]
1343/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1344///
John McCall60d7b3a2010-08-24 06:29:42 +00001345StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001346 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001347 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001348
Steve Naroff5f8aa692008-02-11 23:15:56 +00001349 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001350 msAsm = true;
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001351 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001352 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 DeclSpec DS;
1354 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001355 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001356
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1358 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001359 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001361 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001362
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001364 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001365 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001366 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001368 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001369 }
1370 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001371
John McCall60d7b3a2010-08-24 06:29:42 +00001372 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001373 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001374 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001375
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001376 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001377 ExprVector Constraints(Actions);
1378 ExprVector Exprs(Actions);
1379 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001380
Anders Carlssondfab34a2008-02-05 23:03:50 +00001381 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001382 // We have a simple asm expression like 'asm("foo")'.
1383 SourceLocation RParenLoc = ConsumeParen();
1384 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1385 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1386 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001387 AsmString.take(), move_arg(Clobbers),
Chris Lattner64cb4752009-12-20 23:00:41 +00001388 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001390
Chris Lattner64cb4752009-12-20 23:00:41 +00001391 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001392 bool AteExtraColon = false;
1393 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1394 // In C++ mode, parse "::" like ": :".
1395 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001396 ConsumeToken();
1397
Chris Lattner64056462009-12-20 23:08:04 +00001398 if (!AteExtraColon &&
1399 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001400 return StmtError();
1401 }
Chris Lattner64056462009-12-20 23:08:04 +00001402
Chris Lattner64cb4752009-12-20 23:00:41 +00001403 unsigned NumOutputs = Names.size();
1404
1405 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001406 if (AteExtraColon ||
1407 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1408 // In C++ mode, parse "::" like ": :".
1409 if (AteExtraColon)
1410 AteExtraColon = false;
1411 else {
1412 AteExtraColon = Tok.is(tok::coloncolon);
1413 ConsumeToken();
1414 }
1415
1416 if (!AteExtraColon &&
1417 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001418 return StmtError();
1419 }
1420
1421 assert(Names.size() == Constraints.size() &&
1422 Constraints.size() == Exprs.size() &&
1423 "Input operand size mismatch!");
1424
1425 unsigned NumInputs = Names.size() - NumOutputs;
1426
1427 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001428 if (AteExtraColon || Tok.is(tok::colon)) {
1429 if (!AteExtraColon)
1430 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001431
Chandler Carruth102e1b62010-07-22 07:11:21 +00001432 // Parse the asm-string list for clobbers if present.
1433 if (Tok.isNot(tok::r_paren)) {
1434 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001435 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001436
Chandler Carruth102e1b62010-07-22 07:11:21 +00001437 if (Clobber.isInvalid())
1438 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001439
Chandler Carruth102e1b62010-07-22 07:11:21 +00001440 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001441
Chandler Carruth102e1b62010-07-22 07:11:21 +00001442 if (Tok.isNot(tok::comma)) break;
1443 ConsumeToken();
1444 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001445 }
1446 }
1447
1448 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1449 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001450 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001451 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001452 AsmString.take(), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001453 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001454}
1455
1456/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001457/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001458///
1459/// [GNU] asm-operands:
1460/// asm-operand
1461/// asm-operands ',' asm-operand
1462///
1463/// [GNU] asm-operand:
1464/// asm-string-literal '(' expression ')'
1465/// '[' identifier ']' asm-string-literal '(' expression ')'
1466///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001467//
1468// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001469bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1470 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1471 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001472 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001473 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001474 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
1476 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001478 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001481 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 Diag(Tok, diag::err_expected_ident);
1483 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001484 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001485 }
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Anders Carlssonb235fc22007-11-22 01:36:19 +00001487 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001488 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001489
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001490 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001492 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001493 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001494
John McCall60d7b3a2010-08-24 06:29:42 +00001495 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001496 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001497 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001498 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001499 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001500 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001501
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001502 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001503 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001505 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001507
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001509 SourceLocation OpenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001510 ExprResult Res(ParseExpression());
Eli Friedman72056a22009-05-03 07:49:42 +00001511 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001512 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001514 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001516 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001518 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 ConsumeToken();
1520 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001521
1522 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001523}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001524
John McCalld226f652010-08-21 09:40:31 +00001525Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001526 assert(Tok.is(tok::l_brace));
1527 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001528
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001529 if (PP.isCodeCompletionEnabled())
1530 if (trySkippingFunctionBodyForCodeCompletion())
1531 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidis3437f1f2011-01-03 19:44:02 +00001532
John McCallf312b1e2010-08-26 23:41:50 +00001533 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1534 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001536 // Do not enter a scope for the brace, as the arguments are in the same scope
1537 // (the function body) as the body itself. Instead, just read the statement
1538 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001539 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001540
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001541 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001542 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001543 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001544 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001545
John McCall9ae2f072010-08-23 23:25:46 +00001546 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001547}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001548
Sebastian Redld3a413d2009-04-26 20:35:05 +00001549/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1550///
1551/// function-try-block:
1552/// 'try' ctor-initializer[opt] compound-statement handler-seq
1553///
John McCalld226f652010-08-21 09:40:31 +00001554Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001555 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1556 SourceLocation TryLoc = ConsumeToken();
1557
John McCallf312b1e2010-08-26 23:41:50 +00001558 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1559 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001560
1561 // Constructor initializer list?
1562 if (Tok.is(tok::colon))
1563 ParseConstructorInitializer(Decl);
1564
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001565 if (PP.isCodeCompletionEnabled())
1566 if (trySkippingFunctionBodyForCodeCompletion())
1567 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001568
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001569 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001570 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001571 // If we failed to parse the try-catch, we just give the function an empty
1572 // compound statement as the body.
1573 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001574 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001575 MultiStmtArg(Actions), false);
1576
John McCall9ae2f072010-08-23 23:25:46 +00001577 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001578}
1579
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001580bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001581 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001582 assert(PP.isCodeCompletionEnabled() &&
1583 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001584
1585 // We're in code-completion mode. Skip parsing for all function bodies unless
1586 // the body contains the code-completion point.
1587 TentativeParsingAction PA(*this);
1588 ConsumeBrace();
1589 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1590 /*StopAtCodeCompletion=*/true)) {
1591 PA.Commit();
1592 return true;
1593 }
1594
1595 PA.Revert();
1596 return false;
1597}
1598
Sebastian Redla0fd8652008-12-21 16:41:36 +00001599/// ParseCXXTryBlock - Parse a C++ try-block.
1600///
1601/// try-block:
1602/// 'try' compound-statement handler-seq
1603///
John McCall7f040a92010-12-24 02:08:15 +00001604StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001605 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001606
Sebastian Redla0fd8652008-12-21 16:41:36 +00001607 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1608
1609 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001610 return ParseCXXTryBlockCommon(TryLoc);
1611}
1612
1613/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1614/// function-try-block.
1615///
1616/// try-block:
1617/// 'try' compound-statement handler-seq
1618///
1619/// function-try-block:
1620/// 'try' ctor-initializer[opt] compound-statement handler-seq
1621///
1622/// handler-seq:
1623/// handler handler-seq[opt]
1624///
John McCall60d7b3a2010-08-24 06:29:42 +00001625StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001626 if (Tok.isNot(tok::l_brace))
1627 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001628 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall7f040a92010-12-24 02:08:15 +00001629 ParsedAttributesWithRange attrs;
1630 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001631 if (TryBlock.isInvalid())
1632 return move(TryBlock);
1633
1634 StmtVector Handlers(Actions);
John McCall7f040a92010-12-24 02:08:15 +00001635 MaybeParseCXX0XAttributes(attrs);
1636 ProhibitAttributes(attrs);
1637
Sebastian Redla0fd8652008-12-21 16:41:36 +00001638 if (Tok.isNot(tok::kw_catch))
1639 return StmtError(Diag(Tok, diag::err_expected_catch));
1640 while (Tok.is(tok::kw_catch)) {
John McCall60d7b3a2010-08-24 06:29:42 +00001641 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001642 if (!Handler.isInvalid())
1643 Handlers.push_back(Handler.release());
1644 }
1645 // Don't bother creating the full statement if we don't have any usable
1646 // handlers.
1647 if (Handlers.empty())
1648 return StmtError();
1649
John McCall9ae2f072010-08-23 23:25:46 +00001650 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001651}
1652
1653/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1654///
1655/// handler:
1656/// 'catch' '(' exception-declaration ')' compound-statement
1657///
1658/// exception-declaration:
1659/// type-specifier-seq declarator
1660/// type-specifier-seq abstract-declarator
1661/// type-specifier-seq
1662/// '...'
1663///
John McCall60d7b3a2010-08-24 06:29:42 +00001664StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001665 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1666
1667 SourceLocation CatchLoc = ConsumeToken();
1668
1669 SourceLocation LParenLoc = Tok.getLocation();
1670 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1671 return StmtError();
1672
1673 // C++ 3.3.2p3:
1674 // The name in a catch exception-declaration is local to the handler and
1675 // shall not be redeclared in the outermost block of the handler.
1676 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1677
1678 // exception-declaration is equivalent to '...' or a parameter-declaration
1679 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00001680 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001681 if (Tok.isNot(tok::ellipsis)) {
1682 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001683 if (ParseCXXTypeSpecifierSeq(DS))
1684 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001685 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1686 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001687 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001688 } else
1689 ConsumeToken();
1690
1691 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1692 return StmtError();
1693
1694 if (Tok.isNot(tok::l_brace))
1695 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1696
Sean Huntbbd37c62009-11-21 08:43:09 +00001697 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall7f040a92010-12-24 02:08:15 +00001698 ParsedAttributes attrs;
1699 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001700 if (Block.isInvalid())
1701 return move(Block);
1702
John McCall9ae2f072010-08-23 23:25:46 +00001703 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001704}