blob: acdc0cba4946adcb99a417de7f56169a9f485b24 [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
Chris Lattnere37e2332006-08-15 04:50:22 +0000181/// [GNU] identifier ':' attributes[opt] statement
Chris Lattner6dfd9782006-08-10 18:31:37 +0000182/// declaration (if !OnlyStatement)
183/// expression[opt] ';'
184///
Chris Lattnerf8afb622006-08-10 18:26:31 +0000185void Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000186 IdentifierInfo *II = Tok.getIdentifierInfo();
187 assert(Tok.getKind() == tok::identifier && II && "Not an identifier!");
188
Chris Lattner0c6c0342006-08-12 18:12:45 +0000189 LexerToken IdentTok = Tok; // Save the token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000190 ConsumeToken(); // eat the identifier.
191
192 // identifier ':' statement
193 if (Tok.getKind() == tok::colon) {
194 ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000195
196 // Read label attributes, if present.
197 if (Tok.getKind() == tok::kw___attribute)
198 ParseAttributes();
199
Chris Lattner6dfd9782006-08-10 18:31:37 +0000200 ParseStatement();
201 return;
202 }
203
204 // declaration
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000205 if (!OnlyStatement &&
206 Actions.isTypedefName(*IdentTok.getIdentifierInfo(), CurScope)) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000207 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000208 DeclSpec DS;
209
210 // FIXME: Add the typedef name to the start of the decl-specs.
211 // ParseDeclarationSpecifiers will continue from there.
212 ParseDeclarationSpecifiers(DS);
213
Chris Lattner0e894622006-08-13 19:58:17 +0000214 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
215 // declaration-specifiers init-declarator-list[opt] ';'
216 if (Tok.getKind() == tok::semi) {
217 // TODO: emit error on 'int;' or 'const enum foo;'.
218 // if (!DS.isMissingDeclaratorOk()) Diag(...);
219
220 ConsumeToken();
221 return;
222 }
223
Chris Lattner2f9980e2006-08-10 18:39:24 +0000224 // Parse all the declarators.
225 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
226 ParseDeclarator(DeclaratorInfo);
227
228 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
229 return;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000230 }
231
Chris Lattner0c6c0342006-08-12 18:12:45 +0000232 // Otherwise, this is an expression. Seed it with II and parse it.
233 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
234 if (Res.isInvalid)
235 SkipUntil(tok::semi);
236 else if (Tok.getKind() == tok::semi)
237 ConsumeToken();
238 else {
239 Diag(Tok, diag::err_expected_semi_after, "expression");
240 SkipUntil(tok::semi);
241 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000242}
243
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000244/// ParseCaseStatement
245/// labeled-statement:
246/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000247/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000248///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000249/// Note that this does not parse the 'statement' at the end.
250///
251void Parser::ParseCaseStatement() {
252 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
253 ConsumeToken(); // eat the 'case'.
254
Chris Lattner476c3ad2006-08-13 22:09:58 +0000255 ExprResult Res = ParseConstantExpression();
256 if (Res.isInvalid) {
257 SkipUntil(tok::colon);
258 return;
259 }
260
261 // GNU case range extension.
262 if (Tok.getKind() == tok::ellipsis) {
263 Diag(Tok, diag::ext_gnu_case_range);
264 ConsumeToken();
265
266 ExprResult RHS = ParseConstantExpression();
267 if (RHS.isInvalid) {
268 SkipUntil(tok::colon);
269 return;
270 }
271 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000272
273 if (Tok.getKind() == tok::colon) {
274 ConsumeToken();
275 } else {
276 Diag(Tok, diag::err_expected_colon_after, "'case'");
277 SkipUntil(tok::colon);
278 }
279}
280
281/// ParseDefaultStatement
282/// labeled-statement:
283/// 'default' ':' statement
284/// Note that this does not parse the 'statement' at the end.
285///
286void Parser::ParseDefaultStatement() {
287 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
288 ConsumeToken(); // eat the 'default'.
289
290 if (Tok.getKind() == tok::colon) {
291 ConsumeToken();
292 } else {
293 Diag(Tok, diag::err_expected_colon_after, "'default'");
294 SkipUntil(tok::colon);
295 }
296}
297
298
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000299/// ParseCompoundStatement - Parse a "{}" block.
300///
301/// compound-statement: [C99 6.8.2]
302/// { block-item-list[opt] }
303/// [GNU] { label-declarations block-item-list } [TODO]
304///
305/// block-item-list:
306/// block-item
307/// block-item-list block-item
308///
309/// block-item:
310/// declaration
311/// [GNU] '__extension__' declaration [TODO]
312/// statement
313/// [OMP] openmp-directive [TODO]
314///
315/// [GNU] label-declarations:
316/// [GNU] label-declaration
317/// [GNU] label-declarations label-declaration
318///
319/// [GNU] label-declaration:
320/// [GNU] '__label__' identifier-list ';'
321///
322/// [OMP] openmp-directive: [TODO]
323/// [OMP] barrier-directive
324/// [OMP] flush-directive
325void Parser::ParseCompoundStatement() {
326 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
327 ConsumeBrace(); // eat the '{'.
328
329 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof)
Chris Lattnerc951dae2006-08-10 04:23:57 +0000330 ParseStatementOrDeclaration(false);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000331
332 // We broke out of the while loop because we found a '}' or EOF.
333 if (Tok.getKind() == tok::r_brace)
334 ConsumeBrace();
335 else
336 Diag(Tok, diag::err_expected_rbrace);
337}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000338
339/// ParseIfStatement
340/// if-statement: [C99 6.8.4.1]
341/// 'if' '(' expression ')' statement
342/// 'if' '(' expression ')' statement 'else' statement
343void Parser::ParseIfStatement() {
344 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
345 ConsumeToken(); // eat the 'if'.
346
347 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000348 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000349 SkipUntil(tok::semi);
350 return;
351 }
352
353 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000354 ParenParseOption ParenExprType = SimpleExpr;
355 ParseParenExpression(ParenExprType);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000356
357 // Read the if condition.
358 ParseStatement();
359
360 // If it has an else, parse it.
361 if (Tok.getKind() == tok::kw_else) {
362 ConsumeToken();
363 ParseStatement();
364 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000365}
366
Chris Lattner9075bd72006-08-10 04:59:57 +0000367/// ParseSwitchStatement
368/// switch-statement:
369/// 'switch' '(' expression ')' statement
370void Parser::ParseSwitchStatement() {
371 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
372 ConsumeToken(); // eat the 'switch'.
373
374 if (Tok.getKind() != tok::l_paren) {
375 Diag(Tok, diag::err_expected_lparen_after, "switch");
376 SkipUntil(tok::semi);
377 return;
378 }
379
380 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000381 ParenParseOption ParenExprType = SimpleExpr;
382 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000383
384 // Read the body statement.
385 ParseStatement();
386}
387
388/// ParseWhileStatement
389/// while-statement: [C99 6.8.5.1]
390/// 'while' '(' expression ')' statement
391void Parser::ParseWhileStatement() {
392 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
393 ConsumeToken(); // eat the 'while'.
394
395 if (Tok.getKind() != tok::l_paren) {
396 Diag(Tok, diag::err_expected_lparen_after, "while");
397 SkipUntil(tok::semi);
398 return;
399 }
400
401 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000402 ParenParseOption ParenExprType = SimpleExpr;
403 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000404
405 // Read the body statement.
406 ParseStatement();
407}
408
409/// ParseDoStatement
410/// do-statement: [C99 6.8.5.2]
411/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000412/// Note: this lets the caller parse the end ';'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000413void Parser::ParseDoStatement() {
414 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
415 SourceLocation DoLoc = Tok.getLocation();
416 ConsumeToken(); // eat the 'do'.
417
418 // Read the body statement.
419 ParseStatement();
420
421 if (Tok.getKind() != tok::kw_while) {
422 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000423 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000424 SkipUntil(tok::semi);
425 return;
426 }
427 ConsumeToken();
428
429 if (Tok.getKind() != tok::l_paren) {
430 Diag(Tok, diag::err_expected_lparen_after, "do/while");
431 SkipUntil(tok::semi);
432 return;
433 }
434
435 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000436 ParenParseOption ParenExprType = SimpleExpr;
437 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000438}
439
440/// ParseForStatement
441/// for-statement: [C99 6.8.5.3]
442/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
443/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
444void Parser::ParseForStatement() {
445 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
446 SourceLocation ForLoc = Tok.getLocation();
447 ConsumeToken(); // eat the 'for'.
448
449 if (Tok.getKind() != tok::l_paren) {
450 Diag(Tok, diag::err_expected_lparen_after, "for");
451 SkipUntil(tok::semi);
452 return;
453 }
454
455 SourceLocation LParenLoc = Tok.getLocation();
456 ConsumeParen();
457
Chris Lattner89c50c62006-08-11 06:41:18 +0000458 ExprResult Value;
459
Chris Lattner9075bd72006-08-10 04:59:57 +0000460 // Parse the first part of the for specifier.
461 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000462 // no first part, eat the ';'.
463 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000464 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000465 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000466 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
467 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner53361ac2006-08-10 05:19:57 +0000468 ParseDeclaration(Declarator::ForContext);
Chris Lattner9075bd72006-08-10 04:59:57 +0000469 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000470 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000471
Chris Lattner53361ac2006-08-10 05:19:57 +0000472 if (Tok.getKind() == tok::semi) {
473 ConsumeToken();
474 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000475 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000476 SkipUntil(tok::semi);
477 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000478 }
479
480 // Parse the second part of the for specifier.
481 if (Tok.getKind() == tok::semi) { // for (...;;
482 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000483 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000484 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000485 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000486 }
487
488 if (Tok.getKind() == tok::semi) {
489 ConsumeToken();
490 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000491 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000492 SkipUntil(tok::semi);
493 }
494
495 // Parse the third part of the for specifier.
496 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
497 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000498 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000499 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000500 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000501 }
502
Chris Lattner4564bc12006-08-10 23:14:52 +0000503 // Match the ')'.
504 MatchRHSPunctuation(tok::r_paren, LParenLoc, "(", diag::err_expected_rparen);
Chris Lattner9075bd72006-08-10 04:59:57 +0000505
506 // Read the body statement.
507 ParseStatement();
508}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000509
Chris Lattner503fadc2006-08-10 05:45:44 +0000510/// ParseGotoStatement
511/// jump-statement:
512/// 'goto' identifier ';'
513/// [GNU] 'goto' '*' expression ';'
514///
515/// Note: this lets the caller parse the end ';'.
516///
517void Parser::ParseGotoStatement() {
518 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
519 ConsumeToken(); // eat the 'goto'.
520
521 if (Tok.getKind() == tok::identifier) {
522 ConsumeToken();
523 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
524 // GNU indirect goto extension.
525 Diag(Tok, diag::ext_gnu_indirect_goto);
526 ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000527 ExprResult R = ParseExpression();
528 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
529 SkipUntil(tok::semi, false, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000530 }
531}
532
533/// ParseReturnStatement
534/// jump-statement:
535/// 'return' expression[opt] ';'
536void Parser::ParseReturnStatement() {
537 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
538 ConsumeToken(); // eat the 'return'.
539
Chris Lattnera0927ce2006-08-12 16:59:03 +0000540 if (Tok.getKind() != tok::semi) {
541 ExprResult R = ParseExpression();
542 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
543 SkipUntil(tok::semi, false, true);
544 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000545}