blob: 92b253bdd00e9f0d721affda2d7578e9c7056db1 [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
Chris Lattner476c3ad2006-08-13 22:09:58 +0000240/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000241///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000242/// Note that this does not parse the 'statement' at the end.
243///
244void Parser::ParseCaseStatement() {
245 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
246 ConsumeToken(); // eat the 'case'.
247
Chris Lattner476c3ad2006-08-13 22:09:58 +0000248 ExprResult Res = ParseConstantExpression();
249 if (Res.isInvalid) {
250 SkipUntil(tok::colon);
251 return;
252 }
253
254 // GNU case range extension.
255 if (Tok.getKind() == tok::ellipsis) {
256 Diag(Tok, diag::ext_gnu_case_range);
257 ConsumeToken();
258
259 ExprResult RHS = ParseConstantExpression();
260 if (RHS.isInvalid) {
261 SkipUntil(tok::colon);
262 return;
263 }
264 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000265
266 if (Tok.getKind() == tok::colon) {
267 ConsumeToken();
268 } else {
269 Diag(Tok, diag::err_expected_colon_after, "'case'");
270 SkipUntil(tok::colon);
271 }
272}
273
274/// ParseDefaultStatement
275/// labeled-statement:
276/// 'default' ':' statement
277/// Note that this does not parse the 'statement' at the end.
278///
279void Parser::ParseDefaultStatement() {
280 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
281 ConsumeToken(); // eat the 'default'.
282
283 if (Tok.getKind() == tok::colon) {
284 ConsumeToken();
285 } else {
286 Diag(Tok, diag::err_expected_colon_after, "'default'");
287 SkipUntil(tok::colon);
288 }
289}
290
291
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000292/// ParseCompoundStatement - Parse a "{}" block.
293///
294/// compound-statement: [C99 6.8.2]
295/// { block-item-list[opt] }
296/// [GNU] { label-declarations block-item-list } [TODO]
297///
298/// block-item-list:
299/// block-item
300/// block-item-list block-item
301///
302/// block-item:
303/// declaration
304/// [GNU] '__extension__' declaration [TODO]
305/// statement
306/// [OMP] openmp-directive [TODO]
307///
308/// [GNU] label-declarations:
309/// [GNU] label-declaration
310/// [GNU] label-declarations label-declaration
311///
312/// [GNU] label-declaration:
313/// [GNU] '__label__' identifier-list ';'
314///
315/// [OMP] openmp-directive: [TODO]
316/// [OMP] barrier-directive
317/// [OMP] flush-directive
318void Parser::ParseCompoundStatement() {
319 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
320 ConsumeBrace(); // eat the '{'.
321
322 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof)
Chris Lattnerc951dae2006-08-10 04:23:57 +0000323 ParseStatementOrDeclaration(false);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000324
325 // We broke out of the while loop because we found a '}' or EOF.
326 if (Tok.getKind() == tok::r_brace)
327 ConsumeBrace();
328 else
329 Diag(Tok, diag::err_expected_rbrace);
330}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000331
332/// ParseIfStatement
333/// if-statement: [C99 6.8.4.1]
334/// 'if' '(' expression ')' statement
335/// 'if' '(' expression ')' statement 'else' statement
336void Parser::ParseIfStatement() {
337 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
338 ConsumeToken(); // eat the 'if'.
339
340 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000341 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000342 SkipUntil(tok::semi);
343 return;
344 }
345
346 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000347 ParenParseOption ParenExprType = SimpleExpr;
348 ParseParenExpression(ParenExprType);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000349
350 // Read the if condition.
351 ParseStatement();
352
353 // If it has an else, parse it.
354 if (Tok.getKind() == tok::kw_else) {
355 ConsumeToken();
356 ParseStatement();
357 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000358}
359
Chris Lattner9075bd72006-08-10 04:59:57 +0000360/// ParseSwitchStatement
361/// switch-statement:
362/// 'switch' '(' expression ')' statement
363void Parser::ParseSwitchStatement() {
364 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
365 ConsumeToken(); // eat the 'switch'.
366
367 if (Tok.getKind() != tok::l_paren) {
368 Diag(Tok, diag::err_expected_lparen_after, "switch");
369 SkipUntil(tok::semi);
370 return;
371 }
372
373 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000374 ParenParseOption ParenExprType = SimpleExpr;
375 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000376
377 // Read the body statement.
378 ParseStatement();
379}
380
381/// ParseWhileStatement
382/// while-statement: [C99 6.8.5.1]
383/// 'while' '(' expression ')' statement
384void Parser::ParseWhileStatement() {
385 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
386 ConsumeToken(); // eat the 'while'.
387
388 if (Tok.getKind() != tok::l_paren) {
389 Diag(Tok, diag::err_expected_lparen_after, "while");
390 SkipUntil(tok::semi);
391 return;
392 }
393
394 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000395 ParenParseOption ParenExprType = SimpleExpr;
396 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000397
398 // Read the body statement.
399 ParseStatement();
400}
401
402/// ParseDoStatement
403/// do-statement: [C99 6.8.5.2]
404/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000405/// Note: this lets the caller parse the end ';'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000406void Parser::ParseDoStatement() {
407 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
408 SourceLocation DoLoc = Tok.getLocation();
409 ConsumeToken(); // eat the 'do'.
410
411 // Read the body statement.
412 ParseStatement();
413
414 if (Tok.getKind() != tok::kw_while) {
415 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000416 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000417 SkipUntil(tok::semi);
418 return;
419 }
420 ConsumeToken();
421
422 if (Tok.getKind() != tok::l_paren) {
423 Diag(Tok, diag::err_expected_lparen_after, "do/while");
424 SkipUntil(tok::semi);
425 return;
426 }
427
428 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000429 ParenParseOption ParenExprType = SimpleExpr;
430 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000431}
432
433/// ParseForStatement
434/// for-statement: [C99 6.8.5.3]
435/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
436/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
437void Parser::ParseForStatement() {
438 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
439 SourceLocation ForLoc = Tok.getLocation();
440 ConsumeToken(); // eat the 'for'.
441
442 if (Tok.getKind() != tok::l_paren) {
443 Diag(Tok, diag::err_expected_lparen_after, "for");
444 SkipUntil(tok::semi);
445 return;
446 }
447
448 SourceLocation LParenLoc = Tok.getLocation();
449 ConsumeParen();
450
Chris Lattner89c50c62006-08-11 06:41:18 +0000451 ExprResult Value;
452
Chris Lattner9075bd72006-08-10 04:59:57 +0000453 // Parse the first part of the for specifier.
454 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000455 // no first part, eat the ';'.
456 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000457 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000458 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000459 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
460 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner53361ac2006-08-10 05:19:57 +0000461 ParseDeclaration(Declarator::ForContext);
Chris Lattner9075bd72006-08-10 04:59:57 +0000462 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000463 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000464
Chris Lattner53361ac2006-08-10 05:19:57 +0000465 if (Tok.getKind() == tok::semi) {
466 ConsumeToken();
467 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000468 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000469 SkipUntil(tok::semi);
470 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000471 }
472
473 // Parse the second part of the for specifier.
474 if (Tok.getKind() == tok::semi) { // for (...;;
475 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000476 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000477 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000478 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000479 }
480
481 if (Tok.getKind() == tok::semi) {
482 ConsumeToken();
483 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000484 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000485 SkipUntil(tok::semi);
486 }
487
488 // Parse the third part of the for specifier.
489 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
490 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000491 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000492 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000493 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000494 }
495
Chris Lattner4564bc12006-08-10 23:14:52 +0000496 // Match the ')'.
497 MatchRHSPunctuation(tok::r_paren, LParenLoc, "(", diag::err_expected_rparen);
Chris Lattner9075bd72006-08-10 04:59:57 +0000498
499 // Read the body statement.
500 ParseStatement();
501}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000502
Chris Lattner503fadc2006-08-10 05:45:44 +0000503/// ParseGotoStatement
504/// jump-statement:
505/// 'goto' identifier ';'
506/// [GNU] 'goto' '*' expression ';'
507///
508/// Note: this lets the caller parse the end ';'.
509///
510void Parser::ParseGotoStatement() {
511 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
512 ConsumeToken(); // eat the 'goto'.
513
514 if (Tok.getKind() == tok::identifier) {
515 ConsumeToken();
516 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
517 // GNU indirect goto extension.
518 Diag(Tok, diag::ext_gnu_indirect_goto);
519 ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000520 ExprResult R = ParseExpression();
521 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
522 SkipUntil(tok::semi, false, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000523 }
524}
525
526/// ParseReturnStatement
527/// jump-statement:
528/// 'return' expression[opt] ';'
529void Parser::ParseReturnStatement() {
530 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
531 ConsumeToken(); // eat the 'return'.
532
Chris Lattnera0927ce2006-08-12 16:59:03 +0000533 if (Tok.getKind() != tok::semi) {
534 ExprResult R = ParseExpression();
535 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
536 SkipUntil(tok::semi, false, true);
537 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000538}