blob: d25cc110a8a8c3f31921b6316abeeffa2c796798 [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;
Chris Lattner34a22092009-03-04 04:23:07 +0000314 if (Tok.isNot(tok::colon)) {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000315 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
316 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
317 << FixItHint::CreateInsertion(ExpectedLoc, ":");
318 ColonLoc = ExpectedLoc;
319 } else {
320 ColonLoc = ConsumeToken();
Chris Lattner34a22092009-03-04 04:23:07 +0000321 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000322
John McCalldadc5752010-08-24 06:29:42 +0000323 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000324 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
325 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner34a22092009-03-04 04:23:07 +0000327 // If we had a sema error parsing this case, then just ignore it and
328 // continue parsing the sub-stmt.
329 if (Case.isInvalid()) {
330 if (TopLevelCase.isInvalid()) // No parsed case stmts.
331 return ParseStatement();
332 // Otherwise, just don't add it as a nested case.
333 } else {
334 // If this is the first case statement we parsed, it becomes TopLevelCase.
335 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000336 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000337 if (TopLevelCase.isInvalid())
338 TopLevelCase = move(Case);
339 else
John McCallb268a282010-08-23 23:25:46 +0000340 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000341 DeepestParsedCaseStmt = NextDeepest;
342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattner34a22092009-03-04 04:23:07 +0000344 // Handle all case statements.
345 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner34a22092009-03-04 04:23:07 +0000347 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000348
Chris Lattner34a22092009-03-04 04:23:07 +0000349 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000350 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattner34a22092009-03-04 04:23:07 +0000352 if (Tok.isNot(tok::r_brace)) {
353 SubStmt = ParseStatement();
354 } else {
355 // Nicely diagnose the common error "switch (X) { case 4: }", which is
356 // not valid.
357 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000358 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000359 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattner34a22092009-03-04 04:23:07 +0000362 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000363 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000364 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner34a22092009-03-04 04:23:07 +0000366 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000367 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000368
Chris Lattner34a22092009-03-04 04:23:07 +0000369 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000370 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000371}
372
373/// ParseDefaultStatement
374/// labeled-statement:
375/// 'default' ':' statement
376/// Note that this does not parse the 'statement' at the end.
377///
John McCall53fa7142010-12-24 02:08:15 +0000378StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000379 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000380
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000381 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000382 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000383
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000384 SourceLocation ColonLoc;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000385 if (Tok.isNot(tok::colon)) {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000386 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
387 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
388 << FixItHint::CreateInsertion(ExpectedLoc, ":");
389 ColonLoc = ExpectedLoc;
390 } else {
391 ColonLoc = ConsumeToken();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000392 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000393
Chris Lattner30f910e2006-10-16 05:52:41 +0000394 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000395 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000396 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000397 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000398 }
399
John McCalldadc5752010-08-24 06:29:42 +0000400 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000401 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000402 return StmtError();
403
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000404 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000405 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000406}
407
408
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000409/// ParseCompoundStatement - Parse a "{}" block.
410///
411/// compound-statement: [C99 6.8.2]
412/// { block-item-list[opt] }
413/// [GNU] { label-declarations block-item-list } [TODO]
414///
415/// block-item-list:
416/// block-item
417/// block-item-list block-item
418///
419/// block-item:
420/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000421/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000422/// statement
423/// [OMP] openmp-directive [TODO]
424///
425/// [GNU] label-declarations:
426/// [GNU] label-declaration
427/// [GNU] label-declarations label-declaration
428///
429/// [GNU] label-declaration:
430/// [GNU] '__label__' identifier-list ';'
431///
432/// [OMP] openmp-directive: [TODO]
433/// [OMP] barrier-directive
434/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000435///
John McCall53fa7142010-12-24 02:08:15 +0000436StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000437 bool isStmtExpr) {
438 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000439
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000440 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000441
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000442 // Enter a scope to hold everything within the compound stmt. Compound
443 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000444 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000445
446 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000447 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000448}
449
450
451/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000452/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000453/// consume the '}' at the end of the block. It does not manipulate the scope
454/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000455StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000456 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000457 Tok.getLocation(),
458 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000459 InMessageExpressionRAIIObject InMessage(*this, false);
460
Chris Lattnerf2978802007-01-21 06:52:16 +0000461 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
462
Chris Lattner010015a2007-05-28 07:23:54 +0000463 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000464 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000465
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000466 StmtVector Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000467 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCalldadc5752010-08-24 06:29:42 +0000468 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000469 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000470 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000471 } else {
472 // __extension__ can start declarations and it can also be a unary
473 // operator for expressions. Consume multiple __extension__ markers here
474 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000475 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000476 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000477 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000478 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000479
John McCall53fa7142010-12-24 02:08:15 +0000480 ParsedAttributesWithRange attrs;
481 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000482
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000483 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000484 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000485 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000486 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000487 ExtensionRAIIObject O(Diags);
488
Chris Lattner49836b42009-04-02 04:16:50 +0000489 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000490 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
491 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000492 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000493 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000494 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000495 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000496 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000497
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000498 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000499 SkipUntil(tok::semi);
500 continue;
501 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000502
Alexis Hunt96d5c762009-11-21 08:43:09 +0000503 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000504 // Eat the semicolon at the end of stmt and convert the expr into a
505 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000506 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000507 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000508 }
509 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000510
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000511 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000512 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000513 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000514
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000515 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000516 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000517 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000518 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000519 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000520 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000521
Chris Lattner04132372006-10-16 06:12:55 +0000522 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000523 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000524 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000525}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000526
Chris Lattnerc0081db2008-12-12 06:31:07 +0000527/// ParseParenExprOrCondition:
528/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000529/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000530///
531/// This function parses and performs error recovery on the specified condition
532/// or expression (depending on whether we're in C++ or C mode). This function
533/// goes out of its way to recover well. It returns true if there was a parser
534/// error (the right paren couldn't be found), which indicates that the caller
535/// should try to recover harder. It returns false if the condition is
536/// successfully parsed. Note that a successful parse can still have semantic
537/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000538bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000539 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000540 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000541 bool ConvertToBoolean) {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000542 bool ParseError = false;
543
Chris Lattnerc0081db2008-12-12 06:31:07 +0000544 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000545 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +0000546 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
547 ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000548 else {
549 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000550 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000551
552 // If required, convert to a boolean value.
553 if (!ExprResult.isInvalid() && ConvertToBoolean)
554 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000555 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000556 }
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattnerc0081db2008-12-12 06:31:07 +0000558 // If the parser was confused by the condition and we don't have a ')', try to
559 // recover by skipping ahead to a semi and bailing out. If condexp is
560 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000561 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000562 SkipUntil(tok::semi);
563 // Skipping may have stopped if it found the containing ')'. If so, we can
564 // continue parsing the if statement.
565 if (Tok.isNot(tok::r_paren))
566 return true;
567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattnerc0081db2008-12-12 06:31:07 +0000569 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000570 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000571 return false;
572}
573
574
Chris Lattnerc951dae2006-08-10 04:23:57 +0000575/// ParseIfStatement
576/// if-statement: [C99 6.8.4.1]
577/// 'if' '(' expression ')' statement
578/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000579/// [C++] 'if' '(' condition ')' statement
580/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000581///
John McCall53fa7142010-12-24 02:08:15 +0000582StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000583 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000584
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000585 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000586 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000587
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000588 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000589 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000590 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000591 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000592 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000593
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000594 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
595
Chris Lattner2dd1b722007-08-26 23:08:06 +0000596 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
597 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000598 //
599 // C++ 6.4p3:
600 // A name introduced by a declaration in a condition is in scope from its
601 // point of declaration until the end of the substatements controlled by the
602 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000603 // C++ 3.3.2p4:
604 // Names declared in the for-init-statement, and in the condition of if,
605 // while, for, and switch statements are local to the if, while, for, or
606 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000607 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000608 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000609
Chris Lattnerc951dae2006-08-10 04:23:57 +0000610 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000611 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000612 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000613 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000614 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000615
John McCallb268a282010-08-23 23:25:46 +0000616 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000617
Chris Lattner8fb26252007-08-22 05:28:50 +0000618 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000619 // there is no compound stmt. C90 does not have this clause. We only do this
620 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000621 //
622 // C++ 6.4p1:
623 // The substatement in a selection-statement (each substatement, in the else
624 // form of the if statement) implicitly defines a local scope.
625 //
626 // For C++ we create a scope for the condition and a new scope for
627 // substatements because:
628 // -When the 'then' scope exits, we want the condition declaration to still be
629 // active for the 'else' scope too.
630 // -Sema will detect name clashes by considering declarations of a
631 // 'ControlScope' as part of its direct subscope.
632 // -If we wanted the condition and substatement to be in the same scope, we
633 // would have to notify ParseStatement not to create a new scope. It's
634 // simpler to let it create a new scope.
635 //
Mike Stump11289f42009-09-09 15:08:12 +0000636 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000637 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000638
Chris Lattner5c5808a2007-10-29 05:08:52 +0000639 // Read the 'then' stmt.
640 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000641 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000642
Chris Lattner37e54f42007-08-22 05:16:28 +0000643 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000644 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000645
Chris Lattnerc951dae2006-08-10 04:23:57 +0000646 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000647 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000648 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000649 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000650
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000651 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000652 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000653 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000654
Chris Lattner8fb26252007-08-22 05:28:50 +0000655 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000656 // there is no compound stmt. C90 does not have this clause. We only do
657 // this if the body isn't a compound statement to avoid push/pop in common
658 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000659 //
660 // C++ 6.4p1:
661 // The substatement in a selection-statement (each substatement, in the else
662 // form of the if statement) implicitly defines a local scope.
663 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000664 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000665 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000666
Chris Lattner30f910e2006-10-16 05:52:41 +0000667 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000668
Chris Lattner37e54f42007-08-22 05:16:28 +0000669 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000670 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000671 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000672
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000673 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000675 // If the condition was invalid, discard the if statement. We could recover
676 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000677 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000678 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000679
Chris Lattner5c5808a2007-10-29 05:08:52 +0000680 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000681 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000682 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000683 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
684 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
685 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000686 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000687 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000688 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000689
Chris Lattner5c5808a2007-10-29 05:08:52 +0000690 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000691 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000692 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000693 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000694 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000695
John McCallb268a282010-08-23 23:25:46 +0000696 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000697 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000698}
699
Chris Lattner9075bd72006-08-10 04:59:57 +0000700/// ParseSwitchStatement
701/// switch-statement:
702/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000703/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000704StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000705 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000706
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000707 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000708 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000709
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000710 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000711 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000712 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000713 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000714 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000715
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000716 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
717
Chris Lattner2dd1b722007-08-26 23:08:06 +0000718 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
719 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000720 //
721 // C++ 6.4p3:
722 // A name introduced by a declaration in a condition is in scope from its
723 // point of declaration until the end of the substatements controlled by the
724 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000725 // C++ 3.3.2p4:
726 // Names declared in the for-init-statement, and in the condition of if,
727 // while, for, and switch statements are local to the if, while, for, or
728 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000729 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000730 unsigned ScopeFlags = Scope::BreakScope;
731 if (C99orCXX)
732 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000733 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000734
Chris Lattner9075bd72006-08-10 04:59:57 +0000735 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000736 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000737 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000738 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000739 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000740
John McCalldadc5752010-08-24 06:29:42 +0000741 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000742 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000743
Douglas Gregore60e41a2010-05-06 17:25:47 +0000744 if (Switch.isInvalid()) {
745 // Skip the switch body.
746 // FIXME: This is not optimal recovery, but parsing the body is more
747 // dangerous due to the presence of case and default statements, which
748 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000749 if (Tok.is(tok::l_brace)) {
750 ConsumeBrace();
751 SkipUntil(tok::r_brace, false, false);
752 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000753 SkipUntil(tok::semi);
754 return move(Switch);
755 }
756
Chris Lattner8fb26252007-08-22 05:28:50 +0000757 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000758 // there is no compound stmt. C90 does not have this clause. We only do this
759 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000760 //
761 // C++ 6.4p1:
762 // The substatement in a selection-statement (each substatement, in the else
763 // form of the if statement) implicitly defines a local scope.
764 //
765 // See comments in ParseIfStatement for why we create a scope for the
766 // condition and a new scope for substatement in C++.
767 //
Mike Stump11289f42009-09-09 15:08:12 +0000768 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000769 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000770
Chris Lattner9075bd72006-08-10 04:59:57 +0000771 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000772 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000773
Chris Lattner8fd2d012010-01-24 01:50:29 +0000774 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000775 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000776 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000777
Chris Lattner8fd2d012010-01-24 01:50:29 +0000778 if (Body.isInvalid())
779 // FIXME: Remove the case statement list from the Switch statement.
780 Body = Actions.ActOnNullStmt(Tok.getLocation());
781
John McCallb268a282010-08-23 23:25:46 +0000782 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000783}
784
785/// ParseWhileStatement
786/// while-statement: [C99 6.8.5.1]
787/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000788/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000789StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000790 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000791
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000792 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000793 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000794 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000795
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000796 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000797 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000798 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000799 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000800 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000801
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000802 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
803
Chris Lattner2dd1b722007-08-26 23:08:06 +0000804 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
805 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000806 //
807 // C++ 6.4p3:
808 // A name introduced by a declaration in a condition is in scope from its
809 // point of declaration until the end of the substatements controlled by the
810 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000811 // C++ 3.3.2p4:
812 // Names declared in the for-init-statement, and in the condition of if,
813 // while, for, and switch statements are local to the if, while, for, or
814 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000815 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000816 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000817 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000818 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
819 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000820 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000821 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
822 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000823
Chris Lattner9075bd72006-08-10 04:59:57 +0000824 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000825 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000826 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000827 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000828 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000829
John McCallb268a282010-08-23 23:25:46 +0000830 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattner8fb26252007-08-22 05:28:50 +0000832 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000833 // there is no compound stmt. C90 does not have this clause. We only do this
834 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000835 //
836 // C++ 6.5p2:
837 // The substatement in an iteration-statement implicitly defines a local scope
838 // which is entered and exited each time through the loop.
839 //
840 // See comments in ParseIfStatement for why we create a scope for the
841 // condition and a new scope for substatement in C++.
842 //
Mike Stump11289f42009-09-09 15:08:12 +0000843 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000844 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000845
Chris Lattner9075bd72006-08-10 04:59:57 +0000846 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000847 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000848
Chris Lattner8fb26252007-08-22 05:28:50 +0000849 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000850 InnerScope.Exit();
851 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000852
John McCall48871652010-08-21 09:40:31 +0000853 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000854 return StmtError();
855
John McCallb268a282010-08-23 23:25:46 +0000856 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000857}
858
859/// ParseDoStatement
860/// do-statement: [C99 6.8.5.2]
861/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000862/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +0000863StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000864 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000865
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000866 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000867 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000868
Chris Lattner2dd1b722007-08-26 23:08:06 +0000869 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
870 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000871 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000872 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000873 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000874 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000875 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000876
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000877 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000878
Chris Lattner8fb26252007-08-22 05:28:50 +0000879 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000880 // there is no compound stmt. C90 does not have this clause. We only do this
881 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000882 //
883 // C++ 6.5p2:
884 // The substatement in an iteration-statement implicitly defines a local scope
885 // which is entered and exited each time through the loop.
886 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000887 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000888 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000889 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000890
Chris Lattner9075bd72006-08-10 04:59:57 +0000891 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000892 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000893
Chris Lattner8fb26252007-08-22 05:28:50 +0000894 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000895 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000896
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000897 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000898 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000899 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000900 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000901 SkipUntil(tok::semi, false, true);
902 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000903 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000904 }
Chris Lattneraf635312006-10-16 06:06:51 +0000905 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000906
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000907 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000908 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000909 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000910 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000911 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000912
Chris Lattner10da53c2008-12-12 06:35:28 +0000913 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000914 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000915 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +0000916 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000917 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000918
Sebastian Redlb62406f2008-12-11 19:48:14 +0000919 if (Cond.isInvalid() || Body.isInvalid())
920 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000921
John McCallb268a282010-08-23 23:25:46 +0000922 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
923 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000924}
925
926/// ParseForStatement
927/// for-statement: [C99 6.8.5.3]
928/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
929/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000930/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
931/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000932/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
933/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000934///
935/// [C++] for-init-statement:
936/// [C++] expression-statement
937/// [C++] simple-declaration
938///
John McCall53fa7142010-12-24 02:08:15 +0000939StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000940 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000941
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000942 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000943 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000944
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000945 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000946 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000947 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000948 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000949 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000950
Chris Lattner934074c2009-04-22 00:54:41 +0000951 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000952
Chris Lattner2dd1b722007-08-26 23:08:06 +0000953 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
954 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000955 //
956 // C++ 6.4p3:
957 // A name introduced by a declaration in a condition is in scope from its
958 // point of declaration until the end of the substatements controlled by the
959 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000960 // C++ 3.3.2p4:
961 // Names declared in the for-init-statement, and in the condition of if,
962 // while, for, and switch statements are local to the if, while, for, or
963 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000964 // C++ 6.5.3p1:
965 // Names declared in the for-init-statement are in the same declarative-region
966 // as those declared in the condition.
967 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000968 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000969 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000970 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
971 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000972 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000973 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
974
975 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000976
Chris Lattner04132372006-10-16 06:12:55 +0000977 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000978 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000979
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000980 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +0000981 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000982 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000983 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +0000984 ExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000985 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +0000986 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000987
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000988 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000989 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000990 C99orCXXorObjC? Sema::PCC_ForInit
991 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000992 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000993 }
994
Chris Lattner9075bd72006-08-10 04:59:57 +0000995 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000996 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000997 // no first part, eat the ';'.
998 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +0000999 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001000 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001001 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001002 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001003
John McCall53fa7142010-12-24 02:08:15 +00001004 ParsedAttributesWithRange attrs;
1005 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001006
Chris Lattner49836b42009-04-02 04:16:50 +00001007 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001008 StmtVector Stmts(Actions);
1009 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
John McCall53fa7142010-12-24 02:08:15 +00001010 DeclEnd, attrs, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001011 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001012
Chris Lattner32dc41c2009-03-29 17:27:48 +00001013 if (Tok.is(tok::semi)) { // for (int x = 4;
1014 ConsumeToken();
1015 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001016 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001017 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001018 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001019
1020 if (Tok.is(tok::code_completion)) {
1021 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1022 ConsumeCodeCompletionToken();
1023 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001024 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001025 } else {
1026 Diag(Tok, diag::err_expected_semi_for);
1027 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001028 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001029 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001030 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001031
John McCall34376a62010-12-04 03:47:34 +00001032 ForEach = isTokIdentifier_in();
1033
Chris Lattnercd68f642007-06-27 01:06:29 +00001034 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001035 if (!Value.isInvalid()) {
1036 if (ForEach)
1037 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1038 else
1039 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1040 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001041
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001042 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001043 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001044 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001045 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001046
1047 if (Tok.is(tok::code_completion)) {
1048 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1049 ConsumeCodeCompletionToken();
1050 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001051 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001052 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001053 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001054 SkipUntil(tok::semi);
1055 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001056 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001057 if (!ForEach) {
John McCallb268a282010-08-23 23:25:46 +00001058 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001059 // Parse the second part of the for specifier.
1060 if (Tok.is(tok::semi)) { // for (...;;
1061 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001062 } else {
John McCalldadc5752010-08-24 06:29:42 +00001063 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001064 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001065 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1066 else {
1067 Second = ParseExpression();
1068 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001069 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001070 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001071 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001072 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001073 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001074 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001075
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001076 if (Tok.is(tok::semi)) {
1077 ConsumeToken();
1078 } else {
John McCall48871652010-08-21 09:40:31 +00001079 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001080 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001081 SkipUntil(tok::semi);
1082 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001083
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001084 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001085 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001086 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001087 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001088 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001089 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001090 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001091 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001092
Chris Lattner8fb26252007-08-22 05:28:50 +00001093 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001094 // there is no compound stmt. C90 does not have this clause. We only do this
1095 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001096 //
1097 // C++ 6.5p2:
1098 // The substatement in an iteration-statement implicitly defines a local scope
1099 // which is entered and exited each time through the loop.
1100 //
1101 // See comments in ParseIfStatement for why we create a scope for
1102 // for-init-statement/condition and a new scope for substatement in C++.
1103 //
Mike Stump11289f42009-09-09 15:08:12 +00001104 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001105 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001106
Chris Lattner9075bd72006-08-10 04:59:57 +00001107 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001108 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001109
Chris Lattner8fb26252007-08-22 05:28:50 +00001110 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001111 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001112
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001113 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001114 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001115
1116 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001117 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001118
1119 if (!ForEach)
John McCallb268a282010-08-23 23:25:46 +00001120 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1121 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregore60e41a2010-05-06 17:25:47 +00001123 // FIXME: It isn't clear how to communicate the late destruction of
1124 // C++ temporaries used to create the collection.
John McCallb268a282010-08-23 23:25:46 +00001125 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1126 Collection.take(), RParenLoc,
1127 Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001128}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001129
Chris Lattner503fadc2006-08-10 05:45:44 +00001130/// ParseGotoStatement
1131/// jump-statement:
1132/// 'goto' identifier ';'
1133/// [GNU] 'goto' '*' expression ';'
1134///
1135/// Note: this lets the caller parse the end ';'.
1136///
John McCall53fa7142010-12-24 02:08:15 +00001137StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001138 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001139
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001140 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001141 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001142
John McCalldadc5752010-08-24 06:29:42 +00001143 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001144 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001145 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001146 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001147 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001148 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001149 // GNU indirect goto extension.
1150 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001151 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001152 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001153 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001154 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001155 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001156 }
John McCallb268a282010-08-23 23:25:46 +00001157 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001158 } else {
1159 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001160 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001161 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001162
Sebastian Redlb62406f2008-12-11 19:48:14 +00001163 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001164}
1165
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001166/// ParseContinueStatement
1167/// jump-statement:
1168/// 'continue' ';'
1169///
1170/// Note: this lets the caller parse the end ';'.
1171///
John McCall53fa7142010-12-24 02:08:15 +00001172StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001173 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001174
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001175 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001176 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001177}
1178
1179/// ParseBreakStatement
1180/// jump-statement:
1181/// 'break' ';'
1182///
1183/// Note: this lets the caller parse the end ';'.
1184///
John McCall53fa7142010-12-24 02:08:15 +00001185StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001186 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001187
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001188 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001189 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001190}
1191
Chris Lattner503fadc2006-08-10 05:45:44 +00001192/// ParseReturnStatement
1193/// jump-statement:
1194/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001195StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001196 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001197
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001198 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001199 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001200
John McCalldadc5752010-08-24 06:29:42 +00001201 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001202 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001203 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001204 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001205 ConsumeCodeCompletionToken();
1206 SkipUntil(tok::semi, false, true);
1207 return StmtError();
1208 }
1209
Chris Lattner30f910e2006-10-16 05:52:41 +00001210 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001211 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001212 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001213 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001214 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001215 }
John McCallb268a282010-08-23 23:25:46 +00001216 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001217}
Chris Lattner0116c472006-08-15 06:03:28 +00001218
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001219/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1220/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001221StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1222 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001223 if (Tok.is(tok::l_brace)) {
1224 unsigned short savedBraceCount = BraceCount;
1225 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001226 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001227 ConsumeAnyToken();
1228 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001229 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001230 // From the MS website: If used without braces, the __asm keyword means
1231 // that the rest of the line is an assembly-language statement.
1232 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001233 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001234 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001235 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001236 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001237 ConsumeAnyToken();
1238 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001239 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1240 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001241 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001242 }
Mike Stump6da5d752009-12-11 00:04:56 +00001243 Token t;
1244 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001245 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001246 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001247 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001248 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001249 ExprVector Constraints(Actions);
1250 ExprVector Exprs(Actions);
1251 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001252 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001253 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001254 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001255 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001256}
1257
Chris Lattner0116c472006-08-15 06:03:28 +00001258/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001259/// asm-statement:
1260/// gnu-asm-statement
1261/// ms-asm-statement
1262///
1263/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001264/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1265///
1266/// [GNU] asm-argument:
1267/// asm-string-literal
1268/// asm-string-literal ':' asm-operands[opt]
1269/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1270/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1271/// ':' asm-clobbers
1272///
1273/// [GNU] asm-clobbers:
1274/// asm-string-literal
1275/// asm-clobbers ',' asm-string-literal
1276///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001277/// [MS] ms-asm-statement:
1278/// '__asm' assembly-instruction ';'[opt]
1279/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1280///
1281/// [MS] assembly-instruction-list:
1282/// assembly-instruction ';'[opt]
1283/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1284///
John McCalldadc5752010-08-24 06:29:42 +00001285StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001286 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001287 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001288
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001289 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001290 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001291 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001292 }
Chris Lattner0116c472006-08-15 06:03:28 +00001293 DeclSpec DS;
1294 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001295 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001296
Chris Lattner0116c472006-08-15 06:03:28 +00001297 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001298 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001299 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001300 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001301 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001302
Chris Lattner0116c472006-08-15 06:03:28 +00001303 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001304 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001305 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001306 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001307 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001308 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001309 }
Chris Lattner04132372006-10-16 06:12:55 +00001310 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001311
John McCalldadc5752010-08-24 06:29:42 +00001312 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001313 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001314 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001315
Anders Carlsson9a020f92010-01-30 22:25:16 +00001316 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001317 ExprVector Constraints(Actions);
1318 ExprVector Exprs(Actions);
1319 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001320
Anders Carlsson19fe1162008-02-05 23:03:50 +00001321 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001322 // We have a simple asm expression like 'asm("foo")'.
1323 SourceLocation RParenLoc = ConsumeParen();
1324 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1325 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1326 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001327 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001328 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001329 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001330
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001331 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001332 bool AteExtraColon = false;
1333 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1334 // In C++ mode, parse "::" like ": :".
1335 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001336 ConsumeToken();
1337
Chris Lattner15768502009-12-20 23:08:04 +00001338 if (!AteExtraColon &&
1339 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001340 return StmtError();
1341 }
Chris Lattner15768502009-12-20 23:08:04 +00001342
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001343 unsigned NumOutputs = Names.size();
1344
1345 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001346 if (AteExtraColon ||
1347 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1348 // In C++ mode, parse "::" like ": :".
1349 if (AteExtraColon)
1350 AteExtraColon = false;
1351 else {
1352 AteExtraColon = Tok.is(tok::coloncolon);
1353 ConsumeToken();
1354 }
1355
1356 if (!AteExtraColon &&
1357 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001358 return StmtError();
1359 }
1360
1361 assert(Names.size() == Constraints.size() &&
1362 Constraints.size() == Exprs.size() &&
1363 "Input operand size mismatch!");
1364
1365 unsigned NumInputs = Names.size() - NumOutputs;
1366
1367 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001368 if (AteExtraColon || Tok.is(tok::colon)) {
1369 if (!AteExtraColon)
1370 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001371
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001372 // Parse the asm-string list for clobbers if present.
1373 if (Tok.isNot(tok::r_paren)) {
1374 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001375 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001376
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001377 if (Clobber.isInvalid())
1378 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001379
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001380 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001381
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001382 if (Tok.isNot(tok::comma)) break;
1383 ConsumeToken();
1384 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001385 }
1386 }
1387
1388 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1389 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001390 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001391 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001392 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001393 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001394}
1395
1396/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001397/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001398///
1399/// [GNU] asm-operands:
1400/// asm-operand
1401/// asm-operands ',' asm-operand
1402///
1403/// [GNU] asm-operand:
1404/// asm-string-literal '(' expression ')'
1405/// '[' identifier ']' asm-string-literal '(' expression ')'
1406///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001407//
1408// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001409bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1410 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1411 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001412 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001413 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001414 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001415
1416 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001417 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001418 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001419 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001420
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001421 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001422 Diag(Tok, diag::err_expected_ident);
1423 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001424 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001425 }
Mike Stump11289f42009-09-09 15:08:12 +00001426
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001427 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001428 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001429
Anders Carlsson9a020f92010-01-30 22:25:16 +00001430 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001431 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001432 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001433 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001434
John McCalldadc5752010-08-24 06:29:42 +00001435 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001436 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001437 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001438 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001439 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001440 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001441
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001442 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001443 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001444 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001445 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001446 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001447
Chris Lattner0116c472006-08-15 06:03:28 +00001448 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001449 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001450 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001451 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001452 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001453 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001454 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001455 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001456 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001457 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001458 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001459 ConsumeToken();
1460 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001461
1462 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001463}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001464
John McCall48871652010-08-21 09:40:31 +00001465Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001466 assert(Tok.is(tok::l_brace));
1467 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001468
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +00001469 // When in code-completion, skip parsing for all function bodies unless
1470 // the body contains the code-completion point.
1471 if (PP.isCodeCompletionEnabled()) {
1472 TentativeParsingAction PA(*this);
1473 ConsumeBrace();
1474 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1475 /*StopAtCodeCompletion=*/true)) {
1476 PA.Commit();
1477 return Actions.ActOnFinishFunctionBody(Decl, 0);
1478 }
1479 PA.Revert();
1480 }
1481
John McCallfaf5fb42010-08-26 23:41:50 +00001482 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1483 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001484
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001485 // Do not enter a scope for the brace, as the arguments are in the same scope
1486 // (the function body) as the body itself. Instead, just read the statement
1487 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001488 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001489
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001490 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001491 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001492 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001493 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001494
John McCallb268a282010-08-23 23:25:46 +00001495 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001496}
Sebastian Redlb219c902008-12-21 16:41:36 +00001497
Sebastian Redla7b98a72009-04-26 20:35:05 +00001498/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1499///
1500/// function-try-block:
1501/// 'try' ctor-initializer[opt] compound-statement handler-seq
1502///
John McCall48871652010-08-21 09:40:31 +00001503Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001504 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1505 SourceLocation TryLoc = ConsumeToken();
1506
John McCallfaf5fb42010-08-26 23:41:50 +00001507 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1508 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001509
1510 // Constructor initializer list?
1511 if (Tok.is(tok::colon))
1512 ParseConstructorInitializer(Decl);
1513
Sebastian Redld98ecd62009-04-26 21:08:36 +00001514 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001515 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001516 // If we failed to parse the try-catch, we just give the function an empty
1517 // compound statement as the body.
1518 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001519 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001520 MultiStmtArg(Actions), false);
1521
John McCallb268a282010-08-23 23:25:46 +00001522 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001523}
1524
Sebastian Redlb219c902008-12-21 16:41:36 +00001525/// ParseCXXTryBlock - Parse a C++ try-block.
1526///
1527/// try-block:
1528/// 'try' compound-statement handler-seq
1529///
John McCall53fa7142010-12-24 02:08:15 +00001530StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001531 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001532
Sebastian Redlb219c902008-12-21 16:41:36 +00001533 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1534
1535 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001536 return ParseCXXTryBlockCommon(TryLoc);
1537}
1538
1539/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1540/// function-try-block.
1541///
1542/// try-block:
1543/// 'try' compound-statement handler-seq
1544///
1545/// function-try-block:
1546/// 'try' ctor-initializer[opt] compound-statement handler-seq
1547///
1548/// handler-seq:
1549/// handler handler-seq[opt]
1550///
John McCalldadc5752010-08-24 06:29:42 +00001551StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001552 if (Tok.isNot(tok::l_brace))
1553 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001554 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall53fa7142010-12-24 02:08:15 +00001555 ParsedAttributesWithRange attrs;
1556 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001557 if (TryBlock.isInvalid())
1558 return move(TryBlock);
1559
1560 StmtVector Handlers(Actions);
John McCall53fa7142010-12-24 02:08:15 +00001561 MaybeParseCXX0XAttributes(attrs);
1562 ProhibitAttributes(attrs);
1563
Sebastian Redlb219c902008-12-21 16:41:36 +00001564 if (Tok.isNot(tok::kw_catch))
1565 return StmtError(Diag(Tok, diag::err_expected_catch));
1566 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001567 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001568 if (!Handler.isInvalid())
1569 Handlers.push_back(Handler.release());
1570 }
1571 // Don't bother creating the full statement if we don't have any usable
1572 // handlers.
1573 if (Handlers.empty())
1574 return StmtError();
1575
John McCallb268a282010-08-23 23:25:46 +00001576 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001577}
1578
1579/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1580///
1581/// handler:
1582/// 'catch' '(' exception-declaration ')' compound-statement
1583///
1584/// exception-declaration:
1585/// type-specifier-seq declarator
1586/// type-specifier-seq abstract-declarator
1587/// type-specifier-seq
1588/// '...'
1589///
John McCalldadc5752010-08-24 06:29:42 +00001590StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001591 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1592
1593 SourceLocation CatchLoc = ConsumeToken();
1594
1595 SourceLocation LParenLoc = Tok.getLocation();
1596 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1597 return StmtError();
1598
1599 // C++ 3.3.2p3:
1600 // The name in a catch exception-declaration is local to the handler and
1601 // shall not be redeclared in the outermost block of the handler.
1602 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1603
1604 // exception-declaration is equivalent to '...' or a parameter-declaration
1605 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001606 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001607 if (Tok.isNot(tok::ellipsis)) {
1608 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001609 if (ParseCXXTypeSpecifierSeq(DS))
1610 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001611 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1612 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001613 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001614 } else
1615 ConsumeToken();
1616
1617 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1618 return StmtError();
1619
1620 if (Tok.isNot(tok::l_brace))
1621 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1622
Alexis Hunt96d5c762009-11-21 08:43:09 +00001623 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall53fa7142010-12-24 02:08:15 +00001624 ParsedAttributes attrs;
1625 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001626 if (Block.isInvalid())
1627 return move(Block);
1628
John McCallb268a282010-08-23 23:25:46 +00001629 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001630}