blob: 70062416fb31414e6aeb942971da62bbdf15f45b [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.
Douglas Gregor74253732008-11-19 15:42:04 +0000389 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
390 Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000391 if (Res.isInvalid)
392 continue;
393
Chris Lattner39146d62008-10-20 06:51:33 +0000394 // Eat the semicolon at the end of stmt and convert the expr into a
395 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000396 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)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000428 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 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.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000442 // C++ 3.3.2p4:
443 // Names declared in the for-init-statement, and in the condition of if,
444 // while, for, and switch statements are local to the if, while, for, or
445 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000446 //
447 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000448 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000449
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000451 ExprResult CondExp;
452 if (getLang().CPlusPlus) {
453 SourceLocation LParenLoc = ConsumeParen();
454 CondExp = ParseCXXCondition();
455 MatchRHSPunctuation(tok::r_paren, LParenLoc);
456 } else {
457 CondExp = ParseSimpleParenExpression();
458 }
459
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 if (CondExp.isInvalid) {
461 SkipUntil(tok::semi);
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000462 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000463 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 return true;
465 }
466
Chris Lattner0ecea032007-08-22 05:28:50 +0000467 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000468 // there is no compound stmt. C90 does not have this clause. We only do this
469 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000470 //
471 // C++ 6.4p1:
472 // The substatement in a selection-statement (each substatement, in the else
473 // form of the if statement) implicitly defines a local scope.
474 //
475 // For C++ we create a scope for the condition and a new scope for
476 // substatements because:
477 // -When the 'then' scope exits, we want the condition declaration to still be
478 // active for the 'else' scope too.
479 // -Sema will detect name clashes by considering declarations of a
480 // 'ControlScope' as part of its direct subscope.
481 // -If we wanted the condition and substatement to be in the same scope, we
482 // would have to notify ParseStatement not to create a new scope. It's
483 // simpler to let it create a new scope.
484 //
485 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000486 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000487
Chris Lattnerb96728d2007-10-29 05:08:52 +0000488 // Read the 'then' stmt.
489 SourceLocation ThenStmtLoc = Tok.getLocation();
490 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000491
Chris Lattnera36ce712007-08-22 05:16:28 +0000492 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000493 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000494
495 // If it has an else, parse it.
496 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000497 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000499
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000500 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000502
Chris Lattner0ecea032007-08-22 05:28:50 +0000503 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000504 // there is no compound stmt. C90 does not have this clause. We only do
505 // this if the body isn't a compound statement to avoid push/pop in common
506 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000507 //
508 // C++ 6.4p1:
509 // The substatement in a selection-statement (each substatement, in the else
510 // form of the if statement) implicitly defines a local scope.
511 //
512 NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000513 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000514
Chris Lattnerb96728d2007-10-29 05:08:52 +0000515 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000517
518 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000519 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 }
521
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000522 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000523 ExitScope();
524
Chris Lattnerb96728d2007-10-29 05:08:52 +0000525 // If the then or else stmt is invalid and the other is valid (and present),
526 // make turn the invalid one into a null stmt to avoid dropping the other
527 // part. If both are invalid, return error.
528 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
529 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
530 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
531 // Both invalid, or one is invalid and other is non-present: delete cond and
532 // return error.
533 Actions.DeleteExpr(CondExp.Val);
534 return true;
535 }
536
537 // Now if either are invalid, replace with a ';'.
538 if (ThenStmt.isInvalid)
539 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
540 if (ElseStmt.isInvalid)
541 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
542
Chris Lattnerb96728d2007-10-29 05:08:52 +0000543 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 ElseLoc, ElseStmt.Val);
545}
546
547/// ParseSwitchStatement
548/// switch-statement:
549/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000550/// [C++] 'switch' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000551Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000552 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
554
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000555 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000556 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 SkipUntil(tok::semi);
558 return true;
559 }
Chris Lattner22153252007-08-26 23:08:06 +0000560
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000561 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
562
Chris Lattner22153252007-08-26 23:08:06 +0000563 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
564 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000565 //
566 // C++ 6.4p3:
567 // A name introduced by a declaration in a condition is in scope from its
568 // point of declaration until the end of the substatements controlled by the
569 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000570 // C++ 3.3.2p4:
571 // Names declared in the for-init-statement, and in the condition of if,
572 // while, for, and switch statements are local to the if, while, for, or
573 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000574 //
575 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000576 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000577 else
578 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579
580 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000581 ExprResult Cond;
582 if (getLang().CPlusPlus) {
583 SourceLocation LParenLoc = ConsumeParen();
584 Cond = ParseCXXCondition();
585 MatchRHSPunctuation(tok::r_paren, LParenLoc);
586 } else {
587 Cond = ParseSimpleParenExpression();
588 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000589
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000590 if (Cond.isInvalid) {
591 ExitScope();
592 return true;
593 }
594
Steve Naroff1b273c42007-09-16 14:56:35 +0000595 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000596
Chris Lattner0ecea032007-08-22 05:28:50 +0000597 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000598 // there is no compound stmt. C90 does not have this clause. We only do this
599 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000600 //
601 // C++ 6.4p1:
602 // The substatement in a selection-statement (each substatement, in the else
603 // form of the if statement) implicitly defines a local scope.
604 //
605 // See comments in ParseIfStatement for why we create a scope for the
606 // condition and a new scope for substatement in C++.
607 //
608 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000609 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 // Read the body statement.
612 StmtResult Body = ParseStatement();
613
Chris Lattner0ecea032007-08-22 05:28:50 +0000614 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000615 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000616
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000617 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000618 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000619 // FIXME: Remove the case statement list from the Switch statement.
620 }
621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 ExitScope();
623
Steve Naroff1b273c42007-09-16 14:56:35 +0000624 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000625}
626
627/// ParseWhileStatement
628/// while-statement: [C99 6.8.5.1]
629/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000630/// [C++] 'while' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000631Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000632 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 SourceLocation WhileLoc = Tok.getLocation();
634 ConsumeToken(); // eat the 'while'.
635
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000636 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000637 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 SkipUntil(tok::semi);
639 return true;
640 }
641
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000642 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
643
Chris Lattner22153252007-08-26 23:08:06 +0000644 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
645 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000646 //
647 // C++ 6.4p3:
648 // A name introduced by a declaration in a condition is in scope from its
649 // point of declaration until the end of the substatements controlled by the
650 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000651 // C++ 3.3.2p4:
652 // Names declared in the for-init-statement, and in the condition of if,
653 // while, for, and switch statements are local to the if, while, for, or
654 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000655 //
656 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000657 EnterScope(Scope::BreakScope | Scope::ContinueScope |
658 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000659 else
660 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000661
662 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000663 ExprResult Cond;
664 if (getLang().CPlusPlus) {
665 SourceLocation LParenLoc = ConsumeParen();
666 Cond = ParseCXXCondition();
667 MatchRHSPunctuation(tok::r_paren, LParenLoc);
668 } else {
669 Cond = ParseSimpleParenExpression();
670 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000671
Chris Lattner0ecea032007-08-22 05:28:50 +0000672 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000673 // there is no compound stmt. C90 does not have this clause. We only do this
674 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000675 //
676 // C++ 6.5p2:
677 // The substatement in an iteration-statement implicitly defines a local scope
678 // which is entered and exited each time through the loop.
679 //
680 // See comments in ParseIfStatement for why we create a scope for the
681 // condition and a new scope for substatement in C++.
682 //
683 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000684 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // Read the body statement.
687 StmtResult Body = ParseStatement();
688
Chris Lattner0ecea032007-08-22 05:28:50 +0000689 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000690 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000691
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 ExitScope();
693
694 if (Cond.isInvalid || Body.isInvalid) return true;
695
Steve Naroff1b273c42007-09-16 14:56:35 +0000696 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000697}
698
699/// ParseDoStatement
700/// do-statement: [C99 6.8.5.2]
701/// 'do' statement 'while' '(' expression ')' ';'
702/// Note: this lets the caller parse the end ';'.
703Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000704 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
706
Chris Lattner22153252007-08-26 23:08:06 +0000707 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
708 // the case for C90. Start the loop scope.
709 if (getLang().C99)
710 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
711 else
712 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000713
Chris Lattner0ecea032007-08-22 05:28:50 +0000714 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000715 // there is no compound stmt. C90 does not have this clause. We only do this
716 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000717 //
718 // C++ 6.5p2:
719 // The substatement in an iteration-statement implicitly defines a local scope
720 // which is entered and exited each time through the loop.
721 //
Chris Lattner19504402008-11-13 18:52:53 +0000722 bool NeedsInnerScope =
723 (getLang().C99 || getLang().CPlusPlus) && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000724 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000725
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 // Read the body statement.
727 StmtResult Body = ParseStatement();
728
Chris Lattner0ecea032007-08-22 05:28:50 +0000729 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000730 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000731
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000732 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 ExitScope();
Chris Lattner19504402008-11-13 18:52:53 +0000734 if (!Body.isInvalid) {
735 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000736 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000737 SkipUntil(tok::semi, false, true);
738 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 return true;
740 }
741 SourceLocation WhileLoc = ConsumeToken();
742
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000743 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 ExitScope();
Chris Lattner1ab3b962008-11-18 07:48:38 +0000745 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000746 SkipUntil(tok::semi, false, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 return true;
748 }
749
750 // Parse the condition.
751 ExprResult Cond = ParseSimpleParenExpression();
752
753 ExitScope();
754
755 if (Cond.isInvalid || Body.isInvalid) return true;
756
Steve Naroff1b273c42007-09-16 14:56:35 +0000757 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000758}
759
760/// ParseForStatement
761/// for-statement: [C99 6.8.5.3]
762/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
763/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000764/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
765/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000766/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
767/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000768///
769/// [C++] for-init-statement:
770/// [C++] expression-statement
771/// [C++] simple-declaration
772///
Reid Spencer5f016e22007-07-11 17:01:13 +0000773Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000774 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
776
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000777 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000778 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 SkipUntil(tok::semi);
780 return true;
781 }
782
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000783 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
784
Chris Lattner22153252007-08-26 23:08:06 +0000785 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
786 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000787 //
788 // C++ 6.4p3:
789 // A name introduced by a declaration in a condition is in scope from its
790 // point of declaration until the end of the substatements controlled by the
791 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000792 // C++ 3.3.2p4:
793 // Names declared in the for-init-statement, and in the condition of if,
794 // while, for, and switch statements are local to the if, while, for, or
795 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000796 // C++ 6.5.3p1:
797 // Names declared in the for-init-statement are in the same declarative-region
798 // as those declared in the condition.
799 //
800 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000801 EnterScope(Scope::BreakScope | Scope::ContinueScope |
802 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000803 else
804 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000805
806 SourceLocation LParenLoc = ConsumeParen();
807 ExprResult Value;
808
809 StmtTy *FirstPart = 0;
810 ExprTy *SecondPart = 0;
811 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000812 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813
814 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000815 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // no first part, eat the ';'.
817 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000818 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000820 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000822
823 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000824 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000825 // FIXME: Pass in the right location for the end of the declstmt.
826 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattner691a38b2008-03-13 06:29:54 +0000827 DeclStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000829 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000830 ConsumeToken(); // consume 'in'
831 Value = ParseExpression();
832 if (!Value.isInvalid)
833 SecondPart = Value.Val;
834 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 } else {
836 Value = ParseExpression();
837
838 // Turn the expression into a stmt.
839 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000840 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 if (!R.isInvalid)
842 FirstPart = R.Val;
843 }
844
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000845 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000847 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000848 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000849 ConsumeToken(); // consume 'in'
850 Value = ParseExpression();
851 if (!Value.isInvalid)
852 SecondPart = Value.Val;
853 }
854 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
856 SkipUntil(tok::semi);
857 }
858 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000859 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000860 // Parse the second part of the for specifier.
861 if (Tok.is(tok::semi)) { // for (...;;
862 // no second part.
863 Value = ExprResult();
864 } else {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000865 Value = getLang().CPlusPlus ? ParseCXXCondition()
866 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000867 if (!Value.isInvalid)
868 SecondPart = Value.Val;
869 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000870
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000871 if (Tok.is(tok::semi)) {
872 ConsumeToken();
873 } else {
874 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
875 SkipUntil(tok::semi);
876 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000877
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000878 // Parse the third part of the for specifier.
879 if (Tok.is(tok::r_paren)) { // for (...;...;)
880 // no third part.
881 Value = ExprResult();
882 } else {
883 Value = ParseExpression();
884 if (!Value.isInvalid) {
885 // Turn the expression into a stmt.
886 StmtResult R = Actions.ActOnExprStmt(Value.Val);
887 if (!R.isInvalid)
888 ThirdPart = R.Val;
889 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 }
891 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 // Match the ')'.
893 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
894
Chris Lattner0ecea032007-08-22 05:28:50 +0000895 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000896 // there is no compound stmt. C90 does not have this clause. We only do this
897 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000898 //
899 // C++ 6.5p2:
900 // The substatement in an iteration-statement implicitly defines a local scope
901 // which is entered and exited each time through the loop.
902 //
903 // See comments in ParseIfStatement for why we create a scope for
904 // for-init-statement/condition and a new scope for substatement in C++.
905 //
906 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000907 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000908
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 // Read the body statement.
910 StmtResult Body = ParseStatement();
911
Chris Lattner0ecea032007-08-22 05:28:50 +0000912 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000913 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000914
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 // Leave the for-scope.
916 ExitScope();
917
918 if (Body.isInvalid)
919 return Body;
920
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000921 if (!ForEach)
922 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
923 SecondPart, ThirdPart, RParenLoc, Body.Val);
924 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000925 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000926 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927}
928
929/// ParseGotoStatement
930/// jump-statement:
931/// 'goto' identifier ';'
932/// [GNU] 'goto' '*' expression ';'
933///
934/// Note: this lets the caller parse the end ';'.
935///
936Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000937 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
939
940 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000941 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000942 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 Tok.getIdentifierInfo());
944 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000945 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 // GNU indirect goto extension.
947 Diag(Tok, diag::ext_gnu_indirect_goto);
948 SourceLocation StarLoc = ConsumeToken();
949 ExprResult R = ParseExpression();
950 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
951 SkipUntil(tok::semi, false, true);
952 return true;
953 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000954 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000955 } else {
956 Diag(Tok, diag::err_expected_ident);
957 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000959
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 return Res;
961}
962
963/// ParseContinueStatement
964/// jump-statement:
965/// 'continue' ';'
966///
967/// Note: this lets the caller parse the end ';'.
968///
969Parser::StmtResult Parser::ParseContinueStatement() {
970 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000971 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000972}
973
974/// ParseBreakStatement
975/// jump-statement:
976/// 'break' ';'
977///
978/// Note: this lets the caller parse the end ';'.
979///
980Parser::StmtResult Parser::ParseBreakStatement() {
981 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000982 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000983}
984
985/// ParseReturnStatement
986/// jump-statement:
987/// 'return' expression[opt] ';'
988Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000989 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
991
992 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000993 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 R = ParseExpression();
995 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
996 SkipUntil(tok::semi, false, true);
997 return true;
998 }
999 }
Steve Naroff1b273c42007-09-16 14:56:35 +00001000 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001001}
1002
Steve Naroff5f8aa692008-02-11 23:15:56 +00001003/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1004/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +00001005Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001006 if (Tok.is(tok::l_brace)) {
1007 unsigned short savedBraceCount = BraceCount;
1008 do {
1009 ConsumeAnyToken();
1010 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1011 } else {
1012 // From the MS website: If used without braces, the __asm keyword means
1013 // that the rest of the line is an assembly-language statement.
1014 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001015 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001016 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1017 do {
1018 ConsumeAnyToken();
1019 TokLoc = Tok.getLocation();
1020 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1021 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1022 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001023 }
Steve Naroffd77bc282008-04-07 21:06:54 +00001024 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001025}
1026
Reid Spencer5f016e22007-07-11 17:01:13 +00001027/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001028/// asm-statement:
1029/// gnu-asm-statement
1030/// ms-asm-statement
1031///
1032/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001033/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1034///
1035/// [GNU] asm-argument:
1036/// asm-string-literal
1037/// asm-string-literal ':' asm-operands[opt]
1038/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1039/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1040/// ':' asm-clobbers
1041///
1042/// [GNU] asm-clobbers:
1043/// asm-string-literal
1044/// asm-clobbers ',' asm-string-literal
1045///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001046/// [MS] ms-asm-statement:
1047/// '__asm' assembly-instruction ';'[opt]
1048/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1049///
1050/// [MS] assembly-instruction-list:
1051/// assembly-instruction ';'[opt]
1052/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1053///
Steve Naroffd62701b2008-02-07 03:50:06 +00001054Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001055 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001056 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001057
Steve Naroff5f8aa692008-02-11 23:15:56 +00001058 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001059 msAsm = true;
1060 return FuzzyParseMicrosoftAsmStatement();
1061 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 DeclSpec DS;
1063 SourceLocation Loc = Tok.getLocation();
1064 ParseTypeQualifierListOpt(DS);
1065
1066 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1067 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001068 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001070 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Reid Spencer5f016e22007-07-11 17:01:13 +00001071
1072 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001073 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001074 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001075 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001076 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 SkipUntil(tok::r_paren);
1078 return true;
1079 }
1080 Loc = ConsumeParen();
1081
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001082 ExprResult AsmString = ParseAsmStringLiteral();
1083 if (AsmString.isInvalid)
1084 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001085
1086 llvm::SmallVector<std::string, 4> Names;
1087 llvm::SmallVector<ExprTy*, 4> Constraints;
1088 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001089 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001090
Anders Carlssondfab34a2008-02-05 23:03:50 +00001091 unsigned NumInputs = 0, NumOutputs = 0;
1092
1093 SourceLocation RParenLoc;
1094 if (Tok.is(tok::r_paren)) {
1095 // We have a simple asm expression
1096 isSimple = true;
1097
1098 RParenLoc = ConsumeParen();
1099 } else {
1100 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001101 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1102 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001103
1104 NumOutputs = Names.size();
1105
1106 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001107 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1108 return true;
1109
Anders Carlssondfab34a2008-02-05 23:03:50 +00001110 assert(Names.size() == Constraints.size() &&
1111 Constraints.size() == Exprs.size()
1112 && "Input operand size mismatch!");
1113
1114 NumInputs = Names.size() - NumOutputs;
1115
1116 // Parse the clobbers, if present.
1117 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001118 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +00001119
1120 // Parse the asm-string list for clobbers.
1121 while (1) {
1122 ExprResult Clobber = ParseAsmStringLiteral();
1123
1124 if (Clobber.isInvalid)
1125 break;
1126
1127 Clobbers.push_back(Clobber.Val);
1128
1129 if (Tok.isNot(tok::comma)) break;
1130 ConsumeToken();
1131 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 }
Anders Carlssondfab34a2008-02-05 23:03:50 +00001133
1134 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 }
1136
Anders Carlssondfab34a2008-02-05 23:03:50 +00001137 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1138 NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001139 &Names[0], &Constraints[0], &Exprs[0],
1140 AsmString.Val,
1141 Clobbers.size(), &Clobbers[0],
1142 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001143}
1144
1145/// ParseAsmOperands - Parse the asm-operands production as used by
1146/// asm-statement. We also parse a leading ':' token. If the leading colon is
1147/// not present, we do not parse anything.
1148///
1149/// [GNU] asm-operands:
1150/// asm-operand
1151/// asm-operands ',' asm-operand
1152///
1153/// [GNU] asm-operand:
1154/// asm-string-literal '(' expression ')'
1155/// '[' identifier ']' asm-string-literal '(' expression ')'
1156///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001157bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001158 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1159 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001161 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 ConsumeToken();
1163
1164 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001165 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001166 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001167
Anders Carlssonb235fc22007-11-22 01:36:19 +00001168 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001170 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 SourceLocation Loc = ConsumeBracket();
1172
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001173 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 Diag(Tok, diag::err_expected_ident);
1175 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001176 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 }
Chris Lattner69efba72007-10-29 04:06:22 +00001178
Anders Carlssonb235fc22007-11-22 01:36:19 +00001179 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001180 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001181
1182 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001184 } else
1185 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001186
Anders Carlssonb235fc22007-11-22 01:36:19 +00001187 ExprResult Constraint = ParseAsmStringLiteral();
1188 if (Constraint.isInvalid) {
1189 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001190 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001191 }
1192 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001194 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001195 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001197 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 }
1199
1200 // Read the parenthesized expression.
1201 ExprResult Res = ParseSimpleParenExpression();
1202 if (Res.isInvalid) {
1203 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001204 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001206 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001208 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 ConsumeToken();
1210 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001211
1212 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001213}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001214
1215Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1216 SourceLocation L, SourceLocation R) {
1217 // Do not enter a scope for the brace, as the arguments are in the same scope
1218 // (the function body) as the body itself. Instead, just read the statement
1219 // list and put it into a CompoundStmt for safe keeping.
1220 StmtResult FnBody = ParseCompoundStatementBody();
1221
1222 // If the function body could not be parsed, make a bogus compoundstmt.
1223 if (FnBody.isInvalid)
1224 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1225
1226 // Leave the function body scope.
1227 ExitScope();
1228
Steve Naroffd6d054d2007-11-11 23:20:51 +00001229 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001230}