blob: 83ec302fc4b77c16d2302c9a1c18853952255f62 [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
433 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
434
Chris Lattnere0cc5082007-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.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000437 if (C99orCXX)
438 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000439
Chris Lattner4b009652007-07-25 00:24:17 +0000440 // Parse the condition.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000441 ExprResult CondExp;
442 if (getLang().CPlusPlus) {
443 SourceLocation LParenLoc = ConsumeParen();
444 CondExp = ParseCXXCondition();
445 MatchRHSPunctuation(tok::r_paren, LParenLoc);
446 } else {
447 CondExp = ParseSimpleParenExpression();
448 }
449
Chris Lattner4b009652007-07-25 00:24:17 +0000450 if (CondExp.isInvalid) {
451 SkipUntil(tok::semi);
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000452 if (C99orCXX)
Chris Lattnere0cc5082007-08-26 23:08:06 +0000453 ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000454 return true;
455 }
456
Chris Lattnerf446f722007-08-22 05:28:50 +0000457 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000458 // there is no compound stmt. C90 does not have this clause. We only do this
459 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000460 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000461 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000462
Chris Lattner84b20712007-10-29 05:08:52 +0000463 // Read the 'then' stmt.
464 SourceLocation ThenStmtLoc = Tok.getLocation();
465 StmtResult ThenStmt = ParseStatement();
Chris Lattner4b009652007-07-25 00:24:17 +0000466
Chris Lattnerd190ac22007-08-22 05:16:28 +0000467 // Pop the 'if' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000468 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000469
470 // If it has an else, parse it.
471 SourceLocation ElseLoc;
Chris Lattner84b20712007-10-29 05:08:52 +0000472 SourceLocation ElseStmtLoc;
Chris Lattner4b009652007-07-25 00:24:17 +0000473 StmtResult ElseStmt(false);
Chris Lattner84b20712007-10-29 05:08:52 +0000474
Chris Lattner4d7d2342007-10-09 17:41:39 +0000475 if (Tok.is(tok::kw_else)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000476 ElseLoc = ConsumeToken();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000477
Chris Lattnerf446f722007-08-22 05:28:50 +0000478 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000479 // there is no compound stmt. C90 does not have this clause. We only do
480 // this if the body isn't a compound statement to avoid push/pop in common
481 // cases.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000482 NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000483 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerd190ac22007-08-22 05:16:28 +0000484
Chris Lattner84b20712007-10-29 05:08:52 +0000485 ElseStmtLoc = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000486 ElseStmt = ParseStatement();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000487
488 // Pop the 'else' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000489 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000490 }
491
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000492 if (C99orCXX)
Chris Lattnere0cc5082007-08-26 23:08:06 +0000493 ExitScope();
494
Chris Lattner84b20712007-10-29 05:08:52 +0000495 // If the then or else stmt is invalid and the other is valid (and present),
496 // make turn the invalid one into a null stmt to avoid dropping the other
497 // part. If both are invalid, return error.
498 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
499 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
500 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
501 // Both invalid, or one is invalid and other is non-present: delete cond and
502 // return error.
503 Actions.DeleteExpr(CondExp.Val);
504 return true;
505 }
506
507 // Now if either are invalid, replace with a ';'.
508 if (ThenStmt.isInvalid)
509 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
510 if (ElseStmt.isInvalid)
511 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
512
Chris Lattner84b20712007-10-29 05:08:52 +0000513 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Chris Lattner4b009652007-07-25 00:24:17 +0000514 ElseLoc, ElseStmt.Val);
515}
516
517/// ParseSwitchStatement
518/// switch-statement:
519/// 'switch' '(' expression ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000520/// [C++] 'switch' '(' condition ')' statement
Chris Lattner4b009652007-07-25 00:24:17 +0000521Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000522 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000523 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
524
Chris Lattner4d7d2342007-10-09 17:41:39 +0000525 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000526 Diag(Tok, diag::err_expected_lparen_after, "switch");
527 SkipUntil(tok::semi);
528 return true;
529 }
Chris Lattnere0cc5082007-08-26 23:08:06 +0000530
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000531 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
532
Chris Lattnere0cc5082007-08-26 23:08:06 +0000533 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
534 // not the case for C90. Start the switch scope.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000535 if (C99orCXX)
536 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000537 else
538 EnterScope(Scope::BreakScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000539
540 // Parse the condition.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000541 ExprResult Cond;
542 if (getLang().CPlusPlus) {
543 SourceLocation LParenLoc = ConsumeParen();
544 Cond = ParseCXXCondition();
545 MatchRHSPunctuation(tok::r_paren, LParenLoc);
546 } else {
547 Cond = ParseSimpleParenExpression();
548 }
Chris Lattner4b009652007-07-25 00:24:17 +0000549
550 if (Cond.isInvalid) {
551 ExitScope();
552 return true;
553 }
554
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000555 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000556
Chris Lattnerf446f722007-08-22 05:28:50 +0000557 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000558 // there is no compound stmt. C90 does not have this clause. We only do this
559 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000560 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000561 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000562
Chris Lattner4b009652007-07-25 00:24:17 +0000563 // Read the body statement.
564 StmtResult Body = ParseStatement();
565
Chris Lattnerf446f722007-08-22 05:28:50 +0000566 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000567 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000568
Chris Lattner4b009652007-07-25 00:24:17 +0000569 if (Body.isInvalid) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000570 Body = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000571 // FIXME: Remove the case statement list from the Switch statement.
572 }
573
574 ExitScope();
575
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000576 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000577}
578
579/// ParseWhileStatement
580/// while-statement: [C99 6.8.5.1]
581/// 'while' '(' expression ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000582/// [C++] 'while' '(' condition ')' statement
Chris Lattner4b009652007-07-25 00:24:17 +0000583Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000584 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000585 SourceLocation WhileLoc = Tok.getLocation();
586 ConsumeToken(); // eat the 'while'.
587
Chris Lattner4d7d2342007-10-09 17:41:39 +0000588 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000589 Diag(Tok, diag::err_expected_lparen_after, "while");
590 SkipUntil(tok::semi);
591 return true;
592 }
593
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000594 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
595
Chris Lattnere0cc5082007-08-26 23:08:06 +0000596 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
597 // the case for C90. Start the loop scope.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000598 if (C99orCXX)
599 EnterScope(Scope::BreakScope | Scope::ContinueScope |
600 Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000601 else
602 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000603
604 // Parse the condition.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000605 ExprResult Cond;
606 if (getLang().CPlusPlus) {
607 SourceLocation LParenLoc = ConsumeParen();
608 Cond = ParseCXXCondition();
609 MatchRHSPunctuation(tok::r_paren, LParenLoc);
610 } else {
611 Cond = ParseSimpleParenExpression();
612 }
Chris Lattner4b009652007-07-25 00:24:17 +0000613
Chris Lattnerf446f722007-08-22 05:28:50 +0000614 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000615 // there is no compound stmt. C90 does not have this clause. We only do this
616 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000617 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000618 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000619
Chris Lattner4b009652007-07-25 00:24:17 +0000620 // Read the body statement.
621 StmtResult Body = ParseStatement();
622
Chris Lattnerf446f722007-08-22 05:28:50 +0000623 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000624 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000625
Chris Lattner4b009652007-07-25 00:24:17 +0000626 ExitScope();
627
628 if (Cond.isInvalid || Body.isInvalid) return true;
629
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000630 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000631}
632
633/// ParseDoStatement
634/// do-statement: [C99 6.8.5.2]
635/// 'do' statement 'while' '(' expression ')' ';'
636/// Note: this lets the caller parse the end ';'.
637Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000638 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000639 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
640
Chris Lattnere0cc5082007-08-26 23:08:06 +0000641 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
642 // the case for C90. Start the loop scope.
643 if (getLang().C99)
644 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
645 else
646 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000647
Chris Lattnerf446f722007-08-22 05:28:50 +0000648 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000649 // there is no compound stmt. C90 does not have this clause. We only do this
650 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000651 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000652 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000653
Chris Lattner4b009652007-07-25 00:24:17 +0000654 // Read the body statement.
655 StmtResult Body = ParseStatement();
656
Chris Lattnerf446f722007-08-22 05:28:50 +0000657 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000658 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000659
Chris Lattner4d7d2342007-10-09 17:41:39 +0000660 if (Tok.isNot(tok::kw_while)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000661 ExitScope();
662 Diag(Tok, diag::err_expected_while);
663 Diag(DoLoc, diag::err_matching, "do");
664 SkipUntil(tok::semi);
665 return true;
666 }
667 SourceLocation WhileLoc = ConsumeToken();
668
Chris Lattner4d7d2342007-10-09 17:41:39 +0000669 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000670 ExitScope();
671 Diag(Tok, diag::err_expected_lparen_after, "do/while");
672 SkipUntil(tok::semi);
673 return true;
674 }
675
676 // Parse the condition.
677 ExprResult Cond = ParseSimpleParenExpression();
678
679 ExitScope();
680
681 if (Cond.isInvalid || Body.isInvalid) return true;
682
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000683 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000684}
685
686/// ParseForStatement
687/// for-statement: [C99 6.8.5.3]
688/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
689/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000690/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
691/// [C++] statement
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000692/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
693/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000694///
695/// [C++] for-init-statement:
696/// [C++] expression-statement
697/// [C++] simple-declaration
698///
Chris Lattner4b009652007-07-25 00:24:17 +0000699Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000700 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000701 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
702
Chris Lattner4d7d2342007-10-09 17:41:39 +0000703 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000704 Diag(Tok, diag::err_expected_lparen_after, "for");
705 SkipUntil(tok::semi);
706 return true;
707 }
708
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000709 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
710
Chris Lattnere0cc5082007-08-26 23:08:06 +0000711 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
712 // the case for C90. Start the loop scope.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000713 if (C99orCXX)
714 EnterScope(Scope::BreakScope | Scope::ContinueScope |
715 Scope::DeclScope | Scope::ControlScope);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000716 else
717 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000718
719 SourceLocation LParenLoc = ConsumeParen();
720 ExprResult Value;
721
722 StmtTy *FirstPart = 0;
723 ExprTy *SecondPart = 0;
724 StmtTy *ThirdPart = 0;
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000725 bool ForEach = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000726
727 // Parse the first part of the for specifier.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000728 if (Tok.is(tok::semi)) { // for (;
Chris Lattner4b009652007-07-25 00:24:17 +0000729 // no first part, eat the ';'.
730 ConsumeToken();
731 } else if (isDeclarationSpecifier()) { // for (int X = 4;
732 // Parse declaration, which eats the ';'.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000733 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Chris Lattner4b009652007-07-25 00:24:17 +0000734 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattnera4ff4272008-03-13 06:29:04 +0000735
736 SourceLocation DeclStart = Tok.getLocation();
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000737 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattnera4ff4272008-03-13 06:29:04 +0000738 // FIXME: Pass in the right location for the end of the declstmt.
739 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattnerdaf1c312008-03-13 06:29:54 +0000740 DeclStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000741 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000742 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000743 ConsumeToken(); // consume 'in'
744 Value = ParseExpression();
745 if (!Value.isInvalid)
746 SecondPart = Value.Val;
747 }
Chris Lattner4b009652007-07-25 00:24:17 +0000748 } else {
749 Value = ParseExpression();
750
751 // Turn the expression into a stmt.
752 if (!Value.isInvalid) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000753 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000754 if (!R.isInvalid)
755 FirstPart = R.Val;
756 }
757
Chris Lattner4d7d2342007-10-09 17:41:39 +0000758 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000759 ConsumeToken();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000760 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000761 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000762 ConsumeToken(); // consume 'in'
763 Value = ParseExpression();
764 if (!Value.isInvalid)
765 SecondPart = Value.Val;
766 }
767 else {
Chris Lattner4b009652007-07-25 00:24:17 +0000768 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
769 SkipUntil(tok::semi);
770 }
771 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000772 if (!ForEach) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000773 // Parse the second part of the for specifier.
774 if (Tok.is(tok::semi)) { // for (...;;
775 // no second part.
776 Value = ExprResult();
777 } else {
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000778 Value = getLang().CPlusPlus ? ParseCXXCondition()
779 : ParseExpression();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000780 if (!Value.isInvalid)
781 SecondPart = Value.Val;
782 }
Chris Lattner4b009652007-07-25 00:24:17 +0000783
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000784 if (Tok.is(tok::semi)) {
785 ConsumeToken();
786 } else {
787 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
788 SkipUntil(tok::semi);
789 }
Chris Lattner4b009652007-07-25 00:24:17 +0000790
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000791 // Parse the third part of the for specifier.
792 if (Tok.is(tok::r_paren)) { // for (...;...;)
793 // no third part.
794 Value = ExprResult();
795 } else {
796 Value = ParseExpression();
797 if (!Value.isInvalid) {
798 // Turn the expression into a stmt.
799 StmtResult R = Actions.ActOnExprStmt(Value.Val);
800 if (!R.isInvalid)
801 ThirdPart = R.Val;
802 }
Chris Lattner4b009652007-07-25 00:24:17 +0000803 }
804 }
Chris Lattner4b009652007-07-25 00:24:17 +0000805 // Match the ')'.
806 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
807
Chris Lattnerf446f722007-08-22 05:28:50 +0000808 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000809 // there is no compound stmt. C90 does not have this clause. We only do this
810 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000811 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattnera7549902007-08-26 06:24:45 +0000812 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000813
Chris Lattner4b009652007-07-25 00:24:17 +0000814 // Read the body statement.
815 StmtResult Body = ParseStatement();
816
Chris Lattnerf446f722007-08-22 05:28:50 +0000817 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000818 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000819
Chris Lattner4b009652007-07-25 00:24:17 +0000820 // Leave the for-scope.
821 ExitScope();
822
823 if (Body.isInvalid)
824 return Body;
825
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000826 if (!ForEach)
827 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
828 SecondPart, ThirdPart, RParenLoc, Body.Val);
829 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000830 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000831 SecondPart, RParenLoc, Body.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000832}
833
834/// ParseGotoStatement
835/// jump-statement:
836/// 'goto' identifier ';'
837/// [GNU] 'goto' '*' expression ';'
838///
839/// Note: this lets the caller parse the end ';'.
840///
841Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000842 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000843 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
844
845 StmtResult Res;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000846 if (Tok.is(tok::identifier)) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000847 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000848 Tok.getIdentifierInfo());
849 ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000850 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner4b009652007-07-25 00:24:17 +0000851 // GNU indirect goto extension.
852 Diag(Tok, diag::ext_gnu_indirect_goto);
853 SourceLocation StarLoc = ConsumeToken();
854 ExprResult R = ParseExpression();
855 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
856 SkipUntil(tok::semi, false, true);
857 return true;
858 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000859 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000860 } else {
861 Diag(Tok, diag::err_expected_ident);
862 return true;
863 }
864
865 return Res;
866}
867
868/// ParseContinueStatement
869/// jump-statement:
870/// 'continue' ';'
871///
872/// Note: this lets the caller parse the end ';'.
873///
874Parser::StmtResult Parser::ParseContinueStatement() {
875 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000876 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000877}
878
879/// ParseBreakStatement
880/// jump-statement:
881/// 'break' ';'
882///
883/// Note: this lets the caller parse the end ';'.
884///
885Parser::StmtResult Parser::ParseBreakStatement() {
886 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000887 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000888}
889
890/// ParseReturnStatement
891/// jump-statement:
892/// 'return' expression[opt] ';'
893Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000894 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000895 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
896
897 ExprResult R(0);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000898 if (Tok.isNot(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000899 R = ParseExpression();
900 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
901 SkipUntil(tok::semi, false, true);
902 return true;
903 }
904 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000905 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000906}
907
Steve Naroff6f9f9552008-02-11 23:15:56 +0000908/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
909/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroff73a07032008-02-07 03:50:06 +0000910Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffbe880ec2008-02-07 23:24:32 +0000911 if (Tok.is(tok::l_brace)) {
912 unsigned short savedBraceCount = BraceCount;
913 do {
914 ConsumeAnyToken();
915 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
916 } else {
917 // From the MS website: If used without braces, the __asm keyword means
918 // that the rest of the line is an assembly-language statement.
919 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffab3dfe02008-02-08 03:36:19 +0000920 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8ce442a2008-02-08 18:01:27 +0000921 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
922 do {
923 ConsumeAnyToken();
924 TokLoc = Tok.getLocation();
925 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
926 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
927 Tok.isNot(tok::eof));
Steve Naroffbe880ec2008-02-07 23:24:32 +0000928 }
Steve Naroff6a522812008-04-07 21:06:54 +0000929 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroff73a07032008-02-07 03:50:06 +0000930}
931
Chris Lattner4b009652007-07-25 00:24:17 +0000932/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff6f9f9552008-02-11 23:15:56 +0000933/// asm-statement:
934/// gnu-asm-statement
935/// ms-asm-statement
936///
937/// [GNU] gnu-asm-statement:
Chris Lattner4b009652007-07-25 00:24:17 +0000938/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
939///
940/// [GNU] asm-argument:
941/// asm-string-literal
942/// asm-string-literal ':' asm-operands[opt]
943/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
944/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
945/// ':' asm-clobbers
946///
947/// [GNU] asm-clobbers:
948/// asm-string-literal
949/// asm-clobbers ',' asm-string-literal
950///
Steve Naroff6f9f9552008-02-11 23:15:56 +0000951/// [MS] ms-asm-statement:
952/// '__asm' assembly-instruction ';'[opt]
953/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
954///
955/// [MS] assembly-instruction-list:
956/// assembly-instruction ';'[opt]
957/// assembly-instruction-list ';' assembly-instruction ';'[opt]
958///
Steve Naroff73a07032008-02-07 03:50:06 +0000959Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000960 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner8a40a832007-10-29 04:04:16 +0000961 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000962
Steve Naroff6f9f9552008-02-11 23:15:56 +0000963 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroff73a07032008-02-07 03:50:06 +0000964 msAsm = true;
965 return FuzzyParseMicrosoftAsmStatement();
966 }
Chris Lattner4b009652007-07-25 00:24:17 +0000967 DeclSpec DS;
968 SourceLocation Loc = Tok.getLocation();
969 ParseTypeQualifierListOpt(DS);
970
971 // GNU asms accept, but warn, about type-qualifiers other than volatile.
972 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
973 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
974 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
975 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
976
977 // Remember if this was a volatile asm.
Anders Carlsson759f45d2007-11-23 23:12:25 +0000978 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000979 bool isSimple = false;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000980 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000981 Diag(Tok, diag::err_expected_lparen_after, "asm");
982 SkipUntil(tok::r_paren);
983 return true;
984 }
985 Loc = ConsumeParen();
986
Anders Carlsson076c1112007-11-20 19:21:03 +0000987 ExprResult AsmString = ParseAsmStringLiteral();
988 if (AsmString.isInvalid)
989 return true;
Anders Carlsson965d5202007-11-22 01:36:19 +0000990
991 llvm::SmallVector<std::string, 4> Names;
992 llvm::SmallVector<ExprTy*, 4> Constraints;
993 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlsson965d5202007-11-22 01:36:19 +0000994 llvm::SmallVector<ExprTy*, 4> Clobbers;
Chris Lattner4b009652007-07-25 00:24:17 +0000995
Anders Carlssonde6a9c42008-02-05 23:03:50 +0000996 unsigned NumInputs = 0, NumOutputs = 0;
997
998 SourceLocation RParenLoc;
999 if (Tok.is(tok::r_paren)) {
1000 // We have a simple asm expression
1001 isSimple = true;
1002
1003 RParenLoc = ConsumeParen();
1004 } else {
1005 // Parse Outputs, if present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001006 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1007 return true;
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001008
1009 NumOutputs = Names.size();
1010
1011 // Parse Inputs, if present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001012 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1013 return true;
1014
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001015 assert(Names.size() == Constraints.size() &&
1016 Constraints.size() == Exprs.size()
1017 && "Input operand size mismatch!");
1018
1019 NumInputs = Names.size() - NumOutputs;
1020
1021 // Parse the clobbers, if present.
1022 if (Tok.is(tok::colon)) {
Anders Carlsson861a2852007-11-21 23:27:34 +00001023 ConsumeToken();
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001024
1025 // Parse the asm-string list for clobbers.
1026 while (1) {
1027 ExprResult Clobber = ParseAsmStringLiteral();
1028
1029 if (Clobber.isInvalid)
1030 break;
1031
1032 Clobbers.push_back(Clobber.Val);
1033
1034 if (Tok.isNot(tok::comma)) break;
1035 ConsumeToken();
1036 }
Chris Lattner4b009652007-07-25 00:24:17 +00001037 }
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001038
1039 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001040 }
1041
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001042 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1043 NumOutputs, NumInputs,
Anders Carlsson965d5202007-11-22 01:36:19 +00001044 &Names[0], &Constraints[0], &Exprs[0],
1045 AsmString.Val,
1046 Clobbers.size(), &Clobbers[0],
1047 RParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001048}
1049
1050/// ParseAsmOperands - Parse the asm-operands production as used by
1051/// asm-statement. We also parse a leading ':' token. If the leading colon is
1052/// not present, we do not parse anything.
1053///
1054/// [GNU] asm-operands:
1055/// asm-operand
1056/// asm-operands ',' asm-operand
1057///
1058/// [GNU] asm-operand:
1059/// asm-string-literal '(' expression ')'
1060/// '[' identifier ']' asm-string-literal '(' expression ')'
1061///
Anders Carlsson749d7b02008-02-09 19:57:29 +00001062bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson965d5202007-11-22 01:36:19 +00001063 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1064 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner4b009652007-07-25 00:24:17 +00001065 // Only do anything if this operand is present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001066 if (Tok.isNot(tok::colon)) return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001067 ConsumeToken();
1068
1069 // 'asm-operands' isn't present?
Chris Lattner4d7d2342007-10-09 17:41:39 +00001070 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson749d7b02008-02-09 19:57:29 +00001071 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001072
Anders Carlsson965d5202007-11-22 01:36:19 +00001073 while (1) {
Chris Lattner4b009652007-07-25 00:24:17 +00001074 // Read the [id] if present.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001075 if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001076 SourceLocation Loc = ConsumeBracket();
1077
Chris Lattner4d7d2342007-10-09 17:41:39 +00001078 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001079 Diag(Tok, diag::err_expected_ident);
1080 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001081 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001082 }
Chris Lattner4e21a9b2007-10-29 04:06:22 +00001083
Anders Carlsson965d5202007-11-22 01:36:19 +00001084 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner4e21a9b2007-10-29 04:06:22 +00001085 ConsumeToken();
Anders Carlsson965d5202007-11-22 01:36:19 +00001086
1087 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner4b009652007-07-25 00:24:17 +00001088 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson965d5202007-11-22 01:36:19 +00001089 } else
1090 Names.push_back(std::string());
Chris Lattner4b009652007-07-25 00:24:17 +00001091
Anders Carlsson965d5202007-11-22 01:36:19 +00001092 ExprResult Constraint = ParseAsmStringLiteral();
1093 if (Constraint.isInvalid) {
1094 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001095 return true;
Anders Carlsson965d5202007-11-22 01:36:19 +00001096 }
1097 Constraints.push_back(Constraint.Val);
Chris Lattner4b009652007-07-25 00:24:17 +00001098
Chris Lattner4d7d2342007-10-09 17:41:39 +00001099 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001100 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1101 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001102 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001103 }
1104
1105 // Read the parenthesized expression.
1106 ExprResult Res = ParseSimpleParenExpression();
1107 if (Res.isInvalid) {
1108 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001109 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001110 }
Anders Carlsson965d5202007-11-22 01:36:19 +00001111 Exprs.push_back(Res.Val);
Chris Lattner4b009652007-07-25 00:24:17 +00001112 // Eat the comma and continue parsing if it exists.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001113 if (Tok.isNot(tok::comma)) return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001114 ConsumeToken();
1115 }
Anders Carlsson749d7b02008-02-09 19:57:29 +00001116
1117 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001118}
Fariborz Jahanian829dfe52007-11-08 19:01:26 +00001119
1120Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1121 SourceLocation L, SourceLocation R) {
1122 // Do not enter a scope for the brace, as the arguments are in the same scope
1123 // (the function body) as the body itself. Instead, just read the statement
1124 // list and put it into a CompoundStmt for safe keeping.
1125 StmtResult FnBody = ParseCompoundStatementBody();
1126
1127 // If the function body could not be parsed, make a bogus compoundstmt.
1128 if (FnBody.isInvalid)
1129 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1130
1131 // Leave the function body scope.
1132 ExitScope();
1133
Steve Naroff99ee4302007-11-11 23:20:51 +00001134 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeone128c1d2007-12-01 08:06:07 +00001135}