blob: de81c7797a0777ec2e8979a15bd6306117c22f76 [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"
18#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000045/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000046/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000053/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
Chris Lattner9075bd72006-08-10 04:59:57 +000062/// expression-statement:
63/// expression[opt] ';'
64///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000065/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000070/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000071///
Fariborz Jahanian90814572007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000074/// [OBC] '@' 'throw' ';'
75///
Sebastian Redl042ad952008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000078 const char *SemiError = 0;
John McCall37ad5512010-08-23 06:44:23 +000079 OwningStmtResult Res;
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000080
81 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000082
Alexis Hunt96d5c762009-11-21 08:43:09 +000083 CXX0XAttributeList Attr;
84 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
85 Attr = ParseCXX0XAttributes();
Ted Kremenekc162e8e2010-02-11 02:19:13 +000086 llvm::OwningPtr<AttributeList> AttrList(Attr.AttrList);
Alexis Hunt96d5c762009-11-21 08:43:09 +000087
Chris Lattner503fadc2006-08-10 05:45:44 +000088 // Cases in this switch statement should fall through if the parser expects
89 // the token to end in a semicolon (in which case SemiError should be set),
90 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000091 tok::TokenKind Kind = Tok.getKind();
92 SourceLocation AtLoc;
93 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000094 case tok::at: // May be a @try or @throw statement
95 {
96 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000097 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000098 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000099
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000100 case tok::code_completion:
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000101 Actions.CodeCompleteOrdinaryName(getCurScope(), Action::PCC_Statement);
Douglas Gregorf4c33342010-05-28 00:22:41 +0000102 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000103 return ParseStatementOrDeclaration(OnlyStatement);
104
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000105 case tok::identifier:
106 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
107 // identifier ':' statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000108 return ParseLabeledStatement(AttrList.take());
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000109 }
110 // PASS THROUGH.
111
Chris Lattner803802d2009-03-24 17:04:48 +0000112 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000113 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000114 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000115 AttrList.take(); //Passing 'Attr' to ParseDeclaration transfers ownership.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000116 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd,
117 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000118 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000119 }
120
121 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000122 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000123 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Alexis Hunt96d5c762009-11-21 08:43:09 +0000126 // FIXME: Use the attributes
Chris Lattner803802d2009-03-24 17:04:48 +0000127 // expression[opt] ';'
128 OwningExprResult Expr(ParseExpression());
129 if (Expr.isInvalid()) {
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000130 // If the expression is invalid, skip ahead to the next semicolon or '}'.
131 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattner803802d2009-03-24 17:04:48 +0000132 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000133 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
134 if (Tok.is(tok::semi))
135 ConsumeToken();
Chris Lattner803802d2009-03-24 17:04:48 +0000136 return StmtError();
137 }
138 // Otherwise, eat the semicolon.
139 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000140 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr));
Chris Lattner803802d2009-03-24 17:04:48 +0000141 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000142
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000143 case tok::kw_case: // C99 6.8.1: labeled-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000144 return ParseCaseStatement(AttrList.take());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000145 case tok::kw_default: // C99 6.8.1: labeled-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000146 return ParseDefaultStatement(AttrList.take());
Sebastian Redl042ad952008-12-11 19:30:53 +0000147
Chris Lattner9075bd72006-08-10 04:59:57 +0000148 case tok::l_brace: // C99 6.8.2: compound-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000149 return ParseCompoundStatement(AttrList.take());
Chris Lattner0f203a72007-05-28 01:45:28 +0000150 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000151 return Actions.ActOnNullStmt(ConsumeToken());
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
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000154 return ParseIfStatement(AttrList.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000155 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000156 return ParseSwitchStatement(AttrList.take());
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
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000159 return ParseWhileStatement(AttrList.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000160 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000161 Res = ParseDoStatement(AttrList.take());
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
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000165 return ParseForStatement(AttrList.take());
Chris Lattner503fadc2006-08-10 05:45:44 +0000166
167 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000168 Res = ParseGotoStatement(AttrList.take());
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
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000172 Res = ParseContinueStatement(AttrList.take());
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
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000176 Res = ParseBreakStatement(AttrList.take());
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
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000180 Res = ParseReturnStatement(AttrList.take());
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: {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000185 if (Attr.HasAttr)
186 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
187 << Attr.Range;
Steve Naroffb2c80c72008-02-07 03:50:06 +0000188 bool msAsm = false;
189 Res = ParseAsmStatement(msAsm);
Sebastian Redl042ad952008-12-11 19:30:53 +0000190 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000191 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000192 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000193 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000194
Sebastian Redlb219c902008-12-21 16:41:36 +0000195 case tok::kw_try: // C++ 15: try-block
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000196 return ParseCXXTryBlock(AttrList.take());
Sebastian Redlb219c902008-12-21 16:41:36 +0000197 }
198
Chris Lattner503fadc2006-08-10 05:45:44 +0000199 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000200 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000201 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000202 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000203 // If the result was valid, then we do want to diagnose this. Use
204 // ExpectAndConsume to emit the diagnostic, even though we know it won't
205 // succeed.
206 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000207 // Skip until we see a } or ;, but don't eat it.
208 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000209 }
Mike Stump11289f42009-09-09 15:08:12 +0000210
Sebastian Redl042ad952008-12-11 19:30:53 +0000211 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000212}
213
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000214/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000215///
216/// labeled-statement:
217/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000218/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000219///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000220Parser::OwningStmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000221 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
222 "Not an identifier!");
223
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000224 llvm::OwningPtr<AttributeList> AttrList(Attr);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000225 Token IdentTok = Tok; // Save the whole token.
226 ConsumeToken(); // eat the identifier.
227
228 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000229
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000230 // identifier ':' statement
231 SourceLocation ColonLoc = ConsumeToken();
232
233 // Read label attributes, if present.
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000234 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000235 AttrList.reset(addAttributeLists(AttrList.take(), ParseGNUAttributes()));
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000236
Sebastian Redl042ad952008-12-11 19:30:53 +0000237 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000238
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000239 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000240 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000241 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000242
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000243 // FIXME: use attributes?
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000244 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
245 IdentTok.getIdentifierInfo(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000246 ColonLoc, move(SubStmt));
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000247}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000248
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000249/// ParseCaseStatement
250/// labeled-statement:
251/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000252/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000253///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000254Parser::OwningStmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000255 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000256 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000257 delete Attr;
Mike Stump11289f42009-09-09 15:08:12 +0000258
Chris Lattner34a22092009-03-04 04:23:07 +0000259 // It is very very common for code to contain many case statements recursively
260 // nested, as in (but usually without indentation):
261 // case 1:
262 // case 2:
263 // case 3:
264 // case 4:
265 // case 5: etc.
266 //
267 // Parsing this naively works, but is both inefficient and can cause us to run
268 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000269 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000270 // but all the grossness is constrained to ParseCaseStatement (and some
271 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner34a22092009-03-04 04:23:07 +0000273 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
274 // example above.
John McCall37ad5512010-08-23 06:44:23 +0000275 OwningStmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattner34a22092009-03-04 04:23:07 +0000277 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
278 // gets updated each time a new case is parsed, and whose body is unset so
279 // far. When parsing 'case 4', this is the 'case 3' node.
280 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattner34a22092009-03-04 04:23:07 +0000282 // While we have case statements, eat and stack them.
283 do {
284 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000285
Douglas Gregord328d572009-09-21 18:10:23 +0000286 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000287 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000288 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000289 }
290
Chris Lattner125c0ee2009-12-10 00:38:54 +0000291 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
292 /// Disable this form of error recovery while we're parsing the case
293 /// expression.
294 ColonProtectionRAIIObject ColonProtection(*this);
295
Chris Lattner34a22092009-03-04 04:23:07 +0000296 OwningExprResult LHS(ParseConstantExpression());
297 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000298 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000299 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000300 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000301
Chris Lattner34a22092009-03-04 04:23:07 +0000302 // GNU case range extension.
303 SourceLocation DotDotDotLoc;
John McCall37ad5512010-08-23 06:44:23 +0000304 OwningExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000305 if (Tok.is(tok::ellipsis)) {
306 Diag(Tok, diag::ext_gnu_case_range);
307 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000308
Chris Lattner34a22092009-03-04 04:23:07 +0000309 RHS = ParseConstantExpression();
310 if (RHS.isInvalid()) {
311 SkipUntil(tok::colon);
312 return StmtError();
313 }
314 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000315
316 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000317
Chris Lattner34a22092009-03-04 04:23:07 +0000318 if (Tok.isNot(tok::colon)) {
319 Diag(Tok, diag::err_expected_colon_after) << "'case'";
320 SkipUntil(tok::colon);
321 return StmtError();
322 }
323
324 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattner34a22092009-03-04 04:23:07 +0000326 OwningStmtResult Case =
327 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
328 move(RHS), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000329
Chris Lattner34a22092009-03-04 04:23:07 +0000330 // If we had a sema error parsing this case, then just ignore it and
331 // continue parsing the sub-stmt.
332 if (Case.isInvalid()) {
333 if (TopLevelCase.isInvalid()) // No parsed case stmts.
334 return ParseStatement();
335 // Otherwise, just don't add it as a nested case.
336 } else {
337 // If this is the first case statement we parsed, it becomes TopLevelCase.
338 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000339 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000340 if (TopLevelCase.isInvalid())
341 TopLevelCase = move(Case);
342 else
343 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
344 DeepestParsedCaseStmt = NextDeepest;
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner34a22092009-03-04 04:23:07 +0000347 // Handle all case statements.
348 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattner34a22092009-03-04 04:23:07 +0000350 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattner34a22092009-03-04 04:23:07 +0000352 // If we found a non-case statement, start by parsing it.
John McCall37ad5512010-08-23 06:44:23 +0000353 OwningStmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattner34a22092009-03-04 04:23:07 +0000355 if (Tok.isNot(tok::r_brace)) {
356 SubStmt = ParseStatement();
357 } else {
358 // Nicely diagnose the common error "switch (X) { case 4: }", which is
359 // not valid.
360 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000361 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000362 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000363 }
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattner34a22092009-03-04 04:23:07 +0000365 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000366 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000367 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattner34a22092009-03-04 04:23:07 +0000369 // Install the body into the most deeply-nested case.
370 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl042ad952008-12-11 19:30:53 +0000371
Chris Lattner34a22092009-03-04 04:23:07 +0000372 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000373 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000374}
375
376/// ParseDefaultStatement
377/// labeled-statement:
378/// 'default' ':' statement
379/// Note that this does not parse the 'statement' at the end.
380///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000381Parser::OwningStmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
382 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000383 delete Attr;
384
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000385 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000386 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000387
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000388 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000389 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000390 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000391 return StmtError();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000392 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000393
Chris Lattneraf635312006-10-16 06:06:51 +0000394 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000395
Chris Lattner30f910e2006-10-16 05:52:41 +0000396 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000397 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000398 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000399 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000400 }
401
Sebastian Redl042ad952008-12-11 19:30:53 +0000402 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000403 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000404 return StmtError();
405
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000406 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000407 move(SubStmt), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000408}
409
410
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000411/// ParseCompoundStatement - Parse a "{}" block.
412///
413/// compound-statement: [C99 6.8.2]
414/// { block-item-list[opt] }
415/// [GNU] { label-declarations block-item-list } [TODO]
416///
417/// block-item-list:
418/// block-item
419/// block-item-list block-item
420///
421/// block-item:
422/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000423/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000424/// statement
425/// [OMP] openmp-directive [TODO]
426///
427/// [GNU] label-declarations:
428/// [GNU] label-declaration
429/// [GNU] label-declarations label-declaration
430///
431/// [GNU] label-declaration:
432/// [GNU] '__label__' identifier-list ';'
433///
434/// [OMP] openmp-directive: [TODO]
435/// [OMP] barrier-directive
436/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000437///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000438Parser::OwningStmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
439 bool isStmtExpr) {
440 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000441 delete Attr;
442
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000443 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000444
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000445 // Enter a scope to hold everything within the compound stmt. Compound
446 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000447 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000448
449 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000450 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000451}
452
453
454/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000455/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000456/// consume the '}' at the end of the block. It does not manipulate the scope
457/// stack.
Sebastian Redl042ad952008-12-11 19:30:53 +0000458Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000459 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000460 Tok.getLocation(),
461 "in compound statement ('{}')");
Mike Stump11289f42009-09-09 15:08:12 +0000462
Chris Lattnerf2978802007-01-21 06:52:16 +0000463 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
464
Chris Lattner010015a2007-05-28 07:23:54 +0000465 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000466 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000467
468 typedef StmtVector StmtsTy;
469 StmtsTy Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000470 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall37ad5512010-08-23 06:44:23 +0000471 OwningStmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000472 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000473 R = ParseStatementOrDeclaration(false);
474 } else {
475 // __extension__ can start declarations and it can also be a unary
476 // operator for expressions. Consume multiple __extension__ markers here
477 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000478 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000479 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000480 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000481 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000482
Alexis Hunt96d5c762009-11-21 08:43:09 +0000483 CXX0XAttributeList Attr;
484 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
485 Attr = ParseCXX0XAttributes();
486
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000487 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000488 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000489 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000490 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000491 ExtensionRAIIObject O(Diags);
492
Chris Lattner49836b42009-04-02 04:16:50 +0000493 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000494 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
495 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000496 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000497 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000498 // Otherwise this was a unary __extension__ marker.
499 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000500
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000501 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000502 SkipUntil(tok::semi);
503 continue;
504 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000505
Alexis Hunt96d5c762009-11-21 08:43:09 +0000506 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000507 // Eat the semicolon at the end of stmt and convert the expr into a
508 // statement.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000509 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000510 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000511 }
512 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000513
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000514 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000515 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000516 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000517
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000518 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000519 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000520 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl042ad952008-12-11 19:30:53 +0000521 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000522 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000523
Chris Lattner04132372006-10-16 06:12:55 +0000524 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000525 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000526 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000527}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000528
Chris Lattnerc0081db2008-12-12 06:31:07 +0000529/// ParseParenExprOrCondition:
530/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000531/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000532///
533/// This function parses and performs error recovery on the specified condition
534/// or expression (depending on whether we're in C++ or C mode). This function
535/// goes out of its way to recover well. It returns true if there was a parser
536/// error (the right paren couldn't be found), which indicates that the caller
537/// should try to recover harder. It returns false if the condition is
538/// successfully parsed. Note that a successful parse can still have semantic
539/// errors in the condition.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000540bool Parser::ParseParenExprOrCondition(OwningExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000541 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000542 SourceLocation Loc,
543 bool ConvertToBoolean) {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000544 bool ParseError = false;
545
Chris Lattnerc0081db2008-12-12 06:31:07 +0000546 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000547 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +0000548 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
549 ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000550 else {
551 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000552 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000553
554 // If required, convert to a boolean value.
555 if (!ExprResult.isInvalid() && ConvertToBoolean)
556 ExprResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000557 = Actions.ActOnBooleanCondition(getCurScope(), Loc, move(ExprResult));
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000558 }
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattnerc0081db2008-12-12 06:31:07 +0000560 // If the parser was confused by the condition and we don't have a ')', try to
561 // recover by skipping ahead to a semi and bailing out. If condexp is
562 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000563 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000564 SkipUntil(tok::semi);
565 // Skipping may have stopped if it found the containing ')'. If so, we can
566 // continue parsing the if statement.
567 if (Tok.isNot(tok::r_paren))
568 return true;
569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Chris Lattnerc0081db2008-12-12 06:31:07 +0000571 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000572 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000573 return false;
574}
575
576
Chris Lattnerc951dae2006-08-10 04:23:57 +0000577/// ParseIfStatement
578/// if-statement: [C99 6.8.4.1]
579/// 'if' '(' expression ')' statement
580/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000581/// [C++] 'if' '(' condition ')' statement
582/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000583///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000584Parser::OwningStmtResult Parser::ParseIfStatement(AttributeList *Attr) {
585 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000586 delete Attr;
587
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 McCall37ad5512010-08-23 06:44:23 +0000614 OwningExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000615 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +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
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000619 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp));
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();
Sebastian Redl042ad952008-12-11 19:30:53 +0000644 OwningStmtResult 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 McCall37ad5512010-08-23 06:44:23 +0000652 OwningStmtResult 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
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000699 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, move(ThenStmt),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000700 ElseLoc, move(ElseStmt));
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
Alexis Hunt96d5c762009-11-21 08:43:09 +0000707Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
708 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000709 delete Attr;
710
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000711 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000712 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000713
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000714 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000715 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000716 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000717 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000718 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000719
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000720 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
721
Chris Lattner2dd1b722007-08-26 23:08:06 +0000722 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
723 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000724 //
725 // C++ 6.4p3:
726 // A name introduced by a declaration in a condition is in scope from its
727 // point of declaration until the end of the substatements controlled by the
728 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000729 // C++ 3.3.2p4:
730 // Names declared in the for-init-statement, and in the condition of if,
731 // while, for, and switch statements are local to the if, while, for, or
732 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000733 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000734 unsigned ScopeFlags = Scope::BreakScope;
735 if (C99orCXX)
736 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000737 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000738
Chris Lattner9075bd72006-08-10 04:59:57 +0000739 // Parse the condition.
John McCall37ad5512010-08-23 06:44:23 +0000740 OwningExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000741 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000742 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000743 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000744
Douglas Gregore60e41a2010-05-06 17:25:47 +0000745 OwningStmtResult Switch
746 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, move(Cond), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000747
Douglas Gregore60e41a2010-05-06 17:25:47 +0000748 if (Switch.isInvalid()) {
749 // Skip the switch body.
750 // FIXME: This is not optimal recovery, but parsing the body is more
751 // dangerous due to the presence of case and default statements, which
752 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000753 if (Tok.is(tok::l_brace)) {
754 ConsumeBrace();
755 SkipUntil(tok::r_brace, false, false);
756 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000757 SkipUntil(tok::semi);
758 return move(Switch);
759 }
760
Chris Lattner8fb26252007-08-22 05:28:50 +0000761 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000762 // there is no compound stmt. C90 does not have this clause. We only do this
763 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000764 //
765 // C++ 6.4p1:
766 // The substatement in a selection-statement (each substatement, in the else
767 // form of the if statement) implicitly defines a local scope.
768 //
769 // See comments in ParseIfStatement for why we create a scope for the
770 // condition and a new scope for substatement in C++.
771 //
Mike Stump11289f42009-09-09 15:08:12 +0000772 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000773 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000774
Chris Lattner9075bd72006-08-10 04:59:57 +0000775 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000776 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000777
Chris Lattner8fd2d012010-01-24 01:50:29 +0000778 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000779 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000780 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000781
Chris Lattner8fd2d012010-01-24 01:50:29 +0000782 if (Body.isInvalid())
783 // FIXME: Remove the case statement list from the Switch statement.
784 Body = Actions.ActOnNullStmt(Tok.getLocation());
785
Sebastian Redl726a0d92009-02-05 15:02:23 +0000786 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000787}
788
789/// ParseWhileStatement
790/// while-statement: [C99 6.8.5.1]
791/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000792/// [C++] 'while' '(' condition ')' statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000793Parser::OwningStmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
794 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000795 delete Attr;
796
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000797 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000798 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000799 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000800
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000801 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000802 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000803 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000804 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000805 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000806
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000807 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
808
Chris Lattner2dd1b722007-08-26 23:08:06 +0000809 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
810 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000811 //
812 // C++ 6.4p3:
813 // A name introduced by a declaration in a condition is in scope from its
814 // point of declaration until the end of the substatements controlled by the
815 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000816 // C++ 3.3.2p4:
817 // Names declared in the for-init-statement, and in the condition of if,
818 // while, for, and switch statements are local to the if, while, for, or
819 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000820 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000821 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000822 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000823 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
824 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000825 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000826 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
827 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000828
Chris Lattner9075bd72006-08-10 04:59:57 +0000829 // Parse the condition.
John McCall37ad5512010-08-23 06:44:23 +0000830 OwningExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000831 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000832 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000833 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000834
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000835 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Mike Stump11289f42009-09-09 15:08:12 +0000836
Chris Lattner8fb26252007-08-22 05:28:50 +0000837 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000838 // there is no compound stmt. C90 does not have this clause. We only do this
839 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000840 //
841 // C++ 6.5p2:
842 // The substatement in an iteration-statement implicitly defines a local scope
843 // which is entered and exited each time through the loop.
844 //
845 // See comments in ParseIfStatement for why we create a scope for the
846 // condition and a new scope for substatement in C++.
847 //
Mike Stump11289f42009-09-09 15:08:12 +0000848 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000849 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000850
Chris Lattner9075bd72006-08-10 04:59:57 +0000851 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000852 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000853
Chris Lattner8fb26252007-08-22 05:28:50 +0000854 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000855 InnerScope.Exit();
856 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000857
John McCall48871652010-08-21 09:40:31 +0000858 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000859 return StmtError();
860
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000861 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000862}
863
864/// ParseDoStatement
865/// do-statement: [C99 6.8.5.2]
866/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000867/// Note: this lets the caller parse the end ';'.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000868Parser::OwningStmtResult Parser::ParseDoStatement(AttributeList *Attr) {
869 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000870 delete Attr;
871
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000872 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000873 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000874
Chris Lattner2dd1b722007-08-26 23:08:06 +0000875 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
876 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000877 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000878 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000879 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000880 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000881 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000882
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000883 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000884
Chris Lattner8fb26252007-08-22 05:28:50 +0000885 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000886 // there is no compound stmt. C90 does not have this clause. We only do this
887 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000888 //
889 // C++ 6.5p2:
890 // The substatement in an iteration-statement implicitly defines a local scope
891 // which is entered and exited each time through the loop.
892 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000893 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000894 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000895 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000896
Chris Lattner9075bd72006-08-10 04:59:57 +0000897 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000898 OwningStmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000899
Chris Lattner8fb26252007-08-22 05:28:50 +0000900 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000901 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000902
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000903 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000904 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000905 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000906 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000907 SkipUntil(tok::semi, false, true);
908 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000909 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000910 }
Chris Lattneraf635312006-10-16 06:06:51 +0000911 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000912
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000913 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000914 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000915 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000916 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000917 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000918
Chris Lattner10da53c2008-12-12 06:35:28 +0000919 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000920 SourceLocation LPLoc = ConsumeParen();
921 OwningExprResult Cond = ParseExpression();
922 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000923 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000924
Sebastian Redlb62406f2008-12-11 19:48:14 +0000925 if (Cond.isInvalid() || Body.isInvalid())
926 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000927
Chris Lattner815b70e2009-06-12 23:04:47 +0000928 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
929 move(Cond), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000930}
931
932/// ParseForStatement
933/// for-statement: [C99 6.8.5.3]
934/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
935/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000936/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
937/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000938/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
939/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000940///
941/// [C++] for-init-statement:
942/// [C++] expression-statement
943/// [C++] simple-declaration
944///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000945Parser::OwningStmtResult Parser::ParseForStatement(AttributeList *Attr) {
946 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000947 delete Attr;
948
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000949 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000950 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000951
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000952 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000953 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000954 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000955 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000956 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000957
Chris Lattner934074c2009-04-22 00:54:41 +0000958 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000959
Chris Lattner2dd1b722007-08-26 23:08:06 +0000960 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
961 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000962 //
963 // C++ 6.4p3:
964 // A name introduced by a declaration in a condition is in scope from its
965 // point of declaration until the end of the substatements controlled by the
966 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000967 // C++ 3.3.2p4:
968 // Names declared in the for-init-statement, and in the condition of if,
969 // while, for, and switch statements are local to the if, while, for, or
970 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000971 // C++ 6.5.3p1:
972 // Names declared in the for-init-statement are in the same declarative-region
973 // as those declared in the condition.
974 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000975 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000976 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000977 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
978 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000979 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000980 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
981
982 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000983
Chris Lattner04132372006-10-16 06:12:55 +0000984 SourceLocation LParenLoc = ConsumeParen();
John McCall37ad5512010-08-23 06:44:23 +0000985 OwningExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000986
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000987 bool ForEach = false;
John McCall37ad5512010-08-23 06:44:23 +0000988 OwningStmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000989 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000990 FullExprArg SecondPart(Actions);
John McCall37ad5512010-08-23 06:44:23 +0000991 OwningExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000992 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +0000993 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000994
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000995 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000996 Actions.CodeCompleteOrdinaryName(getCurScope(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000997 C99orCXXorObjC? Action::PCC_ForInit
998 : Action::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000999 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001000 }
1001
Chris Lattner9075bd72006-08-10 04:59:57 +00001002 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001003 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001004 // no first part, eat the ';'.
1005 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001006 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001007 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001008 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001009 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001010
Alexis Hunt96d5c762009-11-21 08:43:09 +00001011 AttributeList *AttrList = 0;
1012 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
1013 AttrList = ParseCXX0XAttributes().AttrList;
1014
Chris Lattner49836b42009-04-02 04:16:50 +00001015 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001016 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +00001017 AttrList, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001018 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001019
Chris Lattner32dc41c2009-03-29 17:27:48 +00001020 if (Tok.is(tok::semi)) { // for (int x = 4;
1021 ConsumeToken();
1022 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001023 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001024 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001025 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001026
1027 if (Tok.is(tok::code_completion)) {
1028 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1029 ConsumeCodeCompletionToken();
1030 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001031 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001032 } else {
1033 Diag(Tok, diag::err_expected_semi_for);
1034 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001035 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001036 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001037 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001038
Chris Lattnercd68f642007-06-27 01:06:29 +00001039 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001040 if (!Value.isInvalid())
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001041 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001042
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001043 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001044 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001045 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001046 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001047
1048 if (Tok.is(tok::code_completion)) {
1049 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1050 ConsumeCodeCompletionToken();
1051 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001052 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001053 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001054 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001055 SkipUntil(tok::semi);
1056 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001057 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001058 if (!ForEach) {
Douglas Gregore60e41a2010-05-06 17:25:47 +00001059 assert(!SecondPart->get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001060 // Parse the second part of the for specifier.
1061 if (Tok.is(tok::semi)) { // for (...;;
1062 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001063 } else {
John McCall37ad5512010-08-23 06:44:23 +00001064 OwningExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001065 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001066 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1067 else {
1068 Second = ParseExpression();
1069 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001070 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001071 move(Second));
1072 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001073 SecondPartIsInvalid = Second.isInvalid();
Douglas Gregore60e41a2010-05-06 17:25:47 +00001074 SecondPart = Actions.MakeFullExpr(Second);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001075 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001076
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001077 if (Tok.is(tok::semi)) {
1078 ConsumeToken();
1079 } else {
John McCall48871652010-08-21 09:40:31 +00001080 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001081 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001082 SkipUntil(tok::semi);
1083 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001084
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001085 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001086 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
1087 OwningExprResult Third = ParseExpression();
1088 ThirdPart = Actions.MakeFullExpr(Third);
1089 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001090 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001091 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001092 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001093
Chris Lattner8fb26252007-08-22 05:28:50 +00001094 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001095 // there is no compound stmt. C90 does not have this clause. We only do this
1096 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001097 //
1098 // C++ 6.5p2:
1099 // The substatement in an iteration-statement implicitly defines a local scope
1100 // which is entered and exited each time through the loop.
1101 //
1102 // See comments in ParseIfStatement for why we create a scope for
1103 // for-init-statement/condition and a new scope for substatement in C++.
1104 //
Mike Stump11289f42009-09-09 15:08:12 +00001105 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001106 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001107
Chris Lattner9075bd72006-08-10 04:59:57 +00001108 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +00001109 OwningStmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001110
Chris Lattner8fb26252007-08-22 05:28:50 +00001111 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001112 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001113
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001114 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001115 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001116
1117 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001118 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001119
1120 if (!ForEach)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001121 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart), SecondPart,
1122 SecondVar, ThirdPart, RParenLoc, move(Body));
Mike Stump11289f42009-09-09 15:08:12 +00001123
Douglas Gregore60e41a2010-05-06 17:25:47 +00001124 // FIXME: It isn't clear how to communicate the late destruction of
1125 // C++ temporaries used to create the collection.
1126 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, move(FirstPart),
1127 move(Collection), RParenLoc,
1128 move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +00001129}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001130
Chris Lattner503fadc2006-08-10 05:45:44 +00001131/// ParseGotoStatement
1132/// jump-statement:
1133/// 'goto' identifier ';'
1134/// [GNU] 'goto' '*' expression ';'
1135///
1136/// Note: this lets the caller parse the end ';'.
1137///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001138Parser::OwningStmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
1139 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001140 delete Attr;
1141
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001142 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001143 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001144
John McCall37ad5512010-08-23 06:44:23 +00001145 OwningStmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001146 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001147 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001148 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001149 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001150 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001151 // GNU indirect goto extension.
1152 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001153 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001154 OwningExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001155 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001156 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001157 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001158 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001159 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattnere34b2c22007-07-22 04:13:33 +00001160 } else {
1161 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001162 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001163 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001164
Sebastian Redlb62406f2008-12-11 19:48:14 +00001165 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001166}
1167
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001168/// ParseContinueStatement
1169/// jump-statement:
1170/// 'continue' ';'
1171///
1172/// Note: this lets the caller parse the end ';'.
1173///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001174Parser::OwningStmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
1175 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001176 delete Attr;
1177
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///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001188Parser::OwningStmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
1189 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001190 delete Attr;
1191
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001192 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001193 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001194}
1195
Chris Lattner503fadc2006-08-10 05:45:44 +00001196/// ParseReturnStatement
1197/// jump-statement:
1198/// 'return' expression[opt] ';'
Alexis Hunt96d5c762009-11-21 08:43:09 +00001199Parser::OwningStmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
1200 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001201 delete Attr;
1202
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001203 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001204 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001205
John McCall37ad5512010-08-23 06:44:23 +00001206 OwningExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001207 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001208 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001209 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001210 ConsumeCodeCompletionToken();
1211 SkipUntil(tok::semi, false, true);
1212 return StmtError();
1213 }
1214
Chris Lattner30f910e2006-10-16 05:52:41 +00001215 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001216 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001217 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001218 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001219 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001220 }
Anders Carlssona1929472009-08-18 16:11:00 +00001221 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Chris Lattner503fadc2006-08-10 05:45:44 +00001222}
Chris Lattner0116c472006-08-15 06:03:28 +00001223
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001224/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1225/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001226Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001227 if (Tok.is(tok::l_brace)) {
1228 unsigned short savedBraceCount = BraceCount;
1229 do {
1230 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 {
1239 ConsumeAnyToken();
1240 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001241 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1242 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001243 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001244 }
Mike Stump6da5d752009-12-11 00:04:56 +00001245 Token t;
1246 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001247 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001248 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001249 t.setLength(21);
Mike Stump6da5d752009-12-11 00:04:56 +00001250 OwningExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1251 ExprVector Constraints(Actions);
1252 ExprVector Exprs(Actions);
1253 ExprVector Clobbers(Actions);
Anders Carlsson9a020f92010-01-30 22:25:16 +00001254 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001255 move_arg(Constraints), move_arg(Exprs),
1256 move(AsmString), move_arg(Clobbers),
Mike Stump90be58a2010-01-04 22:37:17 +00001257 Tok.getLocation(), true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001258}
1259
Chris Lattner0116c472006-08-15 06:03:28 +00001260/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001261/// asm-statement:
1262/// gnu-asm-statement
1263/// ms-asm-statement
1264///
1265/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001266/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1267///
1268/// [GNU] asm-argument:
1269/// asm-string-literal
1270/// asm-string-literal ':' asm-operands[opt]
1271/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1272/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1273/// ':' asm-clobbers
1274///
1275/// [GNU] asm-clobbers:
1276/// asm-string-literal
1277/// asm-clobbers ',' asm-string-literal
1278///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001279/// [MS] ms-asm-statement:
1280/// '__asm' assembly-instruction ';'[opt]
1281/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1282///
1283/// [MS] assembly-instruction-list:
1284/// assembly-instruction ';'[opt]
1285/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1286///
Sebastian Redlb62406f2008-12-11 19:48:14 +00001287Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001288 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001289 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001290
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001291 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001292 msAsm = true;
1293 return FuzzyParseMicrosoftAsmStatement();
1294 }
Chris Lattner0116c472006-08-15 06:03:28 +00001295 DeclSpec DS;
1296 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001297 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001298
Chris Lattner0116c472006-08-15 06:03:28 +00001299 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001300 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001301 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001302 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001303 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001304
Chris Lattner0116c472006-08-15 06:03:28 +00001305 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001306 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001307 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001308 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001309 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001310 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001311 }
Chris Lattner04132372006-10-16 06:12:55 +00001312 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001313
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001314 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001315 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001316 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001317
Anders Carlsson9a020f92010-01-30 22:25:16 +00001318 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001319 ExprVector Constraints(Actions);
1320 ExprVector Exprs(Actions);
1321 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001322
Anders Carlsson19fe1162008-02-05 23:03:50 +00001323 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001324 // We have a simple asm expression like 'asm("foo")'.
1325 SourceLocation RParenLoc = ConsumeParen();
1326 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1327 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1328 move_arg(Constraints), move_arg(Exprs),
1329 move(AsmString), move_arg(Clobbers),
1330 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001331 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001332
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001333 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001334 bool AteExtraColon = false;
1335 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1336 // In C++ mode, parse "::" like ": :".
1337 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001338 ConsumeToken();
1339
Chris Lattner15768502009-12-20 23:08:04 +00001340 if (!AteExtraColon &&
1341 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001342 return StmtError();
1343 }
Chris Lattner15768502009-12-20 23:08:04 +00001344
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001345 unsigned NumOutputs = Names.size();
1346
1347 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001348 if (AteExtraColon ||
1349 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1350 // In C++ mode, parse "::" like ": :".
1351 if (AteExtraColon)
1352 AteExtraColon = false;
1353 else {
1354 AteExtraColon = Tok.is(tok::coloncolon);
1355 ConsumeToken();
1356 }
1357
1358 if (!AteExtraColon &&
1359 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001360 return StmtError();
1361 }
1362
1363 assert(Names.size() == Constraints.size() &&
1364 Constraints.size() == Exprs.size() &&
1365 "Input operand size mismatch!");
1366
1367 unsigned NumInputs = Names.size() - NumOutputs;
1368
1369 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001370 if (AteExtraColon || Tok.is(tok::colon)) {
1371 if (!AteExtraColon)
1372 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001373
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001374 // Parse the asm-string list for clobbers if present.
1375 if (Tok.isNot(tok::r_paren)) {
1376 while (1) {
1377 OwningExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001378
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001379 if (Clobber.isInvalid())
1380 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001381
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001382 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001383
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001384 if (Tok.isNot(tok::comma)) break;
1385 ConsumeToken();
1386 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001387 }
1388 }
1389
1390 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1391 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001392 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001393 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001394 move(AsmString), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001395 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001396}
1397
1398/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001399/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001400///
1401/// [GNU] asm-operands:
1402/// asm-operand
1403/// asm-operands ',' asm-operand
1404///
1405/// [GNU] asm-operand:
1406/// asm-string-literal '(' expression ')'
1407/// '[' identifier ']' asm-string-literal '(' expression ')'
1408///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001409//
1410// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001411bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1412 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1413 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001414 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001415 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001416 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001417
1418 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001419 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001420 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001421 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001422
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001423 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001424 Diag(Tok, diag::err_expected_ident);
1425 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001426 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001427 }
Mike Stump11289f42009-09-09 15:08:12 +00001428
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001429 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001430 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001431
Anders Carlsson9a020f92010-01-30 22:25:16 +00001432 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001433 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001434 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001435 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001436
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001437 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001438 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001439 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001440 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001441 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001442 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001443
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001444 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001445 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001446 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001447 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001448 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001449
Chris Lattner0116c472006-08-15 06:03:28 +00001450 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001451 SourceLocation OpenLoc = ConsumeParen();
1452 OwningExprResult Res(ParseExpression());
1453 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001454 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001455 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001456 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001457 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001458 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001459 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001460 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001461 ConsumeToken();
1462 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001463
1464 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001465}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001466
John McCall48871652010-08-21 09:40:31 +00001467Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001468 assert(Tok.is(tok::l_brace));
1469 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001470
Chris Lattnereae6cb62009-03-05 08:00:35 +00001471 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1472 PP.getSourceManager(),
1473 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001474
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001475 // Do not enter a scope for the brace, as the arguments are in the same scope
1476 // (the function body) as the body itself. Instead, just read the statement
1477 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl042ad952008-12-11 19:30:53 +00001478 OwningStmtResult FnBody(ParseCompoundStatementBody());
1479
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001480 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001481 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001482 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001483 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001484
Sebastian Redl726a0d92009-02-05 15:02:23 +00001485 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001486}
Sebastian Redlb219c902008-12-21 16:41:36 +00001487
Sebastian Redla7b98a72009-04-26 20:35:05 +00001488/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1489///
1490/// function-try-block:
1491/// 'try' ctor-initializer[opt] compound-statement handler-seq
1492///
John McCall48871652010-08-21 09:40:31 +00001493Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001494 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1495 SourceLocation TryLoc = ConsumeToken();
1496
1497 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1498 PP.getSourceManager(),
1499 "parsing function try block");
1500
1501 // Constructor initializer list?
1502 if (Tok.is(tok::colon))
1503 ParseConstructorInitializer(Decl);
1504
Sebastian Redld98ecd62009-04-26 21:08:36 +00001505 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001506 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1507 // If we failed to parse the try-catch, we just give the function an empty
1508 // compound statement as the body.
1509 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001510 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001511 MultiStmtArg(Actions), false);
1512
1513 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1514}
1515
Sebastian Redlb219c902008-12-21 16:41:36 +00001516/// ParseCXXTryBlock - Parse a C++ try-block.
1517///
1518/// try-block:
1519/// 'try' compound-statement handler-seq
1520///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001521Parser::OwningStmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
1522 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001523 delete Attr;
1524
Sebastian Redlb219c902008-12-21 16:41:36 +00001525 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1526
1527 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001528 return ParseCXXTryBlockCommon(TryLoc);
1529}
1530
1531/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1532/// function-try-block.
1533///
1534/// try-block:
1535/// 'try' compound-statement handler-seq
1536///
1537/// function-try-block:
1538/// 'try' ctor-initializer[opt] compound-statement handler-seq
1539///
1540/// handler-seq:
1541/// handler handler-seq[opt]
1542///
1543Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001544 if (Tok.isNot(tok::l_brace))
1545 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001546 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1547 OwningStmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001548 if (TryBlock.isInvalid())
1549 return move(TryBlock);
1550
1551 StmtVector Handlers(Actions);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001552 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1553 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1554 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1555 << Attr.Range;
1556 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001557 if (Tok.isNot(tok::kw_catch))
1558 return StmtError(Diag(Tok, diag::err_expected_catch));
1559 while (Tok.is(tok::kw_catch)) {
1560 OwningStmtResult Handler(ParseCXXCatchBlock());
1561 if (!Handler.isInvalid())
1562 Handlers.push_back(Handler.release());
1563 }
1564 // Don't bother creating the full statement if we don't have any usable
1565 // handlers.
1566 if (Handlers.empty())
1567 return StmtError();
1568
Sebastian Redl726a0d92009-02-05 15:02:23 +00001569 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001570}
1571
1572/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1573///
1574/// handler:
1575/// 'catch' '(' exception-declaration ')' compound-statement
1576///
1577/// exception-declaration:
1578/// type-specifier-seq declarator
1579/// type-specifier-seq abstract-declarator
1580/// type-specifier-seq
1581/// '...'
1582///
1583Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1584 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1585
1586 SourceLocation CatchLoc = ConsumeToken();
1587
1588 SourceLocation LParenLoc = Tok.getLocation();
1589 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1590 return StmtError();
1591
1592 // C++ 3.3.2p3:
1593 // The name in a catch exception-declaration is local to the handler and
1594 // shall not be redeclared in the outermost block of the handler.
1595 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1596
1597 // exception-declaration is equivalent to '...' or a parameter-declaration
1598 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001599 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001600 if (Tok.isNot(tok::ellipsis)) {
1601 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001602 if (ParseCXXTypeSpecifierSeq(DS))
1603 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001604 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1605 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001606 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001607 } else
1608 ConsumeToken();
1609
1610 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1611 return StmtError();
1612
1613 if (Tok.isNot(tok::l_brace))
1614 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1615
Alexis Hunt96d5c762009-11-21 08:43:09 +00001616 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1617 OwningStmtResult Block(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001618 if (Block.isInvalid())
1619 return move(Block);
1620
Sebastian Redl726a0d92009-02-05 15:02:23 +00001621 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redlb219c902008-12-21 16:41:36 +00001622}