blob: 49b8180904867153a99f5408b81253fd75e91c2b [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"
Chris Lattner39146d62008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/Diagnostic.h"
Steve Naroffb746ce82008-02-07 23:24:32 +000018#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Parse/DeclSpec.h"
20#include "clang/Parse/Scope.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.8: Statements and Blocks.
25//===----------------------------------------------------------------------===//
26
27/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
28/// StatementOrDeclaration:
29/// statement
30/// declaration
31///
32/// statement:
33/// labeled-statement
34/// compound-statement
35/// expression-statement
36/// selection-statement
37/// iteration-statement
38/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000039/// [C++] declaration-statement
Fariborz Jahanianb384d322007-10-04 20:19:06 +000040/// [OBC] objc-throw-statement
41/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000042/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000043/// [GNU] asm-statement
44/// [OMP] openmp-construct [TODO]
45///
46/// labeled-statement:
47/// identifier ':' statement
48/// 'case' constant-expression ':' statement
49/// 'default' ':' statement
50///
51/// selection-statement:
52/// if-statement
53/// switch-statement
54///
55/// iteration-statement:
56/// while-statement
57/// do-statement
58/// for-statement
59///
60/// expression-statement:
61/// expression[opt] ';'
62///
63/// jump-statement:
64/// 'goto' identifier ';'
65/// 'continue' ';'
66/// 'break' ';'
67/// 'return' expression[opt] ';'
68/// [GNU] 'goto' '*' expression ';'
69///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000070/// [OBC] objc-throw-statement:
71/// [OBC] '@' 'throw' expression ';'
72/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000073///
74Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
75 const char *SemiError = 0;
76 Parser::StmtResult Res;
77
78 // Cases in this switch statement should fall through if the parser expects
79 // the token to end in a semicolon (in which case SemiError should be set),
80 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000081 tok::TokenKind Kind = Tok.getKind();
82 SourceLocation AtLoc;
83 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000084 case tok::at: // May be a @try or @throw statement
85 {
86 AtLoc = ConsumeToken(); // consume @
Steve Naroff64515f32008-02-05 21:27:35 +000087 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000088 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000089
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +000090 case tok::identifier:
91 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
92 // identifier ':' statement
93 return ParseLabeledStatement();
94 }
95 // PASS THROUGH.
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000098 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner81c018d2008-03-13 06:29:04 +000099 SourceLocation DeclStart = Tok.getLocation();
100 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
101 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000102 return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000103 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 Diag(Tok, diag::err_expected_statement);
105 return true;
106 } else {
107 // expression[opt] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000108 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 if (Res.isInvalid) {
110 // If the expression is invalid, skip ahead to the next semicolon. Not
111 // doing this opens us up to the possibility of infinite loops if
112 // ParseExpression does not consume any tokens.
113 SkipUntil(tok::semi);
114 return true;
115 }
116 // Otherwise, eat the semicolon.
117 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000118 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120
121 case tok::kw_case: // C99 6.8.1: labeled-statement
122 return ParseCaseStatement();
123 case tok::kw_default: // C99 6.8.1: labeled-statement
124 return ParseDefaultStatement();
125
126 case tok::l_brace: // C99 6.8.2: compound-statement
127 return ParseCompoundStatement();
128 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000129 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000130
131 case tok::kw_if: // C99 6.8.4.1: if-statement
132 return ParseIfStatement();
133 case tok::kw_switch: // C99 6.8.4.2: switch-statement
134 return ParseSwitchStatement();
135
136 case tok::kw_while: // C99 6.8.5.1: while-statement
137 return ParseWhileStatement();
138 case tok::kw_do: // C99 6.8.5.2: do-statement
139 Res = ParseDoStatement();
140 SemiError = "do/while loop";
141 break;
142 case tok::kw_for: // C99 6.8.5.3: for-statement
143 return ParseForStatement();
144
145 case tok::kw_goto: // C99 6.8.6.1: goto-statement
146 Res = ParseGotoStatement();
147 SemiError = "goto statement";
148 break;
149 case tok::kw_continue: // C99 6.8.6.2: continue-statement
150 Res = ParseContinueStatement();
151 SemiError = "continue statement";
152 break;
153 case tok::kw_break: // C99 6.8.6.3: break-statement
154 Res = ParseBreakStatement();
155 SemiError = "break statement";
156 break;
157 case tok::kw_return: // C99 6.8.6.4: return-statement
158 Res = ParseReturnStatement();
159 SemiError = "return statement";
160 break;
161
162 case tok::kw_asm:
Steve Naroffd62701b2008-02-07 03:50:06 +0000163 bool msAsm = false;
164 Res = ParseAsmStatement(msAsm);
165 if (msAsm) return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 SemiError = "asm statement";
167 break;
168 }
169
170 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000171 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 ConsumeToken();
Chris Lattner19504402008-11-13 18:52:53 +0000173 } else if (!Res.isInvalid) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000174 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000175 // Skip until we see a } or ;, but don't eat it.
176 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 }
178 return Res;
179}
180
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000181/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000182///
183/// labeled-statement:
184/// identifier ':' statement
185/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000186///
187Parser::StmtResult Parser::ParseLabeledStatement() {
188 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
189 "Not an identifier!");
190
191 Token IdentTok = Tok; // Save the whole token.
192 ConsumeToken(); // eat the identifier.
193
194 assert(Tok.is(tok::colon) && "Not a label!");
195
196 // identifier ':' statement
197 SourceLocation ColonLoc = ConsumeToken();
198
199 // Read label attributes, if present.
200 DeclTy *AttrList = 0;
201 if (Tok.is(tok::kw___attribute))
202 // TODO: save these somewhere.
203 AttrList = ParseAttributes();
204
205 StmtResult SubStmt = ParseStatement();
206
207 // Broken substmt shouldn't prevent the label from being added to the AST.
208 if (SubStmt.isInvalid)
209 SubStmt = Actions.ActOnNullStmt(ColonLoc);
210
211 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
212 IdentTok.getIdentifierInfo(),
213 ColonLoc, SubStmt.Val);
214}
Reid Spencer5f016e22007-07-11 17:01:13 +0000215
216/// ParseCaseStatement
217/// labeled-statement:
218/// 'case' constant-expression ':' statement
219/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
220///
221/// Note that this does not parse the 'statement' at the end.
222///
223Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000224 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
226
227 ExprResult LHS = ParseConstantExpression();
228 if (LHS.isInvalid) {
229 SkipUntil(tok::colon);
230 return true;
231 }
232
233 // GNU case range extension.
234 SourceLocation DotDotDotLoc;
235 ExprTy *RHSVal = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000236 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 Diag(Tok, diag::ext_gnu_case_range);
238 DotDotDotLoc = ConsumeToken();
239
240 ExprResult RHS = ParseConstantExpression();
241 if (RHS.isInvalid) {
242 SkipUntil(tok::colon);
243 return true;
244 }
245 RHSVal = RHS.Val;
246 }
247
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000248 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000249 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 SkipUntil(tok::colon);
251 return true;
252 }
253
254 SourceLocation ColonLoc = ConsumeToken();
255
256 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000257 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 Diag(Tok, diag::err_label_end_of_compound_statement);
259 return true;
260 }
261
262 StmtResult SubStmt = ParseStatement();
263
264 // Broken substmt shouldn't prevent the case from being added to the AST.
265 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000266 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267
Steve Naroff1b273c42007-09-16 14:56:35 +0000268 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 SubStmt.Val);
270}
271
272/// ParseDefaultStatement
273/// labeled-statement:
274/// 'default' ':' statement
275/// Note that this does not parse the 'statement' at the end.
276///
277Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000278 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
280
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000281 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000282 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 SkipUntil(tok::colon);
284 return true;
285 }
286
287 SourceLocation ColonLoc = ConsumeToken();
288
289 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000290 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 Diag(Tok, diag::err_label_end_of_compound_statement);
292 return true;
293 }
294
295 StmtResult SubStmt = ParseStatement();
296 if (SubStmt.isInvalid)
297 return true;
298
Steve Naroff1b273c42007-09-16 14:56:35 +0000299 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000300}
301
302
303/// ParseCompoundStatement - Parse a "{}" block.
304///
305/// compound-statement: [C99 6.8.2]
306/// { block-item-list[opt] }
307/// [GNU] { label-declarations block-item-list } [TODO]
308///
309/// block-item-list:
310/// block-item
311/// block-item-list block-item
312///
313/// block-item:
314/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000315/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000316/// statement
317/// [OMP] openmp-directive [TODO]
318///
319/// [GNU] label-declarations:
320/// [GNU] label-declaration
321/// [GNU] label-declarations label-declaration
322///
323/// [GNU] label-declaration:
324/// [GNU] '__label__' identifier-list ';'
325///
326/// [OMP] openmp-directive: [TODO]
327/// [OMP] barrier-directive
328/// [OMP] flush-directive
329///
Chris Lattner98414c12007-08-31 21:49:55 +0000330Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000331 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000332
Chris Lattner31e05722007-08-26 06:24:45 +0000333 // Enter a scope to hold everything within the compound stmt. Compound
334 // statements can always hold declarations.
335 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000336
337 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000338 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
340 ExitScope();
341 return Body;
342}
343
344
345/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000346/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000347/// consume the '}' at the end of the block. It does not manipulate the scope
348/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000349Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
351
352 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000353 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000354
355 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000356 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000357 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000358 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000359 R = ParseStatementOrDeclaration(false);
360 } else {
361 // __extension__ can start declarations and it can also be a unary
362 // operator for expressions. Consume multiple __extension__ markers here
363 // until we can determine which is which.
364 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000365 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000366 ConsumeToken();
367
Chris Lattner043a0b52008-03-13 06:32:11 +0000368 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000369 ExtensionRAIIObject O(Diags); // Use RAII to do this.
370
Chris Lattner45a566c2007-08-27 01:01:57 +0000371 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000372 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000373 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000374 SourceLocation DeclStart = Tok.getLocation();
375 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
376 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000377 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000378 } else {
379 // Otherwise this was a unary __extension__ marker. Parse the
380 // subexpression and add the __extension__ unary op.
Chris Lattner45a566c2007-08-27 01:01:57 +0000381 ExprResult Res = ParseCastExpression(false);
Chris Lattner043a0b52008-03-13 06:32:11 +0000382
Chris Lattner45a566c2007-08-27 01:01:57 +0000383 if (Res.isInvalid) {
384 SkipUntil(tok::semi);
385 continue;
386 }
387
388 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000389 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000390 if (Res.isInvalid)
391 continue;
392
Chris Lattner39146d62008-10-20 06:51:33 +0000393 // Eat the semicolon at the end of stmt and convert the expr into a
394 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000395 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000396 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000397 }
398 }
399
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 if (!R.isInvalid && R.Val)
401 Stmts.push_back(R.Val);
402 }
403
404 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000405 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000407 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
409
410 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000411 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000412 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000413}
414
415/// ParseIfStatement
416/// if-statement: [C99 6.8.4.1]
417/// 'if' '(' expression ')' statement
418/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000419/// [C++] 'if' '(' condition ')' statement
420/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000421///
422Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000423 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
425
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000426 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000427 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 SkipUntil(tok::semi);
429 return true;
430 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000431
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000432 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
433
Chris Lattner22153252007-08-26 23:08:06 +0000434 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
435 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000436 //
437 // C++ 6.4p3:
438 // A name introduced by a declaration in a condition is in scope from its
439 // point of declaration until the end of the substatements controlled by the
440 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000441 // C++ 3.3.2p4:
442 // Names declared in the for-init-statement, and in the condition of if,
443 // while, for, and switch statements are local to the if, while, for, or
444 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000445 //
446 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000447 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000448
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000450 ExprResult CondExp;
451 if (getLang().CPlusPlus) {
452 SourceLocation LParenLoc = ConsumeParen();
453 CondExp = ParseCXXCondition();
454 MatchRHSPunctuation(tok::r_paren, LParenLoc);
455 } else {
456 CondExp = ParseSimpleParenExpression();
457 }
458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 if (CondExp.isInvalid) {
460 SkipUntil(tok::semi);
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000461 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000462 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 return true;
464 }
465
Chris Lattner0ecea032007-08-22 05:28:50 +0000466 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000467 // there is no compound stmt. C90 does not have this clause. We only do this
468 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000469 //
470 // C++ 6.4p1:
471 // The substatement in a selection-statement (each substatement, in the else
472 // form of the if statement) implicitly defines a local scope.
473 //
474 // For C++ we create a scope for the condition and a new scope for
475 // substatements because:
476 // -When the 'then' scope exits, we want the condition declaration to still be
477 // active for the 'else' scope too.
478 // -Sema will detect name clashes by considering declarations of a
479 // 'ControlScope' as part of its direct subscope.
480 // -If we wanted the condition and substatement to be in the same scope, we
481 // would have to notify ParseStatement not to create a new scope. It's
482 // simpler to let it create a new scope.
483 //
484 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000485 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000486
Chris Lattnerb96728d2007-10-29 05:08:52 +0000487 // Read the 'then' stmt.
488 SourceLocation ThenStmtLoc = Tok.getLocation();
489 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000490
Chris Lattnera36ce712007-08-22 05:16:28 +0000491 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000492 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000493
494 // If it has an else, parse it.
495 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000496 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000498
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000499 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000501
Chris Lattner0ecea032007-08-22 05:28:50 +0000502 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000503 // there is no compound stmt. C90 does not have this clause. We only do
504 // this if the body isn't a compound statement to avoid push/pop in common
505 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000506 //
507 // C++ 6.4p1:
508 // The substatement in a selection-statement (each substatement, in the else
509 // form of the if statement) implicitly defines a local scope.
510 //
511 NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000512 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000513
Chris Lattnerb96728d2007-10-29 05:08:52 +0000514 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000516
517 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000518 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 }
520
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000521 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000522 ExitScope();
523
Chris Lattnerb96728d2007-10-29 05:08:52 +0000524 // If the then or else stmt is invalid and the other is valid (and present),
525 // make turn the invalid one into a null stmt to avoid dropping the other
526 // part. If both are invalid, return error.
527 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
528 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
529 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
530 // Both invalid, or one is invalid and other is non-present: delete cond and
531 // return error.
532 Actions.DeleteExpr(CondExp.Val);
533 return true;
534 }
535
536 // Now if either are invalid, replace with a ';'.
537 if (ThenStmt.isInvalid)
538 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
539 if (ElseStmt.isInvalid)
540 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
541
Chris Lattnerb96728d2007-10-29 05:08:52 +0000542 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 ElseLoc, ElseStmt.Val);
544}
545
546/// ParseSwitchStatement
547/// switch-statement:
548/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000549/// [C++] 'switch' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000550Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000551 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
553
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000554 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000555 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 SkipUntil(tok::semi);
557 return true;
558 }
Chris Lattner22153252007-08-26 23:08:06 +0000559
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000560 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
561
Chris Lattner22153252007-08-26 23:08:06 +0000562 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
563 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000564 //
565 // C++ 6.4p3:
566 // A name introduced by a declaration in a condition is in scope from its
567 // point of declaration until the end of the substatements controlled by the
568 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000569 // C++ 3.3.2p4:
570 // Names declared in the for-init-statement, and in the condition of if,
571 // while, for, and switch statements are local to the if, while, for, or
572 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000573 //
574 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000575 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000576 else
577 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
579 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000580 ExprResult Cond;
581 if (getLang().CPlusPlus) {
582 SourceLocation LParenLoc = ConsumeParen();
583 Cond = ParseCXXCondition();
584 MatchRHSPunctuation(tok::r_paren, LParenLoc);
585 } else {
586 Cond = ParseSimpleParenExpression();
587 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000588
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000589 if (Cond.isInvalid) {
590 ExitScope();
591 return true;
592 }
593
Steve Naroff1b273c42007-09-16 14:56:35 +0000594 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000595
Chris Lattner0ecea032007-08-22 05:28:50 +0000596 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000597 // there is no compound stmt. C90 does not have this clause. We only do this
598 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000599 //
600 // C++ 6.4p1:
601 // The substatement in a selection-statement (each substatement, in the else
602 // form of the if statement) implicitly defines a local scope.
603 //
604 // See comments in ParseIfStatement for why we create a scope for the
605 // condition and a new scope for substatement in C++.
606 //
607 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000608 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 // Read the body statement.
611 StmtResult Body = ParseStatement();
612
Chris Lattner0ecea032007-08-22 05:28:50 +0000613 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000614 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000615
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000616 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000617 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000618 // FIXME: Remove the case statement list from the Switch statement.
619 }
620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 ExitScope();
622
Steve Naroff1b273c42007-09-16 14:56:35 +0000623 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624}
625
626/// ParseWhileStatement
627/// while-statement: [C99 6.8.5.1]
628/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000629/// [C++] 'while' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000630Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000631 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 SourceLocation WhileLoc = Tok.getLocation();
633 ConsumeToken(); // eat the 'while'.
634
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000635 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000636 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 SkipUntil(tok::semi);
638 return true;
639 }
640
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000641 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
642
Chris Lattner22153252007-08-26 23:08:06 +0000643 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
644 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000645 //
646 // C++ 6.4p3:
647 // A name introduced by a declaration in a condition is in scope from its
648 // point of declaration until the end of the substatements controlled by the
649 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000650 // C++ 3.3.2p4:
651 // Names declared in the for-init-statement, and in the condition of if,
652 // while, for, and switch statements are local to the if, while, for, or
653 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000654 //
655 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000656 EnterScope(Scope::BreakScope | Scope::ContinueScope |
657 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000658 else
659 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000660
661 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000662 ExprResult Cond;
663 if (getLang().CPlusPlus) {
664 SourceLocation LParenLoc = ConsumeParen();
665 Cond = ParseCXXCondition();
666 MatchRHSPunctuation(tok::r_paren, LParenLoc);
667 } else {
668 Cond = ParseSimpleParenExpression();
669 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000670
Chris Lattner0ecea032007-08-22 05:28:50 +0000671 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000672 // there is no compound stmt. C90 does not have this clause. We only do this
673 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000674 //
675 // C++ 6.5p2:
676 // The substatement in an iteration-statement implicitly defines a local scope
677 // which is entered and exited each time through the loop.
678 //
679 // See comments in ParseIfStatement for why we create a scope for the
680 // condition and a new scope for substatement in C++.
681 //
682 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000683 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 // Read the body statement.
686 StmtResult Body = ParseStatement();
687
Chris Lattner0ecea032007-08-22 05:28:50 +0000688 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000689 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 ExitScope();
692
693 if (Cond.isInvalid || Body.isInvalid) return true;
694
Steve Naroff1b273c42007-09-16 14:56:35 +0000695 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000696}
697
698/// ParseDoStatement
699/// do-statement: [C99 6.8.5.2]
700/// 'do' statement 'while' '(' expression ')' ';'
701/// Note: this lets the caller parse the end ';'.
702Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000703 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
705
Chris Lattner22153252007-08-26 23:08:06 +0000706 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
707 // the case for C90. Start the loop scope.
708 if (getLang().C99)
709 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
710 else
711 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712
Chris Lattner0ecea032007-08-22 05:28:50 +0000713 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000714 // there is no compound stmt. C90 does not have this clause. We only do this
715 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000716 //
717 // C++ 6.5p2:
718 // The substatement in an iteration-statement implicitly defines a local scope
719 // which is entered and exited each time through the loop.
720 //
Chris Lattner19504402008-11-13 18:52:53 +0000721 bool NeedsInnerScope =
722 (getLang().C99 || getLang().CPlusPlus) && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000723 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000724
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 // Read the body statement.
726 StmtResult Body = ParseStatement();
727
Chris Lattner0ecea032007-08-22 05:28:50 +0000728 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000729 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000730
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000731 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 ExitScope();
Chris Lattner19504402008-11-13 18:52:53 +0000733 if (!Body.isInvalid) {
734 Diag(Tok, diag::err_expected_while);
Chris Lattner1ab3b962008-11-18 07:48:38 +0000735 Diag(DoLoc, diag::err_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000736 SkipUntil(tok::semi, false, true);
737 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 return true;
739 }
740 SourceLocation WhileLoc = ConsumeToken();
741
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000742 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 ExitScope();
Chris Lattner1ab3b962008-11-18 07:48:38 +0000744 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000745 SkipUntil(tok::semi, false, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 return true;
747 }
748
749 // Parse the condition.
750 ExprResult Cond = ParseSimpleParenExpression();
751
752 ExitScope();
753
754 if (Cond.isInvalid || Body.isInvalid) return true;
755
Steve Naroff1b273c42007-09-16 14:56:35 +0000756 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000757}
758
759/// ParseForStatement
760/// for-statement: [C99 6.8.5.3]
761/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
762/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000763/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
764/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000765/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
766/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000767///
768/// [C++] for-init-statement:
769/// [C++] expression-statement
770/// [C++] simple-declaration
771///
Reid Spencer5f016e22007-07-11 17:01:13 +0000772Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000773 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
775
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000776 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000777 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 SkipUntil(tok::semi);
779 return true;
780 }
781
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000782 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
783
Chris Lattner22153252007-08-26 23:08:06 +0000784 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
785 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000786 //
787 // C++ 6.4p3:
788 // A name introduced by a declaration in a condition is in scope from its
789 // point of declaration until the end of the substatements controlled by the
790 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000791 // C++ 3.3.2p4:
792 // Names declared in the for-init-statement, and in the condition of if,
793 // while, for, and switch statements are local to the if, while, for, or
794 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000795 // C++ 6.5.3p1:
796 // Names declared in the for-init-statement are in the same declarative-region
797 // as those declared in the condition.
798 //
799 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000800 EnterScope(Scope::BreakScope | Scope::ContinueScope |
801 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000802 else
803 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000804
805 SourceLocation LParenLoc = ConsumeParen();
806 ExprResult Value;
807
808 StmtTy *FirstPart = 0;
809 ExprTy *SecondPart = 0;
810 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000811 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000812
813 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000814 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 // no first part, eat the ';'.
816 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000817 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000819 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000821
822 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000823 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000824 // FIXME: Pass in the right location for the end of the declstmt.
825 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattner691a38b2008-03-13 06:29:54 +0000826 DeclStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000828 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000829 ConsumeToken(); // consume 'in'
830 Value = ParseExpression();
831 if (!Value.isInvalid)
832 SecondPart = Value.Val;
833 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 } else {
835 Value = ParseExpression();
836
837 // Turn the expression into a stmt.
838 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000839 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 if (!R.isInvalid)
841 FirstPart = R.Val;
842 }
843
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000844 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000846 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000847 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000848 ConsumeToken(); // consume 'in'
849 Value = ParseExpression();
850 if (!Value.isInvalid)
851 SecondPart = Value.Val;
852 }
853 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
855 SkipUntil(tok::semi);
856 }
857 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000858 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000859 // Parse the second part of the for specifier.
860 if (Tok.is(tok::semi)) { // for (...;;
861 // no second part.
862 Value = ExprResult();
863 } else {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000864 Value = getLang().CPlusPlus ? ParseCXXCondition()
865 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000866 if (!Value.isInvalid)
867 SecondPart = Value.Val;
868 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000869
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000870 if (Tok.is(tok::semi)) {
871 ConsumeToken();
872 } else {
873 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
874 SkipUntil(tok::semi);
875 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000876
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000877 // Parse the third part of the for specifier.
878 if (Tok.is(tok::r_paren)) { // for (...;...;)
879 // no third part.
880 Value = ExprResult();
881 } else {
882 Value = ParseExpression();
883 if (!Value.isInvalid) {
884 // Turn the expression into a stmt.
885 StmtResult R = Actions.ActOnExprStmt(Value.Val);
886 if (!R.isInvalid)
887 ThirdPart = R.Val;
888 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 }
890 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 // Match the ')'.
892 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
893
Chris Lattner0ecea032007-08-22 05:28:50 +0000894 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000895 // there is no compound stmt. C90 does not have this clause. We only do this
896 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000897 //
898 // C++ 6.5p2:
899 // The substatement in an iteration-statement implicitly defines a local scope
900 // which is entered and exited each time through the loop.
901 //
902 // See comments in ParseIfStatement for why we create a scope for
903 // for-init-statement/condition and a new scope for substatement in C++.
904 //
905 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000906 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000907
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 // Read the body statement.
909 StmtResult Body = ParseStatement();
910
Chris Lattner0ecea032007-08-22 05:28:50 +0000911 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000912 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 // Leave the for-scope.
915 ExitScope();
916
917 if (Body.isInvalid)
918 return Body;
919
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000920 if (!ForEach)
921 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
922 SecondPart, ThirdPart, RParenLoc, Body.Val);
923 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000924 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000925 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000926}
927
928/// ParseGotoStatement
929/// jump-statement:
930/// 'goto' identifier ';'
931/// [GNU] 'goto' '*' expression ';'
932///
933/// Note: this lets the caller parse the end ';'.
934///
935Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000936 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
938
939 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000940 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000941 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 Tok.getIdentifierInfo());
943 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000944 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 // GNU indirect goto extension.
946 Diag(Tok, diag::ext_gnu_indirect_goto);
947 SourceLocation StarLoc = ConsumeToken();
948 ExprResult R = ParseExpression();
949 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
950 SkipUntil(tok::semi, false, true);
951 return true;
952 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000953 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000954 } else {
955 Diag(Tok, diag::err_expected_ident);
956 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000958
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 return Res;
960}
961
962/// ParseContinueStatement
963/// jump-statement:
964/// 'continue' ';'
965///
966/// Note: this lets the caller parse the end ';'.
967///
968Parser::StmtResult Parser::ParseContinueStatement() {
969 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000970 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000971}
972
973/// ParseBreakStatement
974/// jump-statement:
975/// 'break' ';'
976///
977/// Note: this lets the caller parse the end ';'.
978///
979Parser::StmtResult Parser::ParseBreakStatement() {
980 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000981 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000982}
983
984/// ParseReturnStatement
985/// jump-statement:
986/// 'return' expression[opt] ';'
987Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000988 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
990
991 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000992 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 R = ParseExpression();
994 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
995 SkipUntil(tok::semi, false, true);
996 return true;
997 }
998 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000999 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001000}
1001
Steve Naroff5f8aa692008-02-11 23:15:56 +00001002/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1003/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +00001004Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001005 if (Tok.is(tok::l_brace)) {
1006 unsigned short savedBraceCount = BraceCount;
1007 do {
1008 ConsumeAnyToken();
1009 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1010 } else {
1011 // From the MS website: If used without braces, the __asm keyword means
1012 // that the rest of the line is an assembly-language statement.
1013 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001014 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001015 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1016 do {
1017 ConsumeAnyToken();
1018 TokLoc = Tok.getLocation();
1019 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1020 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1021 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001022 }
Steve Naroffd77bc282008-04-07 21:06:54 +00001023 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001024}
1025
Reid Spencer5f016e22007-07-11 17:01:13 +00001026/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001027/// asm-statement:
1028/// gnu-asm-statement
1029/// ms-asm-statement
1030///
1031/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001032/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1033///
1034/// [GNU] asm-argument:
1035/// asm-string-literal
1036/// asm-string-literal ':' asm-operands[opt]
1037/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1038/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1039/// ':' asm-clobbers
1040///
1041/// [GNU] asm-clobbers:
1042/// asm-string-literal
1043/// asm-clobbers ',' asm-string-literal
1044///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001045/// [MS] ms-asm-statement:
1046/// '__asm' assembly-instruction ';'[opt]
1047/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1048///
1049/// [MS] assembly-instruction-list:
1050/// assembly-instruction ';'[opt]
1051/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1052///
Steve Naroffd62701b2008-02-07 03:50:06 +00001053Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001054 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001055 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001056
Steve Naroff5f8aa692008-02-11 23:15:56 +00001057 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001058 msAsm = true;
1059 return FuzzyParseMicrosoftAsmStatement();
1060 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 DeclSpec DS;
1062 SourceLocation Loc = Tok.getLocation();
1063 ParseTypeQualifierListOpt(DS);
1064
1065 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1066 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001067 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001069 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Reid Spencer5f016e22007-07-11 17:01:13 +00001070
1071 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001072 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001073 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001074 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001075 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 SkipUntil(tok::r_paren);
1077 return true;
1078 }
1079 Loc = ConsumeParen();
1080
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001081 ExprResult AsmString = ParseAsmStringLiteral();
1082 if (AsmString.isInvalid)
1083 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001084
1085 llvm::SmallVector<std::string, 4> Names;
1086 llvm::SmallVector<ExprTy*, 4> Constraints;
1087 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001088 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001089
Anders Carlssondfab34a2008-02-05 23:03:50 +00001090 unsigned NumInputs = 0, NumOutputs = 0;
1091
1092 SourceLocation RParenLoc;
1093 if (Tok.is(tok::r_paren)) {
1094 // We have a simple asm expression
1095 isSimple = true;
1096
1097 RParenLoc = ConsumeParen();
1098 } else {
1099 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001100 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1101 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001102
1103 NumOutputs = Names.size();
1104
1105 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001106 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1107 return true;
1108
Anders Carlssondfab34a2008-02-05 23:03:50 +00001109 assert(Names.size() == Constraints.size() &&
1110 Constraints.size() == Exprs.size()
1111 && "Input operand size mismatch!");
1112
1113 NumInputs = Names.size() - NumOutputs;
1114
1115 // Parse the clobbers, if present.
1116 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001117 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +00001118
1119 // Parse the asm-string list for clobbers.
1120 while (1) {
1121 ExprResult Clobber = ParseAsmStringLiteral();
1122
1123 if (Clobber.isInvalid)
1124 break;
1125
1126 Clobbers.push_back(Clobber.Val);
1127
1128 if (Tok.isNot(tok::comma)) break;
1129 ConsumeToken();
1130 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 }
Anders Carlssondfab34a2008-02-05 23:03:50 +00001132
1133 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 }
1135
Anders Carlssondfab34a2008-02-05 23:03:50 +00001136 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1137 NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001138 &Names[0], &Constraints[0], &Exprs[0],
1139 AsmString.Val,
1140 Clobbers.size(), &Clobbers[0],
1141 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142}
1143
1144/// ParseAsmOperands - Parse the asm-operands production as used by
1145/// asm-statement. We also parse a leading ':' token. If the leading colon is
1146/// not present, we do not parse anything.
1147///
1148/// [GNU] asm-operands:
1149/// asm-operand
1150/// asm-operands ',' asm-operand
1151///
1152/// [GNU] asm-operand:
1153/// asm-string-literal '(' expression ')'
1154/// '[' identifier ']' asm-string-literal '(' expression ')'
1155///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001156bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001157 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1158 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001160 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 ConsumeToken();
1162
1163 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001164 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001165 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001166
Anders Carlssonb235fc22007-11-22 01:36:19 +00001167 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001169 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 SourceLocation Loc = ConsumeBracket();
1171
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001172 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 Diag(Tok, diag::err_expected_ident);
1174 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001175 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 }
Chris Lattner69efba72007-10-29 04:06:22 +00001177
Anders Carlssonb235fc22007-11-22 01:36:19 +00001178 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001179 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001180
1181 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001183 } else
1184 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001185
Anders Carlssonb235fc22007-11-22 01:36:19 +00001186 ExprResult Constraint = ParseAsmStringLiteral();
1187 if (Constraint.isInvalid) {
1188 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001189 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001190 }
1191 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001192
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001193 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001194 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001196 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 }
1198
1199 // Read the parenthesized expression.
1200 ExprResult Res = ParseSimpleParenExpression();
1201 if (Res.isInvalid) {
1202 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001203 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001205 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001207 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 ConsumeToken();
1209 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001210
1211 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001212}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001213
1214Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1215 SourceLocation L, SourceLocation R) {
1216 // Do not enter a scope for the brace, as the arguments are in the same scope
1217 // (the function body) as the body itself. Instead, just read the statement
1218 // list and put it into a CompoundStmt for safe keeping.
1219 StmtResult FnBody = ParseCompoundStatementBody();
1220
1221 // If the function body could not be parsed, make a bogus compoundstmt.
1222 if (FnBody.isInvalid)
1223 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1224
1225 // Leave the function body scope.
1226 ExitScope();
1227
Steve Naroffd6d054d2007-11-11 23:20:51 +00001228 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001229}