blob: e08c75d571296961b0674563317add46e4a76e3c [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
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000199 if (!OnlyStatement &&
200 Actions.isTypedefName(*IdentTok.getIdentifierInfo(), CurScope)) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000201 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000202 DeclSpec DS;
203
204 // FIXME: Add the typedef name to the start of the decl-specs.
205 // ParseDeclarationSpecifiers will continue from there.
206 ParseDeclarationSpecifiers(DS);
207
Chris Lattner0e894622006-08-13 19:58:17 +0000208 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
209 // declaration-specifiers init-declarator-list[opt] ';'
210 if (Tok.getKind() == tok::semi) {
211 // TODO: emit error on 'int;' or 'const enum foo;'.
212 // if (!DS.isMissingDeclaratorOk()) Diag(...);
213
214 ConsumeToken();
215 return;
216 }
217
Chris Lattner2f9980e2006-08-10 18:39:24 +0000218 // Parse all the declarators.
219 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
220 ParseDeclarator(DeclaratorInfo);
221
222 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
223 return;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000224 }
225
Chris Lattner0c6c0342006-08-12 18:12:45 +0000226 // Otherwise, this is an expression. Seed it with II and parse it.
227 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
228 if (Res.isInvalid)
229 SkipUntil(tok::semi);
230 else if (Tok.getKind() == tok::semi)
231 ConsumeToken();
232 else {
233 Diag(Tok, diag::err_expected_semi_after, "expression");
234 SkipUntil(tok::semi);
235 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000236}
237
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000238/// ParseCaseStatement
239/// labeled-statement:
240/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000241/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000242///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000243/// Note that this does not parse the 'statement' at the end.
244///
245void Parser::ParseCaseStatement() {
246 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
247 ConsumeToken(); // eat the 'case'.
248
Chris Lattner476c3ad2006-08-13 22:09:58 +0000249 ExprResult Res = ParseConstantExpression();
250 if (Res.isInvalid) {
251 SkipUntil(tok::colon);
252 return;
253 }
254
255 // GNU case range extension.
256 if (Tok.getKind() == tok::ellipsis) {
257 Diag(Tok, diag::ext_gnu_case_range);
258 ConsumeToken();
259
260 ExprResult RHS = ParseConstantExpression();
261 if (RHS.isInvalid) {
262 SkipUntil(tok::colon);
263 return;
264 }
265 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000266
267 if (Tok.getKind() == tok::colon) {
268 ConsumeToken();
269 } else {
270 Diag(Tok, diag::err_expected_colon_after, "'case'");
271 SkipUntil(tok::colon);
272 }
273}
274
275/// ParseDefaultStatement
276/// labeled-statement:
277/// 'default' ':' statement
278/// Note that this does not parse the 'statement' at the end.
279///
280void Parser::ParseDefaultStatement() {
281 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
282 ConsumeToken(); // eat the 'default'.
283
284 if (Tok.getKind() == tok::colon) {
285 ConsumeToken();
286 } else {
287 Diag(Tok, diag::err_expected_colon_after, "'default'");
288 SkipUntil(tok::colon);
289 }
290}
291
292
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000293/// ParseCompoundStatement - Parse a "{}" block.
294///
295/// compound-statement: [C99 6.8.2]
296/// { block-item-list[opt] }
297/// [GNU] { label-declarations block-item-list } [TODO]
298///
299/// block-item-list:
300/// block-item
301/// block-item-list block-item
302///
303/// block-item:
304/// declaration
305/// [GNU] '__extension__' declaration [TODO]
306/// statement
307/// [OMP] openmp-directive [TODO]
308///
309/// [GNU] label-declarations:
310/// [GNU] label-declaration
311/// [GNU] label-declarations label-declaration
312///
313/// [GNU] label-declaration:
314/// [GNU] '__label__' identifier-list ';'
315///
316/// [OMP] openmp-directive: [TODO]
317/// [OMP] barrier-directive
318/// [OMP] flush-directive
319void Parser::ParseCompoundStatement() {
320 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
321 ConsumeBrace(); // eat the '{'.
322
323 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof)
Chris Lattnerc951dae2006-08-10 04:23:57 +0000324 ParseStatementOrDeclaration(false);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000325
326 // We broke out of the while loop because we found a '}' or EOF.
327 if (Tok.getKind() == tok::r_brace)
328 ConsumeBrace();
329 else
330 Diag(Tok, diag::err_expected_rbrace);
331}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000332
333/// ParseIfStatement
334/// if-statement: [C99 6.8.4.1]
335/// 'if' '(' expression ')' statement
336/// 'if' '(' expression ')' statement 'else' statement
337void Parser::ParseIfStatement() {
338 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
339 ConsumeToken(); // eat the 'if'.
340
341 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000342 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000343 SkipUntil(tok::semi);
344 return;
345 }
346
347 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000348 ParenParseOption ParenExprType = SimpleExpr;
349 ParseParenExpression(ParenExprType);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000350
351 // Read the if condition.
352 ParseStatement();
353
354 // If it has an else, parse it.
355 if (Tok.getKind() == tok::kw_else) {
356 ConsumeToken();
357 ParseStatement();
358 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000359}
360
Chris Lattner9075bd72006-08-10 04:59:57 +0000361/// ParseSwitchStatement
362/// switch-statement:
363/// 'switch' '(' expression ')' statement
364void Parser::ParseSwitchStatement() {
365 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
366 ConsumeToken(); // eat the 'switch'.
367
368 if (Tok.getKind() != tok::l_paren) {
369 Diag(Tok, diag::err_expected_lparen_after, "switch");
370 SkipUntil(tok::semi);
371 return;
372 }
373
374 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000375 ParenParseOption ParenExprType = SimpleExpr;
376 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000377
378 // Read the body statement.
379 ParseStatement();
380}
381
382/// ParseWhileStatement
383/// while-statement: [C99 6.8.5.1]
384/// 'while' '(' expression ')' statement
385void Parser::ParseWhileStatement() {
386 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
387 ConsumeToken(); // eat the 'while'.
388
389 if (Tok.getKind() != tok::l_paren) {
390 Diag(Tok, diag::err_expected_lparen_after, "while");
391 SkipUntil(tok::semi);
392 return;
393 }
394
395 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000396 ParenParseOption ParenExprType = SimpleExpr;
397 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000398
399 // Read the body statement.
400 ParseStatement();
401}
402
403/// ParseDoStatement
404/// do-statement: [C99 6.8.5.2]
405/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000406/// Note: this lets the caller parse the end ';'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000407void Parser::ParseDoStatement() {
408 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
409 SourceLocation DoLoc = Tok.getLocation();
410 ConsumeToken(); // eat the 'do'.
411
412 // Read the body statement.
413 ParseStatement();
414
415 if (Tok.getKind() != tok::kw_while) {
416 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000417 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000418 SkipUntil(tok::semi);
419 return;
420 }
421 ConsumeToken();
422
423 if (Tok.getKind() != tok::l_paren) {
424 Diag(Tok, diag::err_expected_lparen_after, "do/while");
425 SkipUntil(tok::semi);
426 return;
427 }
428
429 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000430 ParenParseOption ParenExprType = SimpleExpr;
431 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000432}
433
434/// ParseForStatement
435/// for-statement: [C99 6.8.5.3]
436/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
437/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
438void Parser::ParseForStatement() {
439 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
440 SourceLocation ForLoc = Tok.getLocation();
441 ConsumeToken(); // eat the 'for'.
442
443 if (Tok.getKind() != tok::l_paren) {
444 Diag(Tok, diag::err_expected_lparen_after, "for");
445 SkipUntil(tok::semi);
446 return;
447 }
448
449 SourceLocation LParenLoc = Tok.getLocation();
450 ConsumeParen();
451
Chris Lattner89c50c62006-08-11 06:41:18 +0000452 ExprResult Value;
453
Chris Lattner9075bd72006-08-10 04:59:57 +0000454 // Parse the first part of the for specifier.
455 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000456 // no first part, eat the ';'.
457 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000458 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000459 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000460 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
461 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner53361ac2006-08-10 05:19:57 +0000462 ParseDeclaration(Declarator::ForContext);
Chris Lattner9075bd72006-08-10 04:59:57 +0000463 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000464 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000465
Chris Lattner53361ac2006-08-10 05:19:57 +0000466 if (Tok.getKind() == tok::semi) {
467 ConsumeToken();
468 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000469 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000470 SkipUntil(tok::semi);
471 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000472 }
473
474 // Parse the second part of the for specifier.
475 if (Tok.getKind() == tok::semi) { // for (...;;
476 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000477 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000478 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000479 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000480 }
481
482 if (Tok.getKind() == tok::semi) {
483 ConsumeToken();
484 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000485 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000486 SkipUntil(tok::semi);
487 }
488
489 // Parse the third part of the for specifier.
490 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
491 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000492 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000493 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000494 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000495 }
496
Chris Lattner4564bc12006-08-10 23:14:52 +0000497 // Match the ')'.
498 MatchRHSPunctuation(tok::r_paren, LParenLoc, "(", diag::err_expected_rparen);
Chris Lattner9075bd72006-08-10 04:59:57 +0000499
500 // Read the body statement.
501 ParseStatement();
502}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000503
Chris Lattner503fadc2006-08-10 05:45:44 +0000504/// ParseGotoStatement
505/// jump-statement:
506/// 'goto' identifier ';'
507/// [GNU] 'goto' '*' expression ';'
508///
509/// Note: this lets the caller parse the end ';'.
510///
511void Parser::ParseGotoStatement() {
512 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
513 ConsumeToken(); // eat the 'goto'.
514
515 if (Tok.getKind() == tok::identifier) {
516 ConsumeToken();
517 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
518 // GNU indirect goto extension.
519 Diag(Tok, diag::ext_gnu_indirect_goto);
520 ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000521 ExprResult R = ParseExpression();
522 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
523 SkipUntil(tok::semi, false, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000524 }
525}
526
527/// ParseReturnStatement
528/// jump-statement:
529/// 'return' expression[opt] ';'
530void Parser::ParseReturnStatement() {
531 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
532 ConsumeToken(); // eat the 'return'.
533
Chris Lattnera0927ce2006-08-12 16:59:03 +0000534 if (Tok.getKind() != tok::semi) {
535 ExprResult R = ParseExpression();
536 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
537 SkipUntil(tok::semi, false, true);
538 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000539}