blob: 3e7ec533bd5bb42b92647cb9afcc43146612af69 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +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 Kyrtzidisdee82912008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000046/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000047/// [OMP] openmp-construct [TODO]
48///
49/// labeled-statement:
50/// identifier ':' statement
51/// 'case' constant-expression ':' statement
52/// 'default' ':' statement
53///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000054/// selection-statement:
55/// if-statement
56/// switch-statement
57///
58/// iteration-statement:
59/// while-statement
60/// do-statement
61/// for-statement
62///
Chris Lattner9075bd72006-08-10 04:59:57 +000063/// expression-statement:
64/// expression[opt] ';'
65///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000066/// jump-statement:
67/// 'goto' identifier ';'
68/// 'continue' ';'
69/// 'break' ';'
70/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000071/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000072///
Fariborz Jahanian90814572007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000075/// [OBC] '@' 'throw' ';'
76///
John McCalldadc5752010-08-24 06:29:42 +000077StmtResult
Fariborz Jahanian1db5c942010-09-28 20:42:35 +000078Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000079 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000080 StmtResult Res;
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000081
82 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000083
John McCall53fa7142010-12-24 02:08:15 +000084 ParsedAttributesWithRange attrs;
85 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +000086
Chris Lattner503fadc2006-08-10 05:45:44 +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 Jahanian62fd2b42007-09-19 19:14:32 +000090 tok::TokenKind Kind = Tok.getKind();
91 SourceLocation AtLoc;
92 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000093 case tok::at: // May be a @try or @throw statement
94 {
95 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000096 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000097 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000098
Douglas Gregor9d64c5e2009-09-21 20:51:25 +000099 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000100 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorf4c33342010-05-28 00:22:41 +0000101 ConsumeCodeCompletionToken();
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000102 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000103
Argyrios Kyrtzidis07b8b632008-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 McCall53fa7142010-12-24 02:08:15 +0000107 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000108 }
109 // PASS THROUGH.
110
Chris Lattner803802d2009-03-24 17:04:48 +0000111 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000112 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000113 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000114 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000115 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000116 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000117 }
118
119 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000120 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000121 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Alexis Hunt96d5c762009-11-21 08:43:09 +0000124 // FIXME: Use the attributes
Chris Lattner803802d2009-03-24 17:04:48 +0000125 // expression[opt] ';'
John McCalldadc5752010-08-24 06:29:42 +0000126 ExprResult Expr(ParseExpression());
Chris Lattner803802d2009-03-24 17:04:48 +0000127 if (Expr.isInvalid()) {
Argyrios Kyrtzidis90ab3b72010-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 Lattner803802d2009-03-24 17:04:48 +0000130 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000131 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
132 if (Tok.is(tok::semi))
133 ConsumeToken();
Chris Lattner803802d2009-03-24 17:04:48 +0000134 return StmtError();
135 }
136 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000137 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000138 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
Chris Lattner803802d2009-03-24 17:04:48 +0000139 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000140
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000141 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000142 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000143 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000144 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000145
Chris Lattner9075bd72006-08-10 04:59:57 +0000146 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000147 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-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 Redl042ad952008-12-11 19:30:53 +0000152
Chris Lattner9075bd72006-08-10 04:59:57 +0000153 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall53fa7142010-12-24 02:08:15 +0000154 return ParseIfStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000155 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall53fa7142010-12-24 02:08:15 +0000156 return ParseSwitchStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000157
Chris Lattner9075bd72006-08-10 04:59:57 +0000158 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall53fa7142010-12-24 02:08:15 +0000159 return ParseWhileStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000160 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000161 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000162 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000163 break;
164 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall53fa7142010-12-24 02:08:15 +0000165 return ParseForStatement(attrs);
Chris Lattner503fadc2006-08-10 05:45:44 +0000166
167 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000168 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000169 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000170 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000171 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000172 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000173 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000174 break;
175 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000176 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000177 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000178 break;
179 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000180 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000181 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000182 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000183
Sebastian Redlb219c902008-12-21 16:41:36 +0000184 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000185 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000186 bool msAsm = false;
187 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000188 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000189 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000190 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000191 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000192 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000193
Sebastian Redlb219c902008-12-21 16:41:36 +0000194 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000195 return ParseCXXTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000196 }
197
Chris Lattner503fadc2006-08-10 05:45:44 +0000198 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000199 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000200 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000201 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-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 Lattner0046de12008-11-13 18:52:53 +0000206 // Skip until we see a } or ;, but don't eat it.
207 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000208 }
Mike Stump11289f42009-09-09 15:08:12 +0000209
Sebastian Redl042ad952008-12-11 19:30:53 +0000210 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000211}
212
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000213/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000214///
215/// labeled-statement:
216/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000217/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000218///
John McCall53fa7142010-12-24 02:08:15 +0000219StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-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 Redl042ad952008-12-11 19:30:53 +0000227
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000228 // identifier ':' statement
229 SourceLocation ColonLoc = ConsumeToken();
230
231 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000232 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000233
John McCalldadc5752010-08-24 06:29:42 +0000234 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000235
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000236 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000237 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000238 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000239
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000240 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
241 IdentTok.getIdentifierInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000242 ColonLoc, SubStmt.get(), attrs.getList());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000243}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000244
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000245/// ParseCaseStatement
246/// labeled-statement:
247/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000248/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000249///
John McCall53fa7142010-12-24 02:08:15 +0000250StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000251 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000252 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000253
Chris Lattner34a22092009-03-04 04:23:07 +0000254 // It is very very common for code to contain many case statements recursively
255 // nested, as in (but usually without indentation):
256 // case 1:
257 // case 2:
258 // case 3:
259 // case 4:
260 // case 5: etc.
261 //
262 // Parsing this naively works, but is both inefficient and can cause us to run
263 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000264 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000265 // but all the grossness is constrained to ParseCaseStatement (and some
266 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattner34a22092009-03-04 04:23:07 +0000268 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
269 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000270 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Chris Lattner34a22092009-03-04 04:23:07 +0000272 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
273 // gets updated each time a new case is parsed, and whose body is unset so
274 // far. When parsing 'case 4', this is the 'case 3' node.
275 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattner34a22092009-03-04 04:23:07 +0000277 // While we have case statements, eat and stack them.
278 do {
279 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000280
Douglas Gregord328d572009-09-21 18:10:23 +0000281 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000282 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000283 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000284 }
285
Chris Lattner125c0ee2009-12-10 00:38:54 +0000286 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
287 /// Disable this form of error recovery while we're parsing the case
288 /// expression.
289 ColonProtectionRAIIObject ColonProtection(*this);
290
John McCalldadc5752010-08-24 06:29:42 +0000291 ExprResult LHS(ParseConstantExpression());
Chris Lattner34a22092009-03-04 04:23:07 +0000292 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000293 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000294 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000295 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000296
Chris Lattner34a22092009-03-04 04:23:07 +0000297 // GNU case range extension.
298 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000299 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000300 if (Tok.is(tok::ellipsis)) {
301 Diag(Tok, diag::ext_gnu_case_range);
302 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000303
Chris Lattner34a22092009-03-04 04:23:07 +0000304 RHS = ParseConstantExpression();
305 if (RHS.isInvalid()) {
306 SkipUntil(tok::colon);
307 return StmtError();
308 }
309 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000310
311 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000312
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000313 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000314 if (Tok.is(tok::colon)) {
315 ColonLoc = ConsumeToken();
316
317 // Treat "case blah;" as a typo for "case blah:".
318 } else if (Tok.is(tok::semi)) {
319 ColonLoc = ConsumeToken();
320 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
321 << FixItHint::CreateReplacement(ColonLoc, ":");
322 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000323 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
324 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
325 << FixItHint::CreateInsertion(ExpectedLoc, ":");
326 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000327 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000328
John McCalldadc5752010-08-24 06:29:42 +0000329 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000330 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
331 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattner34a22092009-03-04 04:23:07 +0000333 // If we had a sema error parsing this case, then just ignore it and
334 // continue parsing the sub-stmt.
335 if (Case.isInvalid()) {
336 if (TopLevelCase.isInvalid()) // No parsed case stmts.
337 return ParseStatement();
338 // Otherwise, just don't add it as a nested case.
339 } else {
340 // If this is the first case statement we parsed, it becomes TopLevelCase.
341 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000342 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000343 if (TopLevelCase.isInvalid())
344 TopLevelCase = move(Case);
345 else
John McCallb268a282010-08-23 23:25:46 +0000346 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000347 DeepestParsedCaseStmt = NextDeepest;
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattner34a22092009-03-04 04:23:07 +0000350 // Handle all case statements.
351 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner34a22092009-03-04 04:23:07 +0000353 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattner34a22092009-03-04 04:23:07 +0000355 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000356 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattner34a22092009-03-04 04:23:07 +0000358 if (Tok.isNot(tok::r_brace)) {
359 SubStmt = ParseStatement();
360 } else {
361 // Nicely diagnose the common error "switch (X) { case 4: }", which is
362 // not valid.
363 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000364 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000365 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner34a22092009-03-04 04:23:07 +0000368 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000369 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000370 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattner34a22092009-03-04 04:23:07 +0000372 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000373 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000374
Chris Lattner34a22092009-03-04 04:23:07 +0000375 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000376 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000377}
378
379/// ParseDefaultStatement
380/// labeled-statement:
381/// 'default' ':' statement
382/// Note that this does not parse the 'statement' at the end.
383///
John McCall53fa7142010-12-24 02:08:15 +0000384StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000385 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000386
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000387 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000388 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000389
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000390 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000391 if (Tok.is(tok::colon)) {
392 ColonLoc = ConsumeToken();
393
394 // Treat "default;" as a typo for "default:".
395 } else if (Tok.is(tok::semi)) {
396 ColonLoc = ConsumeToken();
397 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
398 << FixItHint::CreateReplacement(ColonLoc, ":");
399 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000400 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
401 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
402 << FixItHint::CreateInsertion(ExpectedLoc, ":");
403 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000404 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000405
Chris Lattner30f910e2006-10-16 05:52:41 +0000406 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000407 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000408 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000409 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000410 }
411
John McCalldadc5752010-08-24 06:29:42 +0000412 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000413 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000414 return StmtError();
415
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000416 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000417 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000418}
419
420
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000421/// ParseCompoundStatement - Parse a "{}" block.
422///
423/// compound-statement: [C99 6.8.2]
424/// { block-item-list[opt] }
425/// [GNU] { label-declarations block-item-list } [TODO]
426///
427/// block-item-list:
428/// block-item
429/// block-item-list block-item
430///
431/// block-item:
432/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000433/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000434/// statement
435/// [OMP] openmp-directive [TODO]
436///
437/// [GNU] label-declarations:
438/// [GNU] label-declaration
439/// [GNU] label-declarations label-declaration
440///
441/// [GNU] label-declaration:
442/// [GNU] '__label__' identifier-list ';'
443///
444/// [OMP] openmp-directive: [TODO]
445/// [OMP] barrier-directive
446/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000447///
John McCall53fa7142010-12-24 02:08:15 +0000448StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000449 bool isStmtExpr) {
450 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000451
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000452 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000453
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000454 // Enter a scope to hold everything within the compound stmt. Compound
455 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000456 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000457
458 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000459 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000460}
461
462
463/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000464/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000465/// consume the '}' at the end of the block. It does not manipulate the scope
466/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000467StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000468 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000469 Tok.getLocation(),
470 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000471 InMessageExpressionRAIIObject InMessage(*this, false);
472
Chris Lattnerf2978802007-01-21 06:52:16 +0000473 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
474
Chris Lattner010015a2007-05-28 07:23:54 +0000475 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000476 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000477
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000478 StmtVector Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000479 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000480
481 if (Tok.is(tok::annot_pragma_unused)) {
482 HandlePragmaUnused();
483 continue;
484 }
485
John McCalldadc5752010-08-24 06:29:42 +0000486 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000487 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000488 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000489 } else {
490 // __extension__ can start declarations and it can also be a unary
491 // operator for expressions. Consume multiple __extension__ markers here
492 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000493 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000494 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000495 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000496 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000497
John McCall53fa7142010-12-24 02:08:15 +0000498 ParsedAttributesWithRange attrs;
499 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000500
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000501 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000502 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000503 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000504 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000505 ExtensionRAIIObject O(Diags);
506
Chris Lattner49836b42009-04-02 04:16:50 +0000507 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000508 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
509 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000510 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000511 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000512 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000513 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000514 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000515
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000516 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000517 SkipUntil(tok::semi);
518 continue;
519 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000520
Alexis Hunt96d5c762009-11-21 08:43:09 +0000521 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000522 // Eat the semicolon at the end of stmt and convert the expr into a
523 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000524 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000525 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000526 }
527 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000528
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000529 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000530 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000531 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000532
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000533 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000534 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000535 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000536 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000537 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000538 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000539
Chris Lattner04132372006-10-16 06:12:55 +0000540 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000541 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000542 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000543}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000544
Chris Lattnerc0081db2008-12-12 06:31:07 +0000545/// ParseParenExprOrCondition:
546/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000547/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000548///
549/// This function parses and performs error recovery on the specified condition
550/// or expression (depending on whether we're in C++ or C mode). This function
551/// goes out of its way to recover well. It returns true if there was a parser
552/// error (the right paren couldn't be found), which indicates that the caller
553/// should try to recover harder. It returns false if the condition is
554/// successfully parsed. Note that a successful parse can still have semantic
555/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000556bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000557 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000558 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000559 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000560 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000561 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000562 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000563 else {
564 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000565 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000566
567 // If required, convert to a boolean value.
568 if (!ExprResult.isInvalid() && ConvertToBoolean)
569 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000570 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000571 }
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattnerc0081db2008-12-12 06:31:07 +0000573 // If the parser was confused by the condition and we don't have a ')', try to
574 // recover by skipping ahead to a semi and bailing out. If condexp is
575 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000576 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000577 SkipUntil(tok::semi);
578 // Skipping may have stopped if it found the containing ')'. If so, we can
579 // continue parsing the if statement.
580 if (Tok.isNot(tok::r_paren))
581 return true;
582 }
Mike Stump11289f42009-09-09 15:08:12 +0000583
Chris Lattnerc0081db2008-12-12 06:31:07 +0000584 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000585 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000586 return false;
587}
588
589
Chris Lattnerc951dae2006-08-10 04:23:57 +0000590/// ParseIfStatement
591/// if-statement: [C99 6.8.4.1]
592/// 'if' '(' expression ')' statement
593/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000594/// [C++] 'if' '(' condition ')' statement
595/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000596///
John McCall53fa7142010-12-24 02:08:15 +0000597StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000598 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000599
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000600 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000601 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000602
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000603 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000604 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000605 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000606 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000607 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000608
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000609 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
610
Chris Lattner2dd1b722007-08-26 23:08:06 +0000611 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
612 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000613 //
614 // C++ 6.4p3:
615 // A name introduced by a declaration in a condition is in scope from its
616 // point of declaration until the end of the substatements controlled by the
617 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000618 // C++ 3.3.2p4:
619 // Names declared in the for-init-statement, and in the condition of if,
620 // while, for, and switch statements are local to the if, while, for, or
621 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000622 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000623 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000624
Chris Lattnerc951dae2006-08-10 04:23:57 +0000625 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000626 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000627 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000628 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000629 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000630
John McCallb268a282010-08-23 23:25:46 +0000631 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000632
Chris Lattner8fb26252007-08-22 05:28:50 +0000633 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000634 // there is no compound stmt. C90 does not have this clause. We only do this
635 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000636 //
637 // C++ 6.4p1:
638 // The substatement in a selection-statement (each substatement, in the else
639 // form of the if statement) implicitly defines a local scope.
640 //
641 // For C++ we create a scope for the condition and a new scope for
642 // substatements because:
643 // -When the 'then' scope exits, we want the condition declaration to still be
644 // active for the 'else' scope too.
645 // -Sema will detect name clashes by considering declarations of a
646 // 'ControlScope' as part of its direct subscope.
647 // -If we wanted the condition and substatement to be in the same scope, we
648 // would have to notify ParseStatement not to create a new scope. It's
649 // simpler to let it create a new scope.
650 //
Mike Stump11289f42009-09-09 15:08:12 +0000651 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000652 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000653
Chris Lattner5c5808a2007-10-29 05:08:52 +0000654 // Read the 'then' stmt.
655 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000656 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000657
Chris Lattner37e54f42007-08-22 05:16:28 +0000658 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000659 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000660
Chris Lattnerc951dae2006-08-10 04:23:57 +0000661 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000662 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000663 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000664 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000665
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000666 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000667 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000668 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000669
Chris Lattner8fb26252007-08-22 05:28:50 +0000670 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000671 // there is no compound stmt. C90 does not have this clause. We only do
672 // this if the body isn't a compound statement to avoid push/pop in common
673 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000674 //
675 // C++ 6.4p1:
676 // The substatement in a selection-statement (each substatement, in the else
677 // form of the if statement) implicitly defines a local scope.
678 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000679 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000680 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000681
Chris Lattner30f910e2006-10-16 05:52:41 +0000682 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000683
Chris Lattner37e54f42007-08-22 05:16:28 +0000684 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000685 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000686 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000687
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000688 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000690 // If the condition was invalid, discard the if statement. We could recover
691 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000692 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000693 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000694
Chris Lattner5c5808a2007-10-29 05:08:52 +0000695 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000696 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000697 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000698 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
699 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
700 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000701 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000702 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000703 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000704
Chris Lattner5c5808a2007-10-29 05:08:52 +0000705 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000706 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000707 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000708 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000709 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000710
John McCallb268a282010-08-23 23:25:46 +0000711 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000712 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000713}
714
Chris Lattner9075bd72006-08-10 04:59:57 +0000715/// ParseSwitchStatement
716/// switch-statement:
717/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000718/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000719StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000720 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000721
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000722 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000723 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000724
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000725 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000726 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000727 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000728 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000729 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000730
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000731 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
732
Chris Lattner2dd1b722007-08-26 23:08:06 +0000733 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
734 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000735 //
736 // C++ 6.4p3:
737 // A name introduced by a declaration in a condition is in scope from its
738 // point of declaration until the end of the substatements controlled by the
739 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000740 // C++ 3.3.2p4:
741 // Names declared in the for-init-statement, and in the condition of if,
742 // while, for, and switch statements are local to the if, while, for, or
743 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000744 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000745 unsigned ScopeFlags = Scope::BreakScope;
746 if (C99orCXX)
747 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000748 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000749
Chris Lattner9075bd72006-08-10 04:59:57 +0000750 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000751 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000752 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000753 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000754 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000755
John McCalldadc5752010-08-24 06:29:42 +0000756 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000757 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000758
Douglas Gregore60e41a2010-05-06 17:25:47 +0000759 if (Switch.isInvalid()) {
760 // Skip the switch body.
761 // FIXME: This is not optimal recovery, but parsing the body is more
762 // dangerous due to the presence of case and default statements, which
763 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000764 if (Tok.is(tok::l_brace)) {
765 ConsumeBrace();
766 SkipUntil(tok::r_brace, false, false);
767 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000768 SkipUntil(tok::semi);
769 return move(Switch);
770 }
771
Chris Lattner8fb26252007-08-22 05:28:50 +0000772 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000773 // there is no compound stmt. C90 does not have this clause. We only do this
774 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000775 //
776 // C++ 6.4p1:
777 // The substatement in a selection-statement (each substatement, in the else
778 // form of the if statement) implicitly defines a local scope.
779 //
780 // See comments in ParseIfStatement for why we create a scope for the
781 // condition and a new scope for substatement in C++.
782 //
Mike Stump11289f42009-09-09 15:08:12 +0000783 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000784 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000785
Chris Lattner9075bd72006-08-10 04:59:57 +0000786 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000787 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000788
Chris Lattner8fd2d012010-01-24 01:50:29 +0000789 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000790 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000791 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000792
Chris Lattner8fd2d012010-01-24 01:50:29 +0000793 if (Body.isInvalid())
794 // FIXME: Remove the case statement list from the Switch statement.
795 Body = Actions.ActOnNullStmt(Tok.getLocation());
796
John McCallb268a282010-08-23 23:25:46 +0000797 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000798}
799
800/// ParseWhileStatement
801/// while-statement: [C99 6.8.5.1]
802/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000803/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000804StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000805 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000806
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000807 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000808 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000809 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000810
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000811 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000812 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000813 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000814 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000815 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000816
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000817 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
818
Chris Lattner2dd1b722007-08-26 23:08:06 +0000819 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
820 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000821 //
822 // C++ 6.4p3:
823 // A name introduced by a declaration in a condition is in scope from its
824 // point of declaration until the end of the substatements controlled by the
825 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000826 // C++ 3.3.2p4:
827 // Names declared in the for-init-statement, and in the condition of if,
828 // while, for, and switch statements are local to the if, while, for, or
829 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000830 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000831 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000832 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000833 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
834 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000835 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000836 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
837 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000838
Chris Lattner9075bd72006-08-10 04:59:57 +0000839 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000840 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000841 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000842 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000843 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000844
John McCallb268a282010-08-23 23:25:46 +0000845 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner8fb26252007-08-22 05:28:50 +0000847 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000848 // there is no compound stmt. C90 does not have this clause. We only do this
849 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000850 //
851 // C++ 6.5p2:
852 // The substatement in an iteration-statement implicitly defines a local scope
853 // which is entered and exited each time through the loop.
854 //
855 // See comments in ParseIfStatement for why we create a scope for the
856 // condition and a new scope for substatement in C++.
857 //
Mike Stump11289f42009-09-09 15:08:12 +0000858 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000859 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000860
Chris Lattner9075bd72006-08-10 04:59:57 +0000861 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000862 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000863
Chris Lattner8fb26252007-08-22 05:28:50 +0000864 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000865 InnerScope.Exit();
866 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000867
John McCall48871652010-08-21 09:40:31 +0000868 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000869 return StmtError();
870
John McCallb268a282010-08-23 23:25:46 +0000871 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000872}
873
874/// ParseDoStatement
875/// do-statement: [C99 6.8.5.2]
876/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000877/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +0000878StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000879 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000880
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000881 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000882 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000883
Chris Lattner2dd1b722007-08-26 23:08:06 +0000884 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
885 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000886 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000887 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000888 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000889 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000890 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000891
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000892 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000893
Chris Lattner8fb26252007-08-22 05:28:50 +0000894 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000895 // there is no compound stmt. C90 does not have this clause. We only do this
896 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000897 //
898 // C++ 6.5p2:
899 // The substatement in an iteration-statement implicitly defines a local scope
900 // which is entered and exited each time through the loop.
901 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000902 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000903 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000904 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000905
Chris Lattner9075bd72006-08-10 04:59:57 +0000906 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000907 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000908
Chris Lattner8fb26252007-08-22 05:28:50 +0000909 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000910 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000911
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000912 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000913 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000914 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000915 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000916 SkipUntil(tok::semi, false, true);
917 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000918 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000919 }
Chris Lattneraf635312006-10-16 06:06:51 +0000920 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000921
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000922 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000923 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000924 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000925 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000926 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000927
Chris Lattner10da53c2008-12-12 06:35:28 +0000928 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000929 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000930 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +0000931 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000932 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000933
Sebastian Redlb62406f2008-12-11 19:48:14 +0000934 if (Cond.isInvalid() || Body.isInvalid())
935 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000936
John McCallb268a282010-08-23 23:25:46 +0000937 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
938 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000939}
940
941/// ParseForStatement
942/// for-statement: [C99 6.8.5.3]
943/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
944/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000945/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
946/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000947/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
948/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000949///
950/// [C++] for-init-statement:
951/// [C++] expression-statement
952/// [C++] simple-declaration
953///
John McCall53fa7142010-12-24 02:08:15 +0000954StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000955 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000956
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000957 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000958 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000959
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000960 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000961 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000962 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000963 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000964 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000965
Chris Lattner934074c2009-04-22 00:54:41 +0000966 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000967
Chris Lattner2dd1b722007-08-26 23:08:06 +0000968 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
969 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000970 //
971 // C++ 6.4p3:
972 // A name introduced by a declaration in a condition is in scope from its
973 // point of declaration until the end of the substatements controlled by the
974 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000975 // C++ 3.3.2p4:
976 // Names declared in the for-init-statement, and in the condition of if,
977 // while, for, and switch statements are local to the if, while, for, or
978 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000979 // C++ 6.5.3p1:
980 // Names declared in the for-init-statement are in the same declarative-region
981 // as those declared in the condition.
982 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000983 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000984 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000985 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
986 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000987 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000988 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
989
990 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000991
Chris Lattner04132372006-10-16 06:12:55 +0000992 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000993 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000994
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000995 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +0000996 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000997 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000998 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +0000999 ExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001000 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001001 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001002
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001003 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001004 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001005 C99orCXXorObjC? Sema::PCC_ForInit
1006 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001007 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001008 }
1009
Chris Lattner9075bd72006-08-10 04:59:57 +00001010 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001011 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001012 // no first part, eat the ';'.
1013 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001014 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001015 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001016 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001017 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001018
John McCall53fa7142010-12-24 02:08:15 +00001019 ParsedAttributesWithRange attrs;
1020 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001021
Chris Lattner49836b42009-04-02 04:16:50 +00001022 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001023 StmtVector Stmts(Actions);
1024 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
John McCall53fa7142010-12-24 02:08:15 +00001025 DeclEnd, attrs, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001026 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattner32dc41c2009-03-29 17:27:48 +00001028 if (Tok.is(tok::semi)) { // for (int x = 4;
1029 ConsumeToken();
1030 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001031 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001032 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001033 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001034
1035 if (Tok.is(tok::code_completion)) {
1036 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1037 ConsumeCodeCompletionToken();
1038 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001039 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001040 } else {
1041 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001042 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001043 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001044 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001045
John McCall34376a62010-12-04 03:47:34 +00001046 ForEach = isTokIdentifier_in();
1047
Chris Lattnercd68f642007-06-27 01:06:29 +00001048 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001049 if (!Value.isInvalid()) {
1050 if (ForEach)
1051 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1052 else
1053 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1054 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001055
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001056 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001057 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001058 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001059 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001060
1061 if (Tok.is(tok::code_completion)) {
1062 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1063 ConsumeCodeCompletionToken();
1064 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001065 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001066 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001067 if (!Value.isInvalid()) {
1068 Diag(Tok, diag::err_expected_semi_for);
1069 } else {
1070 // Skip until semicolon or rparen, don't consume it.
1071 SkipUntil(tok::r_paren, true, true);
1072 if (Tok.is(tok::semi))
1073 ConsumeToken();
1074 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001075 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001076 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001077 if (!ForEach) {
John McCallb268a282010-08-23 23:25:46 +00001078 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001079 // Parse the second part of the for specifier.
1080 if (Tok.is(tok::semi)) { // for (...;;
1081 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001082 } else if (Tok.is(tok::r_paren)) {
1083 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001084 } else {
John McCalldadc5752010-08-24 06:29:42 +00001085 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001086 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001087 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1088 else {
1089 Second = ParseExpression();
1090 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001091 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001092 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001093 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001094 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001095 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001096 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001097
Douglas Gregor230a7e62011-02-17 03:38:46 +00001098 if (Tok.isNot(tok::semi)) {
1099 if (!SecondPartIsInvalid || SecondVar)
1100 Diag(Tok, diag::err_expected_semi_for);
1101 else
1102 // Skip until semicolon or rparen, don't consume it.
1103 SkipUntil(tok::r_paren, true, true);
1104 }
1105
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001106 if (Tok.is(tok::semi)) {
1107 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001108 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001109
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001110 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001111 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001112 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001113 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001114 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001115 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001116 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001117 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001118
Chris Lattner8fb26252007-08-22 05:28:50 +00001119 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001120 // there is no compound stmt. C90 does not have this clause. We only do this
1121 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001122 //
1123 // C++ 6.5p2:
1124 // The substatement in an iteration-statement implicitly defines a local scope
1125 // which is entered and exited each time through the loop.
1126 //
1127 // See comments in ParseIfStatement for why we create a scope for
1128 // for-init-statement/condition and a new scope for substatement in C++.
1129 //
Mike Stump11289f42009-09-09 15:08:12 +00001130 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001131 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001132
Chris Lattner9075bd72006-08-10 04:59:57 +00001133 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001134 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001135
Chris Lattner8fb26252007-08-22 05:28:50 +00001136 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001137 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001138
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001139 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001140 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001141
1142 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001143 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001144
1145 if (!ForEach)
John McCallb268a282010-08-23 23:25:46 +00001146 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1147 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregore60e41a2010-05-06 17:25:47 +00001149 // FIXME: It isn't clear how to communicate the late destruction of
1150 // C++ temporaries used to create the collection.
John McCallb268a282010-08-23 23:25:46 +00001151 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1152 Collection.take(), RParenLoc,
1153 Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001154}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001155
Chris Lattner503fadc2006-08-10 05:45:44 +00001156/// ParseGotoStatement
1157/// jump-statement:
1158/// 'goto' identifier ';'
1159/// [GNU] 'goto' '*' expression ';'
1160///
1161/// Note: this lets the caller parse the end ';'.
1162///
John McCall53fa7142010-12-24 02:08:15 +00001163StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001164 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001165
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001166 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001167 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001168
John McCalldadc5752010-08-24 06:29:42 +00001169 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001170 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001171 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001172 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001173 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001174 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001175 // GNU indirect goto extension.
1176 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001177 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001178 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001179 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001180 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001181 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001182 }
John McCallb268a282010-08-23 23:25:46 +00001183 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001184 } else {
1185 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001186 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001187 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001188
Sebastian Redlb62406f2008-12-11 19:48:14 +00001189 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001190}
1191
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001192/// ParseContinueStatement
1193/// jump-statement:
1194/// 'continue' ';'
1195///
1196/// Note: this lets the caller parse the end ';'.
1197///
John McCall53fa7142010-12-24 02:08:15 +00001198StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001199 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001200
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001201 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001202 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001203}
1204
1205/// ParseBreakStatement
1206/// jump-statement:
1207/// 'break' ';'
1208///
1209/// Note: this lets the caller parse the end ';'.
1210///
John McCall53fa7142010-12-24 02:08:15 +00001211StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001212 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001213
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001214 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001215 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001216}
1217
Chris Lattner503fadc2006-08-10 05:45:44 +00001218/// ParseReturnStatement
1219/// jump-statement:
1220/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001221StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001222 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001223
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001224 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001225 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001226
John McCalldadc5752010-08-24 06:29:42 +00001227 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001228 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001229 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001230 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001231 ConsumeCodeCompletionToken();
1232 SkipUntil(tok::semi, false, true);
1233 return StmtError();
1234 }
1235
Chris Lattner30f910e2006-10-16 05:52:41 +00001236 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001237 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001238 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001239 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001240 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001241 }
John McCallb268a282010-08-23 23:25:46 +00001242 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001243}
Chris Lattner0116c472006-08-15 06:03:28 +00001244
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001245/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1246/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001247StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1248 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001249 if (Tok.is(tok::l_brace)) {
1250 unsigned short savedBraceCount = BraceCount;
1251 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001252 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001253 ConsumeAnyToken();
1254 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001255 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001256 // From the MS website: If used without braces, the __asm keyword means
1257 // that the rest of the line is an assembly-language statement.
1258 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001259 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001260 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001261 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001262 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001263 ConsumeAnyToken();
1264 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001265 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1266 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001267 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001268 }
Mike Stump6da5d752009-12-11 00:04:56 +00001269 Token t;
1270 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001271 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001272 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001273 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001274 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001275 ExprVector Constraints(Actions);
1276 ExprVector Exprs(Actions);
1277 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001278 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001279 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001280 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001281 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001282}
1283
Chris Lattner0116c472006-08-15 06:03:28 +00001284/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001285/// asm-statement:
1286/// gnu-asm-statement
1287/// ms-asm-statement
1288///
1289/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001290/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1291///
1292/// [GNU] asm-argument:
1293/// asm-string-literal
1294/// asm-string-literal ':' asm-operands[opt]
1295/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1296/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1297/// ':' asm-clobbers
1298///
1299/// [GNU] asm-clobbers:
1300/// asm-string-literal
1301/// asm-clobbers ',' asm-string-literal
1302///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001303/// [MS] ms-asm-statement:
1304/// '__asm' assembly-instruction ';'[opt]
1305/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1306///
1307/// [MS] assembly-instruction-list:
1308/// assembly-instruction ';'[opt]
1309/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1310///
John McCalldadc5752010-08-24 06:29:42 +00001311StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001312 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001313 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001314
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001315 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001316 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001317 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001318 }
Chris Lattner0116c472006-08-15 06:03:28 +00001319 DeclSpec DS;
1320 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001321 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001322
Chris Lattner0116c472006-08-15 06:03:28 +00001323 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001324 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001325 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001326 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001327 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001328
Chris Lattner0116c472006-08-15 06:03:28 +00001329 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001330 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001331 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001332 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001333 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001334 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001335 }
Chris Lattner04132372006-10-16 06:12:55 +00001336 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001337
John McCalldadc5752010-08-24 06:29:42 +00001338 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001339 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001340 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001341
Anders Carlsson9a020f92010-01-30 22:25:16 +00001342 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001343 ExprVector Constraints(Actions);
1344 ExprVector Exprs(Actions);
1345 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001346
Anders Carlsson19fe1162008-02-05 23:03:50 +00001347 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001348 // We have a simple asm expression like 'asm("foo")'.
1349 SourceLocation RParenLoc = ConsumeParen();
1350 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1351 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1352 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001353 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001354 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001355 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001356
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001357 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001358 bool AteExtraColon = false;
1359 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1360 // In C++ mode, parse "::" like ": :".
1361 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001362 ConsumeToken();
1363
Chris Lattner15768502009-12-20 23:08:04 +00001364 if (!AteExtraColon &&
1365 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001366 return StmtError();
1367 }
Chris Lattner15768502009-12-20 23:08:04 +00001368
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001369 unsigned NumOutputs = Names.size();
1370
1371 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001372 if (AteExtraColon ||
1373 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1374 // In C++ mode, parse "::" like ": :".
1375 if (AteExtraColon)
1376 AteExtraColon = false;
1377 else {
1378 AteExtraColon = Tok.is(tok::coloncolon);
1379 ConsumeToken();
1380 }
1381
1382 if (!AteExtraColon &&
1383 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001384 return StmtError();
1385 }
1386
1387 assert(Names.size() == Constraints.size() &&
1388 Constraints.size() == Exprs.size() &&
1389 "Input operand size mismatch!");
1390
1391 unsigned NumInputs = Names.size() - NumOutputs;
1392
1393 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001394 if (AteExtraColon || Tok.is(tok::colon)) {
1395 if (!AteExtraColon)
1396 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001397
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001398 // Parse the asm-string list for clobbers if present.
1399 if (Tok.isNot(tok::r_paren)) {
1400 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001401 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001402
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001403 if (Clobber.isInvalid())
1404 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001405
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001406 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001407
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001408 if (Tok.isNot(tok::comma)) break;
1409 ConsumeToken();
1410 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001411 }
1412 }
1413
1414 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1415 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001416 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001417 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001418 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001419 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001420}
1421
1422/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001423/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001424///
1425/// [GNU] asm-operands:
1426/// asm-operand
1427/// asm-operands ',' asm-operand
1428///
1429/// [GNU] asm-operand:
1430/// asm-string-literal '(' expression ')'
1431/// '[' identifier ']' asm-string-literal '(' expression ')'
1432///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001433//
1434// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001435bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1436 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1437 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001438 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001439 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001440 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001441
1442 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001443 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001444 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001445 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001446
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001447 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001448 Diag(Tok, diag::err_expected_ident);
1449 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001450 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001451 }
Mike Stump11289f42009-09-09 15:08:12 +00001452
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001453 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001454 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001455
Anders Carlsson9a020f92010-01-30 22:25:16 +00001456 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001457 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001458 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001459 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001460
John McCalldadc5752010-08-24 06:29:42 +00001461 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001462 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001463 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001464 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001465 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001466 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001467
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001468 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001469 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001470 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001471 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001472 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001473
Chris Lattner0116c472006-08-15 06:03:28 +00001474 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001475 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001476 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001477 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001478 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001479 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001480 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001481 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001482 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001483 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001484 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001485 ConsumeToken();
1486 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001487
1488 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001489}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001490
John McCall48871652010-08-21 09:40:31 +00001491Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001492 assert(Tok.is(tok::l_brace));
1493 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001494
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001495 if (PP.isCodeCompletionEnabled())
1496 if (trySkippingFunctionBodyForCodeCompletion())
1497 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +00001498
John McCallfaf5fb42010-08-26 23:41:50 +00001499 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1500 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001501
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001502 // Do not enter a scope for the brace, as the arguments are in the same scope
1503 // (the function body) as the body itself. Instead, just read the statement
1504 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001505 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001506
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001507 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001508 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001509 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001510 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001511
John McCallb268a282010-08-23 23:25:46 +00001512 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001513}
Sebastian Redlb219c902008-12-21 16:41:36 +00001514
Sebastian Redla7b98a72009-04-26 20:35:05 +00001515/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1516///
1517/// function-try-block:
1518/// 'try' ctor-initializer[opt] compound-statement handler-seq
1519///
John McCall48871652010-08-21 09:40:31 +00001520Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001521 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1522 SourceLocation TryLoc = ConsumeToken();
1523
John McCallfaf5fb42010-08-26 23:41:50 +00001524 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1525 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001526
1527 // Constructor initializer list?
1528 if (Tok.is(tok::colon))
1529 ParseConstructorInitializer(Decl);
1530
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001531 if (PP.isCodeCompletionEnabled())
1532 if (trySkippingFunctionBodyForCodeCompletion())
1533 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001534
Sebastian Redld98ecd62009-04-26 21:08:36 +00001535 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001536 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001537 // If we failed to parse the try-catch, we just give the function an empty
1538 // compound statement as the body.
1539 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001540 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001541 MultiStmtArg(Actions), false);
1542
John McCallb268a282010-08-23 23:25:46 +00001543 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001544}
1545
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001546bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001547 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001548 assert(PP.isCodeCompletionEnabled() &&
1549 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001550
1551 // We're in code-completion mode. Skip parsing for all function bodies unless
1552 // the body contains the code-completion point.
1553 TentativeParsingAction PA(*this);
1554 ConsumeBrace();
1555 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1556 /*StopAtCodeCompletion=*/true)) {
1557 PA.Commit();
1558 return true;
1559 }
1560
1561 PA.Revert();
1562 return false;
1563}
1564
Sebastian Redlb219c902008-12-21 16:41:36 +00001565/// ParseCXXTryBlock - Parse a C++ try-block.
1566///
1567/// try-block:
1568/// 'try' compound-statement handler-seq
1569///
John McCall53fa7142010-12-24 02:08:15 +00001570StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001571 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001572
Sebastian Redlb219c902008-12-21 16:41:36 +00001573 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1574
1575 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001576 return ParseCXXTryBlockCommon(TryLoc);
1577}
1578
1579/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1580/// function-try-block.
1581///
1582/// try-block:
1583/// 'try' compound-statement handler-seq
1584///
1585/// function-try-block:
1586/// 'try' ctor-initializer[opt] compound-statement handler-seq
1587///
1588/// handler-seq:
1589/// handler handler-seq[opt]
1590///
John McCalldadc5752010-08-24 06:29:42 +00001591StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001592 if (Tok.isNot(tok::l_brace))
1593 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001594 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall53fa7142010-12-24 02:08:15 +00001595 ParsedAttributesWithRange attrs;
1596 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001597 if (TryBlock.isInvalid())
1598 return move(TryBlock);
1599
1600 StmtVector Handlers(Actions);
John McCall53fa7142010-12-24 02:08:15 +00001601 MaybeParseCXX0XAttributes(attrs);
1602 ProhibitAttributes(attrs);
1603
Sebastian Redlb219c902008-12-21 16:41:36 +00001604 if (Tok.isNot(tok::kw_catch))
1605 return StmtError(Diag(Tok, diag::err_expected_catch));
1606 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001607 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001608 if (!Handler.isInvalid())
1609 Handlers.push_back(Handler.release());
1610 }
1611 // Don't bother creating the full statement if we don't have any usable
1612 // handlers.
1613 if (Handlers.empty())
1614 return StmtError();
1615
John McCallb268a282010-08-23 23:25:46 +00001616 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001617}
1618
1619/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1620///
1621/// handler:
1622/// 'catch' '(' exception-declaration ')' compound-statement
1623///
1624/// exception-declaration:
1625/// type-specifier-seq declarator
1626/// type-specifier-seq abstract-declarator
1627/// type-specifier-seq
1628/// '...'
1629///
John McCalldadc5752010-08-24 06:29:42 +00001630StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001631 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1632
1633 SourceLocation CatchLoc = ConsumeToken();
1634
1635 SourceLocation LParenLoc = Tok.getLocation();
1636 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1637 return StmtError();
1638
1639 // C++ 3.3.2p3:
1640 // The name in a catch exception-declaration is local to the handler and
1641 // shall not be redeclared in the outermost block of the handler.
1642 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1643
1644 // exception-declaration is equivalent to '...' or a parameter-declaration
1645 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001646 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001647 if (Tok.isNot(tok::ellipsis)) {
1648 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001649 if (ParseCXXTypeSpecifierSeq(DS))
1650 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001651 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1652 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001653 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001654 } else
1655 ConsumeToken();
1656
1657 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1658 return StmtError();
1659
1660 if (Tok.isNot(tok::l_brace))
1661 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1662
Alexis Hunt96d5c762009-11-21 08:43:09 +00001663 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall53fa7142010-12-24 02:08:15 +00001664 ParsedAttributes attrs;
1665 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001666 if (Block.isInvalid())
1667 return move(Block);
1668
John McCallb268a282010-08-23 23:25:46 +00001669 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001670}