blob: 3a6f80da2d997620439e15bdecb4808dfed8d46d [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)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000468
469 if (Tok.is(tok::annot_pragma_unused)) {
470 HandlePragmaUnused();
471 continue;
472 }
473
John McCalldadc5752010-08-24 06:29:42 +0000474 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000475 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000476 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000477 } else {
478 // __extension__ can start declarations and it can also be a unary
479 // operator for expressions. Consume multiple __extension__ markers here
480 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000481 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000482 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000483 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000484 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000485
John McCall53fa7142010-12-24 02:08:15 +0000486 ParsedAttributesWithRange attrs;
487 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000488
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000489 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000490 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000491 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000492 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000493 ExtensionRAIIObject O(Diags);
494
Chris Lattner49836b42009-04-02 04:16:50 +0000495 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000496 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
497 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000498 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000499 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000500 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000501 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000502 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000503
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000504 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000505 SkipUntil(tok::semi);
506 continue;
507 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000508
Alexis Hunt96d5c762009-11-21 08:43:09 +0000509 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000510 // Eat the semicolon at the end of stmt and convert the expr into a
511 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000512 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000513 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000514 }
515 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000516
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000517 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000518 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000519 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000520
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000521 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000522 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000523 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000524 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000525 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000526 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000527
Chris Lattner04132372006-10-16 06:12:55 +0000528 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000529 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000530 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000531}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000532
Chris Lattnerc0081db2008-12-12 06:31:07 +0000533/// ParseParenExprOrCondition:
534/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000535/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000536///
537/// This function parses and performs error recovery on the specified condition
538/// or expression (depending on whether we're in C++ or C mode). This function
539/// goes out of its way to recover well. It returns true if there was a parser
540/// error (the right paren couldn't be found), which indicates that the caller
541/// should try to recover harder. It returns false if the condition is
542/// successfully parsed. Note that a successful parse can still have semantic
543/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000544bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000545 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000546 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000547 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000548 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000549 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000550 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000551 else {
552 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000553 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000554
555 // If required, convert to a boolean value.
556 if (!ExprResult.isInvalid() && ConvertToBoolean)
557 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000558 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattnerc0081db2008-12-12 06:31:07 +0000561 // If the parser was confused by the condition and we don't have a ')', try to
562 // recover by skipping ahead to a semi and bailing out. If condexp is
563 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000564 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000565 SkipUntil(tok::semi);
566 // Skipping may have stopped if it found the containing ')'. If so, we can
567 // continue parsing the if statement.
568 if (Tok.isNot(tok::r_paren))
569 return true;
570 }
Mike Stump11289f42009-09-09 15:08:12 +0000571
Chris Lattnerc0081db2008-12-12 06:31:07 +0000572 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000573 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000574 return false;
575}
576
577
Chris Lattnerc951dae2006-08-10 04:23:57 +0000578/// ParseIfStatement
579/// if-statement: [C99 6.8.4.1]
580/// 'if' '(' expression ')' statement
581/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000582/// [C++] 'if' '(' condition ')' statement
583/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000584///
John McCall53fa7142010-12-24 02:08:15 +0000585StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000586 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000587
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000588 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000589 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000590
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000591 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000592 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000593 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000594 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000595 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000596
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000597 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
598
Chris Lattner2dd1b722007-08-26 23:08:06 +0000599 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
600 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000601 //
602 // C++ 6.4p3:
603 // A name introduced by a declaration in a condition is in scope from its
604 // point of declaration until the end of the substatements controlled by the
605 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000606 // C++ 3.3.2p4:
607 // Names declared in the for-init-statement, and in the condition of if,
608 // while, for, and switch statements are local to the if, while, for, or
609 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000610 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000611 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000612
Chris Lattnerc951dae2006-08-10 04:23:57 +0000613 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000614 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000615 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000616 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000617 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000618
John McCallb268a282010-08-23 23:25:46 +0000619 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner8fb26252007-08-22 05:28:50 +0000621 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000622 // there is no compound stmt. C90 does not have this clause. We only do this
623 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000624 //
625 // C++ 6.4p1:
626 // The substatement in a selection-statement (each substatement, in the else
627 // form of the if statement) implicitly defines a local scope.
628 //
629 // For C++ we create a scope for the condition and a new scope for
630 // substatements because:
631 // -When the 'then' scope exits, we want the condition declaration to still be
632 // active for the 'else' scope too.
633 // -Sema will detect name clashes by considering declarations of a
634 // 'ControlScope' as part of its direct subscope.
635 // -If we wanted the condition and substatement to be in the same scope, we
636 // would have to notify ParseStatement not to create a new scope. It's
637 // simpler to let it create a new scope.
638 //
Mike Stump11289f42009-09-09 15:08:12 +0000639 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000640 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000641
Chris Lattner5c5808a2007-10-29 05:08:52 +0000642 // Read the 'then' stmt.
643 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000644 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000645
Chris Lattner37e54f42007-08-22 05:16:28 +0000646 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000647 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000648
Chris Lattnerc951dae2006-08-10 04:23:57 +0000649 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000650 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000651 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000652 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000653
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000654 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000655 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000656 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000657
Chris Lattner8fb26252007-08-22 05:28:50 +0000658 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000659 // there is no compound stmt. C90 does not have this clause. We only do
660 // this if the body isn't a compound statement to avoid push/pop in common
661 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000662 //
663 // C++ 6.4p1:
664 // The substatement in a selection-statement (each substatement, in the else
665 // form of the if statement) implicitly defines a local scope.
666 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000667 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000668 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000669
Chris Lattner30f910e2006-10-16 05:52:41 +0000670 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000671
Chris Lattner37e54f42007-08-22 05:16:28 +0000672 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000673 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000674 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000675
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000676 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000678 // If the condition was invalid, discard the if statement. We could recover
679 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000680 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000681 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000682
Chris Lattner5c5808a2007-10-29 05:08:52 +0000683 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000684 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000685 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000686 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
687 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
688 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000689 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000690 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000691 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000692
Chris Lattner5c5808a2007-10-29 05:08:52 +0000693 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000694 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000695 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000696 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000697 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000698
John McCallb268a282010-08-23 23:25:46 +0000699 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000700 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000701}
702
Chris Lattner9075bd72006-08-10 04:59:57 +0000703/// ParseSwitchStatement
704/// switch-statement:
705/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000706/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000707StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000708 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000709
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000710 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000711 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000712
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000713 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000714 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000715 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000716 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000717 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000718
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000719 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
720
Chris Lattner2dd1b722007-08-26 23:08:06 +0000721 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
722 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000723 //
724 // C++ 6.4p3:
725 // A name introduced by a declaration in a condition is in scope from its
726 // point of declaration until the end of the substatements controlled by the
727 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000728 // C++ 3.3.2p4:
729 // Names declared in the for-init-statement, and in the condition of if,
730 // while, for, and switch statements are local to the if, while, for, or
731 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000732 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000733 unsigned ScopeFlags = Scope::BreakScope;
734 if (C99orCXX)
735 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000736 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000737
Chris Lattner9075bd72006-08-10 04:59:57 +0000738 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000739 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000740 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000741 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000742 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000743
John McCalldadc5752010-08-24 06:29:42 +0000744 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000745 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000746
Douglas Gregore60e41a2010-05-06 17:25:47 +0000747 if (Switch.isInvalid()) {
748 // Skip the switch body.
749 // FIXME: This is not optimal recovery, but parsing the body is more
750 // dangerous due to the presence of case and default statements, which
751 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000752 if (Tok.is(tok::l_brace)) {
753 ConsumeBrace();
754 SkipUntil(tok::r_brace, false, false);
755 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000756 SkipUntil(tok::semi);
757 return move(Switch);
758 }
759
Chris Lattner8fb26252007-08-22 05:28:50 +0000760 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000761 // there is no compound stmt. C90 does not have this clause. We only do this
762 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000763 //
764 // C++ 6.4p1:
765 // The substatement in a selection-statement (each substatement, in the else
766 // form of the if statement) implicitly defines a local scope.
767 //
768 // See comments in ParseIfStatement for why we create a scope for the
769 // condition and a new scope for substatement in C++.
770 //
Mike Stump11289f42009-09-09 15:08:12 +0000771 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000772 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000773
Chris Lattner9075bd72006-08-10 04:59:57 +0000774 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000775 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000776
Chris Lattner8fd2d012010-01-24 01:50:29 +0000777 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000778 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000779 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000780
Chris Lattner8fd2d012010-01-24 01:50:29 +0000781 if (Body.isInvalid())
782 // FIXME: Remove the case statement list from the Switch statement.
783 Body = Actions.ActOnNullStmt(Tok.getLocation());
784
John McCallb268a282010-08-23 23:25:46 +0000785 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000786}
787
788/// ParseWhileStatement
789/// while-statement: [C99 6.8.5.1]
790/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000791/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000792StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000793 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000794
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000795 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000796 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000797 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000798
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000799 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000800 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000801 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000802 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000803 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000804
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000805 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
806
Chris Lattner2dd1b722007-08-26 23:08:06 +0000807 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
808 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000809 //
810 // C++ 6.4p3:
811 // A name introduced by a declaration in a condition is in scope from its
812 // point of declaration until the end of the substatements controlled by the
813 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000814 // C++ 3.3.2p4:
815 // Names declared in the for-init-statement, and in the condition of if,
816 // while, for, and switch statements are local to the if, while, for, or
817 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000818 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000819 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000820 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000821 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
822 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000823 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000824 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
825 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000826
Chris Lattner9075bd72006-08-10 04:59:57 +0000827 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000828 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000829 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000830 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000831 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000832
John McCallb268a282010-08-23 23:25:46 +0000833 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000834
Chris Lattner8fb26252007-08-22 05:28:50 +0000835 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000836 // there is no compound stmt. C90 does not have this clause. We only do this
837 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000838 //
839 // C++ 6.5p2:
840 // The substatement in an iteration-statement implicitly defines a local scope
841 // which is entered and exited each time through the loop.
842 //
843 // See comments in ParseIfStatement for why we create a scope for the
844 // condition and a new scope for substatement in C++.
845 //
Mike Stump11289f42009-09-09 15:08:12 +0000846 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000847 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000848
Chris Lattner9075bd72006-08-10 04:59:57 +0000849 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000850 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000851
Chris Lattner8fb26252007-08-22 05:28:50 +0000852 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000853 InnerScope.Exit();
854 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000855
John McCall48871652010-08-21 09:40:31 +0000856 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000857 return StmtError();
858
John McCallb268a282010-08-23 23:25:46 +0000859 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000860}
861
862/// ParseDoStatement
863/// do-statement: [C99 6.8.5.2]
864/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000865/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +0000866StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000867 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000868
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000869 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000870 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000871
Chris Lattner2dd1b722007-08-26 23:08:06 +0000872 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
873 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000874 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000875 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000876 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000877 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000878 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000879
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000880 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000881
Chris Lattner8fb26252007-08-22 05:28:50 +0000882 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000883 // there is no compound stmt. C90 does not have this clause. We only do this
884 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000885 //
886 // C++ 6.5p2:
887 // The substatement in an iteration-statement implicitly defines a local scope
888 // which is entered and exited each time through the loop.
889 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000890 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000891 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000892 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000893
Chris Lattner9075bd72006-08-10 04:59:57 +0000894 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000895 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000896
Chris Lattner8fb26252007-08-22 05:28:50 +0000897 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000898 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000899
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000900 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000901 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000902 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000903 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000904 SkipUntil(tok::semi, false, true);
905 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000906 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000907 }
Chris Lattneraf635312006-10-16 06:06:51 +0000908 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000909
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000910 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000911 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000912 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000913 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000914 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000915
Chris Lattner10da53c2008-12-12 06:35:28 +0000916 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000917 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000918 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +0000919 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000920 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000921
Sebastian Redlb62406f2008-12-11 19:48:14 +0000922 if (Cond.isInvalid() || Body.isInvalid())
923 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000924
John McCallb268a282010-08-23 23:25:46 +0000925 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
926 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000927}
928
929/// ParseForStatement
930/// for-statement: [C99 6.8.5.3]
931/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
932/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000933/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
934/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000935/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
936/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000937///
938/// [C++] for-init-statement:
939/// [C++] expression-statement
940/// [C++] simple-declaration
941///
John McCall53fa7142010-12-24 02:08:15 +0000942StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000943 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000944
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000945 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000946 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000947
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000948 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000949 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000950 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000951 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000952 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000953
Chris Lattner934074c2009-04-22 00:54:41 +0000954 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000955
Chris Lattner2dd1b722007-08-26 23:08:06 +0000956 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
957 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000958 //
959 // C++ 6.4p3:
960 // A name introduced by a declaration in a condition is in scope from its
961 // point of declaration until the end of the substatements controlled by the
962 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000963 // C++ 3.3.2p4:
964 // Names declared in the for-init-statement, and in the condition of if,
965 // while, for, and switch statements are local to the if, while, for, or
966 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000967 // C++ 6.5.3p1:
968 // Names declared in the for-init-statement are in the same declarative-region
969 // as those declared in the condition.
970 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000971 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000972 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000973 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
974 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000975 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000976 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
977
978 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000979
Chris Lattner04132372006-10-16 06:12:55 +0000980 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000981 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000982
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000983 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +0000984 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000985 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000986 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +0000987 ExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000988 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +0000989 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000990
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000991 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000992 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000993 C99orCXXorObjC? Sema::PCC_ForInit
994 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000995 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000996 }
997
Chris Lattner9075bd72006-08-10 04:59:57 +0000998 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000999 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001000 // no first part, eat the ';'.
1001 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001002 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001003 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001004 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001005 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001006
John McCall53fa7142010-12-24 02:08:15 +00001007 ParsedAttributesWithRange attrs;
1008 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001009
Chris Lattner49836b42009-04-02 04:16:50 +00001010 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001011 StmtVector Stmts(Actions);
1012 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
John McCall53fa7142010-12-24 02:08:15 +00001013 DeclEnd, attrs, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001014 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattner32dc41c2009-03-29 17:27:48 +00001016 if (Tok.is(tok::semi)) { // for (int x = 4;
1017 ConsumeToken();
1018 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001019 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001020 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001021 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001022
1023 if (Tok.is(tok::code_completion)) {
1024 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1025 ConsumeCodeCompletionToken();
1026 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001027 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001028 } else {
1029 Diag(Tok, diag::err_expected_semi_for);
1030 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001031 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001032 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001033 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001034
John McCall34376a62010-12-04 03:47:34 +00001035 ForEach = isTokIdentifier_in();
1036
Chris Lattnercd68f642007-06-27 01:06:29 +00001037 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001038 if (!Value.isInvalid()) {
1039 if (ForEach)
1040 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1041 else
1042 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1043 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001044
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001045 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001046 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001047 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001048 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001049
1050 if (Tok.is(tok::code_completion)) {
1051 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1052 ConsumeCodeCompletionToken();
1053 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001054 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001055 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001056 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001057 SkipUntil(tok::semi);
1058 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001059 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001060 if (!ForEach) {
John McCallb268a282010-08-23 23:25:46 +00001061 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001062 // Parse the second part of the for specifier.
1063 if (Tok.is(tok::semi)) { // for (...;;
1064 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001065 } else {
John McCalldadc5752010-08-24 06:29:42 +00001066 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001067 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001068 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1069 else {
1070 Second = ParseExpression();
1071 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001072 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001073 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001074 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001075 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001076 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001077 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001078
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001079 if (Tok.is(tok::semi)) {
1080 ConsumeToken();
1081 } else {
John McCall48871652010-08-21 09:40:31 +00001082 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001083 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001084 SkipUntil(tok::semi);
1085 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001086
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001087 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001088 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001089 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001090 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001091 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001092 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001093 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001094 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001095
Chris Lattner8fb26252007-08-22 05:28:50 +00001096 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001097 // there is no compound stmt. C90 does not have this clause. We only do this
1098 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001099 //
1100 // C++ 6.5p2:
1101 // The substatement in an iteration-statement implicitly defines a local scope
1102 // which is entered and exited each time through the loop.
1103 //
1104 // See comments in ParseIfStatement for why we create a scope for
1105 // for-init-statement/condition and a new scope for substatement in C++.
1106 //
Mike Stump11289f42009-09-09 15:08:12 +00001107 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001108 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001109
Chris Lattner9075bd72006-08-10 04:59:57 +00001110 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001111 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001112
Chris Lattner8fb26252007-08-22 05:28:50 +00001113 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001114 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001115
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001116 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001117 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001118
1119 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001120 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001121
1122 if (!ForEach)
John McCallb268a282010-08-23 23:25:46 +00001123 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1124 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001125
Douglas Gregore60e41a2010-05-06 17:25:47 +00001126 // FIXME: It isn't clear how to communicate the late destruction of
1127 // C++ temporaries used to create the collection.
John McCallb268a282010-08-23 23:25:46 +00001128 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1129 Collection.take(), RParenLoc,
1130 Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001131}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001132
Chris Lattner503fadc2006-08-10 05:45:44 +00001133/// ParseGotoStatement
1134/// jump-statement:
1135/// 'goto' identifier ';'
1136/// [GNU] 'goto' '*' expression ';'
1137///
1138/// Note: this lets the caller parse the end ';'.
1139///
John McCall53fa7142010-12-24 02:08:15 +00001140StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001141 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001142
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001143 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001144 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001145
John McCalldadc5752010-08-24 06:29:42 +00001146 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001147 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001148 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001149 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001150 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001151 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001152 // GNU indirect goto extension.
1153 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001154 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001155 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001156 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001157 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001158 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001159 }
John McCallb268a282010-08-23 23:25:46 +00001160 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001161 } else {
1162 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001163 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001164 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001165
Sebastian Redlb62406f2008-12-11 19:48:14 +00001166 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001167}
1168
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001169/// ParseContinueStatement
1170/// jump-statement:
1171/// 'continue' ';'
1172///
1173/// Note: this lets the caller parse the end ';'.
1174///
John McCall53fa7142010-12-24 02:08:15 +00001175StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001176 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001177
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001178 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001179 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001180}
1181
1182/// ParseBreakStatement
1183/// jump-statement:
1184/// 'break' ';'
1185///
1186/// Note: this lets the caller parse the end ';'.
1187///
John McCall53fa7142010-12-24 02:08:15 +00001188StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001189 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001190
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001191 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001192 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001193}
1194
Chris Lattner503fadc2006-08-10 05:45:44 +00001195/// ParseReturnStatement
1196/// jump-statement:
1197/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001198StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001199 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001200
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001201 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001202 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001203
John McCalldadc5752010-08-24 06:29:42 +00001204 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001205 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001206 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001207 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001208 ConsumeCodeCompletionToken();
1209 SkipUntil(tok::semi, false, true);
1210 return StmtError();
1211 }
1212
Chris Lattner30f910e2006-10-16 05:52:41 +00001213 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001214 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001215 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001216 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001217 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001218 }
John McCallb268a282010-08-23 23:25:46 +00001219 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001220}
Chris Lattner0116c472006-08-15 06:03:28 +00001221
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001222/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1223/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001224StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1225 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001226 if (Tok.is(tok::l_brace)) {
1227 unsigned short savedBraceCount = BraceCount;
1228 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001229 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001230 ConsumeAnyToken();
1231 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001232 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001233 // From the MS website: If used without braces, the __asm keyword means
1234 // that the rest of the line is an assembly-language statement.
1235 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001236 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001237 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001238 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001239 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001240 ConsumeAnyToken();
1241 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001242 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1243 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001244 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001245 }
Mike Stump6da5d752009-12-11 00:04:56 +00001246 Token t;
1247 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001248 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001249 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001250 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001251 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001252 ExprVector Constraints(Actions);
1253 ExprVector Exprs(Actions);
1254 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001255 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001256 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001257 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001258 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001259}
1260
Chris Lattner0116c472006-08-15 06:03:28 +00001261/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001262/// asm-statement:
1263/// gnu-asm-statement
1264/// ms-asm-statement
1265///
1266/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001267/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1268///
1269/// [GNU] asm-argument:
1270/// asm-string-literal
1271/// asm-string-literal ':' asm-operands[opt]
1272/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1273/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1274/// ':' asm-clobbers
1275///
1276/// [GNU] asm-clobbers:
1277/// asm-string-literal
1278/// asm-clobbers ',' asm-string-literal
1279///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001280/// [MS] ms-asm-statement:
1281/// '__asm' assembly-instruction ';'[opt]
1282/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1283///
1284/// [MS] assembly-instruction-list:
1285/// assembly-instruction ';'[opt]
1286/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1287///
John McCalldadc5752010-08-24 06:29:42 +00001288StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001289 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001290 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001291
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001292 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001293 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001294 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001295 }
Chris Lattner0116c472006-08-15 06:03:28 +00001296 DeclSpec DS;
1297 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001298 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001299
Chris Lattner0116c472006-08-15 06:03:28 +00001300 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001301 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001302 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001303 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001304 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001305
Chris Lattner0116c472006-08-15 06:03:28 +00001306 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001307 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001308 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001309 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001310 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001311 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001312 }
Chris Lattner04132372006-10-16 06:12:55 +00001313 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001314
John McCalldadc5752010-08-24 06:29:42 +00001315 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001316 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001317 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001318
Anders Carlsson9a020f92010-01-30 22:25:16 +00001319 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001320 ExprVector Constraints(Actions);
1321 ExprVector Exprs(Actions);
1322 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001323
Anders Carlsson19fe1162008-02-05 23:03:50 +00001324 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001325 // We have a simple asm expression like 'asm("foo")'.
1326 SourceLocation RParenLoc = ConsumeParen();
1327 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1328 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1329 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001330 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001331 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001332 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001333
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001334 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001335 bool AteExtraColon = false;
1336 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1337 // In C++ mode, parse "::" like ": :".
1338 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001339 ConsumeToken();
1340
Chris Lattner15768502009-12-20 23:08:04 +00001341 if (!AteExtraColon &&
1342 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001343 return StmtError();
1344 }
Chris Lattner15768502009-12-20 23:08:04 +00001345
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001346 unsigned NumOutputs = Names.size();
1347
1348 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001349 if (AteExtraColon ||
1350 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1351 // In C++ mode, parse "::" like ": :".
1352 if (AteExtraColon)
1353 AteExtraColon = false;
1354 else {
1355 AteExtraColon = Tok.is(tok::coloncolon);
1356 ConsumeToken();
1357 }
1358
1359 if (!AteExtraColon &&
1360 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001361 return StmtError();
1362 }
1363
1364 assert(Names.size() == Constraints.size() &&
1365 Constraints.size() == Exprs.size() &&
1366 "Input operand size mismatch!");
1367
1368 unsigned NumInputs = Names.size() - NumOutputs;
1369
1370 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001371 if (AteExtraColon || Tok.is(tok::colon)) {
1372 if (!AteExtraColon)
1373 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001374
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001375 // Parse the asm-string list for clobbers if present.
1376 if (Tok.isNot(tok::r_paren)) {
1377 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001378 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001379
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001380 if (Clobber.isInvalid())
1381 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001382
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001383 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001384
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001385 if (Tok.isNot(tok::comma)) break;
1386 ConsumeToken();
1387 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001388 }
1389 }
1390
1391 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1392 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001393 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001394 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001395 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001396 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001397}
1398
1399/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001400/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001401///
1402/// [GNU] asm-operands:
1403/// asm-operand
1404/// asm-operands ',' asm-operand
1405///
1406/// [GNU] asm-operand:
1407/// asm-string-literal '(' expression ')'
1408/// '[' identifier ']' asm-string-literal '(' expression ')'
1409///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001410//
1411// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001412bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1413 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1414 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001415 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001416 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001417 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001418
1419 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001420 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001421 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001422 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001423
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001424 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001425 Diag(Tok, diag::err_expected_ident);
1426 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001427 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001428 }
Mike Stump11289f42009-09-09 15:08:12 +00001429
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001430 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001431 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001432
Anders Carlsson9a020f92010-01-30 22:25:16 +00001433 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001434 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001435 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001436 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001437
John McCalldadc5752010-08-24 06:29:42 +00001438 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001439 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001440 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001441 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001442 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001443 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001444
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001445 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001446 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001447 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001448 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001449 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001450
Chris Lattner0116c472006-08-15 06:03:28 +00001451 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001452 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001453 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001454 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001455 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001456 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001457 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001458 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001459 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001460 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001461 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001462 ConsumeToken();
1463 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001464
1465 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001466}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001467
John McCall48871652010-08-21 09:40:31 +00001468Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001469 assert(Tok.is(tok::l_brace));
1470 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001471
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001472 if (PP.isCodeCompletionEnabled())
1473 if (trySkippingFunctionBodyForCodeCompletion())
1474 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +00001475
John McCallfaf5fb42010-08-26 23:41:50 +00001476 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1477 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001478
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001479 // Do not enter a scope for the brace, as the arguments are in the same scope
1480 // (the function body) as the body itself. Instead, just read the statement
1481 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001482 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001483
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001484 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001485 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001486 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001487 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001488
John McCallb268a282010-08-23 23:25:46 +00001489 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001490}
Sebastian Redlb219c902008-12-21 16:41:36 +00001491
Sebastian Redla7b98a72009-04-26 20:35:05 +00001492/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1493///
1494/// function-try-block:
1495/// 'try' ctor-initializer[opt] compound-statement handler-seq
1496///
John McCall48871652010-08-21 09:40:31 +00001497Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001498 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1499 SourceLocation TryLoc = ConsumeToken();
1500
John McCallfaf5fb42010-08-26 23:41:50 +00001501 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1502 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001503
1504 // Constructor initializer list?
1505 if (Tok.is(tok::colon))
1506 ParseConstructorInitializer(Decl);
1507
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001508 if (PP.isCodeCompletionEnabled())
1509 if (trySkippingFunctionBodyForCodeCompletion())
1510 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001511
Sebastian Redld98ecd62009-04-26 21:08:36 +00001512 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001513 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001514 // If we failed to parse the try-catch, we just give the function an empty
1515 // compound statement as the body.
1516 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001517 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001518 MultiStmtArg(Actions), false);
1519
John McCallb268a282010-08-23 23:25:46 +00001520 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001521}
1522
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001523bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001524 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001525 assert(PP.isCodeCompletionEnabled() &&
1526 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001527
1528 // We're in code-completion mode. Skip parsing for all function bodies unless
1529 // the body contains the code-completion point.
1530 TentativeParsingAction PA(*this);
1531 ConsumeBrace();
1532 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1533 /*StopAtCodeCompletion=*/true)) {
1534 PA.Commit();
1535 return true;
1536 }
1537
1538 PA.Revert();
1539 return false;
1540}
1541
Sebastian Redlb219c902008-12-21 16:41:36 +00001542/// ParseCXXTryBlock - Parse a C++ try-block.
1543///
1544/// try-block:
1545/// 'try' compound-statement handler-seq
1546///
John McCall53fa7142010-12-24 02:08:15 +00001547StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001548 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001549
Sebastian Redlb219c902008-12-21 16:41:36 +00001550 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1551
1552 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001553 return ParseCXXTryBlockCommon(TryLoc);
1554}
1555
1556/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1557/// function-try-block.
1558///
1559/// try-block:
1560/// 'try' compound-statement handler-seq
1561///
1562/// function-try-block:
1563/// 'try' ctor-initializer[opt] compound-statement handler-seq
1564///
1565/// handler-seq:
1566/// handler handler-seq[opt]
1567///
John McCalldadc5752010-08-24 06:29:42 +00001568StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001569 if (Tok.isNot(tok::l_brace))
1570 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001571 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall53fa7142010-12-24 02:08:15 +00001572 ParsedAttributesWithRange attrs;
1573 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001574 if (TryBlock.isInvalid())
1575 return move(TryBlock);
1576
1577 StmtVector Handlers(Actions);
John McCall53fa7142010-12-24 02:08:15 +00001578 MaybeParseCXX0XAttributes(attrs);
1579 ProhibitAttributes(attrs);
1580
Sebastian Redlb219c902008-12-21 16:41:36 +00001581 if (Tok.isNot(tok::kw_catch))
1582 return StmtError(Diag(Tok, diag::err_expected_catch));
1583 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001584 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001585 if (!Handler.isInvalid())
1586 Handlers.push_back(Handler.release());
1587 }
1588 // Don't bother creating the full statement if we don't have any usable
1589 // handlers.
1590 if (Handlers.empty())
1591 return StmtError();
1592
John McCallb268a282010-08-23 23:25:46 +00001593 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001594}
1595
1596/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1597///
1598/// handler:
1599/// 'catch' '(' exception-declaration ')' compound-statement
1600///
1601/// exception-declaration:
1602/// type-specifier-seq declarator
1603/// type-specifier-seq abstract-declarator
1604/// type-specifier-seq
1605/// '...'
1606///
John McCalldadc5752010-08-24 06:29:42 +00001607StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001608 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1609
1610 SourceLocation CatchLoc = ConsumeToken();
1611
1612 SourceLocation LParenLoc = Tok.getLocation();
1613 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1614 return StmtError();
1615
1616 // C++ 3.3.2p3:
1617 // The name in a catch exception-declaration is local to the handler and
1618 // shall not be redeclared in the outermost block of the handler.
1619 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1620
1621 // exception-declaration is equivalent to '...' or a parameter-declaration
1622 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001623 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001624 if (Tok.isNot(tok::ellipsis)) {
1625 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001626 if (ParseCXXTypeSpecifierSeq(DS))
1627 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001628 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1629 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001630 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001631 } else
1632 ConsumeToken();
1633
1634 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1635 return StmtError();
1636
1637 if (Tok.isNot(tok::l_brace))
1638 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1639
Alexis Hunt96d5c762009-11-21 08:43:09 +00001640 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall53fa7142010-12-24 02:08:15 +00001641 ParsedAttributes attrs;
1642 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001643 if (Block.isInvalid())
1644 return move(Block);
1645
John McCallb268a282010-08-23 23:25:46 +00001646 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001647}