blob: cb35740465a5e2231460ed86e70208d0581a8703 [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"
Sebastian Redl511ed552008-11-25 22:21:31 +000017#include "AstGuard.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroff4e79d342008-02-07 23:24:32 +000019#include "clang/Basic/SourceManager.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner33ad2ca2006-11-05 23:47:55 +000021#include "clang/Parse/Scope.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
Fariborz Jahanian90814572007-10-04 20:19:06 +000041/// [OBC] objc-throw-statement
42/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000043/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000044/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000045/// [OMP] openmp-construct [TODO]
46///
47/// labeled-statement:
48/// identifier ':' statement
49/// 'case' constant-expression ':' statement
50/// 'default' ':' statement
51///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000052/// selection-statement:
53/// if-statement
54/// switch-statement
55///
56/// iteration-statement:
57/// while-statement
58/// do-statement
59/// for-statement
60///
Chris Lattner9075bd72006-08-10 04:59:57 +000061/// expression-statement:
62/// expression[opt] ';'
63///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000064/// jump-statement:
65/// 'goto' identifier ';'
66/// 'continue' ';'
67/// 'break' ';'
68/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000069/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000070///
Fariborz Jahanian90814572007-10-04 20:19:06 +000071/// [OBC] objc-throw-statement:
72/// [OBC] '@' 'throw' expression ';'
73/// [OBC] '@' 'throw' ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000074///
Chris Lattner30f910e2006-10-16 05:52:41 +000075Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000076 const char *SemiError = 0;
Sebastian Redlc13f2682008-12-09 20:22:58 +000077 OwningStmtResult Res(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000078
Chris Lattner503fadc2006-08-10 05:45:44 +000079 // Cases in this switch statement should fall through if the parser expects
80 // the token to end in a semicolon (in which case SemiError should be set),
81 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000082 tok::TokenKind Kind = Tok.getKind();
83 SourceLocation AtLoc;
84 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000085 case tok::at: // May be a @try or @throw statement
86 {
87 AtLoc = ConsumeToken(); // consume @
Steve Naroffe6016792008-02-05 21:27:35 +000088 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000089 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000090
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +000091 case tok::identifier:
92 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
93 // identifier ':' statement
94 return ParseLabeledStatement();
95 }
96 // PASS THROUGH.
97
Chris Lattner0ccd51e2006-08-09 05:47:47 +000098 default:
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +000099 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner2e232092008-03-13 06:29:04 +0000100 SourceLocation DeclStart = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000101 DeclTy *Decl = ParseDeclaration(Declarator::BlockContext);
Chris Lattner2e232092008-03-13 06:29:04 +0000102 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000103 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000104 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000105 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +0000106 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +0000107 } else {
108 // expression[opt] ';'
Sebastian Redlc13f2682008-12-09 20:22:58 +0000109 OwningExprResult Expr(Actions, ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000110 if (Expr.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000111 // If the expression is invalid, skip ahead to the next semicolon. Not
112 // doing this opens us up to the possibility of infinite loops if
113 // ParseExpression does not consume any tokens.
114 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000115 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000116 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000117 // Otherwise, eat the semicolon.
118 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000119 return Actions.ActOnExprStmt(Expr.release());
Chris Lattnerf8afb622006-08-10 18:26:31 +0000120 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000121
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000122 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000123 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000124 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000125 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000126
127 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000128 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000129 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff66356bd2007-09-16 14:56:35 +0000130 return Actions.ActOnNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000131
Chris Lattner9075bd72006-08-10 04:59:57 +0000132 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000133 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000134 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000135 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000136
Chris Lattner9075bd72006-08-10 04:59:57 +0000137 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000138 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000139 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000140 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000141 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000142 break;
143 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000144 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000145
146 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000147 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000148 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000149 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000150 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000151 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000152 SemiError = "continue statement";
153 break;
154 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000155 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000156 SemiError = "break statement";
157 break;
158 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000159 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000160 SemiError = "return statement";
161 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000162
163 case tok::kw_asm:
Steve Naroffb2c80c72008-02-07 03:50:06 +0000164 bool msAsm = false;
165 Res = ParseAsmStatement(msAsm);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000166 if (msAsm) return Res.result();
Chris Lattner0116c472006-08-15 06:03:28 +0000167 SemiError = "asm statement";
168 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000169 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000170
171 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000172 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000173 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000174 } else if (!Res.isInvalid()) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000175 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner0046de12008-11-13 18:52:53 +0000176 // Skip until we see a } or ;, but don't eat it.
177 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000178 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000179 return Res.result();
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000180}
181
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000182/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000183///
184/// labeled-statement:
185/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000186/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000187///
188Parser::StmtResult Parser::ParseLabeledStatement() {
189 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
190 "Not an identifier!");
191
192 Token IdentTok = Tok; // Save the whole token.
193 ConsumeToken(); // eat the identifier.
194
195 assert(Tok.is(tok::colon) && "Not a label!");
196
197 // identifier ':' statement
198 SourceLocation ColonLoc = ConsumeToken();
199
200 // Read label attributes, if present.
201 DeclTy *AttrList = 0;
202 if (Tok.is(tok::kw___attribute))
203 // TODO: save these somewhere.
204 AttrList = ParseAttributes();
205
Sebastian Redlc13f2682008-12-09 20:22:58 +0000206 OwningStmtResult SubStmt(Actions, ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000207
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000208 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000209 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000210 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000211
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000212 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
213 IdentTok.getIdentifierInfo(),
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000214 ColonLoc, SubStmt.release());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000215}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000216
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000217/// ParseCaseStatement
218/// labeled-statement:
219/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000220/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000221///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000222/// Note that this does not parse the 'statement' at the end.
223///
Chris Lattner30f910e2006-10-16 05:52:41 +0000224Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000225 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000226 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000227
Sebastian Redlc13f2682008-12-09 20:22:58 +0000228 OwningExprResult LHS(Actions, ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000229 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000230 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000231 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000232 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000233
Chris Lattner476c3ad2006-08-13 22:09:58 +0000234 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000235 SourceLocation DotDotDotLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +0000236 OwningExprResult RHS(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000237 if (Tok.is(tok::ellipsis)) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000238 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000239 DotDotDotLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000240
241 RHS = ParseConstantExpression();
242 if (RHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000243 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000244 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000245 }
246 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000247
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000248 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000249 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000250 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000251 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000252 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000253
Chris Lattneraf635312006-10-16 06:06:51 +0000254 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000255
256 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000257 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000258 Diag(Tok, diag::err_label_end_of_compound_statement);
259 return true;
260 }
261
Sebastian Redlc13f2682008-12-09 20:22:58 +0000262 OwningStmtResult SubStmt(Actions, ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000263
264 // Broken substmt shouldn't prevent the case from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000265 if (SubStmt.isInvalid())
Steve Naroff66356bd2007-09-16 14:56:35 +0000266 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000267
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000268 return Actions.ActOnCaseStmt(CaseLoc, LHS.release(), DotDotDotLoc,
269 RHS.release(), ColonLoc, SubStmt.release());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000270}
271
272/// ParseDefaultStatement
273/// labeled-statement:
274/// 'default' ':' statement
275/// Note that this does not parse the 'statement' at the end.
276///
Chris Lattner30f910e2006-10-16 05:52:41 +0000277Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000278 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000279 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000280
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000281 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000282 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000283 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000284 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000285 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000286
Chris Lattneraf635312006-10-16 06:06:51 +0000287 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000288
289 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000290 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000291 Diag(Tok, diag::err_label_end_of_compound_statement);
292 return true;
293 }
294
Sebastian Redlc13f2682008-12-09 20:22:58 +0000295 OwningStmtResult SubStmt(Actions, ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000296 if (SubStmt.isInvalid())
Chris Lattner30f910e2006-10-16 05:52:41 +0000297 return true;
298
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000299 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000300 SubStmt.release(), CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000301}
302
303
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000304/// ParseCompoundStatement - Parse a "{}" block.
305///
306/// compound-statement: [C99 6.8.2]
307/// { block-item-list[opt] }
308/// [GNU] { label-declarations block-item-list } [TODO]
309///
310/// block-item-list:
311/// block-item
312/// block-item-list block-item
313///
314/// block-item:
315/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000316/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000317/// statement
318/// [OMP] openmp-directive [TODO]
319///
320/// [GNU] label-declarations:
321/// [GNU] label-declaration
322/// [GNU] label-declarations label-declaration
323///
324/// [GNU] label-declaration:
325/// [GNU] '__label__' identifier-list ';'
326///
327/// [OMP] openmp-directive: [TODO]
328/// [OMP] barrier-directive
329/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000330///
Chris Lattnercac27a52007-08-31 21:49:55 +0000331Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000332 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000333
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000334 // Enter a scope to hold everything within the compound stmt. Compound
335 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000336 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000337
338 // Parse the statements in the body.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000339 OwningStmtResult Body(Actions, ParseCompoundStatementBody(isStmtExpr));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000340 return Body.result();
Chris Lattnerf2978802007-01-21 06:52:16 +0000341}
342
343
344/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000345/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000346/// consume the '}' at the end of the block. It does not manipulate the scope
347/// stack.
Chris Lattnercac27a52007-08-31 21:49:55 +0000348Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerf2978802007-01-21 06:52:16 +0000349 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
350
Chris Lattner010015a2007-05-28 07:23:54 +0000351 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000352 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000353
354 typedef StmtVector StmtsTy;
355 StmtsTy Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000356 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redlc13f2682008-12-09 20:22:58 +0000357 OwningStmtResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000358 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000359 R = ParseStatementOrDeclaration(false);
360 } else {
361 // __extension__ can start declarations and it can also be a unary
362 // operator for expressions. Consume multiple __extension__ markers here
363 // until we can determine which is which.
364 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000365 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000366 ConsumeToken();
367
Chris Lattnerfdc07482008-03-13 06:32:11 +0000368 // __extension__ silences extension warnings in the subexpression.
Chris Lattner1ff6e732008-10-20 06:51:33 +0000369 ExtensionRAIIObject O(Diags); // Use RAII to do this.
370
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000371 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000372 if (isDeclarationStatement()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000373 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner2e232092008-03-13 06:29:04 +0000374 SourceLocation DeclStart = Tok.getLocation();
375 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
376 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerb943aa82008-03-13 06:29:54 +0000377 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000378 } else {
379 // Otherwise this was a unary __extension__ marker. Parse the
380 // subexpression and add the __extension__ unary op.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000381 OwningExprResult Res(Actions, ParseCastExpression(false));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000382
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000383 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000384 SkipUntil(tok::semi);
385 continue;
386 }
387
388 // Add the __extension__ node to the AST.
Douglas Gregord08452f2008-11-19 15:42:04 +0000389 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000390 Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000391 if (Res.isInvalid())
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000392 continue;
393
Chris Lattner1ff6e732008-10-20 06:51:33 +0000394 // Eat the semicolon at the end of stmt and convert the expr into a
395 // statement.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000396 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000397 R = Actions.ActOnExprStmt(Res.release());
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000398 }
399 }
400
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000401 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000402 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000403 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000404
405 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000406 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000407 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffe97c4ab2008-01-31 18:29:10 +0000408 return true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000409 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000410
Chris Lattner04132372006-10-16 06:12:55 +0000411 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff66356bd2007-09-16 14:56:35 +0000412 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +0000413 Stmts.take(), Stmts.size(), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000414}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000415
416/// ParseIfStatement
417/// if-statement: [C99 6.8.4.1]
418/// 'if' '(' expression ')' statement
419/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000420/// [C++] 'if' '(' condition ')' statement
421/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000422///
423Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000424 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000425 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000426
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000427 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000428 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000429 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000430 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000431 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000432
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000433 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
434
Chris Lattner2dd1b722007-08-26 23:08:06 +0000435 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
436 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000437 //
438 // C++ 6.4p3:
439 // A name introduced by a declaration in a condition is in scope from its
440 // point of declaration until the end of the substatements controlled by the
441 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000442 // C++ 3.3.2p4:
443 // Names declared in the for-init-statement, and in the condition of if,
444 // while, for, and switch statements are local to the if, while, for, or
445 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000446 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000447 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000448
Chris Lattnerc951dae2006-08-10 04:23:57 +0000449 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000450 OwningExprResult CondExp(Actions);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000451 if (getLang().CPlusPlus) {
452 SourceLocation LParenLoc = ConsumeParen();
453 CondExp = ParseCXXCondition();
454 MatchRHSPunctuation(tok::r_paren, LParenLoc);
455 } else {
456 CondExp = ParseSimpleParenExpression();
457 }
458
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000459 if (CondExp.isInvalid()) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000460 SkipUntil(tok::semi);
461 return true;
462 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000463
Chris Lattner8fb26252007-08-22 05:28:50 +0000464 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000465 // there is no compound stmt. C90 does not have this clause. We only do this
466 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000467 //
468 // C++ 6.4p1:
469 // The substatement in a selection-statement (each substatement, in the else
470 // form of the if statement) implicitly defines a local scope.
471 //
472 // For C++ we create a scope for the condition and a new scope for
473 // substatements because:
474 // -When the 'then' scope exits, we want the condition declaration to still be
475 // active for the 'else' scope too.
476 // -Sema will detect name clashes by considering declarations of a
477 // 'ControlScope' as part of its direct subscope.
478 // -If we wanted the condition and substatement to be in the same scope, we
479 // would have to notify ParseStatement not to create a new scope. It's
480 // simpler to let it create a new scope.
481 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000482 ParseScope InnerScope(this, Scope::DeclScope,
483 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000484
Chris Lattner5c5808a2007-10-29 05:08:52 +0000485 // Read the 'then' stmt.
486 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redlc13f2682008-12-09 20:22:58 +0000487 OwningStmtResult ThenStmt(Actions, ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000488
Chris Lattner37e54f42007-08-22 05:16:28 +0000489 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000490 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000491
492 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000493 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000494 SourceLocation ElseStmtLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +0000495 OwningStmtResult ElseStmt(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000496
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000497 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000498 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000499
Chris Lattner8fb26252007-08-22 05:28:50 +0000500 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000501 // there is no compound stmt. C90 does not have this clause. We only do
502 // this if the body isn't a compound statement to avoid push/pop in common
503 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000504 //
505 // C++ 6.4p1:
506 // The substatement in a selection-statement (each substatement, in the else
507 // form of the if statement) implicitly defines a local scope.
508 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000509 ParseScope InnerScope(this, Scope::DeclScope,
510 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000511
Douglas Gregor85970ca2008-12-10 23:01:14 +0000512 bool WithinElse = CurScope->isWithinElse();
513 CurScope->setWithinElse(true);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000514 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000515 ElseStmt = ParseStatement();
Douglas Gregor85970ca2008-12-10 23:01:14 +0000516 CurScope->setWithinElse(WithinElse);
Chris Lattner37e54f42007-08-22 05:16:28 +0000517
518 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000519 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000520 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000521
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000522 IfScope.Exit();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000523
Chris Lattner5c5808a2007-10-29 05:08:52 +0000524 // If the then or else stmt is invalid and the other is valid (and present),
525 // make turn the invalid one into a null stmt to avoid dropping the other
526 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000527 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
528 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
529 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000530 // Both invalid, or one is invalid and other is non-present: return error.
Chris Lattner5c5808a2007-10-29 05:08:52 +0000531 return true;
532 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000533
Chris Lattner5c5808a2007-10-29 05:08:52 +0000534 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000535 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000536 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000537 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000538 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000539
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000540 return Actions.ActOnIfStmt(IfLoc, CondExp.release(), ThenStmt.release(),
541 ElseLoc, ElseStmt.release());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000542}
543
Chris Lattner9075bd72006-08-10 04:59:57 +0000544/// ParseSwitchStatement
545/// switch-statement:
546/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000547/// [C++] 'switch' '(' condition ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000548Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000549 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000550 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000551
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000552 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000553 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000554 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000555 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000556 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000557
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000558 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
559
Chris Lattner2dd1b722007-08-26 23:08:06 +0000560 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
561 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000562 //
563 // C++ 6.4p3:
564 // A name introduced by a declaration in a condition is in scope from its
565 // point of declaration until the end of the substatements controlled by the
566 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000567 // C++ 3.3.2p4:
568 // Names declared in the for-init-statement, and in the condition of if,
569 // while, for, and switch statements are local to the if, while, for, or
570 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000571 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000572 unsigned ScopeFlags
573 = C99orCXX? Scope::BreakScope | Scope::DeclScope | Scope::ControlScope
574 : Scope::BreakScope;
575 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000576
Chris Lattner9075bd72006-08-10 04:59:57 +0000577 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000578 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000579 if (getLang().CPlusPlus) {
580 SourceLocation LParenLoc = ConsumeParen();
581 Cond = ParseCXXCondition();
582 MatchRHSPunctuation(tok::r_paren, LParenLoc);
583 } else {
584 Cond = ParseSimpleParenExpression();
585 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000586
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000587 if (Cond.isInvalid())
Anders Carlsson51873c22007-07-22 07:07:56 +0000588 return true;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000589
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000590 OwningStmtResult Switch(Actions,
591 Actions.ActOnStartOfSwitchStmt(Cond.release()));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000592
Chris Lattner8fb26252007-08-22 05:28:50 +0000593 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000594 // there is no compound stmt. C90 does not have this clause. We only do this
595 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000596 //
597 // C++ 6.4p1:
598 // The substatement in a selection-statement (each substatement, in the else
599 // form of the if statement) implicitly defines a local scope.
600 //
601 // See comments in ParseIfStatement for why we create a scope for the
602 // condition and a new scope for substatement in C++.
603 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000604 ParseScope InnerScope(this, Scope::DeclScope,
605 C99orCXX && Tok.isNot(tok::l_brace));
Chris Lattner8fb26252007-08-22 05:28:50 +0000606
Chris Lattner9075bd72006-08-10 04:59:57 +0000607 // Read the body statement.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000608 OwningStmtResult Body(Actions, ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000609
Chris Lattner8fb26252007-08-22 05:28:50 +0000610 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000611 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000612
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000613 if (Body.isInvalid()) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000614 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000615 // FIXME: Remove the case statement list from the Switch statement.
616 }
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000617
618 SwitchScope.Exit();
Chris Lattner30f910e2006-10-16 05:52:41 +0000619
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000620 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.release(),
621 Body.release());
Chris Lattner9075bd72006-08-10 04:59:57 +0000622}
623
624/// ParseWhileStatement
625/// while-statement: [C99 6.8.5.1]
626/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000627/// [C++] 'while' '(' condition ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000628Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000629 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000630 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000631 ConsumeToken(); // eat the 'while'.
632
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000633 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000634 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000635 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000636 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000637 }
638
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000639 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
640
Chris Lattner2dd1b722007-08-26 23:08:06 +0000641 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
642 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000643 //
644 // C++ 6.4p3:
645 // A name introduced by a declaration in a condition is in scope from its
646 // point of declaration until the end of the substatements controlled by the
647 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000648 // C++ 3.3.2p4:
649 // Names declared in the for-init-statement, and in the condition of if,
650 // while, for, and switch statements are local to the if, while, for, or
651 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000652 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000653 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000654 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000655 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
656 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000657 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000658 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
659 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000660
Chris Lattner9075bd72006-08-10 04:59:57 +0000661 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000662 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000663 if (getLang().CPlusPlus) {
664 SourceLocation LParenLoc = ConsumeParen();
665 Cond = ParseCXXCondition();
666 MatchRHSPunctuation(tok::r_paren, LParenLoc);
667 } else {
668 Cond = ParseSimpleParenExpression();
669 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000670
Chris Lattner8fb26252007-08-22 05:28:50 +0000671 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000672 // there is no compound stmt. C90 does not have this clause. We only do this
673 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000674 //
675 // C++ 6.5p2:
676 // The substatement in an iteration-statement implicitly defines a local scope
677 // which is entered and exited each time through the loop.
678 //
679 // See comments in ParseIfStatement for why we create a scope for the
680 // condition and a new scope for substatement in C++.
681 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000682 ParseScope InnerScope(this, Scope::DeclScope,
683 C99orCXX && Tok.isNot(tok::l_brace));
Chris Lattner8fb26252007-08-22 05:28:50 +0000684
Chris Lattner9075bd72006-08-10 04:59:57 +0000685 // Read the body statement.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000686 OwningStmtResult Body(Actions, ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000687
Chris Lattner8fb26252007-08-22 05:28:50 +0000688 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000689 InnerScope.Exit();
690 WhileScope.Exit();
Chris Lattner30f910e2006-10-16 05:52:41 +0000691
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000692 if (Cond.isInvalid() || Body.isInvalid()) return true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000693
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000694 return Actions.ActOnWhileStmt(WhileLoc, Cond.release(), Body.release());
Chris Lattner9075bd72006-08-10 04:59:57 +0000695}
696
697/// ParseDoStatement
698/// do-statement: [C99 6.8.5.2]
699/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000700/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000701Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000702 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000703 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000704
Chris Lattner2dd1b722007-08-26 23:08:06 +0000705 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
706 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000707 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000708 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000709 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000710 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000711 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
712
713 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000714
Chris Lattner8fb26252007-08-22 05:28:50 +0000715 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000716 // there is no compound stmt. C90 does not have this clause. We only do this
717 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000718 //
719 // C++ 6.5p2:
720 // The substatement in an iteration-statement implicitly defines a local scope
721 // which is entered and exited each time through the loop.
722 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000723 ParseScope InnerScope(this, Scope::DeclScope,
724 (getLang().C99 || getLang().CPlusPlus) &&
725 Tok.isNot(tok::l_brace));
Chris Lattner8fb26252007-08-22 05:28:50 +0000726
Chris Lattner9075bd72006-08-10 04:59:57 +0000727 // Read the body statement.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000728 OwningStmtResult Body(Actions, ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000729
Chris Lattner8fb26252007-08-22 05:28:50 +0000730 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000731 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000732
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000733 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000734 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000735 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000736 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000737 SkipUntil(tok::semi, false, true);
738 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000739 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000740 }
Chris Lattneraf635312006-10-16 06:06:51 +0000741 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000742
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000743 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000744 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000745 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000746 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000747 }
748
749 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000750 OwningExprResult Cond(Actions, ParseSimpleParenExpression());
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000751 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000752
753 if (Cond.isInvalid() || Body.isInvalid()) return true;
754
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000755 return Actions.ActOnDoStmt(DoLoc, Body.release(), WhileLoc, Cond.release());
Chris Lattner9075bd72006-08-10 04:59:57 +0000756}
757
758/// ParseForStatement
759/// for-statement: [C99 6.8.5.3]
760/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
761/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000762/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
763/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000764/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
765/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000766///
767/// [C++] for-init-statement:
768/// [C++] expression-statement
769/// [C++] simple-declaration
770///
Chris Lattner30f910e2006-10-16 05:52:41 +0000771Parser::StmtResult Parser::ParseForStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000772 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000773 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000774
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000775 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000776 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000777 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000778 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000779 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000780
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000781 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
782
Chris Lattner2dd1b722007-08-26 23:08:06 +0000783 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
784 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000785 //
786 // C++ 6.4p3:
787 // A name introduced by a declaration in a condition is in scope from its
788 // point of declaration until the end of the substatements controlled by the
789 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000790 // C++ 3.3.2p4:
791 // Names declared in the for-init-statement, and in the condition of if,
792 // while, for, and switch statements are local to the if, while, for, or
793 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000794 // C++ 6.5.3p1:
795 // Names declared in the for-init-statement are in the same declarative-region
796 // as those declared in the condition.
797 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000798 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000799 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000800 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
801 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000802 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000803 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
804
805 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000806
Chris Lattner04132372006-10-16 06:12:55 +0000807 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlc13f2682008-12-09 20:22:58 +0000808 OwningExprResult Value(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000809
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000810 bool ForEach = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +0000811 OwningStmtResult FirstPart(Actions), ThirdPart(Actions);
812 OwningExprResult SecondPart(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000813
Chris Lattner9075bd72006-08-10 04:59:57 +0000814 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000815 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000816 // no first part, eat the ';'.
817 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +0000818 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000819 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000820 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +0000821 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner2e232092008-03-13 06:29:04 +0000822
823 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000824 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner2e232092008-03-13 06:29:04 +0000825 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000826 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
827 DeclStart);
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000828 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000829 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000830 SecondPart = ParseExpression();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000831 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000832 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000833 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000834
Chris Lattnercd68f642007-06-27 01:06:29 +0000835 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000836 if (!Value.isInvalid())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000837 FirstPart = Actions.ActOnExprStmt(Value.release());
838
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000839 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000840 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000841 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000842 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000843 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000844 SecondPart = ParseExpression();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000845 }
846 else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000847 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000848 SkipUntil(tok::semi);
849 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000850 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000851 if (!ForEach) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000852 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000853 // Parse the second part of the for specifier.
854 if (Tok.is(tok::semi)) { // for (...;;
855 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000856 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000857 SecondPart = getLang().CPlusPlus ? ParseCXXCondition()
858 : ParseExpression();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000859 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000860
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000861 if (Tok.is(tok::semi)) {
862 ConsumeToken();
863 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000864 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000865 SkipUntil(tok::semi);
866 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000867
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000868 // Parse the third part of the for specifier.
869 if (Tok.is(tok::r_paren)) { // for (...;...;)
870 // no third part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000871 } else {
872 Value = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000873 if (!Value.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000874 // Turn the expression into a stmt.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000875 ThirdPart = Actions.ActOnExprStmt(Value.release());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000876 }
Chris Lattnercd68f642007-06-27 01:06:29 +0000877 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000878 }
Chris Lattner4564bc12006-08-10 23:14:52 +0000879 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000880 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000881
Chris Lattner8fb26252007-08-22 05:28:50 +0000882 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000883 // there is no compound stmt. C90 does not have this clause. We only do this
884 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000885 //
886 // C++ 6.5p2:
887 // The substatement in an iteration-statement implicitly defines a local scope
888 // which is entered and exited each time through the loop.
889 //
890 // See comments in ParseIfStatement for why we create a scope for
891 // for-init-statement/condition and a new scope for substatement in C++.
892 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000893 ParseScope InnerScope(this, Scope::DeclScope,
894 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000895
Chris Lattner9075bd72006-08-10 04:59:57 +0000896 // Read the body statement.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000897 OwningStmtResult Body(Actions, ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000898
Chris Lattner8fb26252007-08-22 05:28:50 +0000899 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000900 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000901
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000902 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000903 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000904
905 if (Body.isInvalid())
906 return true;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000907
908 if (!ForEach)
909 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.release(),
910 SecondPart.release(), ThirdPart.release(),
911 RParenLoc, Body.release());
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000912 else
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000913 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000914 FirstPart.release(),
915 SecondPart.release(),
916 RParenLoc, Body.release());
Chris Lattner9075bd72006-08-10 04:59:57 +0000917}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000918
Chris Lattner503fadc2006-08-10 05:45:44 +0000919/// ParseGotoStatement
920/// jump-statement:
921/// 'goto' identifier ';'
922/// [GNU] 'goto' '*' expression ';'
923///
924/// Note: this lets the caller parse the end ';'.
925///
Chris Lattner30f910e2006-10-16 05:52:41 +0000926Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000927 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000928 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000929
Sebastian Redlc13f2682008-12-09 20:22:58 +0000930 OwningStmtResult Res(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000931 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000932 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000933 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000934 ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000935 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000936 // GNU indirect goto extension.
937 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000938 SourceLocation StarLoc = ConsumeToken();
Sebastian Redlc13f2682008-12-09 20:22:58 +0000939 OwningExprResult R(Actions, ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000940 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000941 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000942 return true;
943 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000944 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.release());
Chris Lattnere34b2c22007-07-22 04:13:33 +0000945 } else {
946 Diag(Tok, diag::err_expected_ident);
947 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000948 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000949
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000950 return Res.result();
Chris Lattner503fadc2006-08-10 05:45:44 +0000951}
952
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000953/// ParseContinueStatement
954/// jump-statement:
955/// 'continue' ';'
956///
957/// Note: this lets the caller parse the end ';'.
958///
959Parser::StmtResult Parser::ParseContinueStatement() {
960 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000961 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000962}
963
964/// ParseBreakStatement
965/// jump-statement:
966/// 'break' ';'
967///
968/// Note: this lets the caller parse the end ';'.
969///
970Parser::StmtResult Parser::ParseBreakStatement() {
971 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000972 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000973}
974
Chris Lattner503fadc2006-08-10 05:45:44 +0000975/// ParseReturnStatement
976/// jump-statement:
977/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +0000978Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000979 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000980 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000981
Sebastian Redlc13f2682008-12-09 20:22:58 +0000982 OwningExprResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000983 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000984 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000985 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000986 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000987 return true;
988 }
Chris Lattnera0927ce2006-08-12 16:59:03 +0000989 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000990 return Actions.ActOnReturnStmt(ReturnLoc, R.release());
Chris Lattner503fadc2006-08-10 05:45:44 +0000991}
Chris Lattner0116c472006-08-15 06:03:28 +0000992
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000993/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
994/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffb2c80c72008-02-07 03:50:06 +0000995Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +0000996 if (Tok.is(tok::l_brace)) {
997 unsigned short savedBraceCount = BraceCount;
998 do {
999 ConsumeAnyToken();
1000 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1001 } else {
1002 // From the MS website: If used without braces, the __asm keyword means
1003 // that the rest of the line is an assembly-language statement.
1004 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001005 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001006 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1007 do {
1008 ConsumeAnyToken();
1009 TokLoc = Tok.getLocation();
1010 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1011 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1012 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001013 }
Steve Narofffba39422008-04-07 21:06:54 +00001014 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffb2c80c72008-02-07 03:50:06 +00001015}
1016
Chris Lattner0116c472006-08-15 06:03:28 +00001017/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001018/// asm-statement:
1019/// gnu-asm-statement
1020/// ms-asm-statement
1021///
1022/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001023/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1024///
1025/// [GNU] asm-argument:
1026/// asm-string-literal
1027/// asm-string-literal ':' asm-operands[opt]
1028/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1029/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1030/// ':' asm-clobbers
1031///
1032/// [GNU] asm-clobbers:
1033/// asm-string-literal
1034/// asm-clobbers ',' asm-string-literal
1035///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001036/// [MS] ms-asm-statement:
1037/// '__asm' assembly-instruction ';'[opt]
1038/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1039///
1040/// [MS] assembly-instruction-list:
1041/// assembly-instruction ';'[opt]
1042/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1043///
Steve Naroffb2c80c72008-02-07 03:50:06 +00001044Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001045 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001046 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner0116c472006-08-15 06:03:28 +00001047
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001048 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001049 msAsm = true;
1050 return FuzzyParseMicrosoftAsmStatement();
1051 }
Chris Lattner0116c472006-08-15 06:03:28 +00001052 DeclSpec DS;
1053 SourceLocation Loc = Tok.getLocation();
1054 ParseTypeQualifierListOpt(DS);
1055
1056 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001057 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001058 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001059 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001060 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Chris Lattner0116c472006-08-15 06:03:28 +00001061
1062 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001063 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001064 bool isSimple = false;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001065 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001066 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001067 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +00001068 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001069 }
Chris Lattner04132372006-10-16 06:12:55 +00001070 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +00001071
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001072 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001073 if (AsmString.isInvalid())
Anders Carlsson81a5a692007-11-20 19:21:03 +00001074 return true;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001075
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001076 llvm::SmallVector<std::string, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001077 ExprVector Constraints(Actions);
1078 ExprVector Exprs(Actions);
1079 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001080
Anders Carlsson19fe1162008-02-05 23:03:50 +00001081 unsigned NumInputs = 0, NumOutputs = 0;
1082
1083 SourceLocation RParenLoc;
1084 if (Tok.is(tok::r_paren)) {
1085 // We have a simple asm expression
1086 isSimple = true;
1087
1088 RParenLoc = ConsumeParen();
1089 } else {
Sebastian Redl511ed552008-11-25 22:21:31 +00001090 // Parse Outputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001091 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1092 return true;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001093
1094 NumOutputs = Names.size();
1095
1096 // Parse Inputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001097 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1098 return true;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001099
Anders Carlsson19fe1162008-02-05 23:03:50 +00001100 assert(Names.size() == Constraints.size() &&
1101 Constraints.size() == Exprs.size()
1102 && "Input operand size mismatch!");
1103
1104 NumInputs = Names.size() - NumOutputs;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001105
Anders Carlsson19fe1162008-02-05 23:03:50 +00001106 // Parse the clobbers, if present.
1107 if (Tok.is(tok::colon)) {
Anders Carlsson091a0592007-11-21 23:27:34 +00001108 ConsumeToken();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001109
Anders Carlsson19fe1162008-02-05 23:03:50 +00001110 // Parse the asm-string list for clobbers.
1111 while (1) {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001112 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlsson19fe1162008-02-05 23:03:50 +00001113
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001114 if (Clobber.isInvalid())
Anders Carlsson19fe1162008-02-05 23:03:50 +00001115 break;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001116
1117 Clobbers.push_back(Clobber.release());
1118
Anders Carlsson19fe1162008-02-05 23:03:50 +00001119 if (Tok.isNot(tok::comma)) break;
1120 ConsumeToken();
1121 }
Chris Lattner0116c472006-08-15 06:03:28 +00001122 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001123
Anders Carlsson19fe1162008-02-05 23:03:50 +00001124 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner0116c472006-08-15 06:03:28 +00001125 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001126
Anders Carlsson19fe1162008-02-05 23:03:50 +00001127 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1128 NumOutputs, NumInputs,
Sebastian Redl511ed552008-11-25 22:21:31 +00001129 &Names[0], Constraints.take(),
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001130 Exprs.take(), AsmString.release(),
Sebastian Redl511ed552008-11-25 22:21:31 +00001131 Clobbers.size(), Clobbers.take(),
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001132 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001133}
1134
1135/// ParseAsmOperands - Parse the asm-operands production as used by
1136/// asm-statement. We also parse a leading ':' token. If the leading colon is
1137/// not present, we do not parse anything.
1138///
1139/// [GNU] asm-operands:
1140/// asm-operand
1141/// asm-operands ',' asm-operand
1142///
1143/// [GNU] asm-operand:
1144/// asm-string-literal '(' expression ')'
1145/// '[' identifier ']' asm-string-literal '(' expression ')'
1146///
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001147bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001148 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1149 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001150 // Only do anything if this operand is present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001151 if (Tok.isNot(tok::colon)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001152 ConsumeToken();
1153
1154 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001155 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001156 return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001157
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001158 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001159 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001160 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001161 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +00001162
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001163 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001164 Diag(Tok, diag::err_expected_ident);
1165 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001166 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001167 }
Chris Lattner645ff3f2007-10-29 04:06:22 +00001168
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001169 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001170 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001171
1172 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner0116c472006-08-15 06:03:28 +00001173 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001174 } else
1175 Names.push_back(std::string());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001176
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001177 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001178 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001179 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001180 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001181 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001182 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001183
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001184 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001185 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001186 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001187 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001188 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001189
Chris Lattner0116c472006-08-15 06:03:28 +00001190 // Read the parenthesized expression.
Sebastian Redlc13f2682008-12-09 20:22:58 +00001191 OwningExprResult Res(Actions, ParseSimpleParenExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001192 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001193 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001194 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001195 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001196 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001197 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001198 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001199 ConsumeToken();
1200 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001201
1202 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001203}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001204
1205Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1206 SourceLocation L, SourceLocation R) {
1207 // Do not enter a scope for the brace, as the arguments are in the same scope
1208 // (the function body) as the body itself. Instead, just read the statement
1209 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redlc13f2682008-12-09 20:22:58 +00001210 OwningStmtResult FnBody(Actions, ParseCompoundStatementBody());
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001211
1212 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001213 if (FnBody.isInvalid())
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001214 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1215
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001216 return Actions.ActOnFinishFunctionBody(Decl, FnBody.release());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001217}