blob: 78d0d2f2d5512e940002ffb0260a45752a114e64 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000046/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000047/// [OMP] openmp-construct [TODO]
48///
49/// labeled-statement:
50/// identifier ':' statement
51/// 'case' constant-expression ':' statement
52/// 'default' ':' statement
53///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000054/// selection-statement:
55/// if-statement
56/// switch-statement
57///
58/// iteration-statement:
59/// while-statement
60/// do-statement
61/// for-statement
62///
Chris Lattner9075bd72006-08-10 04:59:57 +000063/// expression-statement:
64/// expression[opt] ';'
65///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000066/// jump-statement:
67/// 'goto' identifier ';'
68/// 'continue' ';'
69/// 'break' ';'
70/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000071/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000072///
Fariborz Jahanian90814572007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000075/// [OBC] '@' 'throw' ';'
76///
John McCalldadc5752010-08-24 06:29:42 +000077StmtResult
Fariborz Jahanian1db5c942010-09-28 20:42:35 +000078Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000079 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000080 StmtResult Res;
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000081
82 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000083
Alexis Hunt96d5c762009-11-21 08:43:09 +000084 CXX0XAttributeList Attr;
85 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
86 Attr = ParseCXX0XAttributes();
Ted Kremenekc162e8e2010-02-11 02:19:13 +000087 llvm::OwningPtr<AttributeList> AttrList(Attr.AttrList);
Alexis Hunt96d5c762009-11-21 08:43:09 +000088
Chris Lattner503fadc2006-08-10 05:45:44 +000089 // Cases in this switch statement should fall through if the parser expects
90 // the token to end in a semicolon (in which case SemiError should be set),
91 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000092 tok::TokenKind Kind = Tok.getKind();
93 SourceLocation AtLoc;
94 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000095 case tok::at: // May be a @try or @throw statement
96 {
97 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000098 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000099 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000100
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000101 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000102 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorf4c33342010-05-28 00:22:41 +0000103 ConsumeCodeCompletionToken();
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000104 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000105
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000106 case tok::identifier:
107 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
108 // identifier ':' statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000109 return ParseLabeledStatement(AttrList.take());
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000110 }
111 // PASS THROUGH.
112
Chris Lattner803802d2009-03-24 17:04:48 +0000113 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000114 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000115 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000116 AttrList.take(); //Passing 'Attr' to ParseDeclaration transfers ownership.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000117 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext, DeclEnd,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000118 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000119 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000120 }
121
122 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000123 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000124 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000125 }
Mike Stump11289f42009-09-09 15:08:12 +0000126
Alexis Hunt96d5c762009-11-21 08:43:09 +0000127 // FIXME: Use the attributes
Chris Lattner803802d2009-03-24 17:04:48 +0000128 // expression[opt] ';'
John McCalldadc5752010-08-24 06:29:42 +0000129 ExprResult Expr(ParseExpression());
Chris Lattner803802d2009-03-24 17:04:48 +0000130 if (Expr.isInvalid()) {
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000131 // If the expression is invalid, skip ahead to the next semicolon or '}'.
132 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattner803802d2009-03-24 17:04:48 +0000133 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000134 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
135 if (Tok.is(tok::semi))
136 ConsumeToken();
Chris Lattner803802d2009-03-24 17:04:48 +0000137 return StmtError();
138 }
139 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000140 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000141 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
Chris Lattner803802d2009-03-24 17:04:48 +0000142 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000143
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000144 case tok::kw_case: // C99 6.8.1: labeled-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000145 return ParseCaseStatement(AttrList.take());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000146 case tok::kw_default: // C99 6.8.1: labeled-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000147 return ParseDefaultStatement(AttrList.take());
Sebastian Redl042ad952008-12-11 19:30:53 +0000148
Chris Lattner9075bd72006-08-10 04:59:57 +0000149 case tok::l_brace: // C99 6.8.2: compound-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000150 return ParseCompoundStatement(AttrList.take());
Chris Lattner0f203a72007-05-28 01:45:28 +0000151 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000152 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl042ad952008-12-11 19:30:53 +0000153
Chris Lattner9075bd72006-08-10 04:59:57 +0000154 case tok::kw_if: // C99 6.8.4.1: if-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000155 return ParseIfStatement(AttrList.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000156 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000157 return ParseSwitchStatement(AttrList.take());
Sebastian Redl042ad952008-12-11 19:30:53 +0000158
Chris Lattner9075bd72006-08-10 04:59:57 +0000159 case tok::kw_while: // C99 6.8.5.1: while-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000160 return ParseWhileStatement(AttrList.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000161 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000162 Res = ParseDoStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000163 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000164 break;
165 case tok::kw_for: // C99 6.8.5.3: for-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000166 return ParseForStatement(AttrList.take());
Chris Lattner503fadc2006-08-10 05:45:44 +0000167
168 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000169 Res = ParseGotoStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000170 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000171 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000172 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000173 Res = ParseContinueStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000174 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000175 break;
176 case tok::kw_break: // C99 6.8.6.3: break-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000177 Res = ParseBreakStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000178 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000179 break;
180 case tok::kw_return: // C99 6.8.6.4: return-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000181 Res = ParseReturnStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000182 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000183 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000184
Sebastian Redlb219c902008-12-21 16:41:36 +0000185 case tok::kw_asm: {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000186 if (Attr.HasAttr)
187 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
188 << Attr.Range;
Steve Naroffb2c80c72008-02-07 03:50:06 +0000189 bool msAsm = false;
190 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000191 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000192 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000193 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000194 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000195 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000196
Sebastian Redlb219c902008-12-21 16:41:36 +0000197 case tok::kw_try: // C++ 15: try-block
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000198 return ParseCXXTryBlock(AttrList.take());
Sebastian Redlb219c902008-12-21 16:41:36 +0000199 }
200
Chris Lattner503fadc2006-08-10 05:45:44 +0000201 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000202 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000203 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000204 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000205 // If the result was valid, then we do want to diagnose this. Use
206 // ExpectAndConsume to emit the diagnostic, even though we know it won't
207 // succeed.
208 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000209 // Skip until we see a } or ;, but don't eat it.
210 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000211 }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Sebastian Redl042ad952008-12-11 19:30:53 +0000213 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000214}
215
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000216/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000217///
218/// labeled-statement:
219/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000220/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000221///
John McCalldadc5752010-08-24 06:29:42 +0000222StmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000223 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
224 "Not an identifier!");
225
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000226 llvm::OwningPtr<AttributeList> AttrList(Attr);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000227 Token IdentTok = Tok; // Save the whole token.
228 ConsumeToken(); // eat the identifier.
229
230 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000231
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000232 // identifier ':' statement
233 SourceLocation ColonLoc = ConsumeToken();
234
235 // Read label attributes, if present.
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000236 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000237 AttrList.reset(addAttributeLists(AttrList.take(), ParseGNUAttributes()));
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000238
John McCalldadc5752010-08-24 06:29:42 +0000239 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000240
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000241 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000242 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000243 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000244
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000245 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
246 IdentTok.getIdentifierInfo(),
Argyrios Kyrtzidis9f483542010-09-28 14:54:07 +0000247 ColonLoc, SubStmt.get(), AttrList.take());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000248}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000249
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000250/// ParseCaseStatement
251/// labeled-statement:
252/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000253/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000254///
John McCalldadc5752010-08-24 06:29:42 +0000255StmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000256 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000257 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000258 delete Attr;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Chris Lattner34a22092009-03-04 04:23:07 +0000260 // It is very very common for code to contain many case statements recursively
261 // nested, as in (but usually without indentation):
262 // case 1:
263 // case 2:
264 // case 3:
265 // case 4:
266 // case 5: etc.
267 //
268 // Parsing this naively works, but is both inefficient and can cause us to run
269 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000270 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000271 // but all the grossness is constrained to ParseCaseStatement (and some
272 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattner34a22092009-03-04 04:23:07 +0000274 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
275 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000276 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattner34a22092009-03-04 04:23:07 +0000278 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
279 // gets updated each time a new case is parsed, and whose body is unset so
280 // far. When parsing 'case 4', this is the 'case 3' node.
281 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner34a22092009-03-04 04:23:07 +0000283 // While we have case statements, eat and stack them.
284 do {
285 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000286
Douglas Gregord328d572009-09-21 18:10:23 +0000287 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000288 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000289 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000290 }
291
Chris Lattner125c0ee2009-12-10 00:38:54 +0000292 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
293 /// Disable this form of error recovery while we're parsing the case
294 /// expression.
295 ColonProtectionRAIIObject ColonProtection(*this);
296
John McCalldadc5752010-08-24 06:29:42 +0000297 ExprResult LHS(ParseConstantExpression());
Chris Lattner34a22092009-03-04 04:23:07 +0000298 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000299 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000300 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000301 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000302
Chris Lattner34a22092009-03-04 04:23:07 +0000303 // GNU case range extension.
304 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000305 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000306 if (Tok.is(tok::ellipsis)) {
307 Diag(Tok, diag::ext_gnu_case_range);
308 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000309
Chris Lattner34a22092009-03-04 04:23:07 +0000310 RHS = ParseConstantExpression();
311 if (RHS.isInvalid()) {
312 SkipUntil(tok::colon);
313 return StmtError();
314 }
315 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000316
317 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000318
Chris Lattner34a22092009-03-04 04:23:07 +0000319 if (Tok.isNot(tok::colon)) {
320 Diag(Tok, diag::err_expected_colon_after) << "'case'";
321 SkipUntil(tok::colon);
322 return StmtError();
323 }
324
325 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000326
John McCalldadc5752010-08-24 06:29:42 +0000327 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000328 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
329 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattner34a22092009-03-04 04:23:07 +0000331 // If we had a sema error parsing this case, then just ignore it and
332 // continue parsing the sub-stmt.
333 if (Case.isInvalid()) {
334 if (TopLevelCase.isInvalid()) // No parsed case stmts.
335 return ParseStatement();
336 // Otherwise, just don't add it as a nested case.
337 } else {
338 // If this is the first case statement we parsed, it becomes TopLevelCase.
339 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000340 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000341 if (TopLevelCase.isInvalid())
342 TopLevelCase = move(Case);
343 else
John McCallb268a282010-08-23 23:25:46 +0000344 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000345 DeepestParsedCaseStmt = NextDeepest;
346 }
Mike Stump11289f42009-09-09 15:08:12 +0000347
Chris Lattner34a22092009-03-04 04:23:07 +0000348 // Handle all case statements.
349 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000350
Chris Lattner34a22092009-03-04 04:23:07 +0000351 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner34a22092009-03-04 04:23:07 +0000353 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000354 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattner34a22092009-03-04 04:23:07 +0000356 if (Tok.isNot(tok::r_brace)) {
357 SubStmt = ParseStatement();
358 } else {
359 // Nicely diagnose the common error "switch (X) { case 4: }", which is
360 // not valid.
361 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000362 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000363 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner34a22092009-03-04 04:23:07 +0000366 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000367 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000368 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000369
Chris Lattner34a22092009-03-04 04:23:07 +0000370 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000371 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000372
Chris Lattner34a22092009-03-04 04:23:07 +0000373 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000374 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000375}
376
377/// ParseDefaultStatement
378/// labeled-statement:
379/// 'default' ':' statement
380/// Note that this does not parse the 'statement' at the end.
381///
John McCalldadc5752010-08-24 06:29:42 +0000382StmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000383 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000384 delete Attr;
385
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000386 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000387 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000388
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000389 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000390 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000391 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000392 return StmtError();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000393 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000394
Chris Lattneraf635312006-10-16 06:06:51 +0000395 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000396
Chris Lattner30f910e2006-10-16 05:52:41 +0000397 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000398 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000399 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000400 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000401 }
402
John McCalldadc5752010-08-24 06:29:42 +0000403 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000404 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000405 return StmtError();
406
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000407 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000408 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000409}
410
411
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000412/// ParseCompoundStatement - Parse a "{}" block.
413///
414/// compound-statement: [C99 6.8.2]
415/// { block-item-list[opt] }
416/// [GNU] { label-declarations block-item-list } [TODO]
417///
418/// block-item-list:
419/// block-item
420/// block-item-list block-item
421///
422/// block-item:
423/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000424/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000425/// statement
426/// [OMP] openmp-directive [TODO]
427///
428/// [GNU] label-declarations:
429/// [GNU] label-declaration
430/// [GNU] label-declarations label-declaration
431///
432/// [GNU] label-declaration:
433/// [GNU] '__label__' identifier-list ';'
434///
435/// [OMP] openmp-directive: [TODO]
436/// [OMP] barrier-directive
437/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000438///
John McCalldadc5752010-08-24 06:29:42 +0000439StmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000440 bool isStmtExpr) {
441 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000442 delete Attr;
443
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000444 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000445
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000446 // Enter a scope to hold everything within the compound stmt. Compound
447 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000448 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000449
450 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000451 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000452}
453
454
455/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000456/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000457/// consume the '}' at the end of the block. It does not manipulate the scope
458/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000459StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000460 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000461 Tok.getLocation(),
462 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000463 InMessageExpressionRAIIObject InMessage(*this, false);
464
Chris Lattnerf2978802007-01-21 06:52:16 +0000465 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
466
Chris Lattner010015a2007-05-28 07:23:54 +0000467 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000468 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000469
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000470 StmtVector Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000471 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCalldadc5752010-08-24 06:29:42 +0000472 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000473 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000474 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000475 } else {
476 // __extension__ can start declarations and it can also be a unary
477 // operator for expressions. Consume multiple __extension__ markers here
478 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000479 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000480 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000481 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000482 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000483
Alexis Hunt96d5c762009-11-21 08:43:09 +0000484 CXX0XAttributeList Attr;
485 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
486 Attr = ParseCXX0XAttributes();
487
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000488 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000489 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000490 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000491 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000492 ExtensionRAIIObject O(Diags);
493
Chris Lattner49836b42009-04-02 04:16:50 +0000494 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000495 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
496 Declarator::BlockContext, DeclEnd,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000497 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000498 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000499 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000500 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000501 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000502
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000503 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000504 SkipUntil(tok::semi);
505 continue;
506 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000507
Alexis Hunt96d5c762009-11-21 08:43:09 +0000508 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000509 // Eat the semicolon at the end of stmt and convert the expr into a
510 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000511 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000512 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000513 }
514 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000515
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000516 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000517 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000518 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000519
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000520 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000521 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000522 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000523 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000524 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000525 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000526
Chris Lattner04132372006-10-16 06:12:55 +0000527 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000528 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000529 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000530}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000531
Chris Lattnerc0081db2008-12-12 06:31:07 +0000532/// ParseParenExprOrCondition:
533/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000534/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000535///
536/// This function parses and performs error recovery on the specified condition
537/// or expression (depending on whether we're in C++ or C mode). This function
538/// goes out of its way to recover well. It returns true if there was a parser
539/// error (the right paren couldn't be found), which indicates that the caller
540/// should try to recover harder. It returns false if the condition is
541/// successfully parsed. Note that a successful parse can still have semantic
542/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000543bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000544 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000545 SourceLocation Loc,
546 bool ConvertToBoolean) {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000547 bool ParseError = false;
548
Chris Lattnerc0081db2008-12-12 06:31:07 +0000549 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000550 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +0000551 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
552 ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000553 else {
554 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000555 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000556
557 // If required, convert to a boolean value.
558 if (!ExprResult.isInvalid() && ConvertToBoolean)
559 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000560 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000561 }
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattnerc0081db2008-12-12 06:31:07 +0000563 // If the parser was confused by the condition and we don't have a ')', try to
564 // recover by skipping ahead to a semi and bailing out. If condexp is
565 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000566 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000567 SkipUntil(tok::semi);
568 // Skipping may have stopped if it found the containing ')'. If so, we can
569 // continue parsing the if statement.
570 if (Tok.isNot(tok::r_paren))
571 return true;
572 }
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattnerc0081db2008-12-12 06:31:07 +0000574 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000575 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000576 return false;
577}
578
579
Chris Lattnerc951dae2006-08-10 04:23:57 +0000580/// ParseIfStatement
581/// if-statement: [C99 6.8.4.1]
582/// 'if' '(' expression ')' statement
583/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000584/// [C++] 'if' '(' condition ')' statement
585/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000586///
John McCalldadc5752010-08-24 06:29:42 +0000587StmtResult Parser::ParseIfStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000588 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000589 delete Attr;
590
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000591 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000592 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000593
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000594 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000595 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000596 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000597 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000598 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000599
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000600 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
601
Chris Lattner2dd1b722007-08-26 23:08:06 +0000602 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
603 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000604 //
605 // C++ 6.4p3:
606 // A name introduced by a declaration in a condition is in scope from its
607 // point of declaration until the end of the substatements controlled by the
608 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000609 // C++ 3.3.2p4:
610 // Names declared in the for-init-statement, and in the condition of if,
611 // while, for, and switch statements are local to the if, while, for, or
612 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000613 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000614 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000615
Chris Lattnerc951dae2006-08-10 04:23:57 +0000616 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000617 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000618 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000619 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000620 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000621
John McCallb268a282010-08-23 23:25:46 +0000622 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000623
Chris Lattner8fb26252007-08-22 05:28:50 +0000624 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000625 // there is no compound stmt. C90 does not have this clause. We only do this
626 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000627 //
628 // C++ 6.4p1:
629 // The substatement in a selection-statement (each substatement, in the else
630 // form of the if statement) implicitly defines a local scope.
631 //
632 // For C++ we create a scope for the condition and a new scope for
633 // substatements because:
634 // -When the 'then' scope exits, we want the condition declaration to still be
635 // active for the 'else' scope too.
636 // -Sema will detect name clashes by considering declarations of a
637 // 'ControlScope' as part of its direct subscope.
638 // -If we wanted the condition and substatement to be in the same scope, we
639 // would have to notify ParseStatement not to create a new scope. It's
640 // simpler to let it create a new scope.
641 //
Mike Stump11289f42009-09-09 15:08:12 +0000642 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000643 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000644
Chris Lattner5c5808a2007-10-29 05:08:52 +0000645 // Read the 'then' stmt.
646 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000647 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000648
Chris Lattner37e54f42007-08-22 05:16:28 +0000649 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000650 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000651
Chris Lattnerc951dae2006-08-10 04:23:57 +0000652 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000653 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000654 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000655 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000656
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000657 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000658 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000659 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000660
Chris Lattner8fb26252007-08-22 05:28:50 +0000661 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000662 // there is no compound stmt. C90 does not have this clause. We only do
663 // this if the body isn't a compound statement to avoid push/pop in common
664 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000665 //
666 // C++ 6.4p1:
667 // The substatement in a selection-statement (each substatement, in the else
668 // form of the if statement) implicitly defines a local scope.
669 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000670 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000671 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000672
Chris Lattner30f910e2006-10-16 05:52:41 +0000673 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000674
Chris Lattner37e54f42007-08-22 05:16:28 +0000675 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000676 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000677 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000678
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000679 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000681 // If the condition was invalid, discard the if statement. We could recover
682 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000683 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000684 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000685
Chris Lattner5c5808a2007-10-29 05:08:52 +0000686 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000687 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000688 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000689 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
690 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
691 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000692 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000693 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000694 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000695
Chris Lattner5c5808a2007-10-29 05:08:52 +0000696 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000697 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000698 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000699 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000700 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000701
John McCallb268a282010-08-23 23:25:46 +0000702 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
703 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000704}
705
Chris Lattner9075bd72006-08-10 04:59:57 +0000706/// ParseSwitchStatement
707/// switch-statement:
708/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000709/// [C++] 'switch' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000710StmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000711 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000712 delete Attr;
713
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000714 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000715 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000716
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000717 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000718 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000719 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000720 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000721 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000722
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000723 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
724
Chris Lattner2dd1b722007-08-26 23:08:06 +0000725 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
726 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000727 //
728 // C++ 6.4p3:
729 // A name introduced by a declaration in a condition is in scope from its
730 // point of declaration until the end of the substatements controlled by the
731 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000732 // C++ 3.3.2p4:
733 // Names declared in the for-init-statement, and in the condition of if,
734 // while, for, and switch statements are local to the if, while, for, or
735 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000736 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000737 unsigned ScopeFlags = Scope::BreakScope;
738 if (C99orCXX)
739 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000740 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000741
Chris Lattner9075bd72006-08-10 04:59:57 +0000742 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000743 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000744 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000745 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000746 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000747
John McCalldadc5752010-08-24 06:29:42 +0000748 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000749 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000750
Douglas Gregore60e41a2010-05-06 17:25:47 +0000751 if (Switch.isInvalid()) {
752 // Skip the switch body.
753 // FIXME: This is not optimal recovery, but parsing the body is more
754 // dangerous due to the presence of case and default statements, which
755 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000756 if (Tok.is(tok::l_brace)) {
757 ConsumeBrace();
758 SkipUntil(tok::r_brace, false, false);
759 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000760 SkipUntil(tok::semi);
761 return move(Switch);
762 }
763
Chris Lattner8fb26252007-08-22 05:28:50 +0000764 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000765 // there is no compound stmt. C90 does not have this clause. We only do this
766 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000767 //
768 // C++ 6.4p1:
769 // The substatement in a selection-statement (each substatement, in the else
770 // form of the if statement) implicitly defines a local scope.
771 //
772 // See comments in ParseIfStatement for why we create a scope for the
773 // condition and a new scope for substatement in C++.
774 //
Mike Stump11289f42009-09-09 15:08:12 +0000775 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000776 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000777
Chris Lattner9075bd72006-08-10 04:59:57 +0000778 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000779 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000780
Chris Lattner8fd2d012010-01-24 01:50:29 +0000781 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000782 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000783 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000784
Chris Lattner8fd2d012010-01-24 01:50:29 +0000785 if (Body.isInvalid())
786 // FIXME: Remove the case statement list from the Switch statement.
787 Body = Actions.ActOnNullStmt(Tok.getLocation());
788
John McCallb268a282010-08-23 23:25:46 +0000789 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000790}
791
792/// ParseWhileStatement
793/// while-statement: [C99 6.8.5.1]
794/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000795/// [C++] 'while' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000796StmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000797 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000798 delete Attr;
799
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000800 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000801 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000802 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000803
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000804 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000805 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000806 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000807 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000808 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000809
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000810 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
811
Chris Lattner2dd1b722007-08-26 23:08:06 +0000812 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
813 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000814 //
815 // C++ 6.4p3:
816 // A name introduced by a declaration in a condition is in scope from its
817 // point of declaration until the end of the substatements controlled by the
818 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000819 // C++ 3.3.2p4:
820 // Names declared in the for-init-statement, and in the condition of if,
821 // while, for, and switch statements are local to the if, while, for, or
822 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000823 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000824 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000825 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000826 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
827 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000828 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000829 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
830 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000831
Chris Lattner9075bd72006-08-10 04:59:57 +0000832 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000833 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000834 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000835 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000836 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000837
John McCallb268a282010-08-23 23:25:46 +0000838 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000839
Chris Lattner8fb26252007-08-22 05:28:50 +0000840 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000841 // there is no compound stmt. C90 does not have this clause. We only do this
842 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000843 //
844 // C++ 6.5p2:
845 // The substatement in an iteration-statement implicitly defines a local scope
846 // which is entered and exited each time through the loop.
847 //
848 // See comments in ParseIfStatement for why we create a scope for the
849 // condition and a new scope for substatement in C++.
850 //
Mike Stump11289f42009-09-09 15:08:12 +0000851 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000852 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000853
Chris Lattner9075bd72006-08-10 04:59:57 +0000854 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000855 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000856
Chris Lattner8fb26252007-08-22 05:28:50 +0000857 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000858 InnerScope.Exit();
859 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000860
John McCall48871652010-08-21 09:40:31 +0000861 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000862 return StmtError();
863
John McCallb268a282010-08-23 23:25:46 +0000864 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000865}
866
867/// ParseDoStatement
868/// do-statement: [C99 6.8.5.2]
869/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000870/// Note: this lets the caller parse the end ';'.
John McCalldadc5752010-08-24 06:29:42 +0000871StmtResult Parser::ParseDoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000872 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000873 delete Attr;
874
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000875 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000876 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000877
Chris Lattner2dd1b722007-08-26 23:08:06 +0000878 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
879 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000880 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000881 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000882 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000883 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000884 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000885
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000886 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000887
Chris Lattner8fb26252007-08-22 05:28:50 +0000888 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000889 // there is no compound stmt. C90 does not have this clause. We only do this
890 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000891 //
892 // C++ 6.5p2:
893 // The substatement in an iteration-statement implicitly defines a local scope
894 // which is entered and exited each time through the loop.
895 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000896 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000897 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000898 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000899
Chris Lattner9075bd72006-08-10 04:59:57 +0000900 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000901 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000902
Chris Lattner8fb26252007-08-22 05:28:50 +0000903 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000904 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000905
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000906 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000907 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000908 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000909 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000910 SkipUntil(tok::semi, false, true);
911 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000912 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000913 }
Chris Lattneraf635312006-10-16 06:06:51 +0000914 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000915
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000916 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000917 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000918 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000919 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000920 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000921
Chris Lattner10da53c2008-12-12 06:35:28 +0000922 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000923 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000924 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +0000925 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000926 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000927
Sebastian Redlb62406f2008-12-11 19:48:14 +0000928 if (Cond.isInvalid() || Body.isInvalid())
929 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000930
John McCallb268a282010-08-23 23:25:46 +0000931 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
932 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000933}
934
935/// ParseForStatement
936/// for-statement: [C99 6.8.5.3]
937/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
938/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000939/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
940/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000941/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
942/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000943///
944/// [C++] for-init-statement:
945/// [C++] expression-statement
946/// [C++] simple-declaration
947///
John McCalldadc5752010-08-24 06:29:42 +0000948StmtResult Parser::ParseForStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000949 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000950 delete Attr;
951
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000952 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000953 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000954
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000955 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000956 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000957 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000958 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000959 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000960
Chris Lattner934074c2009-04-22 00:54:41 +0000961 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000962
Chris Lattner2dd1b722007-08-26 23:08:06 +0000963 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
964 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000965 //
966 // C++ 6.4p3:
967 // A name introduced by a declaration in a condition is in scope from its
968 // point of declaration until the end of the substatements controlled by the
969 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000970 // C++ 3.3.2p4:
971 // Names declared in the for-init-statement, and in the condition of if,
972 // while, for, and switch statements are local to the if, while, for, or
973 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000974 // C++ 6.5.3p1:
975 // Names declared in the for-init-statement are in the same declarative-region
976 // as those declared in the condition.
977 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000978 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000979 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000980 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
981 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000982 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000983 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
984
985 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000986
Chris Lattner04132372006-10-16 06:12:55 +0000987 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000988 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000989
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000990 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +0000991 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000992 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000993 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +0000994 ExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000995 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +0000996 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000997
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000998 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000999 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001000 C99orCXXorObjC? Sema::PCC_ForInit
1001 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001002 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001003 }
1004
Chris Lattner9075bd72006-08-10 04:59:57 +00001005 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001006 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001007 // no first part, eat the ';'.
1008 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001009 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001010 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001011 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001012 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001013
Alexis Hunt96d5c762009-11-21 08:43:09 +00001014 AttributeList *AttrList = 0;
1015 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
1016 AttrList = ParseCXX0XAttributes().AttrList;
1017
Chris Lattner49836b42009-04-02 04:16:50 +00001018 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001019 StmtVector Stmts(Actions);
1020 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
1021 DeclEnd, AttrList, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001022 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner32dc41c2009-03-29 17:27:48 +00001024 if (Tok.is(tok::semi)) { // for (int x = 4;
1025 ConsumeToken();
1026 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001027 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001028 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001029 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001030
1031 if (Tok.is(tok::code_completion)) {
1032 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1033 ConsumeCodeCompletionToken();
1034 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001035 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001036 } else {
1037 Diag(Tok, diag::err_expected_semi_for);
1038 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001039 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001040 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001041 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001042
Chris Lattnercd68f642007-06-27 01:06:29 +00001043 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001044 if (!Value.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001045 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001046
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001047 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001048 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001049 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001050 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001051
1052 if (Tok.is(tok::code_completion)) {
1053 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1054 ConsumeCodeCompletionToken();
1055 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001056 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001057 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001058 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001059 SkipUntil(tok::semi);
1060 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001061 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001062 if (!ForEach) {
John McCallb268a282010-08-23 23:25:46 +00001063 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001064 // Parse the second part of the for specifier.
1065 if (Tok.is(tok::semi)) { // for (...;;
1066 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001067 } else {
John McCalldadc5752010-08-24 06:29:42 +00001068 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001069 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001070 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1071 else {
1072 Second = ParseExpression();
1073 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001074 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001075 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001076 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001077 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001078 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001079 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001080
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001081 if (Tok.is(tok::semi)) {
1082 ConsumeToken();
1083 } else {
John McCall48871652010-08-21 09:40:31 +00001084 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001085 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001086 SkipUntil(tok::semi);
1087 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001088
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001089 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001090 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001091 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001092 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001093 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001094 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001095 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001096 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001097
Chris Lattner8fb26252007-08-22 05:28:50 +00001098 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001099 // there is no compound stmt. C90 does not have this clause. We only do this
1100 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001101 //
1102 // C++ 6.5p2:
1103 // The substatement in an iteration-statement implicitly defines a local scope
1104 // which is entered and exited each time through the loop.
1105 //
1106 // See comments in ParseIfStatement for why we create a scope for
1107 // for-init-statement/condition and a new scope for substatement in C++.
1108 //
Mike Stump11289f42009-09-09 15:08:12 +00001109 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001110 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001111
Chris Lattner9075bd72006-08-10 04:59:57 +00001112 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001113 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001114
Chris Lattner8fb26252007-08-22 05:28:50 +00001115 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001116 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001117
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001118 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001119 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001120
1121 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001122 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001123
1124 if (!ForEach)
John McCallb268a282010-08-23 23:25:46 +00001125 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1126 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001127
Douglas Gregore60e41a2010-05-06 17:25:47 +00001128 // FIXME: It isn't clear how to communicate the late destruction of
1129 // C++ temporaries used to create the collection.
John McCallb268a282010-08-23 23:25:46 +00001130 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1131 Collection.take(), RParenLoc,
1132 Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001133}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001134
Chris Lattner503fadc2006-08-10 05:45:44 +00001135/// ParseGotoStatement
1136/// jump-statement:
1137/// 'goto' identifier ';'
1138/// [GNU] 'goto' '*' expression ';'
1139///
1140/// Note: this lets the caller parse the end ';'.
1141///
John McCalldadc5752010-08-24 06:29:42 +00001142StmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001143 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001144 delete Attr;
1145
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001146 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001147 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001148
John McCalldadc5752010-08-24 06:29:42 +00001149 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001150 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001151 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001152 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001153 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001154 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001155 // GNU indirect goto extension.
1156 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001157 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001158 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001159 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001160 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001161 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001162 }
John McCallb268a282010-08-23 23:25:46 +00001163 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001164 } else {
1165 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001166 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001167 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001168
Sebastian Redlb62406f2008-12-11 19:48:14 +00001169 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001170}
1171
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001172/// ParseContinueStatement
1173/// jump-statement:
1174/// 'continue' ';'
1175///
1176/// Note: this lets the caller parse the end ';'.
1177///
John McCalldadc5752010-08-24 06:29:42 +00001178StmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001179 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001180 delete Attr;
1181
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001182 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001183 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001184}
1185
1186/// ParseBreakStatement
1187/// jump-statement:
1188/// 'break' ';'
1189///
1190/// Note: this lets the caller parse the end ';'.
1191///
John McCalldadc5752010-08-24 06:29:42 +00001192StmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001193 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001194 delete Attr;
1195
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001196 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001197 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001198}
1199
Chris Lattner503fadc2006-08-10 05:45:44 +00001200/// ParseReturnStatement
1201/// jump-statement:
1202/// 'return' expression[opt] ';'
John McCalldadc5752010-08-24 06:29:42 +00001203StmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001204 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001205 delete Attr;
1206
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001207 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001208 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001209
John McCalldadc5752010-08-24 06:29:42 +00001210 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001211 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001212 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001213 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001214 ConsumeCodeCompletionToken();
1215 SkipUntil(tok::semi, false, true);
1216 return StmtError();
1217 }
1218
Chris Lattner30f910e2006-10-16 05:52:41 +00001219 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001220 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001221 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001222 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001223 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001224 }
John McCallb268a282010-08-23 23:25:46 +00001225 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001226}
Chris Lattner0116c472006-08-15 06:03:28 +00001227
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001228/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1229/// routine is called to skip/ignore tokens that comprise the MS asm statement.
John McCalldadc5752010-08-24 06:29:42 +00001230StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001231 if (Tok.is(tok::l_brace)) {
1232 unsigned short savedBraceCount = BraceCount;
1233 do {
1234 ConsumeAnyToken();
1235 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001236 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001237 // From the MS website: If used without braces, the __asm keyword means
1238 // that the rest of the line is an assembly-language statement.
1239 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001240 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001241 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001242 do {
1243 ConsumeAnyToken();
1244 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001245 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1246 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001247 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001248 }
Mike Stump6da5d752009-12-11 00:04:56 +00001249 Token t;
1250 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001251 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001252 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001253 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001254 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001255 ExprVector Constraints(Actions);
1256 ExprVector Exprs(Actions);
1257 ExprVector Clobbers(Actions);
Anders Carlsson9a020f92010-01-30 22:25:16 +00001258 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001259 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001260 AsmString.take(), move_arg(Clobbers),
Mike Stump90be58a2010-01-04 22:37:17 +00001261 Tok.getLocation(), true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001262}
1263
Chris Lattner0116c472006-08-15 06:03:28 +00001264/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001265/// asm-statement:
1266/// gnu-asm-statement
1267/// ms-asm-statement
1268///
1269/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001270/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1271///
1272/// [GNU] asm-argument:
1273/// asm-string-literal
1274/// asm-string-literal ':' asm-operands[opt]
1275/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1276/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1277/// ':' asm-clobbers
1278///
1279/// [GNU] asm-clobbers:
1280/// asm-string-literal
1281/// asm-clobbers ',' asm-string-literal
1282///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001283/// [MS] ms-asm-statement:
1284/// '__asm' assembly-instruction ';'[opt]
1285/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1286///
1287/// [MS] assembly-instruction-list:
1288/// assembly-instruction ';'[opt]
1289/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1290///
John McCalldadc5752010-08-24 06:29:42 +00001291StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001292 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001293 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001294
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001295 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001296 msAsm = true;
1297 return FuzzyParseMicrosoftAsmStatement();
1298 }
Chris Lattner0116c472006-08-15 06:03:28 +00001299 DeclSpec DS;
1300 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001301 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001302
Chris Lattner0116c472006-08-15 06:03:28 +00001303 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001304 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001305 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001306 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001307 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001308
Chris Lattner0116c472006-08-15 06:03:28 +00001309 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001310 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001311 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001312 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001313 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001314 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001315 }
Chris Lattner04132372006-10-16 06:12:55 +00001316 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001317
John McCalldadc5752010-08-24 06:29:42 +00001318 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001319 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001320 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001321
Anders Carlsson9a020f92010-01-30 22:25:16 +00001322 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001323 ExprVector Constraints(Actions);
1324 ExprVector Exprs(Actions);
1325 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001326
Anders Carlsson19fe1162008-02-05 23:03:50 +00001327 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001328 // We have a simple asm expression like 'asm("foo")'.
1329 SourceLocation RParenLoc = ConsumeParen();
1330 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1331 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1332 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001333 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001334 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001335 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001336
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001337 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001338 bool AteExtraColon = false;
1339 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1340 // In C++ mode, parse "::" like ": :".
1341 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001342 ConsumeToken();
1343
Chris Lattner15768502009-12-20 23:08:04 +00001344 if (!AteExtraColon &&
1345 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001346 return StmtError();
1347 }
Chris Lattner15768502009-12-20 23:08:04 +00001348
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001349 unsigned NumOutputs = Names.size();
1350
1351 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001352 if (AteExtraColon ||
1353 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1354 // In C++ mode, parse "::" like ": :".
1355 if (AteExtraColon)
1356 AteExtraColon = false;
1357 else {
1358 AteExtraColon = Tok.is(tok::coloncolon);
1359 ConsumeToken();
1360 }
1361
1362 if (!AteExtraColon &&
1363 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001364 return StmtError();
1365 }
1366
1367 assert(Names.size() == Constraints.size() &&
1368 Constraints.size() == Exprs.size() &&
1369 "Input operand size mismatch!");
1370
1371 unsigned NumInputs = Names.size() - NumOutputs;
1372
1373 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001374 if (AteExtraColon || Tok.is(tok::colon)) {
1375 if (!AteExtraColon)
1376 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001377
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001378 // Parse the asm-string list for clobbers if present.
1379 if (Tok.isNot(tok::r_paren)) {
1380 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001381 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001382
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001383 if (Clobber.isInvalid())
1384 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001385
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001386 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001387
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001388 if (Tok.isNot(tok::comma)) break;
1389 ConsumeToken();
1390 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001391 }
1392 }
1393
1394 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1395 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001396 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001397 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001398 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001399 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001400}
1401
1402/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001403/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001404///
1405/// [GNU] asm-operands:
1406/// asm-operand
1407/// asm-operands ',' asm-operand
1408///
1409/// [GNU] asm-operand:
1410/// asm-string-literal '(' expression ')'
1411/// '[' identifier ']' asm-string-literal '(' expression ')'
1412///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001413//
1414// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001415bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1416 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1417 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001418 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001419 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001420 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001421
1422 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001423 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001424 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001425 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001426
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001427 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001428 Diag(Tok, diag::err_expected_ident);
1429 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001430 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001431 }
Mike Stump11289f42009-09-09 15:08:12 +00001432
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001433 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001434 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001435
Anders Carlsson9a020f92010-01-30 22:25:16 +00001436 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001437 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001438 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001439 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001440
John McCalldadc5752010-08-24 06:29:42 +00001441 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001442 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001443 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001444 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001445 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001446 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001447
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001448 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001449 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001450 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001451 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001452 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001453
Chris Lattner0116c472006-08-15 06:03:28 +00001454 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001455 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001456 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001457 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001458 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001459 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001460 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001461 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001462 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001463 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001464 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001465 ConsumeToken();
1466 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001467
1468 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001469}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001470
John McCall48871652010-08-21 09:40:31 +00001471Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001472 assert(Tok.is(tok::l_brace));
1473 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001474
John McCallfaf5fb42010-08-26 23:41:50 +00001475 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1476 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001477
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001478 // Do not enter a scope for the brace, as the arguments are in the same scope
1479 // (the function body) as the body itself. Instead, just read the statement
1480 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001481 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001482
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001483 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001484 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001485 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001486 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001487
John McCallb268a282010-08-23 23:25:46 +00001488 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001489}
Sebastian Redlb219c902008-12-21 16:41:36 +00001490
Sebastian Redla7b98a72009-04-26 20:35:05 +00001491/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1492///
1493/// function-try-block:
1494/// 'try' ctor-initializer[opt] compound-statement handler-seq
1495///
John McCall48871652010-08-21 09:40:31 +00001496Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001497 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1498 SourceLocation TryLoc = ConsumeToken();
1499
John McCallfaf5fb42010-08-26 23:41:50 +00001500 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1501 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001502
1503 // Constructor initializer list?
1504 if (Tok.is(tok::colon))
1505 ParseConstructorInitializer(Decl);
1506
Sebastian Redld98ecd62009-04-26 21:08:36 +00001507 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001508 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001509 // If we failed to parse the try-catch, we just give the function an empty
1510 // compound statement as the body.
1511 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001512 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001513 MultiStmtArg(Actions), false);
1514
John McCallb268a282010-08-23 23:25:46 +00001515 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001516}
1517
Sebastian Redlb219c902008-12-21 16:41:36 +00001518/// ParseCXXTryBlock - Parse a C++ try-block.
1519///
1520/// try-block:
1521/// 'try' compound-statement handler-seq
1522///
John McCalldadc5752010-08-24 06:29:42 +00001523StmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001524 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001525 delete Attr;
1526
Sebastian Redlb219c902008-12-21 16:41:36 +00001527 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1528
1529 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001530 return ParseCXXTryBlockCommon(TryLoc);
1531}
1532
1533/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1534/// function-try-block.
1535///
1536/// try-block:
1537/// 'try' compound-statement handler-seq
1538///
1539/// function-try-block:
1540/// 'try' ctor-initializer[opt] compound-statement handler-seq
1541///
1542/// handler-seq:
1543/// handler handler-seq[opt]
1544///
John McCalldadc5752010-08-24 06:29:42 +00001545StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001546 if (Tok.isNot(tok::l_brace))
1547 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001548 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001549 StmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001550 if (TryBlock.isInvalid())
1551 return move(TryBlock);
1552
1553 StmtVector Handlers(Actions);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001554 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1555 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1556 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1557 << Attr.Range;
1558 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001559 if (Tok.isNot(tok::kw_catch))
1560 return StmtError(Diag(Tok, diag::err_expected_catch));
1561 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001562 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001563 if (!Handler.isInvalid())
1564 Handlers.push_back(Handler.release());
1565 }
1566 // Don't bother creating the full statement if we don't have any usable
1567 // handlers.
1568 if (Handlers.empty())
1569 return StmtError();
1570
John McCallb268a282010-08-23 23:25:46 +00001571 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001572}
1573
1574/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1575///
1576/// handler:
1577/// 'catch' '(' exception-declaration ')' compound-statement
1578///
1579/// exception-declaration:
1580/// type-specifier-seq declarator
1581/// type-specifier-seq abstract-declarator
1582/// type-specifier-seq
1583/// '...'
1584///
John McCalldadc5752010-08-24 06:29:42 +00001585StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001586 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1587
1588 SourceLocation CatchLoc = ConsumeToken();
1589
1590 SourceLocation LParenLoc = Tok.getLocation();
1591 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1592 return StmtError();
1593
1594 // C++ 3.3.2p3:
1595 // The name in a catch exception-declaration is local to the handler and
1596 // shall not be redeclared in the outermost block of the handler.
1597 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1598
1599 // exception-declaration is equivalent to '...' or a parameter-declaration
1600 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001601 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001602 if (Tok.isNot(tok::ellipsis)) {
1603 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001604 if (ParseCXXTypeSpecifierSeq(DS))
1605 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001606 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1607 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001608 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001609 } else
1610 ConsumeToken();
1611
1612 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1613 return StmtError();
1614
1615 if (Tok.isNot(tok::l_brace))
1616 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1617
Alexis Hunt96d5c762009-11-21 08:43:09 +00001618 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001619 StmtResult Block(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001620 if (Block.isInvalid())
1621 return move(Block);
1622
John McCallb268a282010-08-23 23:25:46 +00001623 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001624}