blob: e3c15680c231f253fc1d1c4316c0881d753065c2 [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);
Chris Lattner0f203a72007-05-28 01:45:28 +0000150 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000151 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl042ad952008-12-11 19:30:53 +0000152
Chris Lattner9075bd72006-08-10 04:59:57 +0000153 case tok::kw_if: // C99 6.8.4.1: if-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000154 return ParseIfStatement(AttrList);
Chris Lattner9075bd72006-08-10 04:59:57 +0000155 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000156 return ParseSwitchStatement(AttrList);
Sebastian Redl042ad952008-12-11 19:30:53 +0000157
Chris Lattner9075bd72006-08-10 04:59:57 +0000158 case tok::kw_while: // C99 6.8.5.1: while-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000159 return ParseWhileStatement(AttrList);
Chris Lattner9075bd72006-08-10 04:59:57 +0000160 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000161 Res = ParseDoStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000162 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000163 break;
164 case tok::kw_for: // C99 6.8.5.3: for-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000165 return ParseForStatement(AttrList);
Chris Lattner503fadc2006-08-10 05:45:44 +0000166
167 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000168 Res = ParseGotoStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000169 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000170 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000171 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000172 Res = ParseContinueStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000173 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000174 break;
175 case tok::kw_break: // C99 6.8.6.3: break-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000176 Res = ParseBreakStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000177 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000178 break;
179 case tok::kw_return: // C99 6.8.6.4: return-statement
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000180 Res = ParseReturnStatement(AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000181 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000182 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000183
Sebastian Redlb219c902008-12-21 16:41:36 +0000184 case tok::kw_asm: {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000185 if (Attr.HasAttr)
186 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
187 << Attr.Range;
Steve Naroffb2c80c72008-02-07 03:50:06 +0000188 bool msAsm = false;
189 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000190 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000191 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000192 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000193 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000194 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000195
Sebastian Redlb219c902008-12-21 16:41:36 +0000196 case tok::kw_try: // C++ 15: try-block
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000197 return ParseCXXTryBlock(AttrList);
Sebastian Redlb219c902008-12-21 16:41:36 +0000198 }
199
Chris Lattner503fadc2006-08-10 05:45:44 +0000200 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000201 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000202 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000203 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000204 // If the result was valid, then we do want to diagnose this. Use
205 // ExpectAndConsume to emit the diagnostic, even though we know it won't
206 // succeed.
207 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000208 // Skip until we see a } or ;, but don't eat it.
209 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000210 }
Mike Stump11289f42009-09-09 15:08:12 +0000211
Sebastian Redl042ad952008-12-11 19:30:53 +0000212 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000213}
214
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000215/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000216///
217/// labeled-statement:
218/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000219/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000220///
John McCalldadc5752010-08-24 06:29:42 +0000221StmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000222 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
223 "Not an identifier!");
224
225 Token IdentTok = Tok; // Save the whole token.
226 ConsumeToken(); // eat the identifier.
227
228 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000229
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000230 // identifier ':' statement
231 SourceLocation ColonLoc = ConsumeToken();
232
233 // Read label attributes, if present.
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000234 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000235 Attr = addAttributeLists(Attr, ParseGNUAttributes());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000236
John McCalldadc5752010-08-24 06:29:42 +0000237 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000238
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000239 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000240 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000241 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000242
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000243 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
244 IdentTok.getIdentifierInfo(),
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000245 ColonLoc, SubStmt.get(), Attr);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000246}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000247
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000248/// ParseCaseStatement
249/// labeled-statement:
250/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000251/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000252///
John McCalldadc5752010-08-24 06:29:42 +0000253StmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000254 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000255 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattner34a22092009-03-04 04:23:07 +0000257 // It is very very common for code to contain many case statements recursively
258 // nested, as in (but usually without indentation):
259 // case 1:
260 // case 2:
261 // case 3:
262 // case 4:
263 // case 5: etc.
264 //
265 // Parsing this naively works, but is both inefficient and can cause us to run
266 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000267 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000268 // but all the grossness is constrained to ParseCaseStatement (and some
269 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000270
Chris Lattner34a22092009-03-04 04:23:07 +0000271 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
272 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000273 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattner34a22092009-03-04 04:23:07 +0000275 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
276 // gets updated each time a new case is parsed, and whose body is unset so
277 // far. When parsing 'case 4', this is the 'case 3' node.
278 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000279
Chris Lattner34a22092009-03-04 04:23:07 +0000280 // While we have case statements, eat and stack them.
281 do {
282 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000283
Douglas Gregord328d572009-09-21 18:10:23 +0000284 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000285 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000286 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000287 }
288
Chris Lattner125c0ee2009-12-10 00:38:54 +0000289 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
290 /// Disable this form of error recovery while we're parsing the case
291 /// expression.
292 ColonProtectionRAIIObject ColonProtection(*this);
293
John McCalldadc5752010-08-24 06:29:42 +0000294 ExprResult LHS(ParseConstantExpression());
Chris Lattner34a22092009-03-04 04:23:07 +0000295 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000296 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000297 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000298 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000299
Chris Lattner34a22092009-03-04 04:23:07 +0000300 // GNU case range extension.
301 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000302 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000303 if (Tok.is(tok::ellipsis)) {
304 Diag(Tok, diag::ext_gnu_case_range);
305 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000306
Chris Lattner34a22092009-03-04 04:23:07 +0000307 RHS = ParseConstantExpression();
308 if (RHS.isInvalid()) {
309 SkipUntil(tok::colon);
310 return StmtError();
311 }
312 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000313
314 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000315
Chris Lattner34a22092009-03-04 04:23:07 +0000316 if (Tok.isNot(tok::colon)) {
317 Diag(Tok, diag::err_expected_colon_after) << "'case'";
318 SkipUntil(tok::colon);
319 return StmtError();
320 }
321
322 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000323
John McCalldadc5752010-08-24 06:29:42 +0000324 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000325 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
326 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattner34a22092009-03-04 04:23:07 +0000328 // If we had a sema error parsing this case, then just ignore it and
329 // continue parsing the sub-stmt.
330 if (Case.isInvalid()) {
331 if (TopLevelCase.isInvalid()) // No parsed case stmts.
332 return ParseStatement();
333 // Otherwise, just don't add it as a nested case.
334 } else {
335 // If this is the first case statement we parsed, it becomes TopLevelCase.
336 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000337 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000338 if (TopLevelCase.isInvalid())
339 TopLevelCase = move(Case);
340 else
John McCallb268a282010-08-23 23:25:46 +0000341 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000342 DeepestParsedCaseStmt = NextDeepest;
343 }
Mike Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattner34a22092009-03-04 04:23:07 +0000345 // Handle all case statements.
346 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000347
Chris Lattner34a22092009-03-04 04:23:07 +0000348 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattner34a22092009-03-04 04:23:07 +0000350 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000351 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner34a22092009-03-04 04:23:07 +0000353 if (Tok.isNot(tok::r_brace)) {
354 SubStmt = ParseStatement();
355 } else {
356 // Nicely diagnose the common error "switch (X) { case 4: }", which is
357 // not valid.
358 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000359 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000360 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000361 }
Mike Stump11289f42009-09-09 15:08:12 +0000362
Chris Lattner34a22092009-03-04 04:23:07 +0000363 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000364 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000365 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattner34a22092009-03-04 04:23:07 +0000367 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000368 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000369
Chris Lattner34a22092009-03-04 04:23:07 +0000370 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000371 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000372}
373
374/// ParseDefaultStatement
375/// labeled-statement:
376/// 'default' ':' statement
377/// Note that this does not parse the 'statement' at the end.
378///
John McCalldadc5752010-08-24 06:29:42 +0000379StmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000380 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000381
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000382 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000383 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000384
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000385 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000386 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000387 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000388 return StmtError();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000389 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000390
Chris Lattneraf635312006-10-16 06:06:51 +0000391 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000392
Chris Lattner30f910e2006-10-16 05:52:41 +0000393 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000394 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000395 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000396 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000397 }
398
John McCalldadc5752010-08-24 06:29:42 +0000399 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000400 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000401 return StmtError();
402
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000403 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000404 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000405}
406
407
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000408/// ParseCompoundStatement - Parse a "{}" block.
409///
410/// compound-statement: [C99 6.8.2]
411/// { block-item-list[opt] }
412/// [GNU] { label-declarations block-item-list } [TODO]
413///
414/// block-item-list:
415/// block-item
416/// block-item-list block-item
417///
418/// block-item:
419/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000420/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000421/// statement
422/// [OMP] openmp-directive [TODO]
423///
424/// [GNU] label-declarations:
425/// [GNU] label-declaration
426/// [GNU] label-declarations label-declaration
427///
428/// [GNU] label-declaration:
429/// [GNU] '__label__' identifier-list ';'
430///
431/// [OMP] openmp-directive: [TODO]
432/// [OMP] barrier-directive
433/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000434///
John McCalldadc5752010-08-24 06:29:42 +0000435StmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000436 bool isStmtExpr) {
437 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000438
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000439 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000440
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000441 // Enter a scope to hold everything within the compound stmt. Compound
442 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000443 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000444
445 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000446 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000447}
448
449
450/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000451/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000452/// consume the '}' at the end of the block. It does not manipulate the scope
453/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000454StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000455 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000456 Tok.getLocation(),
457 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000458 InMessageExpressionRAIIObject InMessage(*this, false);
459
Chris Lattnerf2978802007-01-21 06:52:16 +0000460 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
461
Chris Lattner010015a2007-05-28 07:23:54 +0000462 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000463 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000464
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000465 StmtVector Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000466 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCalldadc5752010-08-24 06:29:42 +0000467 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000468 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000469 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000470 } else {
471 // __extension__ can start declarations and it can also be a unary
472 // operator for expressions. Consume multiple __extension__ markers here
473 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000474 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000475 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000476 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000477 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000478
Alexis Hunt96d5c762009-11-21 08:43:09 +0000479 CXX0XAttributeList Attr;
480 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
481 Attr = ParseCXX0XAttributes();
482
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000483 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000484 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000485 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000486 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000487 ExtensionRAIIObject O(Diags);
488
Chris Lattner49836b42009-04-02 04:16:50 +0000489 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000490 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
491 Declarator::BlockContext, DeclEnd,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000492 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000493 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000494 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000495 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000496 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000497
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000498 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000499 SkipUntil(tok::semi);
500 continue;
501 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000502
Alexis Hunt96d5c762009-11-21 08:43:09 +0000503 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000504 // Eat the semicolon at the end of stmt and convert the expr into a
505 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000506 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000507 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000508 }
509 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000510
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000511 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000512 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000513 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000514
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000515 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000516 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000517 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000518 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000519 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000520 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000521
Chris Lattner04132372006-10-16 06:12:55 +0000522 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000523 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000524 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000525}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000526
Chris Lattnerc0081db2008-12-12 06:31:07 +0000527/// ParseParenExprOrCondition:
528/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000529/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000530///
531/// This function parses and performs error recovery on the specified condition
532/// or expression (depending on whether we're in C++ or C mode). This function
533/// goes out of its way to recover well. It returns true if there was a parser
534/// error (the right paren couldn't be found), which indicates that the caller
535/// should try to recover harder. It returns false if the condition is
536/// successfully parsed. Note that a successful parse can still have semantic
537/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000538bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000539 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000540 SourceLocation Loc,
Argyrios Kyrtzidis90ee2a42010-11-19 20:54:25 +0000541 bool ConvertToBoolean,
542 bool *MacroExpandedAfterRParen) {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000543 bool ParseError = false;
544
Chris Lattnerc0081db2008-12-12 06:31:07 +0000545 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000546 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +0000547 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
548 ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000549 else {
550 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000551 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000552
553 // If required, convert to a boolean value.
554 if (!ExprResult.isInvalid() && ConvertToBoolean)
555 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000556 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000557 }
Mike Stump11289f42009-09-09 15:08:12 +0000558
Chris Lattnerc0081db2008-12-12 06:31:07 +0000559 // If the parser was confused by the condition and we don't have a ')', try to
560 // recover by skipping ahead to a semi and bailing out. If condexp is
561 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000562 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000563 SkipUntil(tok::semi);
564 // Skipping may have stopped if it found the containing ')'. If so, we can
565 // continue parsing the if statement.
566 if (Tok.isNot(tok::r_paren))
567 return true;
568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Chris Lattnerc0081db2008-12-12 06:31:07 +0000570 // Otherwise the condition is valid or the rparen is present.
Argyrios Kyrtzidis90ee2a42010-11-19 20:54:25 +0000571
572 // Catch a macro expansion after ')'. This is used to know that there is a
573 // macro for 'if' body and not warn for empty body if the macro is empty.
574 PPMacroExpansionTrap MacroExpansionTrap(PP);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000575 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis90ee2a42010-11-19 20:54:25 +0000576 if (MacroExpandedAfterRParen)
577 *MacroExpandedAfterRParen = MacroExpansionTrap.hasMacroExpansionOccured();
578
Chris Lattnerc0081db2008-12-12 06:31:07 +0000579 return false;
580}
581
582
Chris Lattnerc951dae2006-08-10 04:23:57 +0000583/// ParseIfStatement
584/// if-statement: [C99 6.8.4.1]
585/// 'if' '(' expression ')' statement
586/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000587/// [C++] 'if' '(' condition ')' statement
588/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000589///
John McCalldadc5752010-08-24 06:29:42 +0000590StmtResult Parser::ParseIfStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000591 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000592
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000593 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000594 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000595
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000596 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000597 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000598 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000599 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000600 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000601
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000602 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
603
Chris Lattner2dd1b722007-08-26 23:08:06 +0000604 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
605 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000606 //
607 // C++ 6.4p3:
608 // A name introduced by a declaration in a condition is in scope from its
609 // point of declaration until the end of the substatements controlled by the
610 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000611 // C++ 3.3.2p4:
612 // Names declared in the for-init-statement, and in the condition of if,
613 // while, for, and switch statements are local to the if, while, for, or
614 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000615 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000616 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000617
Chris Lattnerc951dae2006-08-10 04:23:57 +0000618 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000619 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000620 Decl *CondVar = 0;
Argyrios Kyrtzidis90ee2a42010-11-19 20:54:25 +0000621 bool MacroExpandedInThenStmt;
622 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true,
623 &MacroExpandedInThenStmt))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000624 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000625
John McCallb268a282010-08-23 23:25:46 +0000626 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000627
Chris Lattner8fb26252007-08-22 05:28:50 +0000628 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000629 // there is no compound stmt. C90 does not have this clause. We only do this
630 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000631 //
632 // C++ 6.4p1:
633 // The substatement in a selection-statement (each substatement, in the else
634 // form of the if statement) implicitly defines a local scope.
635 //
636 // For C++ we create a scope for the condition and a new scope for
637 // substatements because:
638 // -When the 'then' scope exits, we want the condition declaration to still be
639 // active for the 'else' scope too.
640 // -Sema will detect name clashes by considering declarations of a
641 // 'ControlScope' as part of its direct subscope.
642 // -If we wanted the condition and substatement to be in the same scope, we
643 // would have to notify ParseStatement not to create a new scope. It's
644 // simpler to let it create a new scope.
645 //
Mike Stump11289f42009-09-09 15:08:12 +0000646 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000647 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000648
Chris Lattner5c5808a2007-10-29 05:08:52 +0000649 // Read the 'then' stmt.
650 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000651 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000652
Chris Lattner37e54f42007-08-22 05:16:28 +0000653 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000654 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000655
Chris Lattnerc951dae2006-08-10 04:23:57 +0000656 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000657 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000658 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000659 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000660
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000661 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000662 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000663 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000664
Chris Lattner8fb26252007-08-22 05:28:50 +0000665 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000666 // there is no compound stmt. C90 does not have this clause. We only do
667 // this if the body isn't a compound statement to avoid push/pop in common
668 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000669 //
670 // C++ 6.4p1:
671 // The substatement in a selection-statement (each substatement, in the else
672 // form of the if statement) implicitly defines a local scope.
673 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000674 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000675 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000676
Chris Lattner30f910e2006-10-16 05:52:41 +0000677 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000678
Chris Lattner37e54f42007-08-22 05:16:28 +0000679 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000680 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000681 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000682
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000683 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000685 // If the condition was invalid, discard the if statement. We could recover
686 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000687 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000688 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000689
Chris Lattner5c5808a2007-10-29 05:08:52 +0000690 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000691 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000692 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000693 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
694 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
695 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000696 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000697 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000698 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000699
Chris Lattner5c5808a2007-10-29 05:08:52 +0000700 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000701 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000702 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000703 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000704 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000705
John McCallb268a282010-08-23 23:25:46 +0000706 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis90ee2a42010-11-19 20:54:25 +0000707 MacroExpandedInThenStmt, ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000708}
709
Chris Lattner9075bd72006-08-10 04:59:57 +0000710/// ParseSwitchStatement
711/// switch-statement:
712/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000713/// [C++] 'switch' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000714StmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000715 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000716
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000717 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000718 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000719
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000720 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000721 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000722 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000723 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000724 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000725
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000726 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
727
Chris Lattner2dd1b722007-08-26 23:08:06 +0000728 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
729 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000730 //
731 // C++ 6.4p3:
732 // A name introduced by a declaration in a condition is in scope from its
733 // point of declaration until the end of the substatements controlled by the
734 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000735 // C++ 3.3.2p4:
736 // Names declared in the for-init-statement, and in the condition of if,
737 // while, for, and switch statements are local to the if, while, for, or
738 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000739 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000740 unsigned ScopeFlags = Scope::BreakScope;
741 if (C99orCXX)
742 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000743 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000744
Chris Lattner9075bd72006-08-10 04:59:57 +0000745 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000746 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000747 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000748 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000749 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000750
John McCalldadc5752010-08-24 06:29:42 +0000751 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000752 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000753
Douglas Gregore60e41a2010-05-06 17:25:47 +0000754 if (Switch.isInvalid()) {
755 // Skip the switch body.
756 // FIXME: This is not optimal recovery, but parsing the body is more
757 // dangerous due to the presence of case and default statements, which
758 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000759 if (Tok.is(tok::l_brace)) {
760 ConsumeBrace();
761 SkipUntil(tok::r_brace, false, false);
762 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000763 SkipUntil(tok::semi);
764 return move(Switch);
765 }
766
Chris Lattner8fb26252007-08-22 05:28:50 +0000767 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000768 // there is no compound stmt. C90 does not have this clause. We only do this
769 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000770 //
771 // C++ 6.4p1:
772 // The substatement in a selection-statement (each substatement, in the else
773 // form of the if statement) implicitly defines a local scope.
774 //
775 // See comments in ParseIfStatement for why we create a scope for the
776 // condition and a new scope for substatement in C++.
777 //
Mike Stump11289f42009-09-09 15:08:12 +0000778 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000779 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000780
Chris Lattner9075bd72006-08-10 04:59:57 +0000781 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000782 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000783
Chris Lattner8fd2d012010-01-24 01:50:29 +0000784 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000785 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000786 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000787
Chris Lattner8fd2d012010-01-24 01:50:29 +0000788 if (Body.isInvalid())
789 // FIXME: Remove the case statement list from the Switch statement.
790 Body = Actions.ActOnNullStmt(Tok.getLocation());
791
John McCallb268a282010-08-23 23:25:46 +0000792 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000793}
794
795/// ParseWhileStatement
796/// while-statement: [C99 6.8.5.1]
797/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000798/// [C++] 'while' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000799StmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000800 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000801
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000802 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000803 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000804 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000805
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000806 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000807 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000808 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000809 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000810 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000811
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000812 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
813
Chris Lattner2dd1b722007-08-26 23:08:06 +0000814 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
815 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000816 //
817 // C++ 6.4p3:
818 // A name introduced by a declaration in a condition is in scope from its
819 // point of declaration until the end of the substatements controlled by the
820 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000821 // C++ 3.3.2p4:
822 // Names declared in the for-init-statement, and in the condition of if,
823 // while, for, and switch statements are local to the if, while, for, or
824 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000825 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000826 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000827 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000828 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
829 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000830 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000831 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
832 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000833
Chris Lattner9075bd72006-08-10 04:59:57 +0000834 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000835 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000836 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000837 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000838 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000839
John McCallb268a282010-08-23 23:25:46 +0000840 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner8fb26252007-08-22 05:28:50 +0000842 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000843 // there is no compound stmt. C90 does not have this clause. We only do this
844 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000845 //
846 // C++ 6.5p2:
847 // The substatement in an iteration-statement implicitly defines a local scope
848 // which is entered and exited each time through the loop.
849 //
850 // See comments in ParseIfStatement for why we create a scope for the
851 // condition and a new scope for substatement in C++.
852 //
Mike Stump11289f42009-09-09 15:08:12 +0000853 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000854 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000855
Chris Lattner9075bd72006-08-10 04:59:57 +0000856 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000857 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000858
Chris Lattner8fb26252007-08-22 05:28:50 +0000859 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000860 InnerScope.Exit();
861 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000862
John McCall48871652010-08-21 09:40:31 +0000863 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000864 return StmtError();
865
John McCallb268a282010-08-23 23:25:46 +0000866 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000867}
868
869/// ParseDoStatement
870/// do-statement: [C99 6.8.5.2]
871/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000872/// Note: this lets the caller parse the end ';'.
John McCalldadc5752010-08-24 06:29:42 +0000873StmtResult Parser::ParseDoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000874 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000875
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000876 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000877 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000878
Chris Lattner2dd1b722007-08-26 23:08:06 +0000879 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
880 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000881 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000882 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000883 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000884 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000885 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000886
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000887 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000888
Chris Lattner8fb26252007-08-22 05:28:50 +0000889 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000890 // there is no compound stmt. C90 does not have this clause. We only do this
891 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000892 //
893 // C++ 6.5p2:
894 // The substatement in an iteration-statement implicitly defines a local scope
895 // which is entered and exited each time through the loop.
896 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000897 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000898 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000899 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000900
Chris Lattner9075bd72006-08-10 04:59:57 +0000901 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000902 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000903
Chris Lattner8fb26252007-08-22 05:28:50 +0000904 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000905 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000906
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000907 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000908 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000909 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000910 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000911 SkipUntil(tok::semi, false, true);
912 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000913 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000914 }
Chris Lattneraf635312006-10-16 06:06:51 +0000915 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000916
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000917 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000918 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000919 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000920 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000921 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000922
Chris Lattner10da53c2008-12-12 06:35:28 +0000923 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000924 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000925 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +0000926 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000927 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000928
Sebastian Redlb62406f2008-12-11 19:48:14 +0000929 if (Cond.isInvalid() || Body.isInvalid())
930 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000931
John McCallb268a282010-08-23 23:25:46 +0000932 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
933 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000934}
935
936/// ParseForStatement
937/// for-statement: [C99 6.8.5.3]
938/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
939/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000940/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
941/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000942/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
943/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000944///
945/// [C++] for-init-statement:
946/// [C++] expression-statement
947/// [C++] simple-declaration
948///
John McCalldadc5752010-08-24 06:29:42 +0000949StmtResult Parser::ParseForStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000950 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000951
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000952 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000953 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000954
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000955 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000956 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000957 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000958 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000959 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000960
Chris Lattner934074c2009-04-22 00:54:41 +0000961 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000962
Chris Lattner2dd1b722007-08-26 23:08:06 +0000963 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
964 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000965 //
966 // C++ 6.4p3:
967 // A name introduced by a declaration in a condition is in scope from its
968 // point of declaration until the end of the substatements controlled by the
969 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000970 // C++ 3.3.2p4:
971 // Names declared in the for-init-statement, and in the condition of if,
972 // while, for, and switch statements are local to the if, while, for, or
973 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000974 // C++ 6.5.3p1:
975 // Names declared in the for-init-statement are in the same declarative-region
976 // as those declared in the condition.
977 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000978 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000979 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000980 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
981 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000982 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000983 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
984
985 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000986
Chris Lattner04132372006-10-16 06:12:55 +0000987 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000988 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000989
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000990 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +0000991 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000992 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000993 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +0000994 ExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000995 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +0000996 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000997
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000998 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000999 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001000 C99orCXXorObjC? Sema::PCC_ForInit
1001 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001002 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001003 }
1004
Chris Lattner9075bd72006-08-10 04:59:57 +00001005 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001006 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001007 // no first part, eat the ';'.
1008 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001009 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001010 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001011 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001012 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001013
Alexis Hunt96d5c762009-11-21 08:43:09 +00001014 AttributeList *AttrList = 0;
1015 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
1016 AttrList = ParseCXX0XAttributes().AttrList;
1017
Chris Lattner49836b42009-04-02 04:16:50 +00001018 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001019 StmtVector Stmts(Actions);
1020 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
1021 DeclEnd, AttrList, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001022 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner32dc41c2009-03-29 17:27:48 +00001024 if (Tok.is(tok::semi)) { // for (int x = 4;
1025 ConsumeToken();
1026 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001027 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001028 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001029 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001030
1031 if (Tok.is(tok::code_completion)) {
1032 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1033 ConsumeCodeCompletionToken();
1034 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001035 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001036 } else {
1037 Diag(Tok, diag::err_expected_semi_for);
1038 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001039 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001040 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001041 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001042
Chris Lattnercd68f642007-06-27 01:06:29 +00001043 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001044 if (!Value.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001045 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001046
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001047 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001048 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001049 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001050 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001051
1052 if (Tok.is(tok::code_completion)) {
1053 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1054 ConsumeCodeCompletionToken();
1055 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001056 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001057 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001058 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001059 SkipUntil(tok::semi);
1060 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001061 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001062 if (!ForEach) {
John McCallb268a282010-08-23 23:25:46 +00001063 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001064 // Parse the second part of the for specifier.
1065 if (Tok.is(tok::semi)) { // for (...;;
1066 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001067 } else {
John McCalldadc5752010-08-24 06:29:42 +00001068 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001069 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001070 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1071 else {
1072 Second = ParseExpression();
1073 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001074 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001075 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001076 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001077 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001078 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001079 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001080
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001081 if (Tok.is(tok::semi)) {
1082 ConsumeToken();
1083 } else {
John McCall48871652010-08-21 09:40:31 +00001084 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001085 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001086 SkipUntil(tok::semi);
1087 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001088
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001089 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001090 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001091 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001092 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001093 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001094 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001095 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001096 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001097
Chris Lattner8fb26252007-08-22 05:28:50 +00001098 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001099 // there is no compound stmt. C90 does not have this clause. We only do this
1100 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001101 //
1102 // C++ 6.5p2:
1103 // The substatement in an iteration-statement implicitly defines a local scope
1104 // which is entered and exited each time through the loop.
1105 //
1106 // See comments in ParseIfStatement for why we create a scope for
1107 // for-init-statement/condition and a new scope for substatement in C++.
1108 //
Mike Stump11289f42009-09-09 15:08:12 +00001109 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001110 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001111
Chris Lattner9075bd72006-08-10 04:59:57 +00001112 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001113 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001114
Chris Lattner8fb26252007-08-22 05:28:50 +00001115 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001116 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001117
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001118 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001119 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001120
1121 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001122 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001123
1124 if (!ForEach)
John McCallb268a282010-08-23 23:25:46 +00001125 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1126 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001127
Douglas Gregore60e41a2010-05-06 17:25:47 +00001128 // FIXME: It isn't clear how to communicate the late destruction of
1129 // C++ temporaries used to create the collection.
John McCallb268a282010-08-23 23:25:46 +00001130 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1131 Collection.take(), RParenLoc,
1132 Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001133}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001134
Chris Lattner503fadc2006-08-10 05:45:44 +00001135/// ParseGotoStatement
1136/// jump-statement:
1137/// 'goto' identifier ';'
1138/// [GNU] 'goto' '*' expression ';'
1139///
1140/// Note: this lets the caller parse the end ';'.
1141///
John McCalldadc5752010-08-24 06:29:42 +00001142StmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001143 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001144
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001145 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001146 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001147
John McCalldadc5752010-08-24 06:29:42 +00001148 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001149 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001150 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001151 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001152 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001153 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001154 // GNU indirect goto extension.
1155 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001156 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001157 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001158 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001159 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001160 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001161 }
John McCallb268a282010-08-23 23:25:46 +00001162 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001163 } else {
1164 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001165 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001166 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001167
Sebastian Redlb62406f2008-12-11 19:48:14 +00001168 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001169}
1170
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001171/// ParseContinueStatement
1172/// jump-statement:
1173/// 'continue' ';'
1174///
1175/// Note: this lets the caller parse the end ';'.
1176///
John McCalldadc5752010-08-24 06:29:42 +00001177StmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001178 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001179
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001180 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001181 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001182}
1183
1184/// ParseBreakStatement
1185/// jump-statement:
1186/// 'break' ';'
1187///
1188/// Note: this lets the caller parse the end ';'.
1189///
John McCalldadc5752010-08-24 06:29:42 +00001190StmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001191 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001192
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001193 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001194 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001195}
1196
Chris Lattner503fadc2006-08-10 05:45:44 +00001197/// ParseReturnStatement
1198/// jump-statement:
1199/// 'return' expression[opt] ';'
John McCalldadc5752010-08-24 06:29:42 +00001200StmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001201 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001202
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001203 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001204 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001205
John McCalldadc5752010-08-24 06:29:42 +00001206 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001207 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001208 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001209 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001210 ConsumeCodeCompletionToken();
1211 SkipUntil(tok::semi, false, true);
1212 return StmtError();
1213 }
1214
Chris Lattner30f910e2006-10-16 05:52:41 +00001215 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001216 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001217 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001218 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001219 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001220 }
John McCallb268a282010-08-23 23:25:46 +00001221 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001222}
Chris Lattner0116c472006-08-15 06:03:28 +00001223
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001224/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1225/// routine is called to skip/ignore tokens that comprise the MS asm statement.
John McCalldadc5752010-08-24 06:29:42 +00001226StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001227 if (Tok.is(tok::l_brace)) {
1228 unsigned short savedBraceCount = BraceCount;
1229 do {
1230 ConsumeAnyToken();
1231 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001232 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001233 // From the MS website: If used without braces, the __asm keyword means
1234 // that the rest of the line is an assembly-language statement.
1235 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001236 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001237 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001238 do {
1239 ConsumeAnyToken();
1240 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001241 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1242 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001243 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001244 }
Mike Stump6da5d752009-12-11 00:04:56 +00001245 Token t;
1246 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001247 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001248 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001249 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001250 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001251 ExprVector Constraints(Actions);
1252 ExprVector Exprs(Actions);
1253 ExprVector Clobbers(Actions);
Anders Carlsson9a020f92010-01-30 22:25:16 +00001254 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001255 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001256 AsmString.take(), move_arg(Clobbers),
Mike Stump90be58a2010-01-04 22:37:17 +00001257 Tok.getLocation(), true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001258}
1259
Chris Lattner0116c472006-08-15 06:03:28 +00001260/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001261/// asm-statement:
1262/// gnu-asm-statement
1263/// ms-asm-statement
1264///
1265/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001266/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1267///
1268/// [GNU] asm-argument:
1269/// asm-string-literal
1270/// asm-string-literal ':' asm-operands[opt]
1271/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1272/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1273/// ':' asm-clobbers
1274///
1275/// [GNU] asm-clobbers:
1276/// asm-string-literal
1277/// asm-clobbers ',' asm-string-literal
1278///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001279/// [MS] ms-asm-statement:
1280/// '__asm' assembly-instruction ';'[opt]
1281/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1282///
1283/// [MS] assembly-instruction-list:
1284/// assembly-instruction ';'[opt]
1285/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1286///
John McCalldadc5752010-08-24 06:29:42 +00001287StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001288 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001289 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001290
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001291 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001292 msAsm = true;
1293 return FuzzyParseMicrosoftAsmStatement();
1294 }
Chris Lattner0116c472006-08-15 06:03:28 +00001295 DeclSpec DS;
1296 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001297 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001298
Chris Lattner0116c472006-08-15 06:03:28 +00001299 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001300 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001301 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001302 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001303 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001304
Chris Lattner0116c472006-08-15 06:03:28 +00001305 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001306 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001307 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001308 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001309 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001310 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001311 }
Chris Lattner04132372006-10-16 06:12:55 +00001312 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001313
John McCalldadc5752010-08-24 06:29:42 +00001314 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001315 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001316 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001317
Anders Carlsson9a020f92010-01-30 22:25:16 +00001318 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001319 ExprVector Constraints(Actions);
1320 ExprVector Exprs(Actions);
1321 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001322
Anders Carlsson19fe1162008-02-05 23:03:50 +00001323 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001324 // We have a simple asm expression like 'asm("foo")'.
1325 SourceLocation RParenLoc = ConsumeParen();
1326 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1327 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1328 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001329 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001330 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001331 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001332
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001333 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001334 bool AteExtraColon = false;
1335 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1336 // In C++ mode, parse "::" like ": :".
1337 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001338 ConsumeToken();
1339
Chris Lattner15768502009-12-20 23:08:04 +00001340 if (!AteExtraColon &&
1341 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001342 return StmtError();
1343 }
Chris Lattner15768502009-12-20 23:08:04 +00001344
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001345 unsigned NumOutputs = Names.size();
1346
1347 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001348 if (AteExtraColon ||
1349 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1350 // In C++ mode, parse "::" like ": :".
1351 if (AteExtraColon)
1352 AteExtraColon = false;
1353 else {
1354 AteExtraColon = Tok.is(tok::coloncolon);
1355 ConsumeToken();
1356 }
1357
1358 if (!AteExtraColon &&
1359 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001360 return StmtError();
1361 }
1362
1363 assert(Names.size() == Constraints.size() &&
1364 Constraints.size() == Exprs.size() &&
1365 "Input operand size mismatch!");
1366
1367 unsigned NumInputs = Names.size() - NumOutputs;
1368
1369 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001370 if (AteExtraColon || Tok.is(tok::colon)) {
1371 if (!AteExtraColon)
1372 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001373
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001374 // Parse the asm-string list for clobbers if present.
1375 if (Tok.isNot(tok::r_paren)) {
1376 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001377 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001378
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001379 if (Clobber.isInvalid())
1380 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001381
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001382 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001383
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001384 if (Tok.isNot(tok::comma)) break;
1385 ConsumeToken();
1386 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001387 }
1388 }
1389
1390 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1391 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001392 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001393 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001394 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001395 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001396}
1397
1398/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001399/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001400///
1401/// [GNU] asm-operands:
1402/// asm-operand
1403/// asm-operands ',' asm-operand
1404///
1405/// [GNU] asm-operand:
1406/// asm-string-literal '(' expression ')'
1407/// '[' identifier ']' asm-string-literal '(' expression ')'
1408///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001409//
1410// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001411bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1412 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1413 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001414 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001415 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001416 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001417
1418 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001419 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001420 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001421 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001422
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001423 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001424 Diag(Tok, diag::err_expected_ident);
1425 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001426 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001427 }
Mike Stump11289f42009-09-09 15:08:12 +00001428
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001429 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001430 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001431
Anders Carlsson9a020f92010-01-30 22:25:16 +00001432 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001433 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001434 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001435 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001436
John McCalldadc5752010-08-24 06:29:42 +00001437 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001438 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001439 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001440 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001441 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001442 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001443
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001444 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001445 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001446 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001447 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001448 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001449
Chris Lattner0116c472006-08-15 06:03:28 +00001450 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001451 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001452 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001453 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001454 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001455 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001456 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001457 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001458 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001459 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001460 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001461 ConsumeToken();
1462 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001463
1464 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001465}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001466
John McCall48871652010-08-21 09:40:31 +00001467Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001468 assert(Tok.is(tok::l_brace));
1469 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001470
John McCallfaf5fb42010-08-26 23:41:50 +00001471 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1472 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001473
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001474 // Do not enter a scope for the brace, as the arguments are in the same scope
1475 // (the function body) as the body itself. Instead, just read the statement
1476 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001477 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001478
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001479 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001480 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001481 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001482 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001483
John McCallb268a282010-08-23 23:25:46 +00001484 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001485}
Sebastian Redlb219c902008-12-21 16:41:36 +00001486
Sebastian Redla7b98a72009-04-26 20:35:05 +00001487/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1488///
1489/// function-try-block:
1490/// 'try' ctor-initializer[opt] compound-statement handler-seq
1491///
John McCall48871652010-08-21 09:40:31 +00001492Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001493 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1494 SourceLocation TryLoc = ConsumeToken();
1495
John McCallfaf5fb42010-08-26 23:41:50 +00001496 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1497 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001498
1499 // Constructor initializer list?
1500 if (Tok.is(tok::colon))
1501 ParseConstructorInitializer(Decl);
1502
Sebastian Redld98ecd62009-04-26 21:08:36 +00001503 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001504 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001505 // If we failed to parse the try-catch, we just give the function an empty
1506 // compound statement as the body.
1507 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001508 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001509 MultiStmtArg(Actions), false);
1510
John McCallb268a282010-08-23 23:25:46 +00001511 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001512}
1513
Sebastian Redlb219c902008-12-21 16:41:36 +00001514/// ParseCXXTryBlock - Parse a C++ try-block.
1515///
1516/// try-block:
1517/// 'try' compound-statement handler-seq
1518///
John McCalldadc5752010-08-24 06:29:42 +00001519StmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001520 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001521
Sebastian Redlb219c902008-12-21 16:41:36 +00001522 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1523
1524 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001525 return ParseCXXTryBlockCommon(TryLoc);
1526}
1527
1528/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1529/// function-try-block.
1530///
1531/// try-block:
1532/// 'try' compound-statement handler-seq
1533///
1534/// function-try-block:
1535/// 'try' ctor-initializer[opt] compound-statement handler-seq
1536///
1537/// handler-seq:
1538/// handler handler-seq[opt]
1539///
John McCalldadc5752010-08-24 06:29:42 +00001540StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001541 if (Tok.isNot(tok::l_brace))
1542 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001543 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001544 StmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001545 if (TryBlock.isInvalid())
1546 return move(TryBlock);
1547
1548 StmtVector Handlers(Actions);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001549 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1550 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1551 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1552 << Attr.Range;
1553 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001554 if (Tok.isNot(tok::kw_catch))
1555 return StmtError(Diag(Tok, diag::err_expected_catch));
1556 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001557 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001558 if (!Handler.isInvalid())
1559 Handlers.push_back(Handler.release());
1560 }
1561 // Don't bother creating the full statement if we don't have any usable
1562 // handlers.
1563 if (Handlers.empty())
1564 return StmtError();
1565
John McCallb268a282010-08-23 23:25:46 +00001566 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001567}
1568
1569/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1570///
1571/// handler:
1572/// 'catch' '(' exception-declaration ')' compound-statement
1573///
1574/// exception-declaration:
1575/// type-specifier-seq declarator
1576/// type-specifier-seq abstract-declarator
1577/// type-specifier-seq
1578/// '...'
1579///
John McCalldadc5752010-08-24 06:29:42 +00001580StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001581 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1582
1583 SourceLocation CatchLoc = ConsumeToken();
1584
1585 SourceLocation LParenLoc = Tok.getLocation();
1586 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1587 return StmtError();
1588
1589 // C++ 3.3.2p3:
1590 // The name in a catch exception-declaration is local to the handler and
1591 // shall not be redeclared in the outermost block of the handler.
1592 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1593
1594 // exception-declaration is equivalent to '...' or a parameter-declaration
1595 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001596 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001597 if (Tok.isNot(tok::ellipsis)) {
1598 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001599 if (ParseCXXTypeSpecifierSeq(DS))
1600 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001601 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1602 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001603 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001604 } else
1605 ConsumeToken();
1606
1607 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1608 return StmtError();
1609
1610 if (Tok.isNot(tok::l_brace))
1611 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1612
Alexis Hunt96d5c762009-11-21 08:43:09 +00001613 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001614 StmtResult Block(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001615 if (Block.isInvalid())
1616 return move(Block);
1617
John McCallb268a282010-08-23 23:25:46 +00001618 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001619}