blob: 26d2279d31ef1e62ef96cccaadc7a466031202d7 [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 Kremenek5eec2b02010-11-10 05:59:39 +000087 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 Kremenek5eec2b02010-11-10 05:59:39 +0000109 return ParseLabeledStatement(AttrList);
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 Kremenek5eec2b02010-11-10 05:59:39 +0000116 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
117 DeclEnd, Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000118 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000119 }
120
121 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000122 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000123 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Alexis Hunt96d5c762009-11-21 08:43:09 +0000126 // FIXME: Use the attributes
Chris Lattner803802d2009-03-24 17:04:48 +0000127 // expression[opt] ';'
John McCalldadc5752010-08-24 06:29:42 +0000128 ExprResult Expr(ParseExpression());
Chris Lattner803802d2009-03-24 17:04:48 +0000129 if (Expr.isInvalid()) {
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000130 // If the expression is invalid, skip ahead to the next semicolon or '}'.
131 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattner803802d2009-03-24 17:04:48 +0000132 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000133 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
134 if (Tok.is(tok::semi))
135 ConsumeToken();
Chris Lattner803802d2009-03-24 17:04:48 +0000136 return StmtError();
137 }
138 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000139 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000140 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
Chris Lattner803802d2009-03-24 17:04:48 +0000141 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000142
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000143 case tok::kw_case: // C99 6.8.1: labeled-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000144 return ParseCaseStatement(AttrList);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000145 case tok::kw_default: // C99 6.8.1: labeled-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000146 return ParseDefaultStatement(AttrList);
Sebastian Redl042ad952008-12-11 19:30:53 +0000147
Chris Lattner9075bd72006-08-10 04:59:57 +0000148 case tok::l_brace: // C99 6.8.2: compound-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000149 return ParseCompoundStatement(AttrList);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000150 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
151 bool LeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
152 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacro);
153 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000154
Chris Lattner9075bd72006-08-10 04:59:57 +0000155 case tok::kw_if: // C99 6.8.4.1: if-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000156 return ParseIfStatement(AttrList);
Chris Lattner9075bd72006-08-10 04:59:57 +0000157 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000158 return ParseSwitchStatement(AttrList);
Sebastian Redl042ad952008-12-11 19:30:53 +0000159
Chris Lattner9075bd72006-08-10 04:59:57 +0000160 case tok::kw_while: // C99 6.8.5.1: while-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000161 return ParseWhileStatement(AttrList);
Chris Lattner9075bd72006-08-10 04:59:57 +0000162 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000163 Res = ParseDoStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000164 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000165 break;
166 case tok::kw_for: // C99 6.8.5.3: for-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000167 return ParseForStatement(AttrList);
Chris Lattner503fadc2006-08-10 05:45:44 +0000168
169 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000170 Res = ParseGotoStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000171 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000172 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000173 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000174 Res = ParseContinueStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000175 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000176 break;
177 case tok::kw_break: // C99 6.8.6.3: break-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000178 Res = ParseBreakStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000179 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000180 break;
181 case tok::kw_return: // C99 6.8.6.4: return-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000182 Res = ParseReturnStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000183 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000184 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000185
Sebastian Redlb219c902008-12-21 16:41:36 +0000186 case tok::kw_asm: {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000187 if (Attr.HasAttr)
188 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
189 << Attr.Range;
Steve Naroffb2c80c72008-02-07 03:50:06 +0000190 bool msAsm = false;
191 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000192 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000193 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000194 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000195 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000196 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000197
Sebastian Redlb219c902008-12-21 16:41:36 +0000198 case tok::kw_try: // C++ 15: try-block
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000199 return ParseCXXTryBlock(AttrList);
Sebastian Redlb219c902008-12-21 16:41:36 +0000200 }
201
Chris Lattner503fadc2006-08-10 05:45:44 +0000202 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000203 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000204 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000205 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000206 // If the result was valid, then we do want to diagnose this. Use
207 // ExpectAndConsume to emit the diagnostic, even though we know it won't
208 // succeed.
209 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000210 // Skip until we see a } or ;, but don't eat it.
211 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000212 }
Mike Stump11289f42009-09-09 15:08:12 +0000213
Sebastian Redl042ad952008-12-11 19:30:53 +0000214 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000215}
216
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000217/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000218///
219/// labeled-statement:
220/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000221/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000222///
John McCalldadc5752010-08-24 06:29:42 +0000223StmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000224 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
225 "Not an identifier!");
226
227 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 Kremenek5eec2b02010-11-10 05:59:39 +0000237 Attr = addAttributeLists(Attr, 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(),
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000247 ColonLoc, SubStmt.get(), Attr);
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?
Mike Stump11289f42009-09-09 15:08:12 +0000258
Chris Lattner34a22092009-03-04 04:23:07 +0000259 // It is very very common for code to contain many case statements recursively
260 // nested, as in (but usually without indentation):
261 // case 1:
262 // case 2:
263 // case 3:
264 // case 4:
265 // case 5: etc.
266 //
267 // Parsing this naively works, but is both inefficient and can cause us to run
268 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000269 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000270 // but all the grossness is constrained to ParseCaseStatement (and some
271 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner34a22092009-03-04 04:23:07 +0000273 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
274 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000275 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000276
Chris Lattner34a22092009-03-04 04:23:07 +0000277 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
278 // gets updated each time a new case is parsed, and whose body is unset so
279 // far. When parsing 'case 4', this is the 'case 3' node.
280 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattner34a22092009-03-04 04:23:07 +0000282 // While we have case statements, eat and stack them.
283 do {
284 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000285
Douglas Gregord328d572009-09-21 18:10:23 +0000286 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000287 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000288 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000289 }
290
Chris Lattner125c0ee2009-12-10 00:38:54 +0000291 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
292 /// Disable this form of error recovery while we're parsing the case
293 /// expression.
294 ColonProtectionRAIIObject ColonProtection(*this);
295
John McCalldadc5752010-08-24 06:29:42 +0000296 ExprResult LHS(ParseConstantExpression());
Chris Lattner34a22092009-03-04 04:23:07 +0000297 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000298 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000299 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000300 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000301
Chris Lattner34a22092009-03-04 04:23:07 +0000302 // GNU case range extension.
303 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000304 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000305 if (Tok.is(tok::ellipsis)) {
306 Diag(Tok, diag::ext_gnu_case_range);
307 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000308
Chris Lattner34a22092009-03-04 04:23:07 +0000309 RHS = ParseConstantExpression();
310 if (RHS.isInvalid()) {
311 SkipUntil(tok::colon);
312 return StmtError();
313 }
314 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000315
316 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000317
Chris Lattner34a22092009-03-04 04:23:07 +0000318 if (Tok.isNot(tok::colon)) {
319 Diag(Tok, diag::err_expected_colon_after) << "'case'";
320 SkipUntil(tok::colon);
321 return StmtError();
322 }
323
324 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000325
John McCalldadc5752010-08-24 06:29:42 +0000326 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000327 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
328 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000329
Chris Lattner34a22092009-03-04 04:23:07 +0000330 // If we had a sema error parsing this case, then just ignore it and
331 // continue parsing the sub-stmt.
332 if (Case.isInvalid()) {
333 if (TopLevelCase.isInvalid()) // No parsed case stmts.
334 return ParseStatement();
335 // Otherwise, just don't add it as a nested case.
336 } else {
337 // If this is the first case statement we parsed, it becomes TopLevelCase.
338 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000339 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000340 if (TopLevelCase.isInvalid())
341 TopLevelCase = move(Case);
342 else
John McCallb268a282010-08-23 23:25:46 +0000343 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000344 DeepestParsedCaseStmt = NextDeepest;
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner34a22092009-03-04 04:23:07 +0000347 // Handle all case statements.
348 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattner34a22092009-03-04 04:23:07 +0000350 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattner34a22092009-03-04 04:23:07 +0000352 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000353 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattner34a22092009-03-04 04:23:07 +0000355 if (Tok.isNot(tok::r_brace)) {
356 SubStmt = ParseStatement();
357 } else {
358 // Nicely diagnose the common error "switch (X) { case 4: }", which is
359 // not valid.
360 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000361 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000362 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000363 }
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattner34a22092009-03-04 04:23:07 +0000365 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000366 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000367 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattner34a22092009-03-04 04:23:07 +0000369 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000370 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000371
Chris Lattner34a22092009-03-04 04:23:07 +0000372 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000373 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000374}
375
376/// ParseDefaultStatement
377/// labeled-statement:
378/// 'default' ':' statement
379/// Note that this does not parse the 'statement' at the end.
380///
John McCalldadc5752010-08-24 06:29:42 +0000381StmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000382 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000383
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000384 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000385 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000386
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000387 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000388 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000389 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000390 return StmtError();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000391 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000392
Chris Lattneraf635312006-10-16 06:06:51 +0000393 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000394
Chris Lattner30f910e2006-10-16 05:52:41 +0000395 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000396 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000397 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000398 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000399 }
400
John McCalldadc5752010-08-24 06:29:42 +0000401 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000402 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000403 return StmtError();
404
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000405 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000406 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000407}
408
409
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000410/// ParseCompoundStatement - Parse a "{}" block.
411///
412/// compound-statement: [C99 6.8.2]
413/// { block-item-list[opt] }
414/// [GNU] { label-declarations block-item-list } [TODO]
415///
416/// block-item-list:
417/// block-item
418/// block-item-list block-item
419///
420/// block-item:
421/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000422/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000423/// statement
424/// [OMP] openmp-directive [TODO]
425///
426/// [GNU] label-declarations:
427/// [GNU] label-declaration
428/// [GNU] label-declarations label-declaration
429///
430/// [GNU] label-declaration:
431/// [GNU] '__label__' identifier-list ';'
432///
433/// [OMP] openmp-directive: [TODO]
434/// [OMP] barrier-directive
435/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000436///
John McCalldadc5752010-08-24 06:29:42 +0000437StmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000438 bool isStmtExpr) {
439 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000440
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000441 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000442
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000443 // Enter a scope to hold everything within the compound stmt. Compound
444 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000445 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000446
447 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000448 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000449}
450
451
452/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000453/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000454/// consume the '}' at the end of the block. It does not manipulate the scope
455/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000456StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000457 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000458 Tok.getLocation(),
459 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000460 InMessageExpressionRAIIObject InMessage(*this, false);
461
Chris Lattnerf2978802007-01-21 06:52:16 +0000462 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
463
Chris Lattner010015a2007-05-28 07:23:54 +0000464 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000465 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000466
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000467 StmtVector Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000468 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCalldadc5752010-08-24 06:29:42 +0000469 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000470 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000471 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000472 } else {
473 // __extension__ can start declarations and it can also be a unary
474 // operator for expressions. Consume multiple __extension__ markers here
475 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000476 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000477 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000478 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000479 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000480
Alexis Hunt96d5c762009-11-21 08:43:09 +0000481 CXX0XAttributeList Attr;
482 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
483 Attr = ParseCXX0XAttributes();
484
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000485 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000486 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000487 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000488 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000489 ExtensionRAIIObject O(Diags);
490
Chris Lattner49836b42009-04-02 04:16:50 +0000491 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000492 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
493 Declarator::BlockContext, DeclEnd,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000494 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000495 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000496 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000497 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000498 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000499
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000500 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000501 SkipUntil(tok::semi);
502 continue;
503 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000504
Alexis Hunt96d5c762009-11-21 08:43:09 +0000505 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000506 // Eat the semicolon at the end of stmt and convert the expr into a
507 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000508 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000509 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000510 }
511 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000512
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000513 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000514 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000515 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000516
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000517 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000518 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000519 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000520 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000521 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000522 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000523
Chris Lattner04132372006-10-16 06:12:55 +0000524 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000525 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000526 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000527}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000528
Chris Lattnerc0081db2008-12-12 06:31:07 +0000529/// ParseParenExprOrCondition:
530/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000531/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000532///
533/// This function parses and performs error recovery on the specified condition
534/// or expression (depending on whether we're in C++ or C mode). This function
535/// goes out of its way to recover well. It returns true if there was a parser
536/// error (the right paren couldn't be found), which indicates that the caller
537/// should try to recover harder. It returns false if the condition is
538/// successfully parsed. Note that a successful parse can still have semantic
539/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000540bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000541 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000542 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000543 bool ConvertToBoolean) {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000544 bool ParseError = false;
545
Chris Lattnerc0081db2008-12-12 06:31:07 +0000546 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000547 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +0000548 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
549 ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000550 else {
551 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000552 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000553
554 // If required, convert to a boolean value.
555 if (!ExprResult.isInvalid() && ConvertToBoolean)
556 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000557 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000558 }
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattnerc0081db2008-12-12 06:31:07 +0000560 // If the parser was confused by the condition and we don't have a ')', try to
561 // recover by skipping ahead to a semi and bailing out. If condexp is
562 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000563 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000564 SkipUntil(tok::semi);
565 // Skipping may have stopped if it found the containing ')'. If so, we can
566 // continue parsing the if statement.
567 if (Tok.isNot(tok::r_paren))
568 return true;
569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Chris Lattnerc0081db2008-12-12 06:31:07 +0000571 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000572 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000573 return false;
574}
575
576
Chris Lattnerc951dae2006-08-10 04:23:57 +0000577/// ParseIfStatement
578/// if-statement: [C99 6.8.4.1]
579/// 'if' '(' expression ')' statement
580/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000581/// [C++] 'if' '(' condition ')' statement
582/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000583///
John McCalldadc5752010-08-24 06:29:42 +0000584StmtResult Parser::ParseIfStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000585 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000586
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000587 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000588 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000589
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000590 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000591 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000592 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000593 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000594 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000595
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000596 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
597
Chris Lattner2dd1b722007-08-26 23:08:06 +0000598 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
599 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000600 //
601 // C++ 6.4p3:
602 // A name introduced by a declaration in a condition is in scope from its
603 // point of declaration until the end of the substatements controlled by the
604 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000605 // C++ 3.3.2p4:
606 // Names declared in the for-init-statement, and in the condition of if,
607 // while, for, and switch statements are local to the if, while, for, or
608 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000609 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000610 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000611
Chris Lattnerc951dae2006-08-10 04:23:57 +0000612 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000613 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000614 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000615 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000616 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000617
John McCallb268a282010-08-23 23:25:46 +0000618 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattner8fb26252007-08-22 05:28:50 +0000620 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000621 // there is no compound stmt. C90 does not have this clause. We only do this
622 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000623 //
624 // C++ 6.4p1:
625 // The substatement in a selection-statement (each substatement, in the else
626 // form of the if statement) implicitly defines a local scope.
627 //
628 // For C++ we create a scope for the condition and a new scope for
629 // substatements because:
630 // -When the 'then' scope exits, we want the condition declaration to still be
631 // active for the 'else' scope too.
632 // -Sema will detect name clashes by considering declarations of a
633 // 'ControlScope' as part of its direct subscope.
634 // -If we wanted the condition and substatement to be in the same scope, we
635 // would have to notify ParseStatement not to create a new scope. It's
636 // simpler to let it create a new scope.
637 //
Mike Stump11289f42009-09-09 15:08:12 +0000638 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000639 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000640
Chris Lattner5c5808a2007-10-29 05:08:52 +0000641 // Read the 'then' stmt.
642 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000643 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000644
Chris Lattner37e54f42007-08-22 05:16:28 +0000645 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000646 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000647
Chris Lattnerc951dae2006-08-10 04:23:57 +0000648 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000649 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000650 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000651 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000652
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000653 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000654 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000655 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000656
Chris Lattner8fb26252007-08-22 05:28:50 +0000657 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000658 // there is no compound stmt. C90 does not have this clause. We only do
659 // this if the body isn't a compound statement to avoid push/pop in common
660 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000661 //
662 // C++ 6.4p1:
663 // The substatement in a selection-statement (each substatement, in the else
664 // form of the if statement) implicitly defines a local scope.
665 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000666 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000667 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000668
Chris Lattner30f910e2006-10-16 05:52:41 +0000669 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000670
Chris Lattner37e54f42007-08-22 05:16:28 +0000671 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000672 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000673 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000674
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000675 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000676
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000677 // If the condition was invalid, discard the if statement. We could recover
678 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000679 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000680 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000681
Chris Lattner5c5808a2007-10-29 05:08:52 +0000682 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000683 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000684 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000685 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
686 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
687 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000688 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000689 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000690 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000691
Chris Lattner5c5808a2007-10-29 05:08:52 +0000692 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000693 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000694 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000695 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000696 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000697
John McCallb268a282010-08-23 23:25:46 +0000698 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000699 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000700}
701
Chris Lattner9075bd72006-08-10 04:59:57 +0000702/// ParseSwitchStatement
703/// switch-statement:
704/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000705/// [C++] 'switch' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000706StmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000707 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000708
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000709 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000710 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000711
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000712 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000713 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000714 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000715 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000716 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000717
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000718 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
719
Chris Lattner2dd1b722007-08-26 23:08:06 +0000720 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
721 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000722 //
723 // C++ 6.4p3:
724 // A name introduced by a declaration in a condition is in scope from its
725 // point of declaration until the end of the substatements controlled by the
726 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000727 // C++ 3.3.2p4:
728 // Names declared in the for-init-statement, and in the condition of if,
729 // while, for, and switch statements are local to the if, while, for, or
730 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000731 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000732 unsigned ScopeFlags = Scope::BreakScope;
733 if (C99orCXX)
734 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000735 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000736
Chris Lattner9075bd72006-08-10 04:59:57 +0000737 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000738 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000739 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000740 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000741 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000742
John McCalldadc5752010-08-24 06:29:42 +0000743 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000744 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000745
Douglas Gregore60e41a2010-05-06 17:25:47 +0000746 if (Switch.isInvalid()) {
747 // Skip the switch body.
748 // FIXME: This is not optimal recovery, but parsing the body is more
749 // dangerous due to the presence of case and default statements, which
750 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000751 if (Tok.is(tok::l_brace)) {
752 ConsumeBrace();
753 SkipUntil(tok::r_brace, false, false);
754 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000755 SkipUntil(tok::semi);
756 return move(Switch);
757 }
758
Chris Lattner8fb26252007-08-22 05:28:50 +0000759 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000760 // there is no compound stmt. C90 does not have this clause. We only do this
761 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000762 //
763 // C++ 6.4p1:
764 // The substatement in a selection-statement (each substatement, in the else
765 // form of the if statement) implicitly defines a local scope.
766 //
767 // See comments in ParseIfStatement for why we create a scope for the
768 // condition and a new scope for substatement in C++.
769 //
Mike Stump11289f42009-09-09 15:08:12 +0000770 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000771 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000772
Chris Lattner9075bd72006-08-10 04:59:57 +0000773 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000774 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000775
Chris Lattner8fd2d012010-01-24 01:50:29 +0000776 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000777 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000778 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000779
Chris Lattner8fd2d012010-01-24 01:50:29 +0000780 if (Body.isInvalid())
781 // FIXME: Remove the case statement list from the Switch statement.
782 Body = Actions.ActOnNullStmt(Tok.getLocation());
783
John McCallb268a282010-08-23 23:25:46 +0000784 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000785}
786
787/// ParseWhileStatement
788/// while-statement: [C99 6.8.5.1]
789/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000790/// [C++] 'while' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000791StmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000792 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000793
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000794 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000795 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000796 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000797
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000798 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000799 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000800 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000801 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000802 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000803
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000804 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
805
Chris Lattner2dd1b722007-08-26 23:08:06 +0000806 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
807 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000808 //
809 // C++ 6.4p3:
810 // A name introduced by a declaration in a condition is in scope from its
811 // point of declaration until the end of the substatements controlled by the
812 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000813 // C++ 3.3.2p4:
814 // Names declared in the for-init-statement, and in the condition of if,
815 // while, for, and switch statements are local to the if, while, for, or
816 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000817 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000818 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000819 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000820 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
821 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000822 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000823 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
824 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000825
Chris Lattner9075bd72006-08-10 04:59:57 +0000826 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000827 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000828 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000829 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000830 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000831
John McCallb268a282010-08-23 23:25:46 +0000832 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000833
Chris Lattner8fb26252007-08-22 05:28:50 +0000834 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000835 // there is no compound stmt. C90 does not have this clause. We only do this
836 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000837 //
838 // C++ 6.5p2:
839 // The substatement in an iteration-statement implicitly defines a local scope
840 // which is entered and exited each time through the loop.
841 //
842 // See comments in ParseIfStatement for why we create a scope for the
843 // condition and a new scope for substatement in C++.
844 //
Mike Stump11289f42009-09-09 15:08:12 +0000845 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000846 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000847
Chris Lattner9075bd72006-08-10 04:59:57 +0000848 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000849 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000850
Chris Lattner8fb26252007-08-22 05:28:50 +0000851 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000852 InnerScope.Exit();
853 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000854
John McCall48871652010-08-21 09:40:31 +0000855 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000856 return StmtError();
857
John McCallb268a282010-08-23 23:25:46 +0000858 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000859}
860
861/// ParseDoStatement
862/// do-statement: [C99 6.8.5.2]
863/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000864/// Note: this lets the caller parse the end ';'.
John McCalldadc5752010-08-24 06:29:42 +0000865StmtResult Parser::ParseDoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000866 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000867
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000868 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000869 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000870
Chris Lattner2dd1b722007-08-26 23:08:06 +0000871 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
872 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000873 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000874 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000875 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000876 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000877 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000878
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000879 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000880
Chris Lattner8fb26252007-08-22 05:28:50 +0000881 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000882 // there is no compound stmt. C90 does not have this clause. We only do this
883 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000884 //
885 // C++ 6.5p2:
886 // The substatement in an iteration-statement implicitly defines a local scope
887 // which is entered and exited each time through the loop.
888 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000889 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000890 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000891 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000892
Chris Lattner9075bd72006-08-10 04:59:57 +0000893 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000894 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000895
Chris Lattner8fb26252007-08-22 05:28:50 +0000896 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000897 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000898
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000899 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000900 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000901 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000902 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000903 SkipUntil(tok::semi, false, true);
904 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000905 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000906 }
Chris Lattneraf635312006-10-16 06:06:51 +0000907 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000908
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000909 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000910 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000911 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000912 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000913 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000914
Chris Lattner10da53c2008-12-12 06:35:28 +0000915 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000916 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000917 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +0000918 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000919 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000920
Sebastian Redlb62406f2008-12-11 19:48:14 +0000921 if (Cond.isInvalid() || Body.isInvalid())
922 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000923
John McCallb268a282010-08-23 23:25:46 +0000924 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
925 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000926}
927
928/// ParseForStatement
929/// for-statement: [C99 6.8.5.3]
930/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
931/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000932/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
933/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000934/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
935/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000936///
937/// [C++] for-init-statement:
938/// [C++] expression-statement
939/// [C++] simple-declaration
940///
John McCalldadc5752010-08-24 06:29:42 +0000941StmtResult Parser::ParseForStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000942 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000943
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000944 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000945 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000946
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000947 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000948 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000949 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000950 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000951 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000952
Chris Lattner934074c2009-04-22 00:54:41 +0000953 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000954
Chris Lattner2dd1b722007-08-26 23:08:06 +0000955 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
956 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000957 //
958 // C++ 6.4p3:
959 // A name introduced by a declaration in a condition is in scope from its
960 // point of declaration until the end of the substatements controlled by the
961 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000962 // C++ 3.3.2p4:
963 // Names declared in the for-init-statement, and in the condition of if,
964 // while, for, and switch statements are local to the if, while, for, or
965 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000966 // C++ 6.5.3p1:
967 // Names declared in the for-init-statement are in the same declarative-region
968 // as those declared in the condition.
969 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000970 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000971 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000972 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
973 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000974 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000975 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
976
977 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000978
Chris Lattner04132372006-10-16 06:12:55 +0000979 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000980 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000981
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000982 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +0000983 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000984 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000985 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +0000986 ExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000987 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +0000988 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000989
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000990 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000991 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000992 C99orCXXorObjC? Sema::PCC_ForInit
993 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000994 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000995 }
996
Chris Lattner9075bd72006-08-10 04:59:57 +0000997 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000998 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000999 // no first part, eat the ';'.
1000 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001001 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001002 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001003 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001004 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001005
Alexis Hunt96d5c762009-11-21 08:43:09 +00001006 AttributeList *AttrList = 0;
1007 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
1008 AttrList = ParseCXX0XAttributes().AttrList;
1009
Chris Lattner49836b42009-04-02 04:16:50 +00001010 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001011 StmtVector Stmts(Actions);
1012 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
1013 DeclEnd, AttrList, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001014 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattner32dc41c2009-03-29 17:27:48 +00001016 if (Tok.is(tok::semi)) { // for (int x = 4;
1017 ConsumeToken();
1018 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001019 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001020 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001021 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001022
1023 if (Tok.is(tok::code_completion)) {
1024 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1025 ConsumeCodeCompletionToken();
1026 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001027 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001028 } else {
1029 Diag(Tok, diag::err_expected_semi_for);
1030 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001031 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001032 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001033 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001034
Chris Lattnercd68f642007-06-27 01:06:29 +00001035 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001036 if (!Value.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001037 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001038
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001039 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001040 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001041 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001042 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001043
1044 if (Tok.is(tok::code_completion)) {
1045 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1046 ConsumeCodeCompletionToken();
1047 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001048 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001049 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001050 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001051 SkipUntil(tok::semi);
1052 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001053 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001054 if (!ForEach) {
John McCallb268a282010-08-23 23:25:46 +00001055 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001056 // Parse the second part of the for specifier.
1057 if (Tok.is(tok::semi)) { // for (...;;
1058 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001059 } else {
John McCalldadc5752010-08-24 06:29:42 +00001060 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001061 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001062 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1063 else {
1064 Second = ParseExpression();
1065 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001066 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001067 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001068 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001069 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001070 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001071 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001072
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001073 if (Tok.is(tok::semi)) {
1074 ConsumeToken();
1075 } else {
John McCall48871652010-08-21 09:40:31 +00001076 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001077 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001078 SkipUntil(tok::semi);
1079 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001080
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001081 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001082 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001083 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001084 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001085 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001086 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001087 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001088 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001089
Chris Lattner8fb26252007-08-22 05:28:50 +00001090 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001091 // there is no compound stmt. C90 does not have this clause. We only do this
1092 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001093 //
1094 // C++ 6.5p2:
1095 // The substatement in an iteration-statement implicitly defines a local scope
1096 // which is entered and exited each time through the loop.
1097 //
1098 // See comments in ParseIfStatement for why we create a scope for
1099 // for-init-statement/condition and a new scope for substatement in C++.
1100 //
Mike Stump11289f42009-09-09 15:08:12 +00001101 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001102 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001103
Chris Lattner9075bd72006-08-10 04:59:57 +00001104 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001105 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001106
Chris Lattner8fb26252007-08-22 05:28:50 +00001107 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001108 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001109
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001110 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001111 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001112
1113 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001114 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001115
1116 if (!ForEach)
John McCallb268a282010-08-23 23:25:46 +00001117 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1118 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001119
Douglas Gregore60e41a2010-05-06 17:25:47 +00001120 // FIXME: It isn't clear how to communicate the late destruction of
1121 // C++ temporaries used to create the collection.
John McCallb268a282010-08-23 23:25:46 +00001122 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1123 Collection.take(), RParenLoc,
1124 Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001125}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001126
Chris Lattner503fadc2006-08-10 05:45:44 +00001127/// ParseGotoStatement
1128/// jump-statement:
1129/// 'goto' identifier ';'
1130/// [GNU] 'goto' '*' expression ';'
1131///
1132/// Note: this lets the caller parse the end ';'.
1133///
John McCalldadc5752010-08-24 06:29:42 +00001134StmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001135 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001136
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001137 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001138 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001139
John McCalldadc5752010-08-24 06:29:42 +00001140 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001141 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001142 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001143 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001144 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001145 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001146 // GNU indirect goto extension.
1147 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001148 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001149 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001150 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001151 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001152 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001153 }
John McCallb268a282010-08-23 23:25:46 +00001154 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001155 } else {
1156 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001157 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001158 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001159
Sebastian Redlb62406f2008-12-11 19:48:14 +00001160 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001161}
1162
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001163/// ParseContinueStatement
1164/// jump-statement:
1165/// 'continue' ';'
1166///
1167/// Note: this lets the caller parse the end ';'.
1168///
John McCalldadc5752010-08-24 06:29:42 +00001169StmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001170 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001171
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001172 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001173 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001174}
1175
1176/// ParseBreakStatement
1177/// jump-statement:
1178/// 'break' ';'
1179///
1180/// Note: this lets the caller parse the end ';'.
1181///
John McCalldadc5752010-08-24 06:29:42 +00001182StmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001183 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001184
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001185 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001186 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001187}
1188
Chris Lattner503fadc2006-08-10 05:45:44 +00001189/// ParseReturnStatement
1190/// jump-statement:
1191/// 'return' expression[opt] ';'
John McCalldadc5752010-08-24 06:29:42 +00001192StmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001193 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001194
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001195 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001196 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001197
John McCalldadc5752010-08-24 06:29:42 +00001198 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001199 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001200 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001201 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001202 ConsumeCodeCompletionToken();
1203 SkipUntil(tok::semi, false, true);
1204 return StmtError();
1205 }
1206
Chris Lattner30f910e2006-10-16 05:52:41 +00001207 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001208 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001209 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001210 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001211 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001212 }
John McCallb268a282010-08-23 23:25:46 +00001213 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001214}
Chris Lattner0116c472006-08-15 06:03:28 +00001215
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001216/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1217/// routine is called to skip/ignore tokens that comprise the MS asm statement.
John McCalldadc5752010-08-24 06:29:42 +00001218StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001219 if (Tok.is(tok::l_brace)) {
1220 unsigned short savedBraceCount = BraceCount;
1221 do {
1222 ConsumeAnyToken();
1223 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001224 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001225 // From the MS website: If used without braces, the __asm keyword means
1226 // that the rest of the line is an assembly-language statement.
1227 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001228 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001229 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001230 do {
1231 ConsumeAnyToken();
1232 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001233 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1234 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001235 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001236 }
Mike Stump6da5d752009-12-11 00:04:56 +00001237 Token t;
1238 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001239 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001240 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001241 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001242 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001243 ExprVector Constraints(Actions);
1244 ExprVector Exprs(Actions);
1245 ExprVector Clobbers(Actions);
Anders Carlsson9a020f92010-01-30 22:25:16 +00001246 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001247 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001248 AsmString.take(), move_arg(Clobbers),
Mike Stump90be58a2010-01-04 22:37:17 +00001249 Tok.getLocation(), true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001250}
1251
Chris Lattner0116c472006-08-15 06:03:28 +00001252/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001253/// asm-statement:
1254/// gnu-asm-statement
1255/// ms-asm-statement
1256///
1257/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001258/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1259///
1260/// [GNU] asm-argument:
1261/// asm-string-literal
1262/// asm-string-literal ':' asm-operands[opt]
1263/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1264/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1265/// ':' asm-clobbers
1266///
1267/// [GNU] asm-clobbers:
1268/// asm-string-literal
1269/// asm-clobbers ',' asm-string-literal
1270///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001271/// [MS] ms-asm-statement:
1272/// '__asm' assembly-instruction ';'[opt]
1273/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1274///
1275/// [MS] assembly-instruction-list:
1276/// assembly-instruction ';'[opt]
1277/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1278///
John McCalldadc5752010-08-24 06:29:42 +00001279StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001280 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001281 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001282
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001283 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001284 msAsm = true;
1285 return FuzzyParseMicrosoftAsmStatement();
1286 }
Chris Lattner0116c472006-08-15 06:03:28 +00001287 DeclSpec DS;
1288 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001289 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001290
Chris Lattner0116c472006-08-15 06:03:28 +00001291 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001292 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001293 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001294 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001295 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001296
Chris Lattner0116c472006-08-15 06:03:28 +00001297 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001298 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001299 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001300 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001301 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001302 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001303 }
Chris Lattner04132372006-10-16 06:12:55 +00001304 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001305
John McCalldadc5752010-08-24 06:29:42 +00001306 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001307 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001308 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001309
Anders Carlsson9a020f92010-01-30 22:25:16 +00001310 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001311 ExprVector Constraints(Actions);
1312 ExprVector Exprs(Actions);
1313 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001314
Anders Carlsson19fe1162008-02-05 23:03:50 +00001315 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001316 // We have a simple asm expression like 'asm("foo")'.
1317 SourceLocation RParenLoc = ConsumeParen();
1318 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1319 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1320 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001321 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001322 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001323 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001324
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001325 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001326 bool AteExtraColon = false;
1327 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1328 // In C++ mode, parse "::" like ": :".
1329 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001330 ConsumeToken();
1331
Chris Lattner15768502009-12-20 23:08:04 +00001332 if (!AteExtraColon &&
1333 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001334 return StmtError();
1335 }
Chris Lattner15768502009-12-20 23:08:04 +00001336
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001337 unsigned NumOutputs = Names.size();
1338
1339 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001340 if (AteExtraColon ||
1341 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1342 // In C++ mode, parse "::" like ": :".
1343 if (AteExtraColon)
1344 AteExtraColon = false;
1345 else {
1346 AteExtraColon = Tok.is(tok::coloncolon);
1347 ConsumeToken();
1348 }
1349
1350 if (!AteExtraColon &&
1351 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001352 return StmtError();
1353 }
1354
1355 assert(Names.size() == Constraints.size() &&
1356 Constraints.size() == Exprs.size() &&
1357 "Input operand size mismatch!");
1358
1359 unsigned NumInputs = Names.size() - NumOutputs;
1360
1361 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001362 if (AteExtraColon || Tok.is(tok::colon)) {
1363 if (!AteExtraColon)
1364 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001365
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001366 // Parse the asm-string list for clobbers if present.
1367 if (Tok.isNot(tok::r_paren)) {
1368 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001369 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001370
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001371 if (Clobber.isInvalid())
1372 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001373
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001374 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001375
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001376 if (Tok.isNot(tok::comma)) break;
1377 ConsumeToken();
1378 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001379 }
1380 }
1381
1382 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1383 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001384 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001385 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001386 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001387 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001388}
1389
1390/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001391/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001392///
1393/// [GNU] asm-operands:
1394/// asm-operand
1395/// asm-operands ',' asm-operand
1396///
1397/// [GNU] asm-operand:
1398/// asm-string-literal '(' expression ')'
1399/// '[' identifier ']' asm-string-literal '(' expression ')'
1400///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001401//
1402// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001403bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1404 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1405 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001406 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001407 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001408 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001409
1410 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001411 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001412 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001413 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001414
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001415 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001416 Diag(Tok, diag::err_expected_ident);
1417 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001418 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001419 }
Mike Stump11289f42009-09-09 15:08:12 +00001420
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001421 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001422 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001423
Anders Carlsson9a020f92010-01-30 22:25:16 +00001424 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001425 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001426 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001427 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001428
John McCalldadc5752010-08-24 06:29:42 +00001429 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001430 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001431 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001432 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001433 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001434 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001435
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001436 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001437 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001438 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001439 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001440 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001441
Chris Lattner0116c472006-08-15 06:03:28 +00001442 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001443 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001444 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001445 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001446 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001447 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001448 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001449 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001450 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001451 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001452 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001453 ConsumeToken();
1454 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001455
1456 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001457}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001458
John McCall48871652010-08-21 09:40:31 +00001459Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001460 assert(Tok.is(tok::l_brace));
1461 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001462
John McCallfaf5fb42010-08-26 23:41:50 +00001463 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1464 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001465
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001466 // Do not enter a scope for the brace, as the arguments are in the same scope
1467 // (the function body) as the body itself. Instead, just read the statement
1468 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001469 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001470
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001471 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001472 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001473 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001474 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001475
John McCallb268a282010-08-23 23:25:46 +00001476 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001477}
Sebastian Redlb219c902008-12-21 16:41:36 +00001478
Sebastian Redla7b98a72009-04-26 20:35:05 +00001479/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1480///
1481/// function-try-block:
1482/// 'try' ctor-initializer[opt] compound-statement handler-seq
1483///
John McCall48871652010-08-21 09:40:31 +00001484Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001485 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1486 SourceLocation TryLoc = ConsumeToken();
1487
John McCallfaf5fb42010-08-26 23:41:50 +00001488 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1489 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001490
1491 // Constructor initializer list?
1492 if (Tok.is(tok::colon))
1493 ParseConstructorInitializer(Decl);
1494
Sebastian Redld98ecd62009-04-26 21:08:36 +00001495 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001496 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001497 // If we failed to parse the try-catch, we just give the function an empty
1498 // compound statement as the body.
1499 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001500 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001501 MultiStmtArg(Actions), false);
1502
John McCallb268a282010-08-23 23:25:46 +00001503 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001504}
1505
Sebastian Redlb219c902008-12-21 16:41:36 +00001506/// ParseCXXTryBlock - Parse a C++ try-block.
1507///
1508/// try-block:
1509/// 'try' compound-statement handler-seq
1510///
John McCalldadc5752010-08-24 06:29:42 +00001511StmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001512 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001513
Sebastian Redlb219c902008-12-21 16:41:36 +00001514 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1515
1516 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001517 return ParseCXXTryBlockCommon(TryLoc);
1518}
1519
1520/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1521/// function-try-block.
1522///
1523/// try-block:
1524/// 'try' compound-statement handler-seq
1525///
1526/// function-try-block:
1527/// 'try' ctor-initializer[opt] compound-statement handler-seq
1528///
1529/// handler-seq:
1530/// handler handler-seq[opt]
1531///
John McCalldadc5752010-08-24 06:29:42 +00001532StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001533 if (Tok.isNot(tok::l_brace))
1534 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001535 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001536 StmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001537 if (TryBlock.isInvalid())
1538 return move(TryBlock);
1539
1540 StmtVector Handlers(Actions);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001541 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1542 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1543 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1544 << Attr.Range;
1545 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001546 if (Tok.isNot(tok::kw_catch))
1547 return StmtError(Diag(Tok, diag::err_expected_catch));
1548 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001549 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001550 if (!Handler.isInvalid())
1551 Handlers.push_back(Handler.release());
1552 }
1553 // Don't bother creating the full statement if we don't have any usable
1554 // handlers.
1555 if (Handlers.empty())
1556 return StmtError();
1557
John McCallb268a282010-08-23 23:25:46 +00001558 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001559}
1560
1561/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1562///
1563/// handler:
1564/// 'catch' '(' exception-declaration ')' compound-statement
1565///
1566/// exception-declaration:
1567/// type-specifier-seq declarator
1568/// type-specifier-seq abstract-declarator
1569/// type-specifier-seq
1570/// '...'
1571///
John McCalldadc5752010-08-24 06:29:42 +00001572StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001573 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1574
1575 SourceLocation CatchLoc = ConsumeToken();
1576
1577 SourceLocation LParenLoc = Tok.getLocation();
1578 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1579 return StmtError();
1580
1581 // C++ 3.3.2p3:
1582 // The name in a catch exception-declaration is local to the handler and
1583 // shall not be redeclared in the outermost block of the handler.
1584 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1585
1586 // exception-declaration is equivalent to '...' or a parameter-declaration
1587 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001588 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001589 if (Tok.isNot(tok::ellipsis)) {
1590 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001591 if (ParseCXXTypeSpecifierSeq(DS))
1592 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001593 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1594 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001595 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001596 } else
1597 ConsumeToken();
1598
1599 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1600 return StmtError();
1601
1602 if (Tok.isNot(tok::l_brace))
1603 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1604
Alexis Hunt96d5c762009-11-21 08:43:09 +00001605 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001606 StmtResult Block(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001607 if (Block.isInvalid())
1608 return move(Block);
1609
John McCallb268a282010-08-23 23:25:46 +00001610 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001611}