blob: c776bb6c15a1e00cc628e47ced713c63c2626e09 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroffb746ce82008-02-07 23:24:32 +000017#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000038/// [C++] declaration-statement
Fariborz Jahanianb384d322007-10-04 20:19:06 +000039/// [OBC] objc-throw-statement
40/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000041/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +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 Jahanianb384d322007-10-04 20:19:06 +000069/// [OBC] objc-throw-statement:
70/// [OBC] '@' 'throw' expression ';'
71/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +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 Jahanian397fcc12007-09-19 19:14:32 +000080 tok::TokenKind Kind = Tok.getKind();
81 SourceLocation AtLoc;
82 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000083 case tok::at: // May be a @try or @throw statement
84 {
85 AtLoc = ConsumeToken(); // consume @
Steve Naroff64515f32008-02-05 21:27:35 +000086 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000087 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000088
Argyrios Kyrtzidisb9f930d2008-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
Reid Spencer5f016e22007-07-11 17:01:13 +000096 default:
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000097 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationSpecifier()) {
Chris Lattner81c018d2008-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 Lattner691a38b2008-03-13 06:29:54 +0000101 return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000102 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 Diag(Tok, diag::err_expected_statement);
104 return true;
105 } else {
106 // expression[opt] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000107 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff1b273c42007-09-16 14:56:35 +0000117 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff1b273c42007-09-16 14:56:35 +0000128 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroffd62701b2008-02-07 03:50:06 +0000162 bool msAsm = false;
163 Res = ParseAsmStatement(msAsm);
164 if (msAsm) return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 SemiError = "asm statement";
166 break;
167 }
168
169 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000170 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 ConsumeToken();
172 } else {
173 Diag(Tok, diag::err_expected_semi_after, SemiError);
174 SkipUntil(tok::semi);
175 }
176 return Res;
177}
178
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000179/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000180///
181/// labeled-statement:
182/// identifier ':' statement
183/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-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}
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000222 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000234 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000246 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000255 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff1b273c42007-09-16 14:56:35 +0000264 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000265
Steve Naroff1b273c42007-09-16 14:56:35 +0000266 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000276 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
278
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000279 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000288 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff1b273c42007-09-16 14:56:35 +0000297 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner45a566c2007-08-27 01:01:57 +0000313/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner98414c12007-08-31 21:49:55 +0000328Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000329 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000330
Chris Lattner31e05722007-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000334
335 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000336 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000337
338 ExitScope();
339 return Body;
340}
341
342
343/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000344/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000345/// consume the '}' at the end of the block. It does not manipulate the scope
346/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000347Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
349
350 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000351 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000352
353 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000354 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000355 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000356 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-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 Lattner4e1d99a2007-10-09 17:41:39 +0000363 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000364 ConsumeToken();
365
Chris Lattner043a0b52008-03-13 06:32:11 +0000366 // __extension__ silences extension warnings in the subexpression.
367 bool SavedExtWarn = Diags.getWarnOnExtensions();
368 Diags.setWarnOnExtensions(false);
369
Chris Lattner45a566c2007-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 Lattner81c018d2008-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 Lattner691a38b2008-03-13 06:29:54 +0000376 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner043a0b52008-03-13 06:32:11 +0000377
378 Diags.setWarnOnExtensions(SavedExtWarn);
Chris Lattner45a566c2007-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 Lattner45a566c2007-08-27 01:01:57 +0000382 ExprResult Res = ParseCastExpression(false);
Chris Lattner043a0b52008-03-13 06:32:11 +0000383 Diags.setWarnOnExtensions(SavedExtWarn);
384
Chris Lattner45a566c2007-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 Narofff69936d2007-09-16 03:34:24 +0000391 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-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 Naroff1b273c42007-09-16 14:56:35 +0000397 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000398 }
399 }
400
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000406 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000408 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 }
410
411 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000412 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000413 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414}
415
416/// ParseIfStatement
417/// if-statement: [C99 6.8.4.1]
418/// 'if' '(' expression ')' statement
419/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000420/// [C++] 'if' '(' condition ')' statement
421/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000422///
423Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000424 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
426
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000427 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 Diag(Tok, diag::err_expected_lparen_after, "if");
429 SkipUntil(tok::semi);
430 return true;
431 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000432
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000433 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
434
Chris Lattner22153252007-08-26 23:08:06 +0000435 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
436 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000437 //
438 // C++ 6.4p3:
439 // A name introduced by a declaration in a condition is in scope from its
440 // point of declaration until the end of the substatements controlled by the
441 // condition.
442 //
443 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000444 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000445
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000447 ExprResult CondExp;
448 if (getLang().CPlusPlus) {
449 SourceLocation LParenLoc = ConsumeParen();
450 CondExp = ParseCXXCondition();
451 MatchRHSPunctuation(tok::r_paren, LParenLoc);
452 } else {
453 CondExp = ParseSimpleParenExpression();
454 }
455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 if (CondExp.isInvalid) {
457 SkipUntil(tok::semi);
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000458 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000459 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 return true;
461 }
462
Chris Lattner0ecea032007-08-22 05:28:50 +0000463 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000464 // there is no compound stmt. C90 does not have this clause. We only do this
465 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000466 //
467 // C++ 6.4p1:
468 // The substatement in a selection-statement (each substatement, in the else
469 // form of the if statement) implicitly defines a local scope.
470 //
471 // For C++ we create a scope for the condition and a new scope for
472 // substatements because:
473 // -When the 'then' scope exits, we want the condition declaration to still be
474 // active for the 'else' scope too.
475 // -Sema will detect name clashes by considering declarations of a
476 // 'ControlScope' as part of its direct subscope.
477 // -If we wanted the condition and substatement to be in the same scope, we
478 // would have to notify ParseStatement not to create a new scope. It's
479 // simpler to let it create a new scope.
480 //
481 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000482 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000483
Chris Lattnerb96728d2007-10-29 05:08:52 +0000484 // Read the 'then' stmt.
485 SourceLocation ThenStmtLoc = Tok.getLocation();
486 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000487
Chris Lattnera36ce712007-08-22 05:16:28 +0000488 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000489 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000490
491 // If it has an else, parse it.
492 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000493 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000495
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000496 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000498
Chris Lattner0ecea032007-08-22 05:28:50 +0000499 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000500 // there is no compound stmt. C90 does not have this clause. We only do
501 // this if the body isn't a compound statement to avoid push/pop in common
502 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000503 //
504 // C++ 6.4p1:
505 // The substatement in a selection-statement (each substatement, in the else
506 // form of the if statement) implicitly defines a local scope.
507 //
508 NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000509 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000510
Chris Lattnerb96728d2007-10-29 05:08:52 +0000511 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000513
514 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000515 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 }
517
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000518 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000519 ExitScope();
520
Chris Lattnerb96728d2007-10-29 05:08:52 +0000521 // If the then or else stmt is invalid and the other is valid (and present),
522 // make turn the invalid one into a null stmt to avoid dropping the other
523 // part. If both are invalid, return error.
524 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
525 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
526 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
527 // Both invalid, or one is invalid and other is non-present: delete cond and
528 // return error.
529 Actions.DeleteExpr(CondExp.Val);
530 return true;
531 }
532
533 // Now if either are invalid, replace with a ';'.
534 if (ThenStmt.isInvalid)
535 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
536 if (ElseStmt.isInvalid)
537 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
538
Chris Lattnerb96728d2007-10-29 05:08:52 +0000539 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 ElseLoc, ElseStmt.Val);
541}
542
543/// ParseSwitchStatement
544/// switch-statement:
545/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000546/// [C++] 'switch' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000547Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000548 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
550
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000551 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 Diag(Tok, diag::err_expected_lparen_after, "switch");
553 SkipUntil(tok::semi);
554 return true;
555 }
Chris Lattner22153252007-08-26 23:08:06 +0000556
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000557 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
558
Chris Lattner22153252007-08-26 23:08:06 +0000559 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
560 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000561 //
562 // C++ 6.4p3:
563 // A name introduced by a declaration in a condition is in scope from its
564 // point of declaration until the end of the substatements controlled by the
565 // condition.
566 //
567 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000568 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000569 else
570 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000571
572 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000573 ExprResult Cond;
574 if (getLang().CPlusPlus) {
575 SourceLocation LParenLoc = ConsumeParen();
576 Cond = ParseCXXCondition();
577 MatchRHSPunctuation(tok::r_paren, LParenLoc);
578 } else {
579 Cond = ParseSimpleParenExpression();
580 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000581
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000582 if (Cond.isInvalid) {
583 ExitScope();
584 return true;
585 }
586
Steve Naroff1b273c42007-09-16 14:56:35 +0000587 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000588
Chris Lattner0ecea032007-08-22 05:28:50 +0000589 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000590 // there is no compound stmt. C90 does not have this clause. We only do this
591 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000592 //
593 // C++ 6.4p1:
594 // The substatement in a selection-statement (each substatement, in the else
595 // form of the if statement) implicitly defines a local scope.
596 //
597 // See comments in ParseIfStatement for why we create a scope for the
598 // condition and a new scope for substatement in C++.
599 //
600 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000601 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 // Read the body statement.
604 StmtResult Body = ParseStatement();
605
Chris Lattner0ecea032007-08-22 05:28:50 +0000606 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000607 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000608
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000609 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000610 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000611 // FIXME: Remove the case statement list from the Switch statement.
612 }
613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 ExitScope();
615
Steve Naroff1b273c42007-09-16 14:56:35 +0000616 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}
618
619/// ParseWhileStatement
620/// while-statement: [C99 6.8.5.1]
621/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000622/// [C++] 'while' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000623Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000624 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 SourceLocation WhileLoc = Tok.getLocation();
626 ConsumeToken(); // eat the 'while'.
627
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000628 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 Diag(Tok, diag::err_expected_lparen_after, "while");
630 SkipUntil(tok::semi);
631 return true;
632 }
633
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000634 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
635
Chris Lattner22153252007-08-26 23:08:06 +0000636 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
637 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000638 //
639 // C++ 6.4p3:
640 // A name introduced by a declaration in a condition is in scope from its
641 // point of declaration until the end of the substatements controlled by the
642 // condition.
643 //
644 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000645 EnterScope(Scope::BreakScope | Scope::ContinueScope |
646 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000647 else
648 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649
650 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000651 ExprResult Cond;
652 if (getLang().CPlusPlus) {
653 SourceLocation LParenLoc = ConsumeParen();
654 Cond = ParseCXXCondition();
655 MatchRHSPunctuation(tok::r_paren, LParenLoc);
656 } else {
657 Cond = ParseSimpleParenExpression();
658 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000659
Chris Lattner0ecea032007-08-22 05:28:50 +0000660 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000661 // there is no compound stmt. C90 does not have this clause. We only do this
662 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000663 //
664 // C++ 6.5p2:
665 // The substatement in an iteration-statement implicitly defines a local scope
666 // which is entered and exited each time through the loop.
667 //
668 // See comments in ParseIfStatement for why we create a scope for the
669 // condition and a new scope for substatement in C++.
670 //
671 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000672 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // Read the body statement.
675 StmtResult Body = ParseStatement();
676
Chris Lattner0ecea032007-08-22 05:28:50 +0000677 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000678 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 ExitScope();
681
682 if (Cond.isInvalid || Body.isInvalid) return true;
683
Steve Naroff1b273c42007-09-16 14:56:35 +0000684 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000685}
686
687/// ParseDoStatement
688/// do-statement: [C99 6.8.5.2]
689/// 'do' statement 'while' '(' expression ')' ';'
690/// Note: this lets the caller parse the end ';'.
691Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000692 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
694
Chris Lattner22153252007-08-26 23:08:06 +0000695 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
696 // the case for C90. Start the loop scope.
697 if (getLang().C99)
698 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
699 else
700 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000701
Chris Lattner0ecea032007-08-22 05:28:50 +0000702 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000703 // there is no compound stmt. C90 does not have this clause. We only do this
704 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000705 //
706 // C++ 6.5p2:
707 // The substatement in an iteration-statement implicitly defines a local scope
708 // which is entered and exited each time through the loop.
709 //
710 bool NeedsInnerScope = (getLang().C99 || getLang().CPlusPlus) &&
711 Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000712 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000713
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 // Read the body statement.
715 StmtResult Body = ParseStatement();
716
Chris Lattner0ecea032007-08-22 05:28:50 +0000717 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000718 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000719
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000720 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 ExitScope();
722 Diag(Tok, diag::err_expected_while);
723 Diag(DoLoc, diag::err_matching, "do");
724 SkipUntil(tok::semi);
725 return true;
726 }
727 SourceLocation WhileLoc = ConsumeToken();
728
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000729 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 ExitScope();
731 Diag(Tok, diag::err_expected_lparen_after, "do/while");
732 SkipUntil(tok::semi);
733 return true;
734 }
735
736 // Parse the condition.
737 ExprResult Cond = ParseSimpleParenExpression();
738
739 ExitScope();
740
741 if (Cond.isInvalid || Body.isInvalid) return true;
742
Steve Naroff1b273c42007-09-16 14:56:35 +0000743 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000744}
745
746/// ParseForStatement
747/// for-statement: [C99 6.8.5.3]
748/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
749/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000750/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
751/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000752/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
753/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000754///
755/// [C++] for-init-statement:
756/// [C++] expression-statement
757/// [C++] simple-declaration
758///
Reid Spencer5f016e22007-07-11 17:01:13 +0000759Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000760 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
762
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000763 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 Diag(Tok, diag::err_expected_lparen_after, "for");
765 SkipUntil(tok::semi);
766 return true;
767 }
768
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000769 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
770
Chris Lattner22153252007-08-26 23:08:06 +0000771 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
772 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000773 //
774 // C++ 6.4p3:
775 // A name introduced by a declaration in a condition is in scope from its
776 // point of declaration until the end of the substatements controlled by the
777 // condition.
778 // C++ 6.5.3p1:
779 // Names declared in the for-init-statement are in the same declarative-region
780 // as those declared in the condition.
781 //
782 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000783 EnterScope(Scope::BreakScope | Scope::ContinueScope |
784 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000785 else
786 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000787
788 SourceLocation LParenLoc = ConsumeParen();
789 ExprResult Value;
790
791 StmtTy *FirstPart = 0;
792 ExprTy *SecondPart = 0;
793 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000794 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000795
796 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000797 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 // no first part, eat the ';'.
799 ConsumeToken();
800 } else if (isDeclarationSpecifier()) { // for (int X = 4;
801 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000802 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000804
805 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000806 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000807 // FIXME: Pass in the right location for the end of the declstmt.
808 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattner691a38b2008-03-13 06:29:54 +0000809 DeclStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000811 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000812 ConsumeToken(); // consume 'in'
813 Value = ParseExpression();
814 if (!Value.isInvalid)
815 SecondPart = Value.Val;
816 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 } else {
818 Value = ParseExpression();
819
820 // Turn the expression into a stmt.
821 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000822 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 if (!R.isInvalid)
824 FirstPart = R.Val;
825 }
826
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000827 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000829 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000830 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000831 ConsumeToken(); // consume 'in'
832 Value = ParseExpression();
833 if (!Value.isInvalid)
834 SecondPart = Value.Val;
835 }
836 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
838 SkipUntil(tok::semi);
839 }
840 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000841 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000842 // Parse the second part of the for specifier.
843 if (Tok.is(tok::semi)) { // for (...;;
844 // no second part.
845 Value = ExprResult();
846 } else {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000847 Value = getLang().CPlusPlus ? ParseCXXCondition()
848 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000849 if (!Value.isInvalid)
850 SecondPart = Value.Val;
851 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000852
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000853 if (Tok.is(tok::semi)) {
854 ConsumeToken();
855 } else {
856 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
857 SkipUntil(tok::semi);
858 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000859
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000860 // Parse the third part of the for specifier.
861 if (Tok.is(tok::r_paren)) { // for (...;...;)
862 // no third part.
863 Value = ExprResult();
864 } else {
865 Value = ParseExpression();
866 if (!Value.isInvalid) {
867 // Turn the expression into a stmt.
868 StmtResult R = Actions.ActOnExprStmt(Value.Val);
869 if (!R.isInvalid)
870 ThirdPart = R.Val;
871 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 }
873 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 // Match the ')'.
875 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
876
Chris Lattner0ecea032007-08-22 05:28:50 +0000877 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000878 // there is no compound stmt. C90 does not have this clause. We only do this
879 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000880 //
881 // C++ 6.5p2:
882 // The substatement in an iteration-statement implicitly defines a local scope
883 // which is entered and exited each time through the loop.
884 //
885 // See comments in ParseIfStatement for why we create a scope for
886 // for-init-statement/condition and a new scope for substatement in C++.
887 //
888 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000889 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000890
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 // Read the body statement.
892 StmtResult Body = ParseStatement();
893
Chris Lattner0ecea032007-08-22 05:28:50 +0000894 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000895 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 // Leave the for-scope.
898 ExitScope();
899
900 if (Body.isInvalid)
901 return Body;
902
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000903 if (!ForEach)
904 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
905 SecondPart, ThirdPart, RParenLoc, Body.Val);
906 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000907 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000908 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000909}
910
911/// ParseGotoStatement
912/// jump-statement:
913/// 'goto' identifier ';'
914/// [GNU] 'goto' '*' expression ';'
915///
916/// Note: this lets the caller parse the end ';'.
917///
918Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000919 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
921
922 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000923 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000924 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 Tok.getIdentifierInfo());
926 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000927 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 // GNU indirect goto extension.
929 Diag(Tok, diag::ext_gnu_indirect_goto);
930 SourceLocation StarLoc = ConsumeToken();
931 ExprResult R = ParseExpression();
932 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
933 SkipUntil(tok::semi, false, true);
934 return true;
935 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000936 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000937 } else {
938 Diag(Tok, diag::err_expected_ident);
939 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000941
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 return Res;
943}
944
945/// ParseContinueStatement
946/// jump-statement:
947/// 'continue' ';'
948///
949/// Note: this lets the caller parse the end ';'.
950///
951Parser::StmtResult Parser::ParseContinueStatement() {
952 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000953 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000954}
955
956/// ParseBreakStatement
957/// jump-statement:
958/// 'break' ';'
959///
960/// Note: this lets the caller parse the end ';'.
961///
962Parser::StmtResult Parser::ParseBreakStatement() {
963 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000964 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000965}
966
967/// ParseReturnStatement
968/// jump-statement:
969/// 'return' expression[opt] ';'
970Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000971 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
973
974 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000975 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 R = ParseExpression();
977 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
978 SkipUntil(tok::semi, false, true);
979 return true;
980 }
981 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000982 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000983}
984
Steve Naroff5f8aa692008-02-11 23:15:56 +0000985/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
986/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +0000987Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +0000988 if (Tok.is(tok::l_brace)) {
989 unsigned short savedBraceCount = BraceCount;
990 do {
991 ConsumeAnyToken();
992 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
993 } else {
994 // From the MS website: If used without braces, the __asm keyword means
995 // that the rest of the line is an assembly-language statement.
996 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +0000997 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +0000998 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
999 do {
1000 ConsumeAnyToken();
1001 TokLoc = Tok.getLocation();
1002 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1003 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1004 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001005 }
Steve Naroffd77bc282008-04-07 21:06:54 +00001006 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001007}
1008
Reid Spencer5f016e22007-07-11 17:01:13 +00001009/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001010/// asm-statement:
1011/// gnu-asm-statement
1012/// ms-asm-statement
1013///
1014/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001015/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1016///
1017/// [GNU] asm-argument:
1018/// asm-string-literal
1019/// asm-string-literal ':' asm-operands[opt]
1020/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1021/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1022/// ':' asm-clobbers
1023///
1024/// [GNU] asm-clobbers:
1025/// asm-string-literal
1026/// asm-clobbers ',' asm-string-literal
1027///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001028/// [MS] ms-asm-statement:
1029/// '__asm' assembly-instruction ';'[opt]
1030/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1031///
1032/// [MS] assembly-instruction-list:
1033/// assembly-instruction ';'[opt]
1034/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1035///
Steve Naroffd62701b2008-02-07 03:50:06 +00001036Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001037 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001038 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001039
Steve Naroff5f8aa692008-02-11 23:15:56 +00001040 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001041 msAsm = true;
1042 return FuzzyParseMicrosoftAsmStatement();
1043 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 DeclSpec DS;
1045 SourceLocation Loc = Tok.getLocation();
1046 ParseTypeQualifierListOpt(DS);
1047
1048 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1049 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1050 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
1051 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
1052 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
1053
1054 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001055 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001056 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001057 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 Diag(Tok, diag::err_expected_lparen_after, "asm");
1059 SkipUntil(tok::r_paren);
1060 return true;
1061 }
1062 Loc = ConsumeParen();
1063
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001064 ExprResult AsmString = ParseAsmStringLiteral();
1065 if (AsmString.isInvalid)
1066 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001067
1068 llvm::SmallVector<std::string, 4> Names;
1069 llvm::SmallVector<ExprTy*, 4> Constraints;
1070 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001071 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001072
Anders Carlssondfab34a2008-02-05 23:03:50 +00001073 unsigned NumInputs = 0, NumOutputs = 0;
1074
1075 SourceLocation RParenLoc;
1076 if (Tok.is(tok::r_paren)) {
1077 // We have a simple asm expression
1078 isSimple = true;
1079
1080 RParenLoc = ConsumeParen();
1081 } else {
1082 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001083 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1084 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001085
1086 NumOutputs = Names.size();
1087
1088 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001089 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1090 return true;
1091
Anders Carlssondfab34a2008-02-05 23:03:50 +00001092 assert(Names.size() == Constraints.size() &&
1093 Constraints.size() == Exprs.size()
1094 && "Input operand size mismatch!");
1095
1096 NumInputs = Names.size() - NumOutputs;
1097
1098 // Parse the clobbers, if present.
1099 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001100 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +00001101
1102 // Parse the asm-string list for clobbers.
1103 while (1) {
1104 ExprResult Clobber = ParseAsmStringLiteral();
1105
1106 if (Clobber.isInvalid)
1107 break;
1108
1109 Clobbers.push_back(Clobber.Val);
1110
1111 if (Tok.isNot(tok::comma)) break;
1112 ConsumeToken();
1113 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 }
Anders Carlssondfab34a2008-02-05 23:03:50 +00001115
1116 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 }
1118
Anders Carlssondfab34a2008-02-05 23:03:50 +00001119 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1120 NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001121 &Names[0], &Constraints[0], &Exprs[0],
1122 AsmString.Val,
1123 Clobbers.size(), &Clobbers[0],
1124 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125}
1126
1127/// ParseAsmOperands - Parse the asm-operands production as used by
1128/// asm-statement. We also parse a leading ':' token. If the leading colon is
1129/// not present, we do not parse anything.
1130///
1131/// [GNU] asm-operands:
1132/// asm-operand
1133/// asm-operands ',' asm-operand
1134///
1135/// [GNU] asm-operand:
1136/// asm-string-literal '(' expression ')'
1137/// '[' identifier ']' asm-string-literal '(' expression ')'
1138///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001139bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001140 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1141 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001143 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 ConsumeToken();
1145
1146 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001147 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001148 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001149
Anders Carlssonb235fc22007-11-22 01:36:19 +00001150 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001152 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 SourceLocation Loc = ConsumeBracket();
1154
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001155 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 Diag(Tok, diag::err_expected_ident);
1157 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001158 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 }
Chris Lattner69efba72007-10-29 04:06:22 +00001160
Anders Carlssonb235fc22007-11-22 01:36:19 +00001161 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001162 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001163
1164 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001166 } else
1167 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001168
Anders Carlssonb235fc22007-11-22 01:36:19 +00001169 ExprResult Constraint = ParseAsmStringLiteral();
1170 if (Constraint.isInvalid) {
1171 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001172 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001173 }
1174 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001175
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001176 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1178 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001179 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 }
1181
1182 // Read the parenthesized expression.
1183 ExprResult Res = ParseSimpleParenExpression();
1184 if (Res.isInvalid) {
1185 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001186 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001188 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001190 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 ConsumeToken();
1192 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001193
1194 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001195}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001196
1197Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1198 SourceLocation L, SourceLocation R) {
1199 // Do not enter a scope for the brace, as the arguments are in the same scope
1200 // (the function body) as the body itself. Instead, just read the statement
1201 // list and put it into a CompoundStmt for safe keeping.
1202 StmtResult FnBody = ParseCompoundStatementBody();
1203
1204 // If the function body could not be parsed, make a bogus compoundstmt.
1205 if (FnBody.isInvalid)
1206 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1207
1208 // Leave the function body scope.
1209 ExitScope();
1210
Steve Naroffd6d054d2007-11-11 23:20:51 +00001211 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001212}