blob: b208c50c81bcb0868995853ada58834506388a99 [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"
Chris Lattner288e86ff12006-11-11 23:03:42 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner33ad2ca2006-11-05 23:47:55 +000018#include "clang/Parse/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000045/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000046/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000053/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
Chris Lattner9075bd72006-08-10 04:59:57 +000062/// expression-statement:
63/// expression[opt] ';'
64///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000065/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000070/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000071///
Fariborz Jahanian90814572007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000074/// [OBC] '@' 'throw' ';'
75///
Sebastian Redl042ad952008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000078 const char *SemiError = 0;
Sebastian Redlc13f2682008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000080
Alexis Hunt96d5c762009-11-21 08:43:09 +000081 CXX0XAttributeList Attr;
82 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
83 Attr = ParseCXX0XAttributes();
Ted Kremenekc162e8e2010-02-11 02:19:13 +000084 llvm::OwningPtr<AttributeList> AttrList(Attr.AttrList);
Alexis Hunt96d5c762009-11-21 08:43:09 +000085
Chris Lattner503fadc2006-08-10 05:45:44 +000086 // Cases in this switch statement should fall through if the parser expects
87 // the token to end in a semicolon (in which case SemiError should be set),
88 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000089 tok::TokenKind Kind = Tok.getKind();
90 SourceLocation AtLoc;
91 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000092 case tok::at: // May be a @try or @throw statement
93 {
94 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000095 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000096 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000097
Douglas Gregor9d64c5e2009-09-21 20:51:25 +000098 case tok::code_completion:
Douglas Gregor504a6ae2010-01-10 23:08:15 +000099 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Statement);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000100 ConsumeToken();
101 return ParseStatementOrDeclaration(OnlyStatement);
102
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000103 case tok::identifier:
104 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
105 // identifier ':' statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000106 return ParseLabeledStatement(AttrList.take());
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000107 }
108 // PASS THROUGH.
109
Chris Lattner803802d2009-03-24 17:04:48 +0000110 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000111 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000112 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000113 AttrList.take(); //Passing 'Attr' to ParseDeclaration transfers ownership.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000114 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd,
115 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000116 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000117 }
118
119 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000120 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000121 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Alexis Hunt96d5c762009-11-21 08:43:09 +0000124 // FIXME: Use the attributes
Chris Lattner803802d2009-03-24 17:04:48 +0000125 // expression[opt] ';'
126 OwningExprResult Expr(ParseExpression());
127 if (Expr.isInvalid()) {
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000128 // If the expression is invalid, skip ahead to the next semicolon or '}'.
129 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattner803802d2009-03-24 17:04:48 +0000130 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidis90ab3b72010-03-31 00:37:59 +0000131 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
132 if (Tok.is(tok::semi))
133 ConsumeToken();
Chris Lattner803802d2009-03-24 17:04:48 +0000134 return StmtError();
135 }
136 // Otherwise, eat the semicolon.
137 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000138 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr));
Chris Lattner803802d2009-03-24 17:04:48 +0000139 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000140
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000141 case tok::kw_case: // C99 6.8.1: labeled-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000142 return ParseCaseStatement(AttrList.take());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000143 case tok::kw_default: // C99 6.8.1: labeled-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000144 return ParseDefaultStatement(AttrList.take());
Sebastian Redl042ad952008-12-11 19:30:53 +0000145
Chris Lattner9075bd72006-08-10 04:59:57 +0000146 case tok::l_brace: // C99 6.8.2: compound-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000147 return ParseCompoundStatement(AttrList.take());
Chris Lattner0f203a72007-05-28 01:45:28 +0000148 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000149 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl042ad952008-12-11 19:30:53 +0000150
Chris Lattner9075bd72006-08-10 04:59:57 +0000151 case tok::kw_if: // C99 6.8.4.1: if-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000152 return ParseIfStatement(AttrList.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000153 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000154 return ParseSwitchStatement(AttrList.take());
Sebastian Redl042ad952008-12-11 19:30:53 +0000155
Chris Lattner9075bd72006-08-10 04:59:57 +0000156 case tok::kw_while: // C99 6.8.5.1: while-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000157 return ParseWhileStatement(AttrList.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000158 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000159 Res = ParseDoStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000160 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000161 break;
162 case tok::kw_for: // C99 6.8.5.3: for-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000163 return ParseForStatement(AttrList.take());
Chris Lattner503fadc2006-08-10 05:45:44 +0000164
165 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000166 Res = ParseGotoStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000167 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000168 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000169 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000170 Res = ParseContinueStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000171 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000172 break;
173 case tok::kw_break: // C99 6.8.6.3: break-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000174 Res = ParseBreakStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000175 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000176 break;
177 case tok::kw_return: // C99 6.8.6.4: return-statement
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000178 Res = ParseReturnStatement(AttrList.take());
Chris Lattner34a95662009-06-14 00:07:48 +0000179 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000180 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000181
Sebastian Redlb219c902008-12-21 16:41:36 +0000182 case tok::kw_asm: {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000183 if (Attr.HasAttr)
184 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
185 << Attr.Range;
Steve Naroffb2c80c72008-02-07 03:50:06 +0000186 bool msAsm = false;
187 Res = ParseAsmStatement(msAsm);
Sebastian Redl042ad952008-12-11 19:30:53 +0000188 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000189 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000190 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000191 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000192
Sebastian Redlb219c902008-12-21 16:41:36 +0000193 case tok::kw_try: // C++ 15: try-block
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000194 return ParseCXXTryBlock(AttrList.take());
Sebastian Redlb219c902008-12-21 16:41:36 +0000195 }
196
Chris Lattner503fadc2006-08-10 05:45:44 +0000197 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000198 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000199 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000200 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000201 // If the result was valid, then we do want to diagnose this. Use
202 // ExpectAndConsume to emit the diagnostic, even though we know it won't
203 // succeed.
204 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000205 // Skip until we see a } or ;, but don't eat it.
206 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000207 }
Mike Stump11289f42009-09-09 15:08:12 +0000208
Sebastian Redl042ad952008-12-11 19:30:53 +0000209 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000210}
211
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000212/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000213///
214/// labeled-statement:
215/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000216/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000217///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000218Parser::OwningStmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000219 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
220 "Not an identifier!");
221
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000222 llvm::OwningPtr<AttributeList> AttrList(Attr);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000223 Token IdentTok = Tok; // Save the whole token.
224 ConsumeToken(); // eat the identifier.
225
226 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000227
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000228 // identifier ':' statement
229 SourceLocation ColonLoc = ConsumeToken();
230
231 // Read label attributes, if present.
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000232 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000233 AttrList.reset(addAttributeLists(AttrList.take(), ParseGNUAttributes()));
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000234
Sebastian Redl042ad952008-12-11 19:30:53 +0000235 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000236
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000237 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000238 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000239 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000240
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000241 // FIXME: use attributes?
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000242 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
243 IdentTok.getIdentifierInfo(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000244 ColonLoc, move(SubStmt));
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000245}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000246
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000247/// ParseCaseStatement
248/// labeled-statement:
249/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000250/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000251///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000252Parser::OwningStmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000253 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000254 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000255 delete Attr;
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.
273 OwningStmtResult TopLevelCase(Actions, 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)) {
285 Actions.CodeCompleteCase(CurScope);
286 ConsumeToken();
287 }
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
Chris Lattner34a22092009-03-04 04:23:07 +0000294 OwningExprResult LHS(ParseConstantExpression());
295 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;
302 OwningExprResult RHS(Actions);
303 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
Chris Lattner34a22092009-03-04 04:23:07 +0000324 OwningStmtResult Case =
325 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
326 move(RHS), 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.
337 StmtTy *NextDeepest = Case.get();
338 if (TopLevelCase.isInvalid())
339 TopLevelCase = move(Case);
340 else
341 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
342 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.
351 OwningStmtResult SubStmt(Actions);
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.
368 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
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///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000379Parser::OwningStmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
380 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000381 delete Attr;
382
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000383 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000384 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000385
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000386 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000387 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000388 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000389 return StmtError();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000390 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000391
Chris Lattneraf635312006-10-16 06:06:51 +0000392 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000393
Chris Lattner30f910e2006-10-16 05:52:41 +0000394 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000395 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000396 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000397 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000398 }
399
Sebastian Redl042ad952008-12-11 19:30:53 +0000400 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000401 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000402 return StmtError();
403
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000404 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +0000405 move(SubStmt), CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000406}
407
408
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000409/// ParseCompoundStatement - Parse a "{}" block.
410///
411/// compound-statement: [C99 6.8.2]
412/// { block-item-list[opt] }
413/// [GNU] { label-declarations block-item-list } [TODO]
414///
415/// block-item-list:
416/// block-item
417/// block-item-list block-item
418///
419/// block-item:
420/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000421/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000422/// statement
423/// [OMP] openmp-directive [TODO]
424///
425/// [GNU] label-declarations:
426/// [GNU] label-declaration
427/// [GNU] label-declarations label-declaration
428///
429/// [GNU] label-declaration:
430/// [GNU] '__label__' identifier-list ';'
431///
432/// [OMP] openmp-directive: [TODO]
433/// [OMP] barrier-directive
434/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000435///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000436Parser::OwningStmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
437 bool isStmtExpr) {
438 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000439 delete Attr;
440
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000441 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000442
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000443 // Enter a scope to hold everything within the compound stmt. Compound
444 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000445 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000446
447 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000448 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000449}
450
451
452/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000453/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000454/// consume the '}' at the end of the block. It does not manipulate the scope
455/// stack.
Sebastian Redl042ad952008-12-11 19:30:53 +0000456Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000457 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000458 Tok.getLocation(),
459 "in compound statement ('{}')");
Mike Stump11289f42009-09-09 15:08:12 +0000460
Chris Lattnerf2978802007-01-21 06:52:16 +0000461 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
462
Chris Lattner010015a2007-05-28 07:23:54 +0000463 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000464 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000465
466 typedef StmtVector StmtsTy;
467 StmtsTy Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000468 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redlc13f2682008-12-09 20:22:58 +0000469 OwningStmtResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000470 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000471 R = ParseStatementOrDeclaration(false);
472 } else {
473 // __extension__ can start declarations and it can also be a unary
474 // operator for expressions. Consume multiple __extension__ markers here
475 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000476 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000477 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000478 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000479 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000480
Alexis Hunt96d5c762009-11-21 08:43:09 +0000481 CXX0XAttributeList Attr;
482 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
483 Attr = ParseCXX0XAttributes();
484
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000485 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000486 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000487 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000488 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000489 ExtensionRAIIObject O(Diags);
490
Chris Lattner49836b42009-04-02 04:16:50 +0000491 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000492 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
493 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000494 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000495 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000496 // Otherwise this was a unary __extension__ marker.
497 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000498
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000499 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000500 SkipUntil(tok::semi);
501 continue;
502 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000503
Alexis Hunt96d5c762009-11-21 08:43:09 +0000504 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000505 // Eat the semicolon at the end of stmt and convert the expr into a
506 // statement.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000507 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000508 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000509 }
510 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000511
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000512 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000513 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000514 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000515
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000516 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000517 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000518 Diag(Tok, diag::err_expected_rbrace);
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.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000538bool Parser::ParseParenExprOrCondition(OwningExprResult &ExprResult,
539 DeclPtrTy &DeclResult) {
540 bool ParseError = false;
541
Chris Lattnerc0081db2008-12-12 06:31:07 +0000542 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000543 if (getLang().CPlusPlus)
544 ParseError = ParseCXXCondition(ExprResult, DeclResult);
545 else {
546 ExprResult = ParseExpression();
547 DeclResult = DeclPtrTy();
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattnerc0081db2008-12-12 06:31:07 +0000550 // If the parser was confused by the condition and we don't have a ')', try to
551 // recover by skipping ahead to a semi and bailing out. If condexp is
552 // semantically invalid but we have well formed code, keep going.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000553 if (ExprResult.isInvalid() && !DeclResult.get() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000554 SkipUntil(tok::semi);
555 // Skipping may have stopped if it found the containing ')'. If so, we can
556 // continue parsing the if statement.
557 if (Tok.isNot(tok::r_paren))
558 return true;
559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattnerc0081db2008-12-12 06:31:07 +0000561 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000562 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000563 return false;
564}
565
566
Chris Lattnerc951dae2006-08-10 04:23:57 +0000567/// ParseIfStatement
568/// if-statement: [C99 6.8.4.1]
569/// 'if' '(' expression ')' statement
570/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000571/// [C++] 'if' '(' condition ')' statement
572/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000573///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000574Parser::OwningStmtResult Parser::ParseIfStatement(AttributeList *Attr) {
575 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000576 delete Attr;
577
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000578 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000579 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000580
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000581 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000582 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000583 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000584 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000585 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000586
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000587 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
588
Chris Lattner2dd1b722007-08-26 23:08:06 +0000589 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
590 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000591 //
592 // C++ 6.4p3:
593 // A name introduced by a declaration in a condition is in scope from its
594 // point of declaration until the end of the substatements controlled by the
595 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000596 // C++ 3.3.2p4:
597 // Names declared in the for-init-statement, and in the condition of if,
598 // while, for, and switch statements are local to the if, while, for, or
599 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000600 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000601 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000602
Chris Lattnerc951dae2006-08-10 04:23:57 +0000603 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000604 OwningExprResult CondExp(Actions);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000605 DeclPtrTy CondVar;
606 if (ParseParenExprOrCondition(CondExp, CondVar))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000607 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000608
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000609 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp));
Mike Stump11289f42009-09-09 15:08:12 +0000610
Chris Lattner8fb26252007-08-22 05:28:50 +0000611 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000612 // there is no compound stmt. C90 does not have this clause. We only do this
613 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000614 //
615 // C++ 6.4p1:
616 // The substatement in a selection-statement (each substatement, in the else
617 // form of the if statement) implicitly defines a local scope.
618 //
619 // For C++ we create a scope for the condition and a new scope for
620 // substatements because:
621 // -When the 'then' scope exits, we want the condition declaration to still be
622 // active for the 'else' scope too.
623 // -Sema will detect name clashes by considering declarations of a
624 // 'ControlScope' as part of its direct subscope.
625 // -If we wanted the condition and substatement to be in the same scope, we
626 // would have to notify ParseStatement not to create a new scope. It's
627 // simpler to let it create a new scope.
628 //
Mike Stump11289f42009-09-09 15:08:12 +0000629 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000630 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000631
Chris Lattner5c5808a2007-10-29 05:08:52 +0000632 // Read the 'then' stmt.
633 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000634 OwningStmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000635
Chris Lattner37e54f42007-08-22 05:16:28 +0000636 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000637 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000638
Chris Lattnerc951dae2006-08-10 04:23:57 +0000639 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000640 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000641 SourceLocation ElseStmtLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +0000642 OwningStmtResult ElseStmt(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000643
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000644 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000645 ElseLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000646
Chris Lattner8fb26252007-08-22 05:28:50 +0000647 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000648 // there is no compound stmt. C90 does not have this clause. We only do
649 // this if the body isn't a compound statement to avoid push/pop in common
650 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000651 //
652 // C++ 6.4p1:
653 // The substatement in a selection-statement (each substatement, in the else
654 // form of the if statement) implicitly defines a local scope.
655 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000656 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000657 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000658
Douglas Gregor85970ca2008-12-10 23:01:14 +0000659 bool WithinElse = CurScope->isWithinElse();
660 CurScope->setWithinElse(true);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000661 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000662 ElseStmt = ParseStatement();
Douglas Gregor85970ca2008-12-10 23:01:14 +0000663 CurScope->setWithinElse(WithinElse);
Chris Lattner37e54f42007-08-22 05:16:28 +0000664
665 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000666 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000667 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000668
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000669 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000671 // If the condition was invalid, discard the if statement. We could recover
672 // better by replacing it with a valid expr, but don't do that yet.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000673 if (CondExp.isInvalid() && !CondVar.get())
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000674 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000675
Chris Lattner5c5808a2007-10-29 05:08:52 +0000676 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000677 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000678 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000679 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
680 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
681 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000682 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000683 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000684 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000685
Chris Lattner5c5808a2007-10-29 05:08:52 +0000686 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000687 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000688 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000689 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000690 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000691
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000692 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, move(ThenStmt),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000693 ElseLoc, move(ElseStmt));
Chris Lattnerc951dae2006-08-10 04:23:57 +0000694}
695
Chris Lattner9075bd72006-08-10 04:59:57 +0000696/// ParseSwitchStatement
697/// switch-statement:
698/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000699/// [C++] 'switch' '(' condition ')' statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000700Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
701 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000702 delete Attr;
703
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000704 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000705 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000706
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000707 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000708 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000709 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000710 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000711 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000712
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000713 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
714
Chris Lattner2dd1b722007-08-26 23:08:06 +0000715 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
716 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000717 //
718 // C++ 6.4p3:
719 // A name introduced by a declaration in a condition is in scope from its
720 // point of declaration until the end of the substatements controlled by the
721 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000722 // C++ 3.3.2p4:
723 // Names declared in the for-init-statement, and in the condition of if,
724 // while, for, and switch statements are local to the if, while, for, or
725 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000726 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000727 unsigned ScopeFlags = Scope::BreakScope;
728 if (C99orCXX)
729 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000730 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000731
Chris Lattner9075bd72006-08-10 04:59:57 +0000732 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000733 OwningExprResult Cond(Actions);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000734 DeclPtrTy CondVar;
735 if (ParseParenExprOrCondition(Cond, CondVar))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000736 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000737
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000738 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000739
Douglas Gregor3ff3af42009-11-25 06:20:02 +0000740 OwningStmtResult Switch = Actions.ActOnStartOfSwitchStmt(FullCond, CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000741
Chris Lattner8fb26252007-08-22 05:28:50 +0000742 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000743 // there is no compound stmt. C90 does not have this clause. We only do this
744 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000745 //
746 // C++ 6.4p1:
747 // The substatement in a selection-statement (each substatement, in the else
748 // form of the if statement) implicitly defines a local scope.
749 //
750 // See comments in ParseIfStatement for why we create a scope for the
751 // condition and a new scope for substatement in C++.
752 //
Mike Stump11289f42009-09-09 15:08:12 +0000753 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000754 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000755
Chris Lattner9075bd72006-08-10 04:59:57 +0000756 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000757 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000758
Chris Lattner8fd2d012010-01-24 01:50:29 +0000759 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000760 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000761 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000762
Chris Lattner8fd2d012010-01-24 01:50:29 +0000763 if (Cond.isInvalid() && !CondVar.get()) {
764 Actions.ActOnSwitchBodyError(SwitchLoc, move(Switch), move(Body));
Chris Lattnerc0081db2008-12-12 06:31:07 +0000765 return StmtError();
Chris Lattner8fd2d012010-01-24 01:50:29 +0000766 }
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000767
Chris Lattner8fd2d012010-01-24 01:50:29 +0000768 if (Body.isInvalid())
769 // FIXME: Remove the case statement list from the Switch statement.
770 Body = Actions.ActOnNullStmt(Tok.getLocation());
771
Sebastian Redl726a0d92009-02-05 15:02:23 +0000772 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000773}
774
775/// ParseWhileStatement
776/// while-statement: [C99 6.8.5.1]
777/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000778/// [C++] 'while' '(' condition ')' statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000779Parser::OwningStmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
780 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000781 delete Attr;
782
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000783 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000784 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000785 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000786
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000787 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000788 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000789 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000790 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000791 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000792
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000793 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
794
Chris Lattner2dd1b722007-08-26 23:08:06 +0000795 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
796 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000797 //
798 // C++ 6.4p3:
799 // A name introduced by a declaration in a condition is in scope from its
800 // point of declaration until the end of the substatements controlled by the
801 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000802 // C++ 3.3.2p4:
803 // Names declared in the for-init-statement, and in the condition of if,
804 // while, for, and switch statements are local to the if, while, for, or
805 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000806 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000807 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000808 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000809 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
810 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000811 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000812 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
813 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000814
Chris Lattner9075bd72006-08-10 04:59:57 +0000815 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000816 OwningExprResult Cond(Actions);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000817 DeclPtrTy CondVar;
818 if (ParseParenExprOrCondition(Cond, CondVar))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000819 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000820
Anders Carlssonafb2dad2009-12-16 02:09:40 +0000821 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner8fb26252007-08-22 05:28:50 +0000823 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000824 // there is no compound stmt. C90 does not have this clause. We only do this
825 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000826 //
827 // C++ 6.5p2:
828 // The substatement in an iteration-statement implicitly defines a local scope
829 // which is entered and exited each time through the loop.
830 //
831 // See comments in ParseIfStatement for why we create a scope for the
832 // condition and a new scope for substatement in C++.
833 //
Mike Stump11289f42009-09-09 15:08:12 +0000834 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000835 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000836
Chris Lattner9075bd72006-08-10 04:59:57 +0000837 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000838 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000839
Chris Lattner8fb26252007-08-22 05:28:50 +0000840 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000841 InnerScope.Exit();
842 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000843
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000844 if ((Cond.isInvalid() && !CondVar.get()) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +0000845 return StmtError();
846
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000847 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000848}
849
850/// ParseDoStatement
851/// do-statement: [C99 6.8.5.2]
852/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000853/// Note: this lets the caller parse the end ';'.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000854Parser::OwningStmtResult Parser::ParseDoStatement(AttributeList *Attr) {
855 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000856 delete Attr;
857
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000858 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000859 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000860
Chris Lattner2dd1b722007-08-26 23:08:06 +0000861 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
862 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000863 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000864 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000865 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000866 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000867 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000868
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000869 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000870
Chris Lattner8fb26252007-08-22 05:28:50 +0000871 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000872 // there is no compound stmt. C90 does not have this clause. We only do this
873 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000874 //
875 // C++ 6.5p2:
876 // The substatement in an iteration-statement implicitly defines a local scope
877 // which is entered and exited each time through the loop.
878 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000879 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000880 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000881 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000882
Chris Lattner9075bd72006-08-10 04:59:57 +0000883 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000884 OwningStmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000885
Chris Lattner8fb26252007-08-22 05:28:50 +0000886 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000887 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000888
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000889 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000890 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000891 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000892 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000893 SkipUntil(tok::semi, false, true);
894 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000895 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000896 }
Chris Lattneraf635312006-10-16 06:06:51 +0000897 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000898
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000899 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000900 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000901 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000902 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000903 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000904
Chris Lattner10da53c2008-12-12 06:35:28 +0000905 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +0000906 SourceLocation LPLoc = ConsumeParen();
907 OwningExprResult Cond = ParseExpression();
908 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000909 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000910
Sebastian Redlb62406f2008-12-11 19:48:14 +0000911 if (Cond.isInvalid() || Body.isInvalid())
912 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000913
Chris Lattner815b70e2009-06-12 23:04:47 +0000914 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
915 move(Cond), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000916}
917
918/// ParseForStatement
919/// for-statement: [C99 6.8.5.3]
920/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
921/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000922/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
923/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000924/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
925/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000926///
927/// [C++] for-init-statement:
928/// [C++] expression-statement
929/// [C++] simple-declaration
930///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000931Parser::OwningStmtResult Parser::ParseForStatement(AttributeList *Attr) {
932 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000933 delete Attr;
934
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000935 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000936 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000937
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000938 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000939 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000940 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000941 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000942 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000943
Chris Lattner934074c2009-04-22 00:54:41 +0000944 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000945
Chris Lattner2dd1b722007-08-26 23:08:06 +0000946 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
947 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000948 //
949 // C++ 6.4p3:
950 // A name introduced by a declaration in a condition is in scope from its
951 // point of declaration until the end of the substatements controlled by the
952 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000953 // C++ 3.3.2p4:
954 // Names declared in the for-init-statement, and in the condition of if,
955 // while, for, and switch statements are local to the if, while, for, or
956 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000957 // C++ 6.5.3p1:
958 // Names declared in the for-init-statement are in the same declarative-region
959 // as those declared in the condition.
960 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000961 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000962 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000963 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
964 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000965 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000966 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
967
968 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000969
Chris Lattner04132372006-10-16 06:12:55 +0000970 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlc13f2682008-12-09 20:22:58 +0000971 OwningExprResult Value(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000972
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000973 bool ForEach = false;
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000974 OwningStmtResult FirstPart(Actions);
975 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000976 DeclPtrTy SecondVar;
977
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000978 if (Tok.is(tok::code_completion)) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000979 Actions.CodeCompleteOrdinaryName(CurScope,
980 C99orCXXorObjC? Action::CCC_ForInit
981 : Action::CCC_Expression);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000982 ConsumeToken();
983 }
984
Chris Lattner9075bd72006-08-10 04:59:57 +0000985 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000986 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000987 // no first part, eat the ';'.
988 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +0000989 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000990 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +0000991 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +0000992 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000993
Alexis Hunt96d5c762009-11-21 08:43:09 +0000994 AttributeList *AttrList = 0;
995 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
996 AttrList = ParseCXX0XAttributes().AttrList;
997
Chris Lattner49836b42009-04-02 04:16:50 +0000998 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000999 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +00001000 AttrList, false);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001001 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001002
Chris Lattner32dc41c2009-03-29 17:27:48 +00001003 if (Tok.is(tok::semi)) { // for (int x = 4;
1004 ConsumeToken();
1005 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001006 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001007 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001008 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001009 SecondPart = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001010 } else {
1011 Diag(Tok, diag::err_expected_semi_for);
1012 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001013 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001014 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001015 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001016
Chris Lattnercd68f642007-06-27 01:06:29 +00001017 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001018 if (!Value.isInvalid())
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001019 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001020
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001021 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001022 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001023 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001024 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001025 SecondPart = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001026 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001027 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +00001028 SkipUntil(tok::semi);
1029 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001030 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +00001031 if (!ForEach) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001032 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001033 // Parse the second part of the for specifier.
1034 if (Tok.is(tok::semi)) { // for (...;;
1035 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001036 } else {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001037 if (getLang().CPlusPlus)
1038 ParseCXXCondition(SecondPart, SecondVar);
1039 else
1040 SecondPart = ParseExpression();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001041 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001042
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001043 if (Tok.is(tok::semi)) {
1044 ConsumeToken();
1045 } else {
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001046 if (!SecondPart.isInvalid() || SecondVar.get())
1047 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001048 SkipUntil(tok::semi);
1049 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001050
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001051 // Parse the third part of the for specifier.
Chris Lattnerbe36eb02009-03-29 17:29:28 +00001052 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlfbfaafc2009-01-16 23:28:06 +00001053 ThirdPart = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +00001054 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001055 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001056 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001057
Chris Lattner8fb26252007-08-22 05:28:50 +00001058 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001059 // there is no compound stmt. C90 does not have this clause. We only do this
1060 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001061 //
1062 // C++ 6.5p2:
1063 // The substatement in an iteration-statement implicitly defines a local scope
1064 // which is entered and exited each time through the loop.
1065 //
1066 // See comments in ParseIfStatement for why we create a scope for
1067 // for-init-statement/condition and a new scope for substatement in C++.
1068 //
Mike Stump11289f42009-09-09 15:08:12 +00001069 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001070 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001071
Chris Lattner9075bd72006-08-10 04:59:57 +00001072 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +00001073 OwningStmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001074
Chris Lattner8fb26252007-08-22 05:28:50 +00001075 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001076 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001077
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001078 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001079 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001080
1081 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001082 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001083
1084 if (!ForEach)
Sebastian Redl726a0d92009-02-05 15:02:23 +00001085 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001086 Actions.MakeFullExpr(SecondPart), SecondVar,
1087 Actions.MakeFullExpr(ThirdPart), RParenLoc,
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001088 move(Body));
Mike Stump11289f42009-09-09 15:08:12 +00001089
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001090 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1091 move(FirstPart),
1092 move(SecondPart),
1093 RParenLoc, move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +00001094}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001095
Chris Lattner503fadc2006-08-10 05:45:44 +00001096/// ParseGotoStatement
1097/// jump-statement:
1098/// 'goto' identifier ';'
1099/// [GNU] 'goto' '*' expression ';'
1100///
1101/// Note: this lets the caller parse the end ';'.
1102///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001103Parser::OwningStmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
1104 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001105 delete Attr;
1106
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001107 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001108 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001109
Sebastian Redlc13f2682008-12-09 20:22:58 +00001110 OwningStmtResult Res(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001111 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001112 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001113 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001114 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001115 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001116 // GNU indirect goto extension.
1117 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001118 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001119 OwningExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001120 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001121 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001122 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001123 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001124 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattnere34b2c22007-07-22 04:13:33 +00001125 } else {
1126 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001127 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001128 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001129
Sebastian Redlb62406f2008-12-11 19:48:14 +00001130 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001131}
1132
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001133/// ParseContinueStatement
1134/// jump-statement:
1135/// 'continue' ';'
1136///
1137/// Note: this lets the caller parse the end ';'.
1138///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001139Parser::OwningStmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
1140 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001141 delete Attr;
1142
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001143 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl573feed2009-01-18 13:19:59 +00001144 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001145}
1146
1147/// ParseBreakStatement
1148/// jump-statement:
1149/// 'break' ';'
1150///
1151/// Note: this lets the caller parse the end ';'.
1152///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001153Parser::OwningStmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
1154 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001155 delete Attr;
1156
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001157 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl573feed2009-01-18 13:19:59 +00001158 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001159}
1160
Chris Lattner503fadc2006-08-10 05:45:44 +00001161/// ParseReturnStatement
1162/// jump-statement:
1163/// 'return' expression[opt] ';'
Alexis Hunt96d5c762009-11-21 08:43:09 +00001164Parser::OwningStmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
1165 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001166 delete Attr;
1167
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001168 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001169 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001170
Sebastian Redlc13f2682008-12-09 20:22:58 +00001171 OwningExprResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001172 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +00001173 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001174 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001175 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001176 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001177 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001178 }
Anders Carlssona1929472009-08-18 16:11:00 +00001179 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Chris Lattner503fadc2006-08-10 05:45:44 +00001180}
Chris Lattner0116c472006-08-15 06:03:28 +00001181
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001182/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1183/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001184Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001185 if (Tok.is(tok::l_brace)) {
1186 unsigned short savedBraceCount = BraceCount;
1187 do {
1188 ConsumeAnyToken();
1189 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001190 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001191 // From the MS website: If used without braces, the __asm keyword means
1192 // that the rest of the line is an assembly-language statement.
1193 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001194 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001195 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001196 do {
1197 ConsumeAnyToken();
1198 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001199 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1200 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001201 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001202 }
Mike Stump6da5d752009-12-11 00:04:56 +00001203 Token t;
1204 t.setKind(tok::string_literal);
1205 t.setLiteralData("\"FIXME: not done\"");
1206 t.clearFlag(Token::NeedsCleaning);
1207 t.setLength(17);
1208 OwningExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1209 ExprVector Constraints(Actions);
1210 ExprVector Exprs(Actions);
1211 ExprVector Clobbers(Actions);
Anders Carlsson9a020f92010-01-30 22:25:16 +00001212 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001213 move_arg(Constraints), move_arg(Exprs),
1214 move(AsmString), move_arg(Clobbers),
Mike Stump90be58a2010-01-04 22:37:17 +00001215 Tok.getLocation(), true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001216}
1217
Chris Lattner0116c472006-08-15 06:03:28 +00001218/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001219/// asm-statement:
1220/// gnu-asm-statement
1221/// ms-asm-statement
1222///
1223/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001224/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1225///
1226/// [GNU] asm-argument:
1227/// asm-string-literal
1228/// asm-string-literal ':' asm-operands[opt]
1229/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1230/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1231/// ':' asm-clobbers
1232///
1233/// [GNU] asm-clobbers:
1234/// asm-string-literal
1235/// asm-clobbers ',' asm-string-literal
1236///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001237/// [MS] ms-asm-statement:
1238/// '__asm' assembly-instruction ';'[opt]
1239/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1240///
1241/// [MS] assembly-instruction-list:
1242/// assembly-instruction ';'[opt]
1243/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1244///
Sebastian Redlb62406f2008-12-11 19:48:14 +00001245Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001246 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001247 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001248
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001249 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001250 msAsm = true;
1251 return FuzzyParseMicrosoftAsmStatement();
1252 }
Chris Lattner0116c472006-08-15 06:03:28 +00001253 DeclSpec DS;
1254 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001255 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001256
Chris Lattner0116c472006-08-15 06:03:28 +00001257 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001258 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001259 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001260 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001261 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001262
Chris Lattner0116c472006-08-15 06:03:28 +00001263 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001264 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001265 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001266 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001267 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001268 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001269 }
Chris Lattner04132372006-10-16 06:12:55 +00001270 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001271
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001272 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001273 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001274 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001275
Anders Carlsson9a020f92010-01-30 22:25:16 +00001276 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001277 ExprVector Constraints(Actions);
1278 ExprVector Exprs(Actions);
1279 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001280
Anders Carlsson19fe1162008-02-05 23:03:50 +00001281 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001282 // We have a simple asm expression like 'asm("foo")'.
1283 SourceLocation RParenLoc = ConsumeParen();
1284 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1285 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1286 move_arg(Constraints), move_arg(Exprs),
1287 move(AsmString), move_arg(Clobbers),
1288 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001289 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001290
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001291 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001292 bool AteExtraColon = false;
1293 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1294 // In C++ mode, parse "::" like ": :".
1295 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001296 ConsumeToken();
1297
Chris Lattner15768502009-12-20 23:08:04 +00001298 if (!AteExtraColon &&
1299 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001300 return StmtError();
1301 }
Chris Lattner15768502009-12-20 23:08:04 +00001302
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001303 unsigned NumOutputs = Names.size();
1304
1305 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001306 if (AteExtraColon ||
1307 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1308 // In C++ mode, parse "::" like ": :".
1309 if (AteExtraColon)
1310 AteExtraColon = false;
1311 else {
1312 AteExtraColon = Tok.is(tok::coloncolon);
1313 ConsumeToken();
1314 }
1315
1316 if (!AteExtraColon &&
1317 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001318 return StmtError();
1319 }
1320
1321 assert(Names.size() == Constraints.size() &&
1322 Constraints.size() == Exprs.size() &&
1323 "Input operand size mismatch!");
1324
1325 unsigned NumInputs = Names.size() - NumOutputs;
1326
1327 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001328 if (AteExtraColon || Tok.is(tok::colon)) {
1329 if (!AteExtraColon)
1330 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001331
1332 // Parse the asm-string list for clobbers.
1333 while (1) {
1334 OwningExprResult Clobber(ParseAsmStringLiteral());
1335
1336 if (Clobber.isInvalid())
1337 break;
1338
1339 Clobbers.push_back(Clobber.release());
1340
1341 if (Tok.isNot(tok::comma)) break;
1342 ConsumeToken();
1343 }
1344 }
1345
1346 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1347 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001348 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001349 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001350 move(AsmString), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001351 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001352}
1353
1354/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001355/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001356///
1357/// [GNU] asm-operands:
1358/// asm-operand
1359/// asm-operands ',' asm-operand
1360///
1361/// [GNU] asm-operand:
1362/// asm-string-literal '(' expression ')'
1363/// '[' identifier ']' asm-string-literal '(' expression ')'
1364///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001365//
1366// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001367bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1368 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1369 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001370 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001371 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001372 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001373
1374 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001375 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001376 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001377 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001379 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001380 Diag(Tok, diag::err_expected_ident);
1381 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001382 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001383 }
Mike Stump11289f42009-09-09 15:08:12 +00001384
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001385 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001386 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001387
Anders Carlsson9a020f92010-01-30 22:25:16 +00001388 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001389 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001390 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001391 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001392
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001393 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001394 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001395 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001396 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001397 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001398 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001399
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001400 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001401 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001402 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001403 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001404 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001405
Chris Lattner0116c472006-08-15 06:03:28 +00001406 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001407 SourceLocation OpenLoc = ConsumeParen();
1408 OwningExprResult Res(ParseExpression());
1409 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001410 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001411 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001412 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001413 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001414 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001415 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001416 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001417 ConsumeToken();
1418 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001419
1420 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001421}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001422
Chris Lattner83f095c2009-03-28 19:18:32 +00001423Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001424 assert(Tok.is(tok::l_brace));
1425 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001426
Chris Lattnereae6cb62009-03-05 08:00:35 +00001427 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1428 PP.getSourceManager(),
1429 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001430
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001431 // Do not enter a scope for the brace, as the arguments are in the same scope
1432 // (the function body) as the body itself. Instead, just read the statement
1433 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl042ad952008-12-11 19:30:53 +00001434 OwningStmtResult FnBody(ParseCompoundStatementBody());
1435
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001436 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001437 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001438 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001439 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001440
Sebastian Redl726a0d92009-02-05 15:02:23 +00001441 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001442}
Sebastian Redlb219c902008-12-21 16:41:36 +00001443
Sebastian Redla7b98a72009-04-26 20:35:05 +00001444/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1445///
1446/// function-try-block:
1447/// 'try' ctor-initializer[opt] compound-statement handler-seq
1448///
1449Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1450 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1451 SourceLocation TryLoc = ConsumeToken();
1452
1453 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1454 PP.getSourceManager(),
1455 "parsing function try block");
1456
1457 // Constructor initializer list?
1458 if (Tok.is(tok::colon))
1459 ParseConstructorInitializer(Decl);
1460
Sebastian Redld98ecd62009-04-26 21:08:36 +00001461 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001462 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1463 // If we failed to parse the try-catch, we just give the function an empty
1464 // compound statement as the body.
1465 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001466 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001467 MultiStmtArg(Actions), false);
1468
1469 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1470}
1471
Sebastian Redlb219c902008-12-21 16:41:36 +00001472/// ParseCXXTryBlock - Parse a C++ try-block.
1473///
1474/// try-block:
1475/// 'try' compound-statement handler-seq
1476///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001477Parser::OwningStmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
1478 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001479 delete Attr;
1480
Sebastian Redlb219c902008-12-21 16:41:36 +00001481 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1482
1483 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001484 return ParseCXXTryBlockCommon(TryLoc);
1485}
1486
1487/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1488/// function-try-block.
1489///
1490/// try-block:
1491/// 'try' compound-statement handler-seq
1492///
1493/// function-try-block:
1494/// 'try' ctor-initializer[opt] compound-statement handler-seq
1495///
1496/// handler-seq:
1497/// handler handler-seq[opt]
1498///
1499Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001500 if (Tok.isNot(tok::l_brace))
1501 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001502 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1503 OwningStmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001504 if (TryBlock.isInvalid())
1505 return move(TryBlock);
1506
1507 StmtVector Handlers(Actions);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001508 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1509 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1510 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1511 << Attr.Range;
1512 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001513 if (Tok.isNot(tok::kw_catch))
1514 return StmtError(Diag(Tok, diag::err_expected_catch));
1515 while (Tok.is(tok::kw_catch)) {
1516 OwningStmtResult Handler(ParseCXXCatchBlock());
1517 if (!Handler.isInvalid())
1518 Handlers.push_back(Handler.release());
1519 }
1520 // Don't bother creating the full statement if we don't have any usable
1521 // handlers.
1522 if (Handlers.empty())
1523 return StmtError();
1524
Sebastian Redl726a0d92009-02-05 15:02:23 +00001525 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001526}
1527
1528/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1529///
1530/// handler:
1531/// 'catch' '(' exception-declaration ')' compound-statement
1532///
1533/// exception-declaration:
1534/// type-specifier-seq declarator
1535/// type-specifier-seq abstract-declarator
1536/// type-specifier-seq
1537/// '...'
1538///
1539Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1540 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1541
1542 SourceLocation CatchLoc = ConsumeToken();
1543
1544 SourceLocation LParenLoc = Tok.getLocation();
1545 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1546 return StmtError();
1547
1548 // C++ 3.3.2p3:
1549 // The name in a catch exception-declaration is local to the handler and
1550 // shall not be redeclared in the outermost block of the handler.
1551 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1552
1553 // exception-declaration is equivalent to '...' or a parameter-declaration
1554 // without default arguments.
Chris Lattner83f095c2009-03-28 19:18:32 +00001555 DeclPtrTy ExceptionDecl;
Sebastian Redlb219c902008-12-21 16:41:36 +00001556 if (Tok.isNot(tok::ellipsis)) {
1557 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001558 if (ParseCXXTypeSpecifierSeq(DS))
1559 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001560 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1561 ParseDeclarator(ExDecl);
1562 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1563 } else
1564 ConsumeToken();
1565
1566 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1567 return StmtError();
1568
1569 if (Tok.isNot(tok::l_brace))
1570 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1571
Alexis Hunt96d5c762009-11-21 08:43:09 +00001572 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1573 OwningStmtResult Block(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001574 if (Block.isInvalid())
1575 return move(Block);
1576
Sebastian Redl726a0d92009-02-05 15:02:23 +00001577 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redlb219c902008-12-21 16:41:36 +00001578}