blob: 66e61dd949f68c562aff05b81c1f148a80a039a3 [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 Lattner1ff6e732008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.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();
84
Chris Lattner503fadc2006-08-10 05:45:44 +000085 // Cases in this switch statement should fall through if the parser expects
86 // the token to end in a semicolon (in which case SemiError should be set),
87 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000088 tok::TokenKind Kind = Tok.getKind();
89 SourceLocation AtLoc;
90 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000091 case tok::at: // May be a @try or @throw statement
92 {
93 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000094 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000095 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000096
Douglas Gregor9d64c5e2009-09-21 20:51:25 +000097 case tok::code_completion:
98 Actions.CodeCompleteOrdinaryName(CurScope);
99 ConsumeToken();
100 return ParseStatementOrDeclaration(OnlyStatement);
101
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000102 case tok::identifier:
103 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
104 // identifier ':' statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000105 return ParseLabeledStatement(Attr.AttrList);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000106 }
107 // PASS THROUGH.
108
Chris Lattner803802d2009-03-24 17:04:48 +0000109 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000110 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000111 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000112 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd,
113 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000114 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000115 }
116
117 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000118 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000119 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000120 }
Mike Stump11289f42009-09-09 15:08:12 +0000121
Alexis Hunt96d5c762009-11-21 08:43:09 +0000122 // FIXME: Use the attributes
Chris Lattner803802d2009-03-24 17:04:48 +0000123 // expression[opt] ';'
124 OwningExprResult Expr(ParseExpression());
125 if (Expr.isInvalid()) {
126 // If the expression is invalid, skip ahead to the next semicolon. Not
127 // doing this opens us up to the possibility of infinite loops if
128 // ParseExpression does not consume any tokens.
129 SkipUntil(tok::semi);
130 return StmtError();
131 }
132 // Otherwise, eat the semicolon.
133 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +0000134 return Actions.ActOnExprStmt(Actions.FullExpr(Expr));
Chris Lattner803802d2009-03-24 17:04:48 +0000135 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000136
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000137 case tok::kw_case: // C99 6.8.1: labeled-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000138 return ParseCaseStatement(Attr.AttrList);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000139 case tok::kw_default: // C99 6.8.1: labeled-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000140 return ParseDefaultStatement(Attr.AttrList);
Sebastian Redl042ad952008-12-11 19:30:53 +0000141
Chris Lattner9075bd72006-08-10 04:59:57 +0000142 case tok::l_brace: // C99 6.8.2: compound-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000143 return ParseCompoundStatement(Attr.AttrList);
Chris Lattner0f203a72007-05-28 01:45:28 +0000144 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000145 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl042ad952008-12-11 19:30:53 +0000146
Chris Lattner9075bd72006-08-10 04:59:57 +0000147 case tok::kw_if: // C99 6.8.4.1: if-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000148 return ParseIfStatement(Attr.AttrList);
Chris Lattner9075bd72006-08-10 04:59:57 +0000149 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000150 return ParseSwitchStatement(Attr.AttrList);
Sebastian Redl042ad952008-12-11 19:30:53 +0000151
Chris Lattner9075bd72006-08-10 04:59:57 +0000152 case tok::kw_while: // C99 6.8.5.1: while-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000153 return ParseWhileStatement(Attr.AttrList);
Chris Lattner9075bd72006-08-10 04:59:57 +0000154 case tok::kw_do: // C99 6.8.5.2: do-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000155 Res = ParseDoStatement(Attr.AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000156 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000157 break;
158 case tok::kw_for: // C99 6.8.5.3: for-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000159 return ParseForStatement(Attr.AttrList);
Chris Lattner503fadc2006-08-10 05:45:44 +0000160
161 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000162 Res = ParseGotoStatement(Attr.AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000163 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000164 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000165 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000166 Res = ParseContinueStatement(Attr.AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000167 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000168 break;
169 case tok::kw_break: // C99 6.8.6.3: break-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000170 Res = ParseBreakStatement(Attr.AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000171 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000172 break;
173 case tok::kw_return: // C99 6.8.6.4: return-statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000174 Res = ParseReturnStatement(Attr.AttrList);
Chris Lattner34a95662009-06-14 00:07:48 +0000175 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000176 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000177
Sebastian Redlb219c902008-12-21 16:41:36 +0000178 case tok::kw_asm: {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000179 if (Attr.HasAttr)
180 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
181 << Attr.Range;
Steve Naroffb2c80c72008-02-07 03:50:06 +0000182 bool msAsm = false;
183 Res = ParseAsmStatement(msAsm);
Sebastian Redl042ad952008-12-11 19:30:53 +0000184 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000185 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000186 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000187 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000188
Sebastian Redlb219c902008-12-21 16:41:36 +0000189 case tok::kw_try: // C++ 15: try-block
Alexis Hunt96d5c762009-11-21 08:43:09 +0000190 return ParseCXXTryBlock(Attr.AttrList);
Sebastian Redlb219c902008-12-21 16:41:36 +0000191 }
192
Chris Lattner503fadc2006-08-10 05:45:44 +0000193 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000194 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000195 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000196 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000197 // If the result was valid, then we do want to diagnose this. Use
198 // ExpectAndConsume to emit the diagnostic, even though we know it won't
199 // succeed.
200 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000201 // Skip until we see a } or ;, but don't eat it.
202 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Sebastian Redl042ad952008-12-11 19:30:53 +0000205 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000206}
207
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000208/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000209///
210/// labeled-statement:
211/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000212/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000213///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000214Parser::OwningStmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000215 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
216 "Not an identifier!");
217
218 Token IdentTok = Tok; // Save the whole token.
219 ConsumeToken(); // eat the identifier.
220
221 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000222
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000223 // identifier ':' statement
224 SourceLocation ColonLoc = ConsumeToken();
225
226 // Read label attributes, if present.
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000227 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000228 Attr = addAttributeLists(Attr, ParseGNUAttributes());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000229
Sebastian Redl042ad952008-12-11 19:30:53 +0000230 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000231
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000232 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000233 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000234 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000235
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000236 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
237 IdentTok.getIdentifierInfo(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000238 ColonLoc, move(SubStmt));
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000239}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000240
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000241/// ParseCaseStatement
242/// labeled-statement:
243/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000244/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000245///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000246Parser::OwningStmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000247 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000248 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000249
Chris Lattner34a22092009-03-04 04:23:07 +0000250 // It is very very common for code to contain many case statements recursively
251 // nested, as in (but usually without indentation):
252 // case 1:
253 // case 2:
254 // case 3:
255 // case 4:
256 // case 5: etc.
257 //
258 // Parsing this naively works, but is both inefficient and can cause us to run
259 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000260 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000261 // but all the grossness is constrained to ParseCaseStatement (and some
262 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000263
Chris Lattner34a22092009-03-04 04:23:07 +0000264 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
265 // example above.
266 OwningStmtResult TopLevelCase(Actions, true);
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattner34a22092009-03-04 04:23:07 +0000268 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
269 // gets updated each time a new case is parsed, and whose body is unset so
270 // far. When parsing 'case 4', this is the 'case 3' node.
271 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner34a22092009-03-04 04:23:07 +0000273 // While we have case statements, eat and stack them.
274 do {
275 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000276
Douglas Gregord328d572009-09-21 18:10:23 +0000277 if (Tok.is(tok::code_completion)) {
278 Actions.CodeCompleteCase(CurScope);
279 ConsumeToken();
280 }
281
Chris Lattner34a22092009-03-04 04:23:07 +0000282 OwningExprResult LHS(ParseConstantExpression());
283 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000284 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000285 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000286 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000287
Chris Lattner34a22092009-03-04 04:23:07 +0000288 // GNU case range extension.
289 SourceLocation DotDotDotLoc;
290 OwningExprResult RHS(Actions);
291 if (Tok.is(tok::ellipsis)) {
292 Diag(Tok, diag::ext_gnu_case_range);
293 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000294
Chris Lattner34a22092009-03-04 04:23:07 +0000295 RHS = ParseConstantExpression();
296 if (RHS.isInvalid()) {
297 SkipUntil(tok::colon);
298 return StmtError();
299 }
300 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000301
Chris Lattner34a22092009-03-04 04:23:07 +0000302 if (Tok.isNot(tok::colon)) {
303 Diag(Tok, diag::err_expected_colon_after) << "'case'";
304 SkipUntil(tok::colon);
305 return StmtError();
306 }
307
308 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattner34a22092009-03-04 04:23:07 +0000310 OwningStmtResult Case =
311 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
312 move(RHS), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000313
Chris Lattner34a22092009-03-04 04:23:07 +0000314 // If we had a sema error parsing this case, then just ignore it and
315 // continue parsing the sub-stmt.
316 if (Case.isInvalid()) {
317 if (TopLevelCase.isInvalid()) // No parsed case stmts.
318 return ParseStatement();
319 // Otherwise, just don't add it as a nested case.
320 } else {
321 // If this is the first case statement we parsed, it becomes TopLevelCase.
322 // Otherwise we link it into the current chain.
323 StmtTy *NextDeepest = Case.get();
324 if (TopLevelCase.isInvalid())
325 TopLevelCase = move(Case);
326 else
327 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
328 DeepestParsedCaseStmt = NextDeepest;
329 }
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chris Lattner34a22092009-03-04 04:23:07 +0000331 // Handle all case statements.
332 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000333
Chris Lattner34a22092009-03-04 04:23:07 +0000334 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000335
Chris Lattner34a22092009-03-04 04:23:07 +0000336 // If we found a non-case statement, start by parsing it.
337 OwningStmtResult SubStmt(Actions);
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattner34a22092009-03-04 04:23:07 +0000339 if (Tok.isNot(tok::r_brace)) {
340 SubStmt = ParseStatement();
341 } else {
342 // Nicely diagnose the common error "switch (X) { case 4: }", which is
343 // not valid.
344 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000345 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000346 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000347 }
Mike Stump11289f42009-09-09 15:08:12 +0000348
Chris Lattner34a22092009-03-04 04:23:07 +0000349 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000350 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000351 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner34a22092009-03-04 04:23:07 +0000353 // Install the body into the most deeply-nested case.
354 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl042ad952008-12-11 19:30:53 +0000355
Chris Lattner34a22092009-03-04 04:23:07 +0000356 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000357 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000358}
359
360/// ParseDefaultStatement
361/// labeled-statement:
362/// 'default' ':' statement
363/// Note that this does not parse the 'statement' at the end.
364///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000365Parser::OwningStmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
366 //FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000367 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000368 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000369
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000370 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000371 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000372 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000373 return StmtError();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000374 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000375
Chris Lattneraf635312006-10-16 06:06:51 +0000376 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000377
Chris Lattner30f910e2006-10-16 05:52:41 +0000378 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000379 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000380 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000381 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000382 }
383
Sebastian Redl042ad952008-12-11 19:30:53 +0000384 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000385 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000386 return StmtError();
387
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000388 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +0000389 move(SubStmt), CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000390}
391
392
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000393/// ParseCompoundStatement - Parse a "{}" block.
394///
395/// compound-statement: [C99 6.8.2]
396/// { block-item-list[opt] }
397/// [GNU] { label-declarations block-item-list } [TODO]
398///
399/// block-item-list:
400/// block-item
401/// block-item-list block-item
402///
403/// block-item:
404/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000405/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000406/// statement
407/// [OMP] openmp-directive [TODO]
408///
409/// [GNU] label-declarations:
410/// [GNU] label-declaration
411/// [GNU] label-declarations label-declaration
412///
413/// [GNU] label-declaration:
414/// [GNU] '__label__' identifier-list ';'
415///
416/// [OMP] openmp-directive: [TODO]
417/// [OMP] barrier-directive
418/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000419///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000420Parser::OwningStmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
421 bool isStmtExpr) {
422 //FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000423 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000424
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000425 // Enter a scope to hold everything within the compound stmt. Compound
426 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000427 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000428
429 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000430 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000431}
432
433
434/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000435/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000436/// consume the '}' at the end of the block. It does not manipulate the scope
437/// stack.
Sebastian Redl042ad952008-12-11 19:30:53 +0000438Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000439 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000440 Tok.getLocation(),
441 "in compound statement ('{}')");
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattnerf2978802007-01-21 06:52:16 +0000443 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
444
Chris Lattner010015a2007-05-28 07:23:54 +0000445 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000446 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000447
448 typedef StmtVector StmtsTy;
449 StmtsTy Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000450 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redlc13f2682008-12-09 20:22:58 +0000451 OwningStmtResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000452 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000453 R = ParseStatementOrDeclaration(false);
454 } else {
455 // __extension__ can start declarations and it can also be a unary
456 // operator for expressions. Consume multiple __extension__ markers here
457 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000458 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000459 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000460 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000461 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000462
Alexis Hunt96d5c762009-11-21 08:43:09 +0000463 CXX0XAttributeList Attr;
464 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
465 Attr = ParseCXX0XAttributes();
466
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000467 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000468 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000469 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000470 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000471 ExtensionRAIIObject O(Diags);
472
Chris Lattner49836b42009-04-02 04:16:50 +0000473 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000474 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
475 Attr);
Chris Lattner49836b42009-04-02 04:16:50 +0000476 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000477 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000478 // Otherwise this was a unary __extension__ marker.
479 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000480
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000481 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000482 SkipUntil(tok::semi);
483 continue;
484 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000485
Alexis Hunt96d5c762009-11-21 08:43:09 +0000486 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000487 // Eat the semicolon at the end of stmt and convert the expr into a
488 // statement.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000489 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +0000490 R = Actions.ActOnExprStmt(Actions.FullExpr(Res));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000491 }
492 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000493
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000494 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000495 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000496 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000497
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000498 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000499 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000500 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl042ad952008-12-11 19:30:53 +0000501 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000502 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000503
Chris Lattner04132372006-10-16 06:12:55 +0000504 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000505 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000506 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000507}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000508
Chris Lattnerc0081db2008-12-12 06:31:07 +0000509/// ParseParenExprOrCondition:
510/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000511/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000512///
513/// This function parses and performs error recovery on the specified condition
514/// or expression (depending on whether we're in C++ or C mode). This function
515/// goes out of its way to recover well. It returns true if there was a parser
516/// error (the right paren couldn't be found), which indicates that the caller
517/// should try to recover harder. It returns false if the condition is
518/// successfully parsed. Note that a successful parse can still have semantic
519/// errors in the condition.
Chris Lattner10da53c2008-12-12 06:35:28 +0000520bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
Chris Lattner815b70e2009-06-12 23:04:47 +0000521 bool OnlyAllowCondition,
522 SourceLocation *LParenLocPtr,
523 SourceLocation *RParenLocPtr) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000524 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner815b70e2009-06-12 23:04:47 +0000525 if (LParenLocPtr) *LParenLocPtr = LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattnerc0081db2008-12-12 06:31:07 +0000527 if (getLang().CPlusPlus)
528 CondExp = ParseCXXCondition();
529 else
530 CondExp = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000531
Chris Lattnerc0081db2008-12-12 06:31:07 +0000532 // If the parser was confused by the condition and we don't have a ')', try to
533 // recover by skipping ahead to a semi and bailing out. If condexp is
534 // semantically invalid but we have well formed code, keep going.
535 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
536 SkipUntil(tok::semi);
537 // Skipping may have stopped if it found the containing ')'. If so, we can
538 // continue parsing the if statement.
539 if (Tok.isNot(tok::r_paren))
540 return true;
541 }
Mike Stump11289f42009-09-09 15:08:12 +0000542
Chris Lattnerc0081db2008-12-12 06:31:07 +0000543 // Otherwise the condition is valid or the rparen is present.
Chris Lattner815b70e2009-06-12 23:04:47 +0000544 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
545 if (RParenLocPtr) *RParenLocPtr = RPLoc;
Chris Lattnerc0081db2008-12-12 06:31:07 +0000546 return false;
547}
548
549
Chris Lattnerc951dae2006-08-10 04:23:57 +0000550/// ParseIfStatement
551/// if-statement: [C99 6.8.4.1]
552/// 'if' '(' expression ')' statement
553/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000554/// [C++] 'if' '(' condition ')' statement
555/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000556///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000557Parser::OwningStmtResult Parser::ParseIfStatement(AttributeList *Attr) {
558 // FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000559 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000560 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000561
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000562 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000563 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000564 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000565 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000566 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000567
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000568 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
569
Chris Lattner2dd1b722007-08-26 23:08:06 +0000570 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
571 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000572 //
573 // C++ 6.4p3:
574 // A name introduced by a declaration in a condition is in scope from its
575 // point of declaration until the end of the substatements controlled by the
576 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000577 // C++ 3.3.2p4:
578 // Names declared in the for-init-statement, and in the condition of if,
579 // while, for, and switch statements are local to the if, while, for, or
580 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000581 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000582 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000583
Chris Lattnerc951dae2006-08-10 04:23:57 +0000584 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000585 OwningExprResult CondExp(Actions);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000586 if (ParseParenExprOrCondition(CondExp))
587 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000588
Anders Carlsson89360092009-06-04 02:18:15 +0000589 FullExprArg FullCondExp(Actions.FullExpr(CondExp));
Mike Stump11289f42009-09-09 15:08:12 +0000590
Chris Lattner8fb26252007-08-22 05:28:50 +0000591 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000592 // there is no compound stmt. C90 does not have this clause. We only do this
593 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000594 //
595 // C++ 6.4p1:
596 // The substatement in a selection-statement (each substatement, in the else
597 // form of the if statement) implicitly defines a local scope.
598 //
599 // For C++ we create a scope for the condition and a new scope for
600 // substatements because:
601 // -When the 'then' scope exits, we want the condition declaration to still be
602 // active for the 'else' scope too.
603 // -Sema will detect name clashes by considering declarations of a
604 // 'ControlScope' as part of its direct subscope.
605 // -If we wanted the condition and substatement to be in the same scope, we
606 // would have to notify ParseStatement not to create a new scope. It's
607 // simpler to let it create a new scope.
608 //
Mike Stump11289f42009-09-09 15:08:12 +0000609 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000610 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000611
Chris Lattner5c5808a2007-10-29 05:08:52 +0000612 // Read the 'then' stmt.
613 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000614 OwningStmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000615
Chris Lattner37e54f42007-08-22 05:16:28 +0000616 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000617 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000618
Chris Lattnerc951dae2006-08-10 04:23:57 +0000619 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000620 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000621 SourceLocation ElseStmtLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +0000622 OwningStmtResult ElseStmt(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000623
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000624 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000625 ElseLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000626
Chris Lattner8fb26252007-08-22 05:28:50 +0000627 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000628 // there is no compound stmt. C90 does not have this clause. We only do
629 // this if the body isn't a compound statement to avoid push/pop in common
630 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000631 //
632 // C++ 6.4p1:
633 // The substatement in a selection-statement (each substatement, in the else
634 // form of the if statement) implicitly defines a local scope.
635 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000636 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000637 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000638
Douglas Gregor85970ca2008-12-10 23:01:14 +0000639 bool WithinElse = CurScope->isWithinElse();
640 CurScope->setWithinElse(true);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000641 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000642 ElseStmt = ParseStatement();
Douglas Gregor85970ca2008-12-10 23:01:14 +0000643 CurScope->setWithinElse(WithinElse);
Chris Lattner37e54f42007-08-22 05:16:28 +0000644
645 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000646 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000647 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000648
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000649 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000650
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000651 // If the condition was invalid, discard the if statement. We could recover
652 // better by replacing it with a valid expr, but don't do that yet.
653 if (CondExp.isInvalid())
654 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000655
Chris Lattner5c5808a2007-10-29 05:08:52 +0000656 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000657 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000658 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000659 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
660 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
661 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000662 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000663 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000664 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000665
Chris Lattner5c5808a2007-10-29 05:08:52 +0000666 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000667 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000668 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000669 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000670 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000671
Mike Stump11289f42009-09-09 15:08:12 +0000672 return Actions.ActOnIfStmt(IfLoc, FullCondExp, move(ThenStmt),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000673 ElseLoc, move(ElseStmt));
Chris Lattnerc951dae2006-08-10 04:23:57 +0000674}
675
Chris Lattner9075bd72006-08-10 04:59:57 +0000676/// ParseSwitchStatement
677/// switch-statement:
678/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000679/// [C++] 'switch' '(' condition ')' statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000680Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
681 // FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000682 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000683 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000684
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000685 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000686 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000687 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000688 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000689 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000690
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000691 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
692
Chris Lattner2dd1b722007-08-26 23:08:06 +0000693 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
694 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000695 //
696 // C++ 6.4p3:
697 // A name introduced by a declaration in a condition is in scope from its
698 // point of declaration until the end of the substatements controlled by the
699 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000700 // C++ 3.3.2p4:
701 // Names declared in the for-init-statement, and in the condition of if,
702 // while, for, and switch statements are local to the if, while, for, or
703 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000704 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000705 unsigned ScopeFlags = Scope::BreakScope;
706 if (C99orCXX)
707 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000708 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000709
Chris Lattner9075bd72006-08-10 04:59:57 +0000710 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000711 OwningExprResult Cond(Actions);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000712 if (ParseParenExprOrCondition(Cond))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000713 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000714
715 OwningStmtResult Switch(Actions);
716 if (!Cond.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000717 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000718
Chris Lattner8fb26252007-08-22 05:28:50 +0000719 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000720 // there is no compound stmt. C90 does not have this clause. We only do this
721 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000722 //
723 // C++ 6.4p1:
724 // The substatement in a selection-statement (each substatement, in the else
725 // form of the if statement) implicitly defines a local scope.
726 //
727 // See comments in ParseIfStatement for why we create a scope for the
728 // condition and a new scope for substatement in C++.
729 //
Mike Stump11289f42009-09-09 15:08:12 +0000730 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000731 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000732
Chris Lattner9075bd72006-08-10 04:59:57 +0000733 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000734 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000735
Chris Lattner8fb26252007-08-22 05:28:50 +0000736 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000737 InnerScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000738
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000739 if (Body.isInvalid()) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000740 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000741 // FIXME: Remove the case statement list from the Switch statement.
742 }
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000743
744 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000745
Chris Lattnerc0081db2008-12-12 06:31:07 +0000746 if (Cond.isInvalid())
747 return StmtError();
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000748
Sebastian Redl726a0d92009-02-05 15:02:23 +0000749 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000750}
751
752/// ParseWhileStatement
753/// while-statement: [C99 6.8.5.1]
754/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000755/// [C++] 'while' '(' condition ')' statement
Alexis Hunt96d5c762009-11-21 08:43:09 +0000756Parser::OwningStmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
757 // FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000758 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000759 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000760 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000761
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000762 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000763 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000764 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000765 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000766 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000767
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000768 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
769
Chris Lattner2dd1b722007-08-26 23:08:06 +0000770 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
771 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000772 //
773 // C++ 6.4p3:
774 // A name introduced by a declaration in a condition is in scope from its
775 // point of declaration until the end of the substatements controlled by the
776 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000777 // C++ 3.3.2p4:
778 // Names declared in the for-init-statement, and in the condition of if,
779 // while, for, and switch statements are local to the if, while, for, or
780 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000781 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000782 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000783 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000784 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
785 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000786 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000787 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
788 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000789
Chris Lattner9075bd72006-08-10 04:59:57 +0000790 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000791 OwningExprResult Cond(Actions);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000792 if (ParseParenExprOrCondition(Cond))
793 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000794
Anders Carlsson89360092009-06-04 02:18:15 +0000795 FullExprArg FullCond(Actions.FullExpr(Cond));
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattner8fb26252007-08-22 05:28:50 +0000797 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000798 // there is no compound stmt. C90 does not have this clause. We only do this
799 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000800 //
801 // C++ 6.5p2:
802 // The substatement in an iteration-statement implicitly defines a local scope
803 // which is entered and exited each time through the loop.
804 //
805 // See comments in ParseIfStatement for why we create a scope for the
806 // condition and a new scope for substatement in C++.
807 //
Mike Stump11289f42009-09-09 15:08:12 +0000808 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000809 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000810
Chris Lattner9075bd72006-08-10 04:59:57 +0000811 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000812 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000813
Chris Lattner8fb26252007-08-22 05:28:50 +0000814 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000815 InnerScope.Exit();
816 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000817
818 if (Cond.isInvalid() || Body.isInvalid())
819 return StmtError();
820
Anders Carlsson89360092009-06-04 02:18:15 +0000821 return Actions.ActOnWhileStmt(WhileLoc, FullCond, move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000822}
823
824/// ParseDoStatement
825/// do-statement: [C99 6.8.5.2]
826/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000827/// Note: this lets the caller parse the end ';'.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000828Parser::OwningStmtResult Parser::ParseDoStatement(AttributeList *Attr) {
829 // FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000830 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000831 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000832
Chris Lattner2dd1b722007-08-26 23:08:06 +0000833 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
834 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000835 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000836 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000837 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000838 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000839 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000840
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000841 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000842
Chris Lattner8fb26252007-08-22 05:28:50 +0000843 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000844 // there is no compound stmt. C90 does not have this clause. We only do this
845 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000846 //
847 // C++ 6.5p2:
848 // The substatement in an iteration-statement implicitly defines a local scope
849 // which is entered and exited each time through the loop.
850 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000851 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000852 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000853 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000854
Chris Lattner9075bd72006-08-10 04:59:57 +0000855 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000856 OwningStmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000857
Chris Lattner8fb26252007-08-22 05:28:50 +0000858 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000859 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000860
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000861 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000862 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000863 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000864 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000865 SkipUntil(tok::semi, false, true);
866 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000867 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000868 }
Chris Lattneraf635312006-10-16 06:06:51 +0000869 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000870
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000871 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000872 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000873 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000874 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000875 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000876
Chris Lattner10da53c2008-12-12 06:35:28 +0000877 // Parse the parenthesized condition.
878 OwningExprResult Cond(Actions);
Chris Lattner815b70e2009-06-12 23:04:47 +0000879 SourceLocation LPLoc, RPLoc;
880 ParseParenExprOrCondition(Cond, true, &LPLoc, &RPLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000881
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000882 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000883
Sebastian Redlb62406f2008-12-11 19:48:14 +0000884 if (Cond.isInvalid() || Body.isInvalid())
885 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000886
Chris Lattner815b70e2009-06-12 23:04:47 +0000887 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
888 move(Cond), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000889}
890
891/// ParseForStatement
892/// for-statement: [C99 6.8.5.3]
893/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
894/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000895/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
896/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000897/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
898/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000899///
900/// [C++] for-init-statement:
901/// [C++] expression-statement
902/// [C++] simple-declaration
903///
Alexis Hunt96d5c762009-11-21 08:43:09 +0000904Parser::OwningStmtResult Parser::ParseForStatement(AttributeList *Attr) {
905 // FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000906 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000907 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000908
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000909 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000910 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000911 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000912 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000913 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000914
Chris Lattner934074c2009-04-22 00:54:41 +0000915 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000916
Chris Lattner2dd1b722007-08-26 23:08:06 +0000917 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
918 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000919 //
920 // C++ 6.4p3:
921 // A name introduced by a declaration in a condition is in scope from its
922 // point of declaration until the end of the substatements controlled by the
923 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000924 // C++ 3.3.2p4:
925 // Names declared in the for-init-statement, and in the condition of if,
926 // while, for, and switch statements are local to the if, while, for, or
927 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000928 // C++ 6.5.3p1:
929 // Names declared in the for-init-statement are in the same declarative-region
930 // as those declared in the condition.
931 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000932 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000933 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000934 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
935 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000936 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000937 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
938
939 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000940
Chris Lattner04132372006-10-16 06:12:55 +0000941 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlc13f2682008-12-09 20:22:58 +0000942 OwningExprResult Value(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000943
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000944 bool ForEach = false;
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000945 OwningStmtResult FirstPart(Actions);
946 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000947
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000948 if (Tok.is(tok::code_completion)) {
949 Actions.CodeCompleteOrdinaryName(CurScope);
950 ConsumeToken();
951 }
952
Chris Lattner9075bd72006-08-10 04:59:57 +0000953 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000954 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000955 // no first part, eat the ';'.
956 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +0000957 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000958 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +0000959 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +0000960 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000961
Alexis Hunt96d5c762009-11-21 08:43:09 +0000962 AttributeList *AttrList = 0;
963 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
964 AttrList = ParseCXX0XAttributes().AttrList;
965
Chris Lattner49836b42009-04-02 04:16:50 +0000966 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000967 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
968 AttrList);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000969 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000970
Chris Lattner32dc41c2009-03-29 17:27:48 +0000971 if (Tok.is(tok::semi)) { // for (int x = 4;
972 ConsumeToken();
973 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +0000974 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +0000975 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000976 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000977 SecondPart = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +0000978 } else {
979 Diag(Tok, diag::err_expected_semi_for);
980 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000981 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000982 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000983 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000984
Chris Lattnercd68f642007-06-27 01:06:29 +0000985 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000986 if (!Value.isInvalid())
Anders Carlsson24824e52009-05-17 21:11:30 +0000987 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000988
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000989 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000990 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000991 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000992 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000993 SecondPart = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000994 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000995 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000996 SkipUntil(tok::semi);
997 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000998 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000999 if (!ForEach) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001000 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001001 // Parse the second part of the for specifier.
1002 if (Tok.is(tok::semi)) { // for (...;;
1003 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001004 } else {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001005 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001006 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001007
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001008 if (Tok.is(tok::semi)) {
1009 ConsumeToken();
1010 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001011 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001012 SkipUntil(tok::semi);
1013 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001014
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001015 // Parse the third part of the for specifier.
Chris Lattnerbe36eb02009-03-29 17:29:28 +00001016 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlfbfaafc2009-01-16 23:28:06 +00001017 ThirdPart = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +00001018 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001019 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001020 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001021
Chris Lattner8fb26252007-08-22 05:28:50 +00001022 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001023 // there is no compound stmt. C90 does not have this clause. We only do this
1024 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001025 //
1026 // C++ 6.5p2:
1027 // The substatement in an iteration-statement implicitly defines a local scope
1028 // which is entered and exited each time through the loop.
1029 //
1030 // See comments in ParseIfStatement for why we create a scope for
1031 // for-init-statement/condition and a new scope for substatement in C++.
1032 //
Mike Stump11289f42009-09-09 15:08:12 +00001033 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001034 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001035
Chris Lattner9075bd72006-08-10 04:59:57 +00001036 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +00001037 OwningStmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001038
Chris Lattner8fb26252007-08-22 05:28:50 +00001039 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001040 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001041
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001042 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001043 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001044
1045 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001046 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001047
1048 if (!ForEach)
Sebastian Redl726a0d92009-02-05 15:02:23 +00001049 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
1050 move(SecondPart), move(ThirdPart),
1051 RParenLoc, move(Body));
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001053 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1054 move(FirstPart),
1055 move(SecondPart),
1056 RParenLoc, move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +00001057}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001058
Chris Lattner503fadc2006-08-10 05:45:44 +00001059/// ParseGotoStatement
1060/// jump-statement:
1061/// 'goto' identifier ';'
1062/// [GNU] 'goto' '*' expression ';'
1063///
1064/// Note: this lets the caller parse the end ';'.
1065///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001066Parser::OwningStmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
1067 // FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001068 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001069 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001070
Sebastian Redlc13f2682008-12-09 20:22:58 +00001071 OwningStmtResult Res(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001072 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001073 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001074 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001075 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001076 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001077 // GNU indirect goto extension.
1078 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001079 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001080 OwningExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001081 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001082 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001083 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001084 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001085 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattnere34b2c22007-07-22 04:13:33 +00001086 } else {
1087 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001088 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001089 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001090
Sebastian Redlb62406f2008-12-11 19:48:14 +00001091 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001092}
1093
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001094/// ParseContinueStatement
1095/// jump-statement:
1096/// 'continue' ';'
1097///
1098/// Note: this lets the caller parse the end ';'.
1099///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001100Parser::OwningStmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
1101 // FIXME: Use attributes?
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001102 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl573feed2009-01-18 13:19:59 +00001103 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001104}
1105
1106/// ParseBreakStatement
1107/// jump-statement:
1108/// 'break' ';'
1109///
1110/// Note: this lets the caller parse the end ';'.
1111///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001112Parser::OwningStmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
1113 // FIXME: Use attributes?
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001114 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl573feed2009-01-18 13:19:59 +00001115 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001116}
1117
Chris Lattner503fadc2006-08-10 05:45:44 +00001118/// ParseReturnStatement
1119/// jump-statement:
1120/// 'return' expression[opt] ';'
Alexis Hunt96d5c762009-11-21 08:43:09 +00001121Parser::OwningStmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
1122 // FIXME: Use attributes?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001123 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001124 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001125
Sebastian Redlc13f2682008-12-09 20:22:58 +00001126 OwningExprResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001127 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +00001128 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001129 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001130 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001131 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001132 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001133 }
Anders Carlssona1929472009-08-18 16:11:00 +00001134 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Chris Lattner503fadc2006-08-10 05:45:44 +00001135}
Chris Lattner0116c472006-08-15 06:03:28 +00001136
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001137/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1138/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001139Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001140 if (Tok.is(tok::l_brace)) {
1141 unsigned short savedBraceCount = BraceCount;
1142 do {
1143 ConsumeAnyToken();
1144 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001145 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001146 // From the MS website: If used without braces, the __asm keyword means
1147 // that the rest of the line is an assembly-language statement.
1148 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001149 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001150 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001151 do {
1152 ConsumeAnyToken();
1153 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001154 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1155 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001156 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001157 }
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001158 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffb2c80c72008-02-07 03:50:06 +00001159}
1160
Chris Lattner0116c472006-08-15 06:03:28 +00001161/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001162/// asm-statement:
1163/// gnu-asm-statement
1164/// ms-asm-statement
1165///
1166/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001167/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1168///
1169/// [GNU] asm-argument:
1170/// asm-string-literal
1171/// asm-string-literal ':' asm-operands[opt]
1172/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1173/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1174/// ':' asm-clobbers
1175///
1176/// [GNU] asm-clobbers:
1177/// asm-string-literal
1178/// asm-clobbers ',' asm-string-literal
1179///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001180/// [MS] ms-asm-statement:
1181/// '__asm' assembly-instruction ';'[opt]
1182/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1183///
1184/// [MS] assembly-instruction-list:
1185/// assembly-instruction ';'[opt]
1186/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1187///
Sebastian Redlb62406f2008-12-11 19:48:14 +00001188Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001189 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001190 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001191
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001192 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001193 msAsm = true;
1194 return FuzzyParseMicrosoftAsmStatement();
1195 }
Chris Lattner0116c472006-08-15 06:03:28 +00001196 DeclSpec DS;
1197 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001198 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001199
Chris Lattner0116c472006-08-15 06:03:28 +00001200 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001201 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001202 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001203 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001204 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001205
Chris Lattner0116c472006-08-15 06:03:28 +00001206 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001207 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001208 bool isSimple = false;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001209 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001210 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001211 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001212 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001213 }
Chris Lattner04132372006-10-16 06:12:55 +00001214 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001215
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001216 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001217 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001218 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001219
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001220 llvm::SmallVector<std::string, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001221 ExprVector Constraints(Actions);
1222 ExprVector Exprs(Actions);
1223 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001224
Anders Carlsson19fe1162008-02-05 23:03:50 +00001225 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001226
Anders Carlsson19fe1162008-02-05 23:03:50 +00001227 SourceLocation RParenLoc;
1228 if (Tok.is(tok::r_paren)) {
1229 // We have a simple asm expression
1230 isSimple = true;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001231
Anders Carlsson19fe1162008-02-05 23:03:50 +00001232 RParenLoc = ConsumeParen();
1233 } else {
Sebastian Redl511ed552008-11-25 22:21:31 +00001234 // Parse Outputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001235 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001236 return StmtError();
1237
Anders Carlsson19fe1162008-02-05 23:03:50 +00001238 NumOutputs = Names.size();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001239
Anders Carlsson19fe1162008-02-05 23:03:50 +00001240 // Parse Inputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001241 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001242 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001243
Anders Carlsson19fe1162008-02-05 23:03:50 +00001244 assert(Names.size() == Constraints.size() &&
Mike Stump11289f42009-09-09 15:08:12 +00001245 Constraints.size() == Exprs.size()
Anders Carlsson19fe1162008-02-05 23:03:50 +00001246 && "Input operand size mismatch!");
1247
1248 NumInputs = Names.size() - NumOutputs;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001249
Anders Carlsson19fe1162008-02-05 23:03:50 +00001250 // Parse the clobbers, if present.
1251 if (Tok.is(tok::colon)) {
Anders Carlsson091a0592007-11-21 23:27:34 +00001252 ConsumeToken();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001253
Anders Carlsson19fe1162008-02-05 23:03:50 +00001254 // Parse the asm-string list for clobbers.
1255 while (1) {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001256 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlsson19fe1162008-02-05 23:03:50 +00001257
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001258 if (Clobber.isInvalid())
Anders Carlsson19fe1162008-02-05 23:03:50 +00001259 break;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001260
1261 Clobbers.push_back(Clobber.release());
1262
Anders Carlsson19fe1162008-02-05 23:03:50 +00001263 if (Tok.isNot(tok::comma)) break;
1264 ConsumeToken();
1265 }
Chris Lattner0116c472006-08-15 06:03:28 +00001266 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001267
Anders Carlsson19fe1162008-02-05 23:03:50 +00001268 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner0116c472006-08-15 06:03:28 +00001269 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001270
Sebastian Redl24b8e152009-01-18 16:53:17 +00001271 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001272 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001273 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001274 move(AsmString), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001275 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001276}
1277
1278/// ParseAsmOperands - Parse the asm-operands production as used by
1279/// asm-statement. We also parse a leading ':' token. If the leading colon is
1280/// not present, we do not parse anything.
1281///
1282/// [GNU] asm-operands:
1283/// asm-operand
1284/// asm-operands ',' asm-operand
1285///
1286/// [GNU] asm-operand:
1287/// asm-string-literal '(' expression ')'
1288/// '[' identifier ']' asm-string-literal '(' expression ')'
1289///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001290//
1291// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001292bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001293 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1294 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001295 // Only do anything if this operand is present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001296 if (Tok.isNot(tok::colon)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001297 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001298
Chris Lattner0116c472006-08-15 06:03:28 +00001299 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001300 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001301 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001302
1303 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001304 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001305 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001306 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001307
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001308 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001309 Diag(Tok, diag::err_expected_ident);
1310 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001311 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001312 }
Mike Stump11289f42009-09-09 15:08:12 +00001313
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001314 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001315 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001316
Daniel Dunbar07d07852009-10-18 21:17:35 +00001317 Names.push_back(II->getName());
Chris Lattner0116c472006-08-15 06:03:28 +00001318 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001319 } else
1320 Names.push_back(std::string());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001321
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001322 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001323 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001324 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001325 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001326 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001327 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001328
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001329 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001330 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001331 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001332 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001333 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001334
Chris Lattner0116c472006-08-15 06:03:28 +00001335 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001336 SourceLocation OpenLoc = ConsumeParen();
1337 OwningExprResult Res(ParseExpression());
1338 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001339 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001340 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001341 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001342 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001343 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001344 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001345 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001346 ConsumeToken();
1347 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001348
1349 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001350}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001351
Chris Lattner83f095c2009-03-28 19:18:32 +00001352Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001353 assert(Tok.is(tok::l_brace));
1354 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001355
Chris Lattnereae6cb62009-03-05 08:00:35 +00001356 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1357 PP.getSourceManager(),
1358 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001359
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001360 // Do not enter a scope for the brace, as the arguments are in the same scope
1361 // (the function body) as the body itself. Instead, just read the statement
1362 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl042ad952008-12-11 19:30:53 +00001363 OwningStmtResult FnBody(ParseCompoundStatementBody());
1364
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001365 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001366 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001367 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001368 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001369
Sebastian Redl726a0d92009-02-05 15:02:23 +00001370 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001371}
Sebastian Redlb219c902008-12-21 16:41:36 +00001372
Sebastian Redla7b98a72009-04-26 20:35:05 +00001373/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1374///
1375/// function-try-block:
1376/// 'try' ctor-initializer[opt] compound-statement handler-seq
1377///
1378Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1379 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1380 SourceLocation TryLoc = ConsumeToken();
1381
1382 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1383 PP.getSourceManager(),
1384 "parsing function try block");
1385
1386 // Constructor initializer list?
1387 if (Tok.is(tok::colon))
1388 ParseConstructorInitializer(Decl);
1389
Sebastian Redld98ecd62009-04-26 21:08:36 +00001390 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001391 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1392 // If we failed to parse the try-catch, we just give the function an empty
1393 // compound statement as the body.
1394 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001395 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001396 MultiStmtArg(Actions), false);
1397
1398 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1399}
1400
Sebastian Redlb219c902008-12-21 16:41:36 +00001401/// ParseCXXTryBlock - Parse a C++ try-block.
1402///
1403/// try-block:
1404/// 'try' compound-statement handler-seq
1405///
Alexis Hunt96d5c762009-11-21 08:43:09 +00001406Parser::OwningStmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
1407 // FIXME: Add attributes?
Sebastian Redlb219c902008-12-21 16:41:36 +00001408 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1409
1410 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001411 return ParseCXXTryBlockCommon(TryLoc);
1412}
1413
1414/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1415/// function-try-block.
1416///
1417/// try-block:
1418/// 'try' compound-statement handler-seq
1419///
1420/// function-try-block:
1421/// 'try' ctor-initializer[opt] compound-statement handler-seq
1422///
1423/// handler-seq:
1424/// handler handler-seq[opt]
1425///
1426Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001427 if (Tok.isNot(tok::l_brace))
1428 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001429 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1430 OwningStmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001431 if (TryBlock.isInvalid())
1432 return move(TryBlock);
1433
1434 StmtVector Handlers(Actions);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001435 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1436 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1437 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1438 << Attr.Range;
1439 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001440 if (Tok.isNot(tok::kw_catch))
1441 return StmtError(Diag(Tok, diag::err_expected_catch));
1442 while (Tok.is(tok::kw_catch)) {
1443 OwningStmtResult Handler(ParseCXXCatchBlock());
1444 if (!Handler.isInvalid())
1445 Handlers.push_back(Handler.release());
1446 }
1447 // Don't bother creating the full statement if we don't have any usable
1448 // handlers.
1449 if (Handlers.empty())
1450 return StmtError();
1451
Sebastian Redl726a0d92009-02-05 15:02:23 +00001452 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001453}
1454
1455/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1456///
1457/// handler:
1458/// 'catch' '(' exception-declaration ')' compound-statement
1459///
1460/// exception-declaration:
1461/// type-specifier-seq declarator
1462/// type-specifier-seq abstract-declarator
1463/// type-specifier-seq
1464/// '...'
1465///
1466Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1467 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1468
1469 SourceLocation CatchLoc = ConsumeToken();
1470
1471 SourceLocation LParenLoc = Tok.getLocation();
1472 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1473 return StmtError();
1474
1475 // C++ 3.3.2p3:
1476 // The name in a catch exception-declaration is local to the handler and
1477 // shall not be redeclared in the outermost block of the handler.
1478 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1479
1480 // exception-declaration is equivalent to '...' or a parameter-declaration
1481 // without default arguments.
Chris Lattner83f095c2009-03-28 19:18:32 +00001482 DeclPtrTy ExceptionDecl;
Sebastian Redlb219c902008-12-21 16:41:36 +00001483 if (Tok.isNot(tok::ellipsis)) {
1484 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001485 if (ParseCXXTypeSpecifierSeq(DS))
1486 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001487 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1488 ParseDeclarator(ExDecl);
1489 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1490 } else
1491 ConsumeToken();
1492
1493 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1494 return StmtError();
1495
1496 if (Tok.isNot(tok::l_brace))
1497 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1498
Alexis Hunt96d5c762009-11-21 08:43:09 +00001499 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1500 OwningStmtResult Block(ParseCompoundStatement(0));
Sebastian Redlb219c902008-12-21 16:41:36 +00001501 if (Block.isInvalid())
1502 return move(Block);
1503
Sebastian Redl726a0d92009-02-05 15:02:23 +00001504 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redlb219c902008-12-21 16:41:36 +00001505}