blob: 12444e87bbdacd80d37944ba6a9117749b29b739 [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,
541 bool ConvertToBoolean) {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000542 bool ParseError = false;
543
Chris Lattnerc0081db2008-12-12 06:31:07 +0000544 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000545 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +0000546 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
547 ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000548 else {
549 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000550 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000551
552 // If required, convert to a boolean value.
553 if (!ExprResult.isInvalid() && ConvertToBoolean)
554 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000555 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000556 }
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattnerc0081db2008-12-12 06:31:07 +0000558 // If the parser was confused by the condition and we don't have a ')', try to
559 // recover by skipping ahead to a semi and bailing out. If condexp is
560 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000561 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000562 SkipUntil(tok::semi);
563 // Skipping may have stopped if it found the containing ')'. If so, we can
564 // continue parsing the if statement.
565 if (Tok.isNot(tok::r_paren))
566 return true;
567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattnerc0081db2008-12-12 06:31:07 +0000569 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000570 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000571 return false;
572}
573
574
Chris Lattnerc951dae2006-08-10 04:23:57 +0000575/// ParseIfStatement
576/// if-statement: [C99 6.8.4.1]
577/// 'if' '(' expression ')' statement
578/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000579/// [C++] 'if' '(' condition ')' statement
580/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000581///
John McCalldadc5752010-08-24 06:29:42 +0000582StmtResult Parser::ParseIfStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000583 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000584
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000585 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000586 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000587
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000588 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000589 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000590 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000591 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000592 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000593
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000594 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
595
Chris Lattner2dd1b722007-08-26 23:08:06 +0000596 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
597 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000598 //
599 // C++ 6.4p3:
600 // A name introduced by a declaration in a condition is in scope from its
601 // point of declaration until the end of the substatements controlled by the
602 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000603 // C++ 3.3.2p4:
604 // Names declared in the for-init-statement, and in the condition of if,
605 // while, for, and switch statements are local to the if, while, for, or
606 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000607 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000608 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000609
Chris Lattnerc951dae2006-08-10 04:23:57 +0000610 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000611 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000612 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000613 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000614 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000615
John McCallb268a282010-08-23 23:25:46 +0000616 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000617
Chris Lattner8fb26252007-08-22 05:28:50 +0000618 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000619 // there is no compound stmt. C90 does not have this clause. We only do this
620 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000621 //
622 // C++ 6.4p1:
623 // The substatement in a selection-statement (each substatement, in the else
624 // form of the if statement) implicitly defines a local scope.
625 //
626 // For C++ we create a scope for the condition and a new scope for
627 // substatements because:
628 // -When the 'then' scope exits, we want the condition declaration to still be
629 // active for the 'else' scope too.
630 // -Sema will detect name clashes by considering declarations of a
631 // 'ControlScope' as part of its direct subscope.
632 // -If we wanted the condition and substatement to be in the same scope, we
633 // would have to notify ParseStatement not to create a new scope. It's
634 // simpler to let it create a new scope.
635 //
Mike Stump11289f42009-09-09 15:08:12 +0000636 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000637 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000638
Chris Lattner5c5808a2007-10-29 05:08:52 +0000639 // Read the 'then' stmt.
640 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000641 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000642
Chris Lattner37e54f42007-08-22 05:16:28 +0000643 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000644 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000645
Chris Lattnerc951dae2006-08-10 04:23:57 +0000646 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000647 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000648 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000649 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000650
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000651 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000652 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000653 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000654
Chris Lattner8fb26252007-08-22 05:28:50 +0000655 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000656 // there is no compound stmt. C90 does not have this clause. We only do
657 // this if the body isn't a compound statement to avoid push/pop in common
658 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000659 //
660 // C++ 6.4p1:
661 // The substatement in a selection-statement (each substatement, in the else
662 // form of the if statement) implicitly defines a local scope.
663 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000664 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000665 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000666
Chris Lattner30f910e2006-10-16 05:52:41 +0000667 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000668
Chris Lattner37e54f42007-08-22 05:16:28 +0000669 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000670 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000671 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000672
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000673 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000675 // If the condition was invalid, discard the if statement. We could recover
676 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000677 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000678 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000679
Chris Lattner5c5808a2007-10-29 05:08:52 +0000680 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000681 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000682 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000683 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
684 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
685 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000686 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000687 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000688 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000689
Chris Lattner5c5808a2007-10-29 05:08:52 +0000690 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000691 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000692 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000693 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000694 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000695
John McCallb268a282010-08-23 23:25:46 +0000696 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
697 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000698}
699
Chris Lattner9075bd72006-08-10 04:59:57 +0000700/// ParseSwitchStatement
701/// switch-statement:
702/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000703/// [C++] 'switch' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000704StmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000705 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000706
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000707 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000708 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000709
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000710 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000711 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000712 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000713 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000714 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000715
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000716 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
717
Chris Lattner2dd1b722007-08-26 23:08:06 +0000718 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
719 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000720 //
721 // C++ 6.4p3:
722 // A name introduced by a declaration in a condition is in scope from its
723 // point of declaration until the end of the substatements controlled by the
724 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000725 // C++ 3.3.2p4:
726 // Names declared in the for-init-statement, and in the condition of if,
727 // while, for, and switch statements are local to the if, while, for, or
728 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000729 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000730 unsigned ScopeFlags = Scope::BreakScope;
731 if (C99orCXX)
732 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000733 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000734
Chris Lattner9075bd72006-08-10 04:59:57 +0000735 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000736 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000737 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000738 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000739 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000740
John McCalldadc5752010-08-24 06:29:42 +0000741 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000742 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000743
Douglas Gregore60e41a2010-05-06 17:25:47 +0000744 if (Switch.isInvalid()) {
745 // Skip the switch body.
746 // FIXME: This is not optimal recovery, but parsing the body is more
747 // dangerous due to the presence of case and default statements, which
748 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000749 if (Tok.is(tok::l_brace)) {
750 ConsumeBrace();
751 SkipUntil(tok::r_brace, false, false);
752 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000753 SkipUntil(tok::semi);
754 return move(Switch);
755 }
756
Chris Lattner8fb26252007-08-22 05:28:50 +0000757 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000758 // there is no compound stmt. C90 does not have this clause. We only do this
759 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000760 //
761 // C++ 6.4p1:
762 // The substatement in a selection-statement (each substatement, in the else
763 // form of the if statement) implicitly defines a local scope.
764 //
765 // See comments in ParseIfStatement for why we create a scope for the
766 // condition and a new scope for substatement in C++.
767 //
Mike Stump11289f42009-09-09 15:08:12 +0000768 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000769 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000770
Chris Lattner9075bd72006-08-10 04:59:57 +0000771 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000772 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000773
Chris Lattner8fd2d012010-01-24 01:50:29 +0000774 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000775 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000776 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000777
Chris Lattner8fd2d012010-01-24 01:50:29 +0000778 if (Body.isInvalid())
779 // FIXME: Remove the case statement list from the Switch statement.
780 Body = Actions.ActOnNullStmt(Tok.getLocation());
781
John McCallb268a282010-08-23 23:25:46 +0000782 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000783}
784
785/// ParseWhileStatement
786/// while-statement: [C99 6.8.5.1]
787/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000788/// [C++] 'while' '(' condition ')' statement
John McCalldadc5752010-08-24 06:29:42 +0000789StmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000790 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000791
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000792 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000793 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000794 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000795
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000796 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000797 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000798 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000799 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000800 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000801
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000802 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
803
Chris Lattner2dd1b722007-08-26 23:08:06 +0000804 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
805 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000806 //
807 // C++ 6.4p3:
808 // A name introduced by a declaration in a condition is in scope from its
809 // point of declaration until the end of the substatements controlled by the
810 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000811 // C++ 3.3.2p4:
812 // Names declared in the for-init-statement, and in the condition of if,
813 // while, for, and switch statements are local to the if, while, for, or
814 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000815 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000816 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000817 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000818 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
819 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000820 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000821 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
822 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000823
Chris Lattner9075bd72006-08-10 04:59:57 +0000824 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000825 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000826 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000827 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000828 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000829
John McCallb268a282010-08-23 23:25:46 +0000830 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattner8fb26252007-08-22 05:28:50 +0000832 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000833 // there is no compound stmt. C90 does not have this clause. We only do this
834 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000835 //
836 // C++ 6.5p2:
837 // The substatement in an iteration-statement implicitly defines a local scope
838 // which is entered and exited each time through the loop.
839 //
840 // See comments in ParseIfStatement for why we create a scope for the
841 // condition and a new scope for substatement in C++.
842 //
Mike Stump11289f42009-09-09 15:08:12 +0000843 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000844 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000845
Chris Lattner9075bd72006-08-10 04:59:57 +0000846 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000847 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000848
Chris Lattner8fb26252007-08-22 05:28:50 +0000849 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000850 InnerScope.Exit();
851 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000852
John McCall48871652010-08-21 09:40:31 +0000853 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000854 return StmtError();
855
John McCallb268a282010-08-23 23:25:46 +0000856 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000857}
858
859/// ParseDoStatement
860/// do-statement: [C99 6.8.5.2]
861/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000862/// Note: this lets the caller parse the end ';'.
John McCalldadc5752010-08-24 06:29:42 +0000863StmtResult Parser::ParseDoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000864 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000865
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000866 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000867 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000868
Chris Lattner2dd1b722007-08-26 23:08:06 +0000869 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
870 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000871 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000872 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000873 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000874 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000875 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000876
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000877 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000878
Chris Lattner8fb26252007-08-22 05:28:50 +0000879 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000880 // there is no compound stmt. C90 does not have this clause. We only do this
881 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000882 //
883 // C++ 6.5p2:
884 // The substatement in an iteration-statement implicitly defines a local scope
885 // which is entered and exited each time through the loop.
886 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000887 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000888 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000889 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000890
Chris Lattner9075bd72006-08-10 04:59:57 +0000891 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000892 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000893
Chris Lattner8fb26252007-08-22 05:28:50 +0000894 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000895 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000896
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000897 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000898 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000899 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000900 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000901 SkipUntil(tok::semi, false, true);
902 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000903 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000904 }
Chris Lattneraf635312006-10-16 06:06:51 +0000905 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000906
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000907 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000908 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000909 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000910 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000911 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000912
Chris Lattner10da53c2008-12-12 06:35:28 +0000913 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000914 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000915 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +0000916 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000917 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000918
Sebastian Redlb62406f2008-12-11 19:48:14 +0000919 if (Cond.isInvalid() || Body.isInvalid())
920 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000921
John McCallb268a282010-08-23 23:25:46 +0000922 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
923 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000924}
925
926/// ParseForStatement
927/// for-statement: [C99 6.8.5.3]
928/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
929/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000930/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
931/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000932/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
933/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000934///
935/// [C++] for-init-statement:
936/// [C++] expression-statement
937/// [C++] simple-declaration
938///
John McCalldadc5752010-08-24 06:29:42 +0000939StmtResult Parser::ParseForStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000940 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000941
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000942 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000943 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000944
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000945 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000946 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000947 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000948 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000949 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000950
Chris Lattner934074c2009-04-22 00:54:41 +0000951 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000952
Chris Lattner2dd1b722007-08-26 23:08:06 +0000953 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
954 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000955 //
956 // C++ 6.4p3:
957 // A name introduced by a declaration in a condition is in scope from its
958 // point of declaration until the end of the substatements controlled by the
959 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000960 // C++ 3.3.2p4:
961 // Names declared in the for-init-statement, and in the condition of if,
962 // while, for, and switch statements are local to the if, while, for, or
963 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000964 // C++ 6.5.3p1:
965 // Names declared in the for-init-statement are in the same declarative-region
966 // as those declared in the condition.
967 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000968 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000969 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000970 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
971 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000972 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000973 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
974
975 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000976
Chris Lattner04132372006-10-16 06:12:55 +0000977 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +0000978 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000979
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000980 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +0000981 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +0000982 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000983 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +0000984 ExprResult Collection;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000985 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +0000986 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000987
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000988 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000989 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000990 C99orCXXorObjC? Sema::PCC_ForInit
991 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000992 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000993 }
994
Chris Lattner9075bd72006-08-10 04:59:57 +0000995 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000996 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000997 // no first part, eat the ';'.
998 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +0000999 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001000 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001001 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001002 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001003
Alexis Hunt96d5c762009-11-21 08:43:09 +00001004 AttributeList *AttrList = 0;
1005 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
1006 AttrList = ParseCXX0XAttributes().AttrList;
1007
Chris Lattner49836b42009-04-02 04:16:50 +00001008 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001009 StmtVector Stmts(Actions);
1010 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
1011 DeclEnd, AttrList, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001012 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001013
Chris Lattner32dc41c2009-03-29 17:27:48 +00001014 if (Tok.is(tok::semi)) { // for (int x = 4;
1015 ConsumeToken();
1016 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001017 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001018 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001019 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001020
1021 if (Tok.is(tok::code_completion)) {
1022 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1023 ConsumeCodeCompletionToken();
1024 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001025 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001026 } else {
1027 Diag(Tok, diag::err_expected_semi_for);
1028 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001029 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001030 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001031 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001032
Chris Lattnercd68f642007-06-27 01:06:29 +00001033 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001034 if (!Value.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001035 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001036
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001037 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001038 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001039 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001040 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001041
1042 if (Tok.is(tok::code_completion)) {
1043 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1044 ConsumeCodeCompletionToken();
1045 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001046 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001047 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001048 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001049 SkipUntil(tok::semi);
1050 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001051 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001052 if (!ForEach) {
John McCallb268a282010-08-23 23:25:46 +00001053 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001054 // Parse the second part of the for specifier.
1055 if (Tok.is(tok::semi)) { // for (...;;
1056 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001057 } else {
John McCalldadc5752010-08-24 06:29:42 +00001058 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001059 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001060 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1061 else {
1062 Second = ParseExpression();
1063 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001064 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001065 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001066 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001067 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001068 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001069 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001070
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001071 if (Tok.is(tok::semi)) {
1072 ConsumeToken();
1073 } else {
John McCall48871652010-08-21 09:40:31 +00001074 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001075 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001076 SkipUntil(tok::semi);
1077 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001078
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001079 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001080 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001081 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001082 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001083 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001084 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001085 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001086 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001087
Chris Lattner8fb26252007-08-22 05:28:50 +00001088 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001089 // there is no compound stmt. C90 does not have this clause. We only do this
1090 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001091 //
1092 // C++ 6.5p2:
1093 // The substatement in an iteration-statement implicitly defines a local scope
1094 // which is entered and exited each time through the loop.
1095 //
1096 // See comments in ParseIfStatement for why we create a scope for
1097 // for-init-statement/condition and a new scope for substatement in C++.
1098 //
Mike Stump11289f42009-09-09 15:08:12 +00001099 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001100 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001101
Chris Lattner9075bd72006-08-10 04:59:57 +00001102 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001103 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001104
Chris Lattner8fb26252007-08-22 05:28:50 +00001105 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001106 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001107
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001108 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001109 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001110
1111 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001112 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001113
1114 if (!ForEach)
John McCallb268a282010-08-23 23:25:46 +00001115 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1116 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001117
Douglas Gregore60e41a2010-05-06 17:25:47 +00001118 // FIXME: It isn't clear how to communicate the late destruction of
1119 // C++ temporaries used to create the collection.
John McCallb268a282010-08-23 23:25:46 +00001120 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1121 Collection.take(), RParenLoc,
1122 Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001123}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001124
Chris Lattner503fadc2006-08-10 05:45:44 +00001125/// ParseGotoStatement
1126/// jump-statement:
1127/// 'goto' identifier ';'
1128/// [GNU] 'goto' '*' expression ';'
1129///
1130/// Note: this lets the caller parse the end ';'.
1131///
John McCalldadc5752010-08-24 06:29:42 +00001132StmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001133 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001134
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001135 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001136 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001137
John McCalldadc5752010-08-24 06:29:42 +00001138 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001139 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001140 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001141 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001142 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001143 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001144 // GNU indirect goto extension.
1145 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001146 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001147 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001148 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001149 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001150 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001151 }
John McCallb268a282010-08-23 23:25:46 +00001152 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001153 } else {
1154 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001155 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001156 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001157
Sebastian Redlb62406f2008-12-11 19:48:14 +00001158 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001159}
1160
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001161/// ParseContinueStatement
1162/// jump-statement:
1163/// 'continue' ';'
1164///
1165/// Note: this lets the caller parse the end ';'.
1166///
John McCalldadc5752010-08-24 06:29:42 +00001167StmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001168 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001169
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001170 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001171 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001172}
1173
1174/// ParseBreakStatement
1175/// jump-statement:
1176/// 'break' ';'
1177///
1178/// Note: this lets the caller parse the end ';'.
1179///
John McCalldadc5752010-08-24 06:29:42 +00001180StmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001181 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001182
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001183 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001184 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001185}
1186
Chris Lattner503fadc2006-08-10 05:45:44 +00001187/// ParseReturnStatement
1188/// jump-statement:
1189/// 'return' expression[opt] ';'
John McCalldadc5752010-08-24 06:29:42 +00001190StmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001191 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001192
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001193 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001194 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001195
John McCalldadc5752010-08-24 06:29:42 +00001196 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001197 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001198 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001199 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001200 ConsumeCodeCompletionToken();
1201 SkipUntil(tok::semi, false, true);
1202 return StmtError();
1203 }
1204
Chris Lattner30f910e2006-10-16 05:52:41 +00001205 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001206 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001207 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001208 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001209 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001210 }
John McCallb268a282010-08-23 23:25:46 +00001211 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001212}
Chris Lattner0116c472006-08-15 06:03:28 +00001213
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001214/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1215/// routine is called to skip/ignore tokens that comprise the MS asm statement.
John McCalldadc5752010-08-24 06:29:42 +00001216StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001217 if (Tok.is(tok::l_brace)) {
1218 unsigned short savedBraceCount = BraceCount;
1219 do {
1220 ConsumeAnyToken();
1221 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001222 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001223 // From the MS website: If used without braces, the __asm keyword means
1224 // that the rest of the line is an assembly-language statement.
1225 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001226 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001227 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001228 do {
1229 ConsumeAnyToken();
1230 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001231 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1232 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001233 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001234 }
Mike Stump6da5d752009-12-11 00:04:56 +00001235 Token t;
1236 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001237 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001238 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001239 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001240 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001241 ExprVector Constraints(Actions);
1242 ExprVector Exprs(Actions);
1243 ExprVector Clobbers(Actions);
Anders Carlsson9a020f92010-01-30 22:25:16 +00001244 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001245 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001246 AsmString.take(), move_arg(Clobbers),
Mike Stump90be58a2010-01-04 22:37:17 +00001247 Tok.getLocation(), true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001248}
1249
Chris Lattner0116c472006-08-15 06:03:28 +00001250/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001251/// asm-statement:
1252/// gnu-asm-statement
1253/// ms-asm-statement
1254///
1255/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001256/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1257///
1258/// [GNU] asm-argument:
1259/// asm-string-literal
1260/// asm-string-literal ':' asm-operands[opt]
1261/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1262/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1263/// ':' asm-clobbers
1264///
1265/// [GNU] asm-clobbers:
1266/// asm-string-literal
1267/// asm-clobbers ',' asm-string-literal
1268///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001269/// [MS] ms-asm-statement:
1270/// '__asm' assembly-instruction ';'[opt]
1271/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1272///
1273/// [MS] assembly-instruction-list:
1274/// assembly-instruction ';'[opt]
1275/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1276///
John McCalldadc5752010-08-24 06:29:42 +00001277StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001278 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001279 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001280
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001281 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001282 msAsm = true;
1283 return FuzzyParseMicrosoftAsmStatement();
1284 }
Chris Lattner0116c472006-08-15 06:03:28 +00001285 DeclSpec DS;
1286 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001287 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001288
Chris Lattner0116c472006-08-15 06:03:28 +00001289 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001290 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001291 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001292 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001293 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001294
Chris Lattner0116c472006-08-15 06:03:28 +00001295 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001296 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001297 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001298 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001299 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001300 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001301 }
Chris Lattner04132372006-10-16 06:12:55 +00001302 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001303
John McCalldadc5752010-08-24 06:29:42 +00001304 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001305 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001306 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001307
Anders Carlsson9a020f92010-01-30 22:25:16 +00001308 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001309 ExprVector Constraints(Actions);
1310 ExprVector Exprs(Actions);
1311 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001312
Anders Carlsson19fe1162008-02-05 23:03:50 +00001313 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001314 // We have a simple asm expression like 'asm("foo")'.
1315 SourceLocation RParenLoc = ConsumeParen();
1316 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1317 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1318 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001319 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001320 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001321 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001322
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001323 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001324 bool AteExtraColon = false;
1325 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1326 // In C++ mode, parse "::" like ": :".
1327 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001328 ConsumeToken();
1329
Chris Lattner15768502009-12-20 23:08:04 +00001330 if (!AteExtraColon &&
1331 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001332 return StmtError();
1333 }
Chris Lattner15768502009-12-20 23:08:04 +00001334
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001335 unsigned NumOutputs = Names.size();
1336
1337 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001338 if (AteExtraColon ||
1339 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1340 // In C++ mode, parse "::" like ": :".
1341 if (AteExtraColon)
1342 AteExtraColon = false;
1343 else {
1344 AteExtraColon = Tok.is(tok::coloncolon);
1345 ConsumeToken();
1346 }
1347
1348 if (!AteExtraColon &&
1349 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001350 return StmtError();
1351 }
1352
1353 assert(Names.size() == Constraints.size() &&
1354 Constraints.size() == Exprs.size() &&
1355 "Input operand size mismatch!");
1356
1357 unsigned NumInputs = Names.size() - NumOutputs;
1358
1359 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001360 if (AteExtraColon || Tok.is(tok::colon)) {
1361 if (!AteExtraColon)
1362 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001363
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001364 // Parse the asm-string list for clobbers if present.
1365 if (Tok.isNot(tok::r_paren)) {
1366 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001367 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001368
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001369 if (Clobber.isInvalid())
1370 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001371
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001372 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001373
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001374 if (Tok.isNot(tok::comma)) break;
1375 ConsumeToken();
1376 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001377 }
1378 }
1379
1380 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1381 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001382 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001383 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001384 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001385 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001386}
1387
1388/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001389/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001390///
1391/// [GNU] asm-operands:
1392/// asm-operand
1393/// asm-operands ',' asm-operand
1394///
1395/// [GNU] asm-operand:
1396/// asm-string-literal '(' expression ')'
1397/// '[' identifier ']' asm-string-literal '(' expression ')'
1398///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001399//
1400// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001401bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1402 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1403 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001404 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001405 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001406 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001407
1408 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001409 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001410 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001411 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001412
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001413 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001414 Diag(Tok, diag::err_expected_ident);
1415 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001416 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001417 }
Mike Stump11289f42009-09-09 15:08:12 +00001418
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001419 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001420 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001421
Anders Carlsson9a020f92010-01-30 22:25:16 +00001422 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001423 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001424 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001425 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001426
John McCalldadc5752010-08-24 06:29:42 +00001427 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001428 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001429 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001430 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001431 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001432 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001433
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001434 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001435 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001436 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001437 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001438 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001439
Chris Lattner0116c472006-08-15 06:03:28 +00001440 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001441 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001442 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001443 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001444 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001445 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001446 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001447 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001448 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001449 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001450 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001451 ConsumeToken();
1452 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001453
1454 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001455}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001456
John McCall48871652010-08-21 09:40:31 +00001457Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001458 assert(Tok.is(tok::l_brace));
1459 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001460
John McCallfaf5fb42010-08-26 23:41:50 +00001461 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1462 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001463
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001464 // Do not enter a scope for the brace, as the arguments are in the same scope
1465 // (the function body) as the body itself. Instead, just read the statement
1466 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001467 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001468
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001469 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001470 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001471 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001472 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001473
John McCallb268a282010-08-23 23:25:46 +00001474 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001475}
Sebastian Redlb219c902008-12-21 16:41:36 +00001476
Sebastian Redla7b98a72009-04-26 20:35:05 +00001477/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1478///
1479/// function-try-block:
1480/// 'try' ctor-initializer[opt] compound-statement handler-seq
1481///
John McCall48871652010-08-21 09:40:31 +00001482Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001483 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1484 SourceLocation TryLoc = ConsumeToken();
1485
John McCallfaf5fb42010-08-26 23:41:50 +00001486 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1487 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001488
1489 // Constructor initializer list?
1490 if (Tok.is(tok::colon))
1491 ParseConstructorInitializer(Decl);
1492
Sebastian Redld98ecd62009-04-26 21:08:36 +00001493 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001494 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001495 // If we failed to parse the try-catch, we just give the function an empty
1496 // compound statement as the body.
1497 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001498 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001499 MultiStmtArg(Actions), false);
1500
John McCallb268a282010-08-23 23:25:46 +00001501 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001502}
1503
Sebastian Redlb219c902008-12-21 16:41:36 +00001504/// ParseCXXTryBlock - Parse a C++ try-block.
1505///
1506/// try-block:
1507/// 'try' compound-statement handler-seq
1508///
John McCalldadc5752010-08-24 06:29:42 +00001509StmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001510 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001511
Sebastian Redlb219c902008-12-21 16:41:36 +00001512 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1513
1514 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001515 return ParseCXXTryBlockCommon(TryLoc);
1516}
1517
1518/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1519/// function-try-block.
1520///
1521/// try-block:
1522/// 'try' compound-statement handler-seq
1523///
1524/// function-try-block:
1525/// 'try' ctor-initializer[opt] compound-statement handler-seq
1526///
1527/// handler-seq:
1528/// handler handler-seq[opt]
1529///
John McCalldadc5752010-08-24 06:29:42 +00001530StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001531 if (Tok.isNot(tok::l_brace))
1532 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001533 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001534 StmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001535 if (TryBlock.isInvalid())
1536 return move(TryBlock);
1537
1538 StmtVector Handlers(Actions);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001539 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1540 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1541 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1542 << Attr.Range;
1543 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001544 if (Tok.isNot(tok::kw_catch))
1545 return StmtError(Diag(Tok, diag::err_expected_catch));
1546 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001547 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001548 if (!Handler.isInvalid())
1549 Handlers.push_back(Handler.release());
1550 }
1551 // Don't bother creating the full statement if we don't have any usable
1552 // handlers.
1553 if (Handlers.empty())
1554 return StmtError();
1555
John McCallb268a282010-08-23 23:25:46 +00001556 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001557}
1558
1559/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1560///
1561/// handler:
1562/// 'catch' '(' exception-declaration ')' compound-statement
1563///
1564/// exception-declaration:
1565/// type-specifier-seq declarator
1566/// type-specifier-seq abstract-declarator
1567/// type-specifier-seq
1568/// '...'
1569///
John McCalldadc5752010-08-24 06:29:42 +00001570StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001571 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1572
1573 SourceLocation CatchLoc = ConsumeToken();
1574
1575 SourceLocation LParenLoc = Tok.getLocation();
1576 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1577 return StmtError();
1578
1579 // C++ 3.3.2p3:
1580 // The name in a catch exception-declaration is local to the handler and
1581 // shall not be redeclared in the outermost block of the handler.
1582 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1583
1584 // exception-declaration is equivalent to '...' or a parameter-declaration
1585 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001586 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001587 if (Tok.isNot(tok::ellipsis)) {
1588 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001589 if (ParseCXXTypeSpecifierSeq(DS))
1590 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001591 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1592 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001593 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001594 } else
1595 ConsumeToken();
1596
1597 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1598 return StmtError();
1599
1600 if (Tok.isNot(tok::l_brace))
1601 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1602
Alexis Hunt96d5c762009-11-21 08:43:09 +00001603 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCalldadc5752010-08-24 06:29:42 +00001604 StmtResult Block(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001605 if (Block.isInvalid())
1606 return move(Block);
1607
John McCallb268a282010-08-23 23:25:46 +00001608 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001609}