blob: eb8a074d5a863c2269bbf3e9b757758268a1a61e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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"
16#include "clang/Basic/Diagnostic.h"
Steve Naroffbe880ec2008-02-07 23:24:32 +000017#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Parse/DeclSpec.h"
19#include "clang/Parse/Scope.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.8: Statements and Blocks.
24//===----------------------------------------------------------------------===//
25
26/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
27/// StatementOrDeclaration:
28/// statement
29/// declaration
30///
31/// statement:
32/// labeled-statement
33/// compound-statement
34/// expression-statement
35/// selection-statement
36/// iteration-statement
37/// jump-statement
Argiris Kirtzidis7fce94d2008-09-07 18:58:01 +000038/// [C++] declaration-statement
Fariborz Jahanian37c9c612007-10-04 20:19:06 +000039/// [OBC] objc-throw-statement
40/// [OBC] objc-try-catch-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +000041/// [OBC] objc-synchronized-statement
Chris Lattner4b009652007-07-25 00:24:17 +000042/// [GNU] asm-statement
43/// [OMP] openmp-construct [TODO]
44///
45/// labeled-statement:
46/// identifier ':' statement
47/// 'case' constant-expression ':' statement
48/// 'default' ':' statement
49///
50/// selection-statement:
51/// if-statement
52/// switch-statement
53///
54/// iteration-statement:
55/// while-statement
56/// do-statement
57/// for-statement
58///
59/// expression-statement:
60/// expression[opt] ';'
61///
62/// jump-statement:
63/// 'goto' identifier ';'
64/// 'continue' ';'
65/// 'break' ';'
66/// 'return' expression[opt] ';'
67/// [GNU] 'goto' '*' expression ';'
68///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +000069/// [OBC] objc-throw-statement:
70/// [OBC] '@' 'throw' expression ';'
71/// [OBC] '@' 'throw' ';'
Chris Lattner4b009652007-07-25 00:24:17 +000072///
73Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
74 const char *SemiError = 0;
75 Parser::StmtResult Res;
76
77 // Cases in this switch statement should fall through if the parser expects
78 // the token to end in a semicolon (in which case SemiError should be set),
79 // or they directly 'return;' if not.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000080 tok::TokenKind Kind = Tok.getKind();
81 SourceLocation AtLoc;
82 switch (Kind) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000083 case tok::at: // May be a @try or @throw statement
84 {
85 AtLoc = ConsumeToken(); // consume @
Steve Naroffc949a462008-02-05 21:27:35 +000086 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000087 }
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000088
Argiris Kirtzidis824a3712008-07-12 21:04:42 +000089 case tok::identifier:
90 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
91 // identifier ':' statement
92 return ParseLabeledStatement();
93 }
94 // PASS THROUGH.
95
Chris Lattner4b009652007-07-25 00:24:17 +000096 default:
Argiris Kirtzidis7fce94d2008-09-07 18:58:01 +000097 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationSpecifier()) {
Chris Lattnera4ff4272008-03-13 06:29:04 +000098 SourceLocation DeclStart = Tok.getLocation();
99 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
100 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerdaf1c312008-03-13 06:29:54 +0000101 return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000102 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000103 Diag(Tok, diag::err_expected_statement);
104 return true;
105 } else {
106 // expression[opt] ';'
Fariborz Jahanian37c9c612007-10-04 20:19:06 +0000107 ExprResult Res = ParseExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000108 if (Res.isInvalid) {
109 // If the expression is invalid, skip ahead to the next semicolon. Not
110 // doing this opens us up to the possibility of infinite loops if
111 // ParseExpression does not consume any tokens.
112 SkipUntil(tok::semi);
113 return true;
114 }
115 // Otherwise, eat the semicolon.
116 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000117 return Actions.ActOnExprStmt(Res.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000118 }
119
120 case tok::kw_case: // C99 6.8.1: labeled-statement
121 return ParseCaseStatement();
122 case tok::kw_default: // C99 6.8.1: labeled-statement
123 return ParseDefaultStatement();
124
125 case tok::l_brace: // C99 6.8.2: compound-statement
126 return ParseCompoundStatement();
127 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000128 return Actions.ActOnNullStmt(ConsumeToken());
Chris Lattner4b009652007-07-25 00:24:17 +0000129
130 case tok::kw_if: // C99 6.8.4.1: if-statement
131 return ParseIfStatement();
132 case tok::kw_switch: // C99 6.8.4.2: switch-statement
133 return ParseSwitchStatement();
134
135 case tok::kw_while: // C99 6.8.5.1: while-statement
136 return ParseWhileStatement();
137 case tok::kw_do: // C99 6.8.5.2: do-statement
138 Res = ParseDoStatement();
139 SemiError = "do/while loop";
140 break;
141 case tok::kw_for: // C99 6.8.5.3: for-statement
142 return ParseForStatement();
143
144 case tok::kw_goto: // C99 6.8.6.1: goto-statement
145 Res = ParseGotoStatement();
146 SemiError = "goto statement";
147 break;
148 case tok::kw_continue: // C99 6.8.6.2: continue-statement
149 Res = ParseContinueStatement();
150 SemiError = "continue statement";
151 break;
152 case tok::kw_break: // C99 6.8.6.3: break-statement
153 Res = ParseBreakStatement();
154 SemiError = "break statement";
155 break;
156 case tok::kw_return: // C99 6.8.6.4: return-statement
157 Res = ParseReturnStatement();
158 SemiError = "return statement";
159 break;
160
161 case tok::kw_asm:
Steve Naroff73a07032008-02-07 03:50:06 +0000162 bool msAsm = false;
163 Res = ParseAsmStatement(msAsm);
164 if (msAsm) return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000165 SemiError = "asm statement";
166 break;
167 }
168
169 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000170 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000171 ConsumeToken();
172 } else {
173 Diag(Tok, diag::err_expected_semi_after, SemiError);
174 SkipUntil(tok::semi);
175 }
176 return Res;
177}
178
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000179/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner4b009652007-07-25 00:24:17 +0000180///
181/// labeled-statement:
182/// identifier ':' statement
183/// [GNU] identifier ':' attributes[opt] statement
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000184///
185Parser::StmtResult Parser::ParseLabeledStatement() {
186 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
187 "Not an identifier!");
188
189 Token IdentTok = Tok; // Save the whole token.
190 ConsumeToken(); // eat the identifier.
191
192 assert(Tok.is(tok::colon) && "Not a label!");
193
194 // identifier ':' statement
195 SourceLocation ColonLoc = ConsumeToken();
196
197 // Read label attributes, if present.
198 DeclTy *AttrList = 0;
199 if (Tok.is(tok::kw___attribute))
200 // TODO: save these somewhere.
201 AttrList = ParseAttributes();
202
203 StmtResult SubStmt = ParseStatement();
204
205 // Broken substmt shouldn't prevent the label from being added to the AST.
206 if (SubStmt.isInvalid)
207 SubStmt = Actions.ActOnNullStmt(ColonLoc);
208
209 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
210 IdentTok.getIdentifierInfo(),
211 ColonLoc, SubStmt.Val);
212}
Chris Lattner4b009652007-07-25 00:24:17 +0000213
214/// ParseCaseStatement
215/// labeled-statement:
216/// 'case' constant-expression ':' statement
217/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
218///
219/// Note that this does not parse the 'statement' at the end.
220///
221Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000222 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000223 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
224
225 ExprResult LHS = ParseConstantExpression();
226 if (LHS.isInvalid) {
227 SkipUntil(tok::colon);
228 return true;
229 }
230
231 // GNU case range extension.
232 SourceLocation DotDotDotLoc;
233 ExprTy *RHSVal = 0;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000234 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000235 Diag(Tok, diag::ext_gnu_case_range);
236 DotDotDotLoc = ConsumeToken();
237
238 ExprResult RHS = ParseConstantExpression();
239 if (RHS.isInvalid) {
240 SkipUntil(tok::colon);
241 return true;
242 }
243 RHSVal = RHS.Val;
244 }
245
Chris Lattner4d7d2342007-10-09 17:41:39 +0000246 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000247 Diag(Tok, diag::err_expected_colon_after, "'case'");
248 SkipUntil(tok::colon);
249 return true;
250 }
251
252 SourceLocation ColonLoc = ConsumeToken();
253
254 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000255 if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000256 Diag(Tok, diag::err_label_end_of_compound_statement);
257 return true;
258 }
259
260 StmtResult SubStmt = ParseStatement();
261
262 // Broken substmt shouldn't prevent the case from being added to the AST.
263 if (SubStmt.isInvalid)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000264 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000265
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000266 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000267 SubStmt.Val);
268}
269
270/// ParseDefaultStatement
271/// labeled-statement:
272/// 'default' ':' statement
273/// Note that this does not parse the 'statement' at the end.
274///
275Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000276 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000277 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
278
Chris Lattner4d7d2342007-10-09 17:41:39 +0000279 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000280 Diag(Tok, diag::err_expected_colon_after, "'default'");
281 SkipUntil(tok::colon);
282 return true;
283 }
284
285 SourceLocation ColonLoc = ConsumeToken();
286
287 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000288 if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000289 Diag(Tok, diag::err_label_end_of_compound_statement);
290 return true;
291 }
292
293 StmtResult SubStmt = ParseStatement();
294 if (SubStmt.isInvalid)
295 return true;
296
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000297 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000298}
299
300
301/// ParseCompoundStatement - Parse a "{}" block.
302///
303/// compound-statement: [C99 6.8.2]
304/// { block-item-list[opt] }
305/// [GNU] { label-declarations block-item-list } [TODO]
306///
307/// block-item-list:
308/// block-item
309/// block-item-list block-item
310///
311/// block-item:
312/// declaration
Chris Lattner81417722007-08-27 01:01:57 +0000313/// [GNU] '__extension__' declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000314/// statement
315/// [OMP] openmp-directive [TODO]
316///
317/// [GNU] label-declarations:
318/// [GNU] label-declaration
319/// [GNU] label-declarations label-declaration
320///
321/// [GNU] label-declaration:
322/// [GNU] '__label__' identifier-list ';'
323///
324/// [OMP] openmp-directive: [TODO]
325/// [OMP] barrier-directive
326/// [OMP] flush-directive
327///
Chris Lattnerf2b07572007-08-31 21:49:55 +0000328Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000329 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000330
Chris Lattnera7549902007-08-26 06:24:45 +0000331 // Enter a scope to hold everything within the compound stmt. Compound
332 // statements can always hold declarations.
333 EnterScope(Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000334
335 // Parse the statements in the body.
Chris Lattnerf2b07572007-08-31 21:49:55 +0000336 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000337
338 ExitScope();
339 return Body;
340}
341
342
343/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000344/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattner4b009652007-07-25 00:24:17 +0000345/// consume the '}' at the end of the block. It does not manipulate the scope
346/// stack.
Chris Lattnerf2b07572007-08-31 21:49:55 +0000347Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattner4b009652007-07-25 00:24:17 +0000348 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
349
350 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner81417722007-08-27 01:01:57 +0000351 // only allowed at the start of a compound stmt regardless of the language.
Chris Lattner4b009652007-07-25 00:24:17 +0000352
353 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000354 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner81417722007-08-27 01:01:57 +0000355 StmtResult R;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000356 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner81417722007-08-27 01:01:57 +0000357 R = ParseStatementOrDeclaration(false);
358 } else {
359 // __extension__ can start declarations and it can also be a unary
360 // operator for expressions. Consume multiple __extension__ markers here
361 // until we can determine which is which.
362 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000363 while (Tok.is(tok::kw___extension__))
Chris Lattner81417722007-08-27 01:01:57 +0000364 ConsumeToken();
365
Chris Lattner1ede3302008-03-13 06:32:11 +0000366 // __extension__ silences extension warnings in the subexpression.
367 bool SavedExtWarn = Diags.getWarnOnExtensions();
368 Diags.setWarnOnExtensions(false);
369
Chris Lattner81417722007-08-27 01:01:57 +0000370 // If this is the start of a declaration, parse it as such.
371 if (isDeclarationSpecifier()) {
372 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattnera4ff4272008-03-13 06:29:04 +0000373 SourceLocation DeclStart = Tok.getLocation();
374 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
375 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerdaf1c312008-03-13 06:29:54 +0000376 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner1ede3302008-03-13 06:32:11 +0000377
378 Diags.setWarnOnExtensions(SavedExtWarn);
Chris Lattner81417722007-08-27 01:01:57 +0000379 } else {
380 // Otherwise this was a unary __extension__ marker. Parse the
381 // subexpression and add the __extension__ unary op.
Chris Lattner81417722007-08-27 01:01:57 +0000382 ExprResult Res = ParseCastExpression(false);
Chris Lattner1ede3302008-03-13 06:32:11 +0000383 Diags.setWarnOnExtensions(SavedExtWarn);
384
Chris Lattner81417722007-08-27 01:01:57 +0000385 if (Res.isInvalid) {
386 SkipUntil(tok::semi);
387 continue;
388 }
389
390 // Add the __extension__ node to the AST.
Steve Naroff87d58b42007-09-16 03:34:24 +0000391 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner81417722007-08-27 01:01:57 +0000392 if (Res.isInvalid)
393 continue;
394
395 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
396 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000397 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner81417722007-08-27 01:01:57 +0000398 }
399 }
400
Chris Lattner4b009652007-07-25 00:24:17 +0000401 if (!R.isInvalid && R.Val)
402 Stmts.push_back(R.Val);
403 }
404
405 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000406 if (Tok.isNot(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000407 Diag(Tok, diag::err_expected_rbrace);
Steve Naroff098c10a2008-01-31 18:29:10 +0000408 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000409 }
410
411 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000412 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattnerf2b07572007-08-31 21:49:55 +0000413 &Stmts[0], Stmts.size(), isStmtExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000414}
415
416/// ParseIfStatement
417/// if-statement: [C99 6.8.4.1]
418/// 'if' '(' expression ')' statement
419/// 'if' '(' expression ')' statement 'else' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000420/// [C++] 'if' '(' condition ')' statement
421/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner4b009652007-07-25 00:24:17 +0000422///
423Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000424 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000425 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
426
Chris Lattner4d7d2342007-10-09 17:41:39 +0000427 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000428 Diag(Tok, diag::err_expected_lparen_after, "if");
429 SkipUntil(tok::semi);
430 return true;
431 }
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000432
Chris Lattnere0cc5082007-08-26 23:08:06 +0000433 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
434 // the case for C90.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000435 if (getLang().C99)
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000436 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000437
Chris Lattner4b009652007-07-25 00:24:17 +0000438 // Parse the condition.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000439 ExprResult CondExp;
440 if (getLang().CPlusPlus) {
441 SourceLocation LParenLoc = ConsumeParen();
442 CondExp = ParseCXXCondition();
443 MatchRHSPunctuation(tok::r_paren, LParenLoc);
444 } else {
445 CondExp = ParseSimpleParenExpression();
446 }
447
Chris Lattner4b009652007-07-25 00:24:17 +0000448 if (CondExp.isInvalid) {
449 SkipUntil(tok::semi);
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000450 if (getLang().C99)
Chris Lattnere0cc5082007-08-26 23:08:06 +0000451 ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000452 return true;
453 }
454
Chris Lattnerf446f722007-08-22 05:28:50 +0000455 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000456 // there is no compound stmt. C90 does not have this clause. We only do this
457 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000458 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000459 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000460
Chris Lattner84b20712007-10-29 05:08:52 +0000461 // Read the 'then' stmt.
462 SourceLocation ThenStmtLoc = Tok.getLocation();
463 StmtResult ThenStmt = ParseStatement();
Chris Lattner4b009652007-07-25 00:24:17 +0000464
Chris Lattnerd190ac22007-08-22 05:16:28 +0000465 // Pop the 'if' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000466 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000467
468 // If it has an else, parse it.
469 SourceLocation ElseLoc;
Chris Lattner84b20712007-10-29 05:08:52 +0000470 SourceLocation ElseStmtLoc;
Chris Lattner4b009652007-07-25 00:24:17 +0000471 StmtResult ElseStmt(false);
Chris Lattner84b20712007-10-29 05:08:52 +0000472
Chris Lattner4d7d2342007-10-09 17:41:39 +0000473 if (Tok.is(tok::kw_else)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000474 ElseLoc = ConsumeToken();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000475
Chris Lattnerf446f722007-08-22 05:28:50 +0000476 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000477 // there is no compound stmt. C90 does not have this clause. We only do
478 // this if the body isn't a compound statement to avoid push/pop in common
479 // cases.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000480 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000481 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerd190ac22007-08-22 05:16:28 +0000482
Chris Lattner84b20712007-10-29 05:08:52 +0000483 ElseStmtLoc = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000484 ElseStmt = ParseStatement();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000485
486 // Pop the 'else' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000487 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000488 }
489
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000490 if (getLang().C99)
Chris Lattnere0cc5082007-08-26 23:08:06 +0000491 ExitScope();
492
Chris Lattner84b20712007-10-29 05:08:52 +0000493 // If the then or else stmt is invalid and the other is valid (and present),
494 // make turn the invalid one into a null stmt to avoid dropping the other
495 // part. If both are invalid, return error.
496 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
497 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
498 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
499 // Both invalid, or one is invalid and other is non-present: delete cond and
500 // return error.
501 Actions.DeleteExpr(CondExp.Val);
502 return true;
503 }
504
505 // Now if either are invalid, replace with a ';'.
506 if (ThenStmt.isInvalid)
507 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
508 if (ElseStmt.isInvalid)
509 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
510
Chris Lattner84b20712007-10-29 05:08:52 +0000511 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Chris Lattner4b009652007-07-25 00:24:17 +0000512 ElseLoc, ElseStmt.Val);
513}
514
515/// ParseSwitchStatement
516/// switch-statement:
517/// 'switch' '(' expression ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000518/// [C++] 'switch' '(' condition ')' statement
Chris Lattner4b009652007-07-25 00:24:17 +0000519Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000520 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000521 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
522
Chris Lattner4d7d2342007-10-09 17:41:39 +0000523 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000524 Diag(Tok, diag::err_expected_lparen_after, "switch");
525 SkipUntil(tok::semi);
526 return true;
527 }
Chris Lattnere0cc5082007-08-26 23:08:06 +0000528
529 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
530 // not the case for C90. Start the switch scope.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000531 if (getLang().C99)
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000532 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000533 else
534 EnterScope(Scope::BreakScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000535
536 // Parse the condition.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000537 ExprResult Cond;
538 if (getLang().CPlusPlus) {
539 SourceLocation LParenLoc = ConsumeParen();
540 Cond = ParseCXXCondition();
541 MatchRHSPunctuation(tok::r_paren, LParenLoc);
542 } else {
543 Cond = ParseSimpleParenExpression();
544 }
Chris Lattner4b009652007-07-25 00:24:17 +0000545
546 if (Cond.isInvalid) {
547 ExitScope();
548 return true;
549 }
550
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000551 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000552
Chris Lattnerf446f722007-08-22 05:28:50 +0000553 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000554 // there is no compound stmt. C90 does not have this clause. We only do this
555 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000556 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000557 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000558
Chris Lattner4b009652007-07-25 00:24:17 +0000559 // Read the body statement.
560 StmtResult Body = ParseStatement();
561
Chris Lattnerf446f722007-08-22 05:28:50 +0000562 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000563 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000564
Chris Lattner4b009652007-07-25 00:24:17 +0000565 if (Body.isInvalid) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000566 Body = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000567 // FIXME: Remove the case statement list from the Switch statement.
568 }
569
570 ExitScope();
571
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000572 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000573}
574
575/// ParseWhileStatement
576/// while-statement: [C99 6.8.5.1]
577/// 'while' '(' expression ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000578/// [C++] 'while' '(' condition ')' statement
Chris Lattner4b009652007-07-25 00:24:17 +0000579Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000580 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000581 SourceLocation WhileLoc = Tok.getLocation();
582 ConsumeToken(); // eat the 'while'.
583
Chris Lattner4d7d2342007-10-09 17:41:39 +0000584 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000585 Diag(Tok, diag::err_expected_lparen_after, "while");
586 SkipUntil(tok::semi);
587 return true;
588 }
589
Chris Lattnere0cc5082007-08-26 23:08:06 +0000590 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
591 // the case for C90. Start the loop scope.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000592 if (getLang().C99)
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000593 EnterScope(Scope::BreakScope | Scope::ContinueScope |
594 Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000595 else
596 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000597
598 // Parse the condition.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000599 ExprResult Cond;
600 if (getLang().CPlusPlus) {
601 SourceLocation LParenLoc = ConsumeParen();
602 Cond = ParseCXXCondition();
603 MatchRHSPunctuation(tok::r_paren, LParenLoc);
604 } else {
605 Cond = ParseSimpleParenExpression();
606 }
Chris Lattner4b009652007-07-25 00:24:17 +0000607
Chris Lattnerf446f722007-08-22 05:28:50 +0000608 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000609 // there is no compound stmt. C90 does not have this clause. We only do this
610 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000611 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000612 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000613
Chris Lattner4b009652007-07-25 00:24:17 +0000614 // Read the body statement.
615 StmtResult Body = ParseStatement();
616
Chris Lattnerf446f722007-08-22 05:28:50 +0000617 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000618 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000619
Chris Lattner4b009652007-07-25 00:24:17 +0000620 ExitScope();
621
622 if (Cond.isInvalid || Body.isInvalid) return true;
623
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000624 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000625}
626
627/// ParseDoStatement
628/// do-statement: [C99 6.8.5.2]
629/// 'do' statement 'while' '(' expression ')' ';'
630/// Note: this lets the caller parse the end ';'.
631Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000632 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000633 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
634
Chris Lattnere0cc5082007-08-26 23:08:06 +0000635 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
636 // the case for C90. Start the loop scope.
637 if (getLang().C99)
638 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
639 else
640 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000641
Chris Lattnerf446f722007-08-22 05:28:50 +0000642 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000643 // there is no compound stmt. C90 does not have this clause. We only do this
644 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000645 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000646 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000647
Chris Lattner4b009652007-07-25 00:24:17 +0000648 // Read the body statement.
649 StmtResult Body = ParseStatement();
650
Chris Lattnerf446f722007-08-22 05:28:50 +0000651 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000652 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000653
Chris Lattner4d7d2342007-10-09 17:41:39 +0000654 if (Tok.isNot(tok::kw_while)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000655 ExitScope();
656 Diag(Tok, diag::err_expected_while);
657 Diag(DoLoc, diag::err_matching, "do");
658 SkipUntil(tok::semi);
659 return true;
660 }
661 SourceLocation WhileLoc = ConsumeToken();
662
Chris Lattner4d7d2342007-10-09 17:41:39 +0000663 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000664 ExitScope();
665 Diag(Tok, diag::err_expected_lparen_after, "do/while");
666 SkipUntil(tok::semi);
667 return true;
668 }
669
670 // Parse the condition.
671 ExprResult Cond = ParseSimpleParenExpression();
672
673 ExitScope();
674
675 if (Cond.isInvalid || Body.isInvalid) return true;
676
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000677 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000678}
679
680/// ParseForStatement
681/// for-statement: [C99 6.8.5.3]
682/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
683/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000684/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
685/// [C++] statement
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000686/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
687/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000688///
689/// [C++] for-init-statement:
690/// [C++] expression-statement
691/// [C++] simple-declaration
692///
Chris Lattner4b009652007-07-25 00:24:17 +0000693Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000694 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000695 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
696
Chris Lattner4d7d2342007-10-09 17:41:39 +0000697 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000698 Diag(Tok, diag::err_expected_lparen_after, "for");
699 SkipUntil(tok::semi);
700 return true;
701 }
702
Chris Lattnere0cc5082007-08-26 23:08:06 +0000703 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
704 // the case for C90. Start the loop scope.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000705 if (getLang().C99)
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000706 EnterScope(Scope::BreakScope | Scope::ContinueScope |
707 Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000708 else
709 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000710
711 SourceLocation LParenLoc = ConsumeParen();
712 ExprResult Value;
713
714 StmtTy *FirstPart = 0;
715 ExprTy *SecondPart = 0;
716 StmtTy *ThirdPart = 0;
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000717 bool ForEach = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000718
719 // Parse the first part of the for specifier.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000720 if (Tok.is(tok::semi)) { // for (;
Chris Lattner4b009652007-07-25 00:24:17 +0000721 // no first part, eat the ';'.
722 ConsumeToken();
723 } else if (isDeclarationSpecifier()) { // for (int X = 4;
724 // Parse declaration, which eats the ';'.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000725 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
Chris Lattner4b009652007-07-25 00:24:17 +0000726 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattnera4ff4272008-03-13 06:29:04 +0000727
728 SourceLocation DeclStart = Tok.getLocation();
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000729 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattnera4ff4272008-03-13 06:29:04 +0000730 // FIXME: Pass in the right location for the end of the declstmt.
731 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattnerdaf1c312008-03-13 06:29:54 +0000732 DeclStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000733 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000734 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000735 ConsumeToken(); // consume 'in'
736 Value = ParseExpression();
737 if (!Value.isInvalid)
738 SecondPart = Value.Val;
739 }
Chris Lattner4b009652007-07-25 00:24:17 +0000740 } else {
741 Value = ParseExpression();
742
743 // Turn the expression into a stmt.
744 if (!Value.isInvalid) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000745 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000746 if (!R.isInvalid)
747 FirstPart = R.Val;
748 }
749
Chris Lattner4d7d2342007-10-09 17:41:39 +0000750 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000751 ConsumeToken();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000752 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000753 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000754 ConsumeToken(); // consume 'in'
755 Value = ParseExpression();
756 if (!Value.isInvalid)
757 SecondPart = Value.Val;
758 }
759 else {
Chris Lattner4b009652007-07-25 00:24:17 +0000760 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
761 SkipUntil(tok::semi);
762 }
763 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000764 if (!ForEach) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000765 // Parse the second part of the for specifier.
766 if (Tok.is(tok::semi)) { // for (...;;
767 // no second part.
768 Value = ExprResult();
769 } else {
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000770 Value = getLang().CPlusPlus ? ParseCXXCondition()
771 : ParseExpression();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000772 if (!Value.isInvalid)
773 SecondPart = Value.Val;
774 }
Chris Lattner4b009652007-07-25 00:24:17 +0000775
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000776 if (Tok.is(tok::semi)) {
777 ConsumeToken();
778 } else {
779 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
780 SkipUntil(tok::semi);
781 }
Chris Lattner4b009652007-07-25 00:24:17 +0000782
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000783 // Parse the third part of the for specifier.
784 if (Tok.is(tok::r_paren)) { // for (...;...;)
785 // no third part.
786 Value = ExprResult();
787 } else {
788 Value = ParseExpression();
789 if (!Value.isInvalid) {
790 // Turn the expression into a stmt.
791 StmtResult R = Actions.ActOnExprStmt(Value.Val);
792 if (!R.isInvalid)
793 ThirdPart = R.Val;
794 }
Chris Lattner4b009652007-07-25 00:24:17 +0000795 }
796 }
Chris Lattner4b009652007-07-25 00:24:17 +0000797 // Match the ')'.
798 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
799
Chris Lattnerf446f722007-08-22 05:28:50 +0000800 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000801 // there is no compound stmt. C90 does not have this clause. We only do this
802 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisa8e14d82008-09-10 23:46:08 +0000803 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000804 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000805
Chris Lattner4b009652007-07-25 00:24:17 +0000806 // Read the body statement.
807 StmtResult Body = ParseStatement();
808
Chris Lattnerf446f722007-08-22 05:28:50 +0000809 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000810 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000811
Chris Lattner4b009652007-07-25 00:24:17 +0000812 // Leave the for-scope.
813 ExitScope();
814
815 if (Body.isInvalid)
816 return Body;
817
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000818 if (!ForEach)
819 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
820 SecondPart, ThirdPart, RParenLoc, Body.Val);
821 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000822 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000823 SecondPart, RParenLoc, Body.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000824}
825
826/// ParseGotoStatement
827/// jump-statement:
828/// 'goto' identifier ';'
829/// [GNU] 'goto' '*' expression ';'
830///
831/// Note: this lets the caller parse the end ';'.
832///
833Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000834 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000835 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
836
837 StmtResult Res;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000838 if (Tok.is(tok::identifier)) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000839 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000840 Tok.getIdentifierInfo());
841 ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000842 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner4b009652007-07-25 00:24:17 +0000843 // GNU indirect goto extension.
844 Diag(Tok, diag::ext_gnu_indirect_goto);
845 SourceLocation StarLoc = ConsumeToken();
846 ExprResult R = ParseExpression();
847 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
848 SkipUntil(tok::semi, false, true);
849 return true;
850 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000851 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000852 } else {
853 Diag(Tok, diag::err_expected_ident);
854 return true;
855 }
856
857 return Res;
858}
859
860/// ParseContinueStatement
861/// jump-statement:
862/// 'continue' ';'
863///
864/// Note: this lets the caller parse the end ';'.
865///
866Parser::StmtResult Parser::ParseContinueStatement() {
867 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000868 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000869}
870
871/// ParseBreakStatement
872/// jump-statement:
873/// 'break' ';'
874///
875/// Note: this lets the caller parse the end ';'.
876///
877Parser::StmtResult Parser::ParseBreakStatement() {
878 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000879 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000880}
881
882/// ParseReturnStatement
883/// jump-statement:
884/// 'return' expression[opt] ';'
885Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000886 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000887 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
888
889 ExprResult R(0);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000890 if (Tok.isNot(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000891 R = ParseExpression();
892 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
893 SkipUntil(tok::semi, false, true);
894 return true;
895 }
896 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000897 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000898}
899
Steve Naroff6f9f9552008-02-11 23:15:56 +0000900/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
901/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroff73a07032008-02-07 03:50:06 +0000902Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffbe880ec2008-02-07 23:24:32 +0000903 if (Tok.is(tok::l_brace)) {
904 unsigned short savedBraceCount = BraceCount;
905 do {
906 ConsumeAnyToken();
907 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
908 } else {
909 // From the MS website: If used without braces, the __asm keyword means
910 // that the rest of the line is an assembly-language statement.
911 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffab3dfe02008-02-08 03:36:19 +0000912 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8ce442a2008-02-08 18:01:27 +0000913 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
914 do {
915 ConsumeAnyToken();
916 TokLoc = Tok.getLocation();
917 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
918 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
919 Tok.isNot(tok::eof));
Steve Naroffbe880ec2008-02-07 23:24:32 +0000920 }
Steve Naroff6a522812008-04-07 21:06:54 +0000921 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroff73a07032008-02-07 03:50:06 +0000922}
923
Chris Lattner4b009652007-07-25 00:24:17 +0000924/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff6f9f9552008-02-11 23:15:56 +0000925/// asm-statement:
926/// gnu-asm-statement
927/// ms-asm-statement
928///
929/// [GNU] gnu-asm-statement:
Chris Lattner4b009652007-07-25 00:24:17 +0000930/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
931///
932/// [GNU] asm-argument:
933/// asm-string-literal
934/// asm-string-literal ':' asm-operands[opt]
935/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
936/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
937/// ':' asm-clobbers
938///
939/// [GNU] asm-clobbers:
940/// asm-string-literal
941/// asm-clobbers ',' asm-string-literal
942///
Steve Naroff6f9f9552008-02-11 23:15:56 +0000943/// [MS] ms-asm-statement:
944/// '__asm' assembly-instruction ';'[opt]
945/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
946///
947/// [MS] assembly-instruction-list:
948/// assembly-instruction ';'[opt]
949/// assembly-instruction-list ';' assembly-instruction ';'[opt]
950///
Steve Naroff73a07032008-02-07 03:50:06 +0000951Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000952 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner8a40a832007-10-29 04:04:16 +0000953 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000954
Steve Naroff6f9f9552008-02-11 23:15:56 +0000955 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroff73a07032008-02-07 03:50:06 +0000956 msAsm = true;
957 return FuzzyParseMicrosoftAsmStatement();
958 }
Chris Lattner4b009652007-07-25 00:24:17 +0000959 DeclSpec DS;
960 SourceLocation Loc = Tok.getLocation();
961 ParseTypeQualifierListOpt(DS);
962
963 // GNU asms accept, but warn, about type-qualifiers other than volatile.
964 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
965 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
966 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
967 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
968
969 // Remember if this was a volatile asm.
Anders Carlsson759f45d2007-11-23 23:12:25 +0000970 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000971 bool isSimple = false;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000972 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000973 Diag(Tok, diag::err_expected_lparen_after, "asm");
974 SkipUntil(tok::r_paren);
975 return true;
976 }
977 Loc = ConsumeParen();
978
Anders Carlsson076c1112007-11-20 19:21:03 +0000979 ExprResult AsmString = ParseAsmStringLiteral();
980 if (AsmString.isInvalid)
981 return true;
Anders Carlsson965d5202007-11-22 01:36:19 +0000982
983 llvm::SmallVector<std::string, 4> Names;
984 llvm::SmallVector<ExprTy*, 4> Constraints;
985 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlsson965d5202007-11-22 01:36:19 +0000986 llvm::SmallVector<ExprTy*, 4> Clobbers;
Chris Lattner4b009652007-07-25 00:24:17 +0000987
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000988 unsigned NumInputs = 0, NumOutputs = 0;
989
990 SourceLocation RParenLoc;
991 if (Tok.is(tok::r_paren)) {
992 // We have a simple asm expression
993 isSimple = true;
994
995 RParenLoc = ConsumeParen();
996 } else {
997 // Parse Outputs, if present.
Anders Carlsson749d7b02008-02-09 19:57:29 +0000998 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
999 return true;
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001000
1001 NumOutputs = Names.size();
1002
1003 // Parse Inputs, if present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001004 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1005 return true;
1006
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001007 assert(Names.size() == Constraints.size() &&
1008 Constraints.size() == Exprs.size()
1009 && "Input operand size mismatch!");
1010
1011 NumInputs = Names.size() - NumOutputs;
1012
1013 // Parse the clobbers, if present.
1014 if (Tok.is(tok::colon)) {
Anders Carlsson861a2852007-11-21 23:27:34 +00001015 ConsumeToken();
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001016
1017 // Parse the asm-string list for clobbers.
1018 while (1) {
1019 ExprResult Clobber = ParseAsmStringLiteral();
1020
1021 if (Clobber.isInvalid)
1022 break;
1023
1024 Clobbers.push_back(Clobber.Val);
1025
1026 if (Tok.isNot(tok::comma)) break;
1027 ConsumeToken();
1028 }
Chris Lattner4b009652007-07-25 00:24:17 +00001029 }
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001030
1031 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001032 }
1033
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001034 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1035 NumOutputs, NumInputs,
Anders Carlsson965d5202007-11-22 01:36:19 +00001036 &Names[0], &Constraints[0], &Exprs[0],
1037 AsmString.Val,
1038 Clobbers.size(), &Clobbers[0],
1039 RParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001040}
1041
1042/// ParseAsmOperands - Parse the asm-operands production as used by
1043/// asm-statement. We also parse a leading ':' token. If the leading colon is
1044/// not present, we do not parse anything.
1045///
1046/// [GNU] asm-operands:
1047/// asm-operand
1048/// asm-operands ',' asm-operand
1049///
1050/// [GNU] asm-operand:
1051/// asm-string-literal '(' expression ')'
1052/// '[' identifier ']' asm-string-literal '(' expression ')'
1053///
Anders Carlsson749d7b02008-02-09 19:57:29 +00001054bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson965d5202007-11-22 01:36:19 +00001055 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1056 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner4b009652007-07-25 00:24:17 +00001057 // Only do anything if this operand is present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001058 if (Tok.isNot(tok::colon)) return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001059 ConsumeToken();
1060
1061 // 'asm-operands' isn't present?
Chris Lattner4d7d2342007-10-09 17:41:39 +00001062 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson749d7b02008-02-09 19:57:29 +00001063 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001064
Anders Carlsson965d5202007-11-22 01:36:19 +00001065 while (1) {
Chris Lattner4b009652007-07-25 00:24:17 +00001066 // Read the [id] if present.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001067 if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001068 SourceLocation Loc = ConsumeBracket();
1069
Chris Lattner4d7d2342007-10-09 17:41:39 +00001070 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001071 Diag(Tok, diag::err_expected_ident);
1072 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001073 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001074 }
Chris Lattner4e21a9b2007-10-29 04:06:22 +00001075
Anders Carlsson965d5202007-11-22 01:36:19 +00001076 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner4e21a9b2007-10-29 04:06:22 +00001077 ConsumeToken();
Anders Carlsson965d5202007-11-22 01:36:19 +00001078
1079 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner4b009652007-07-25 00:24:17 +00001080 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson965d5202007-11-22 01:36:19 +00001081 } else
1082 Names.push_back(std::string());
Chris Lattner4b009652007-07-25 00:24:17 +00001083
Anders Carlsson965d5202007-11-22 01:36:19 +00001084 ExprResult Constraint = ParseAsmStringLiteral();
1085 if (Constraint.isInvalid) {
1086 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001087 return true;
Anders Carlsson965d5202007-11-22 01:36:19 +00001088 }
1089 Constraints.push_back(Constraint.Val);
Chris Lattner4b009652007-07-25 00:24:17 +00001090
Chris Lattner4d7d2342007-10-09 17:41:39 +00001091 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001092 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1093 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001094 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001095 }
1096
1097 // Read the parenthesized expression.
1098 ExprResult Res = ParseSimpleParenExpression();
1099 if (Res.isInvalid) {
1100 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001101 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001102 }
Anders Carlsson965d5202007-11-22 01:36:19 +00001103 Exprs.push_back(Res.Val);
Chris Lattner4b009652007-07-25 00:24:17 +00001104 // Eat the comma and continue parsing if it exists.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001105 if (Tok.isNot(tok::comma)) return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001106 ConsumeToken();
1107 }
Anders Carlsson749d7b02008-02-09 19:57:29 +00001108
1109 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001110}
Fariborz Jahanian829dfe52007-11-08 19:01:26 +00001111
1112Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1113 SourceLocation L, SourceLocation R) {
1114 // Do not enter a scope for the brace, as the arguments are in the same scope
1115 // (the function body) as the body itself. Instead, just read the statement
1116 // list and put it into a CompoundStmt for safe keeping.
1117 StmtResult FnBody = ParseCompoundStatementBody();
1118
1119 // If the function body could not be parsed, make a bogus compoundstmt.
1120 if (FnBody.isInvalid)
1121 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1122
1123 // Leave the function body scope.
1124 ExitScope();
1125
Steve Naroff99ee4302007-11-11 23:20:51 +00001126 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeone128c1d2007-12-01 08:06:07 +00001127}