blob: 01f4aa59523a3bec7f05e26ef2a92509d25c2c2e [file] [log] [blame]
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001//===--- Statement.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
16#include "clang/Basic/Diagnostic.h"
Chris Lattner53361ac2006-08-10 05:19:57 +000017#include "clang/Parse/Declarations.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000018using namespace llvm;
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.8: Statements and Blocks.
23//===----------------------------------------------------------------------===//
24
25/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
26/// StatementOrDeclaration:
27/// statement
28/// declaration
29///
30/// statement:
31/// labeled-statement
32/// compound-statement
33/// expression-statement
34/// selection-statement
35/// iteration-statement
36/// jump-statement
37/// [OBC] objc-throw-statement [TODO]
38/// [OBC] objc-try-catch-statement [TODO]
39/// [OBC] objc-synchronized-statement [TODO]
40/// [GNU] asm-statement [TODO]
41/// [OMP] openmp-construct [TODO]
42///
43/// labeled-statement:
44/// identifier ':' statement
45/// 'case' constant-expression ':' statement
46/// 'default' ':' statement
47///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// selection-statement:
49/// if-statement
50/// switch-statement
51///
52/// iteration-statement:
53/// while-statement
54/// do-statement
55/// for-statement
56///
Chris Lattner9075bd72006-08-10 04:59:57 +000057/// expression-statement:
58/// expression[opt] ';'
59///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000060/// jump-statement:
61/// 'goto' identifier ';'
62/// 'continue' ';'
63/// 'break' ';'
64/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000065/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000066///
67/// [OBC] objc-throw-statement: [TODO]
68/// [OBC] '@' 'throw' expression ';' [TODO]
69/// [OBC] '@' 'throw' ';' [TODO]
70///
Chris Lattnerc951dae2006-08-10 04:23:57 +000071void Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000072 const char *SemiError = 0;
73
Chris Lattnerd2685cf2006-08-10 05:59:48 +000074ParseNextStatement:
Chris Lattner503fadc2006-08-10 05:45:44 +000075 // Cases in this switch statement should fall through if the parser expects
76 // the token to end in a semicolon (in which case SemiError should be set),
77 // or they directly 'return;' if not.
Chris Lattner0ccd51e2006-08-09 05:47:47 +000078 switch (Tok.getKind()) {
Chris Lattnerf8afb622006-08-10 18:26:31 +000079 case tok::identifier: // C99 6.8.1: labeled-statement
80 // identifier ':' statement
81 // declaration (if !OnlyStatement)
82 // expression[opt] ';'
83 return ParseIdentifierStatement(OnlyStatement);
84
Chris Lattner0ccd51e2006-08-09 05:47:47 +000085 default:
Chris Lattnerf8afb622006-08-10 18:26:31 +000086 if (!OnlyStatement && isDeclarationSpecifier()) {
87 // TODO: warn/disable if declaration is in the middle of a block and !C99.
88 ParseDeclaration(Declarator::BlockContext);
89 return;
90 } else if (Tok.getKind() == tok::r_brace) {
91 Diag(Tok, diag::err_expected_statement);
92 } else {
93 // expression[opt] ';'
Chris Lattner89c50c62006-08-11 06:41:18 +000094 ExprResult Res = ParseExpression();
95 if (Res.isInvalid) {
96 // If the expression is invalid, skip ahead to the next semicolon. Not
97 // doing this opens us up to the possibility of infinite loops if
98 // ParseExpression does not consume any tokens.
99 SkipUntil(tok::semi);
100 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000101 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000102 return;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000103
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000104 case tok::kw_case: // C99 6.8.1: labeled-statement
105 ParseCaseStatement();
106 if (Tok.getKind() == tok::r_brace) {
107 Diag(Tok, diag::err_label_end_of_compound_statement);
108 return;
109 }
110 OnlyStatement = true;
111 goto ParseNextStatement;
112 case tok::kw_default: // C99 6.8.1: labeled-statement
113 ParseDefaultStatement();
114 if (Tok.getKind() == tok::r_brace) {
115 Diag(Tok, diag::err_label_end_of_compound_statement);
116 return;
117 }
118 OnlyStatement = true;
119 goto ParseNextStatement;
Chris Lattner9075bd72006-08-10 04:59:57 +0000120
121 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000122 ParseCompoundStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000123 return;
Chris Lattner9075bd72006-08-10 04:59:57 +0000124 case tok::semi: // C99 6.8.3: expression[opt] ';'
Chris Lattnerc951dae2006-08-10 04:23:57 +0000125 ConsumeToken();
Chris Lattner503fadc2006-08-10 05:45:44 +0000126 return;
127
Chris Lattner9075bd72006-08-10 04:59:57 +0000128 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattnerc951dae2006-08-10 04:23:57 +0000129 ParseIfStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000130 return;
Chris Lattner9075bd72006-08-10 04:59:57 +0000131 case tok::kw_switch: // C99 6.8.4.2: switch-statement
132 ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000133 return;
134
Chris Lattner9075bd72006-08-10 04:59:57 +0000135 case tok::kw_while: // C99 6.8.5.1: while-statement
136 ParseWhileStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000137 return;
Chris Lattner9075bd72006-08-10 04:59:57 +0000138 case tok::kw_do: // C99 6.8.5.2: do-statement
139 ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000140 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000141 break;
142 case tok::kw_for: // C99 6.8.5.3: for-statement
143 ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000144 return;
145
146 case tok::kw_goto: // C99 6.8.6.1: goto-statement
147 ParseGotoStatement();
148 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000149 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000150 case tok::kw_continue: // C99 6.8.6.2: continue-statement
151 ConsumeToken(); // eat the 'continue'.
152 SemiError = "continue statement";
153 break;
154 case tok::kw_break: // C99 6.8.6.3: break-statement
155 ConsumeToken(); // eat the 'break'.
156 SemiError = "break statement";
157 break;
158 case tok::kw_return: // C99 6.8.6.4: return-statement
159 ParseReturnStatement();
160 SemiError = "return statement";
161 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000162 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000163
164 // If we reached this code, the statement must end in a semicolon.
165 if (Tok.getKind() == tok::semi) {
166 ConsumeToken();
167 } else {
168 Diag(Tok, diag::err_expected_semi_after, SemiError);
169 SkipUntil(tok::semi);
170 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000171}
172
Chris Lattnerf8afb622006-08-10 18:26:31 +0000173/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
174/// have a bit of a quandry here. Reading the identifier is necessary to see if
175/// there is a ':' after it. If there is, this is a label, regardless of what
176/// else the identifier can mean. If not, this is either part of a declaration
177/// (if the identifier is a type-name) or part of an expression.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000178///
179/// labeled-statement:
180/// identifier ':' statement
181/// declaration (if !OnlyStatement)
182/// expression[opt] ';'
183///
Chris Lattnerf8afb622006-08-10 18:26:31 +0000184void Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000185 IdentifierInfo *II = Tok.getIdentifierInfo();
186 assert(Tok.getKind() == tok::identifier && II && "Not an identifier!");
187
Chris Lattner0c6c0342006-08-12 18:12:45 +0000188 LexerToken IdentTok = Tok; // Save the token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000189 ConsumeToken(); // eat the identifier.
190
191 // identifier ':' statement
192 if (Tok.getKind() == tok::colon) {
193 ConsumeToken();
194 ParseStatement();
195 return;
196 }
197
198 // declaration
199 if (!OnlyStatement && 0/*Is typedef name!*/) {
200 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000201 DeclSpec DS;
202
203 // FIXME: Add the typedef name to the start of the decl-specs.
204 // ParseDeclarationSpecifiers will continue from there.
205 ParseDeclarationSpecifiers(DS);
206
Chris Lattner0e894622006-08-13 19:58:17 +0000207 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
208 // declaration-specifiers init-declarator-list[opt] ';'
209 if (Tok.getKind() == tok::semi) {
210 // TODO: emit error on 'int;' or 'const enum foo;'.
211 // if (!DS.isMissingDeclaratorOk()) Diag(...);
212
213 ConsumeToken();
214 return;
215 }
216
Chris Lattner2f9980e2006-08-10 18:39:24 +0000217 // Parse all the declarators.
218 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
219 ParseDeclarator(DeclaratorInfo);
220
221 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
222 return;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000223 }
224
Chris Lattner0c6c0342006-08-12 18:12:45 +0000225 // Otherwise, this is an expression. Seed it with II and parse it.
226 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
227 if (Res.isInvalid)
228 SkipUntil(tok::semi);
229 else if (Tok.getKind() == tok::semi)
230 ConsumeToken();
231 else {
232 Diag(Tok, diag::err_expected_semi_after, "expression");
233 SkipUntil(tok::semi);
234 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000235}
236
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000237/// ParseCaseStatement
238/// labeled-statement:
239/// 'case' constant-expression ':' statement
240///
241/// Note that this does not parse the 'statement' at the end.
242///
243void Parser::ParseCaseStatement() {
244 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
245 ConsumeToken(); // eat the 'case'.
246
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000247 ParseAssignmentExpression(); // Expr without commas.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000248
249 if (Tok.getKind() == tok::colon) {
250 ConsumeToken();
251 } else {
252 Diag(Tok, diag::err_expected_colon_after, "'case'");
253 SkipUntil(tok::colon);
254 }
255}
256
257/// ParseDefaultStatement
258/// labeled-statement:
259/// 'default' ':' statement
260/// Note that this does not parse the 'statement' at the end.
261///
262void Parser::ParseDefaultStatement() {
263 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
264 ConsumeToken(); // eat the 'default'.
265
266 if (Tok.getKind() == tok::colon) {
267 ConsumeToken();
268 } else {
269 Diag(Tok, diag::err_expected_colon_after, "'default'");
270 SkipUntil(tok::colon);
271 }
272}
273
274
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000275/// ParseCompoundStatement - Parse a "{}" block.
276///
277/// compound-statement: [C99 6.8.2]
278/// { block-item-list[opt] }
279/// [GNU] { label-declarations block-item-list } [TODO]
280///
281/// block-item-list:
282/// block-item
283/// block-item-list block-item
284///
285/// block-item:
286/// declaration
287/// [GNU] '__extension__' declaration [TODO]
288/// statement
289/// [OMP] openmp-directive [TODO]
290///
291/// [GNU] label-declarations:
292/// [GNU] label-declaration
293/// [GNU] label-declarations label-declaration
294///
295/// [GNU] label-declaration:
296/// [GNU] '__label__' identifier-list ';'
297///
298/// [OMP] openmp-directive: [TODO]
299/// [OMP] barrier-directive
300/// [OMP] flush-directive
301void Parser::ParseCompoundStatement() {
302 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
303 ConsumeBrace(); // eat the '{'.
304
305 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof)
Chris Lattnerc951dae2006-08-10 04:23:57 +0000306 ParseStatementOrDeclaration(false);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000307
308 // We broke out of the while loop because we found a '}' or EOF.
309 if (Tok.getKind() == tok::r_brace)
310 ConsumeBrace();
311 else
312 Diag(Tok, diag::err_expected_rbrace);
313}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000314
315/// ParseIfStatement
316/// if-statement: [C99 6.8.4.1]
317/// 'if' '(' expression ')' statement
318/// 'if' '(' expression ')' statement 'else' statement
319void Parser::ParseIfStatement() {
320 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
321 ConsumeToken(); // eat the 'if'.
322
323 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000324 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000325 SkipUntil(tok::semi);
326 return;
327 }
328
329 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000330 ParenParseOption ParenExprType = SimpleExpr;
331 ParseParenExpression(ParenExprType);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000332
333 // Read the if condition.
334 ParseStatement();
335
336 // If it has an else, parse it.
337 if (Tok.getKind() == tok::kw_else) {
338 ConsumeToken();
339 ParseStatement();
340 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000341}
342
Chris Lattner9075bd72006-08-10 04:59:57 +0000343/// ParseSwitchStatement
344/// switch-statement:
345/// 'switch' '(' expression ')' statement
346void Parser::ParseSwitchStatement() {
347 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
348 ConsumeToken(); // eat the 'switch'.
349
350 if (Tok.getKind() != tok::l_paren) {
351 Diag(Tok, diag::err_expected_lparen_after, "switch");
352 SkipUntil(tok::semi);
353 return;
354 }
355
356 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000357 ParenParseOption ParenExprType = SimpleExpr;
358 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000359
360 // Read the body statement.
361 ParseStatement();
362}
363
364/// ParseWhileStatement
365/// while-statement: [C99 6.8.5.1]
366/// 'while' '(' expression ')' statement
367void Parser::ParseWhileStatement() {
368 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
369 ConsumeToken(); // eat the 'while'.
370
371 if (Tok.getKind() != tok::l_paren) {
372 Diag(Tok, diag::err_expected_lparen_after, "while");
373 SkipUntil(tok::semi);
374 return;
375 }
376
377 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000378 ParenParseOption ParenExprType = SimpleExpr;
379 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000380
381 // Read the body statement.
382 ParseStatement();
383}
384
385/// ParseDoStatement
386/// do-statement: [C99 6.8.5.2]
387/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000388/// Note: this lets the caller parse the end ';'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000389void Parser::ParseDoStatement() {
390 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
391 SourceLocation DoLoc = Tok.getLocation();
392 ConsumeToken(); // eat the 'do'.
393
394 // Read the body statement.
395 ParseStatement();
396
397 if (Tok.getKind() != tok::kw_while) {
398 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000399 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000400 SkipUntil(tok::semi);
401 return;
402 }
403 ConsumeToken();
404
405 if (Tok.getKind() != tok::l_paren) {
406 Diag(Tok, diag::err_expected_lparen_after, "do/while");
407 SkipUntil(tok::semi);
408 return;
409 }
410
411 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000412 ParenParseOption ParenExprType = SimpleExpr;
413 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000414}
415
416/// ParseForStatement
417/// for-statement: [C99 6.8.5.3]
418/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
419/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
420void Parser::ParseForStatement() {
421 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
422 SourceLocation ForLoc = Tok.getLocation();
423 ConsumeToken(); // eat the 'for'.
424
425 if (Tok.getKind() != tok::l_paren) {
426 Diag(Tok, diag::err_expected_lparen_after, "for");
427 SkipUntil(tok::semi);
428 return;
429 }
430
431 SourceLocation LParenLoc = Tok.getLocation();
432 ConsumeParen();
433
Chris Lattner89c50c62006-08-11 06:41:18 +0000434 ExprResult Value;
435
Chris Lattner9075bd72006-08-10 04:59:57 +0000436 // Parse the first part of the for specifier.
437 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000438 // no first part, eat the ';'.
439 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000440 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000441 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000442 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
443 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner53361ac2006-08-10 05:19:57 +0000444 ParseDeclaration(Declarator::ForContext);
Chris Lattner9075bd72006-08-10 04:59:57 +0000445 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000446 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000447
Chris Lattner53361ac2006-08-10 05:19:57 +0000448 if (Tok.getKind() == tok::semi) {
449 ConsumeToken();
450 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000451 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000452 SkipUntil(tok::semi);
453 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000454 }
455
456 // Parse the second part of the for specifier.
457 if (Tok.getKind() == tok::semi) { // for (...;;
458 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000459 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000460 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000461 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000462 }
463
464 if (Tok.getKind() == tok::semi) {
465 ConsumeToken();
466 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000467 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000468 SkipUntil(tok::semi);
469 }
470
471 // Parse the third part of the for specifier.
472 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
473 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000474 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000475 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000476 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000477 }
478
Chris Lattner4564bc12006-08-10 23:14:52 +0000479 // Match the ')'.
480 MatchRHSPunctuation(tok::r_paren, LParenLoc, "(", diag::err_expected_rparen);
Chris Lattner9075bd72006-08-10 04:59:57 +0000481
482 // Read the body statement.
483 ParseStatement();
484}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000485
Chris Lattner503fadc2006-08-10 05:45:44 +0000486/// ParseGotoStatement
487/// jump-statement:
488/// 'goto' identifier ';'
489/// [GNU] 'goto' '*' expression ';'
490///
491/// Note: this lets the caller parse the end ';'.
492///
493void Parser::ParseGotoStatement() {
494 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
495 ConsumeToken(); // eat the 'goto'.
496
497 if (Tok.getKind() == tok::identifier) {
498 ConsumeToken();
499 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
500 // GNU indirect goto extension.
501 Diag(Tok, diag::ext_gnu_indirect_goto);
502 ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000503 ExprResult R = ParseExpression();
504 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
505 SkipUntil(tok::semi, false, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000506 }
507}
508
509/// ParseReturnStatement
510/// jump-statement:
511/// 'return' expression[opt] ';'
512void Parser::ParseReturnStatement() {
513 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
514 ConsumeToken(); // eat the 'return'.
515
Chris Lattnera0927ce2006-08-12 16:59:03 +0000516 if (Tok.getKind() != tok::semi) {
517 ExprResult R = ParseExpression();
518 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
519 SkipUntil(tok::semi, false, true);
520 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000521}