blob: 909af64b644ae01b7d6f5e0cd2e55e4a38faec92 [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
188 ConsumeToken(); // eat the identifier.
189
190 // identifier ':' statement
191 if (Tok.getKind() == tok::colon) {
192 ConsumeToken();
193 ParseStatement();
194 return;
195 }
196
197 // declaration
198 if (!OnlyStatement && 0/*Is typedef name!*/) {
199 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000200 DeclSpec DS;
201
202 // FIXME: Add the typedef name to the start of the decl-specs.
203 // ParseDeclarationSpecifiers will continue from there.
204 ParseDeclarationSpecifiers(DS);
205
206 // Parse all the declarators.
207 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
208 ParseDeclarator(DeclaratorInfo);
209
210 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
211 return;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000212 }
213
Chris Lattner2f9980e2006-08-10 18:39:24 +0000214 // Otherwise, this is an expression. Seed it with II.
Chris Lattnerf8afb622006-08-10 18:26:31 +0000215
216 assert(0);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000217}
218
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000219/// ParseCaseStatement
220/// labeled-statement:
221/// 'case' constant-expression ':' statement
222///
223/// Note that this does not parse the 'statement' at the end.
224///
225void Parser::ParseCaseStatement() {
226 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
227 ConsumeToken(); // eat the 'case'.
228
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000229 ParseAssignmentExpression(); // Expr without commas.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000230
231 if (Tok.getKind() == tok::colon) {
232 ConsumeToken();
233 } else {
234 Diag(Tok, diag::err_expected_colon_after, "'case'");
235 SkipUntil(tok::colon);
236 }
237}
238
239/// ParseDefaultStatement
240/// labeled-statement:
241/// 'default' ':' statement
242/// Note that this does not parse the 'statement' at the end.
243///
244void Parser::ParseDefaultStatement() {
245 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
246 ConsumeToken(); // eat the 'default'.
247
248 if (Tok.getKind() == tok::colon) {
249 ConsumeToken();
250 } else {
251 Diag(Tok, diag::err_expected_colon_after, "'default'");
252 SkipUntil(tok::colon);
253 }
254}
255
256
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000257/// ParseCompoundStatement - Parse a "{}" block.
258///
259/// compound-statement: [C99 6.8.2]
260/// { block-item-list[opt] }
261/// [GNU] { label-declarations block-item-list } [TODO]
262///
263/// block-item-list:
264/// block-item
265/// block-item-list block-item
266///
267/// block-item:
268/// declaration
269/// [GNU] '__extension__' declaration [TODO]
270/// statement
271/// [OMP] openmp-directive [TODO]
272///
273/// [GNU] label-declarations:
274/// [GNU] label-declaration
275/// [GNU] label-declarations label-declaration
276///
277/// [GNU] label-declaration:
278/// [GNU] '__label__' identifier-list ';'
279///
280/// [OMP] openmp-directive: [TODO]
281/// [OMP] barrier-directive
282/// [OMP] flush-directive
283void Parser::ParseCompoundStatement() {
284 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
285 ConsumeBrace(); // eat the '{'.
286
287 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof)
Chris Lattnerc951dae2006-08-10 04:23:57 +0000288 ParseStatementOrDeclaration(false);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000289
290 // We broke out of the while loop because we found a '}' or EOF.
291 if (Tok.getKind() == tok::r_brace)
292 ConsumeBrace();
293 else
294 Diag(Tok, diag::err_expected_rbrace);
295}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000296
297/// ParseIfStatement
298/// if-statement: [C99 6.8.4.1]
299/// 'if' '(' expression ')' statement
300/// 'if' '(' expression ')' statement 'else' statement
301void Parser::ParseIfStatement() {
302 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
303 ConsumeToken(); // eat the 'if'.
304
305 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000306 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000307 SkipUntil(tok::semi);
308 return;
309 }
310
311 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000312 ParenParseOption ParenExprType = SimpleExpr;
313 ParseParenExpression(ParenExprType);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000314
315 // Read the if condition.
316 ParseStatement();
317
318 // If it has an else, parse it.
319 if (Tok.getKind() == tok::kw_else) {
320 ConsumeToken();
321 ParseStatement();
322 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000323}
324
Chris Lattner9075bd72006-08-10 04:59:57 +0000325/// ParseSwitchStatement
326/// switch-statement:
327/// 'switch' '(' expression ')' statement
328void Parser::ParseSwitchStatement() {
329 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
330 ConsumeToken(); // eat the 'switch'.
331
332 if (Tok.getKind() != tok::l_paren) {
333 Diag(Tok, diag::err_expected_lparen_after, "switch");
334 SkipUntil(tok::semi);
335 return;
336 }
337
338 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000339 ParenParseOption ParenExprType = SimpleExpr;
340 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000341
342 // Read the body statement.
343 ParseStatement();
344}
345
346/// ParseWhileStatement
347/// while-statement: [C99 6.8.5.1]
348/// 'while' '(' expression ')' statement
349void Parser::ParseWhileStatement() {
350 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
351 ConsumeToken(); // eat the 'while'.
352
353 if (Tok.getKind() != tok::l_paren) {
354 Diag(Tok, diag::err_expected_lparen_after, "while");
355 SkipUntil(tok::semi);
356 return;
357 }
358
359 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000360 ParenParseOption ParenExprType = SimpleExpr;
361 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000362
363 // Read the body statement.
364 ParseStatement();
365}
366
367/// ParseDoStatement
368/// do-statement: [C99 6.8.5.2]
369/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000370/// Note: this lets the caller parse the end ';'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000371void Parser::ParseDoStatement() {
372 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
373 SourceLocation DoLoc = Tok.getLocation();
374 ConsumeToken(); // eat the 'do'.
375
376 // Read the body statement.
377 ParseStatement();
378
379 if (Tok.getKind() != tok::kw_while) {
380 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000381 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000382 SkipUntil(tok::semi);
383 return;
384 }
385 ConsumeToken();
386
387 if (Tok.getKind() != tok::l_paren) {
388 Diag(Tok, diag::err_expected_lparen_after, "do/while");
389 SkipUntil(tok::semi);
390 return;
391 }
392
393 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000394 ParenParseOption ParenExprType = SimpleExpr;
395 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000396}
397
398/// ParseForStatement
399/// for-statement: [C99 6.8.5.3]
400/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
401/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
402void Parser::ParseForStatement() {
403 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
404 SourceLocation ForLoc = Tok.getLocation();
405 ConsumeToken(); // eat the 'for'.
406
407 if (Tok.getKind() != tok::l_paren) {
408 Diag(Tok, diag::err_expected_lparen_after, "for");
409 SkipUntil(tok::semi);
410 return;
411 }
412
413 SourceLocation LParenLoc = Tok.getLocation();
414 ConsumeParen();
415
Chris Lattner89c50c62006-08-11 06:41:18 +0000416 ExprResult Value;
417
Chris Lattner9075bd72006-08-10 04:59:57 +0000418 // Parse the first part of the for specifier.
419 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000420 // no first part, eat the ';'.
421 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000422 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000423 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000424 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
425 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner53361ac2006-08-10 05:19:57 +0000426 ParseDeclaration(Declarator::ForContext);
Chris Lattner9075bd72006-08-10 04:59:57 +0000427 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000428 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000429
Chris Lattner53361ac2006-08-10 05:19:57 +0000430 if (Tok.getKind() == tok::semi) {
431 ConsumeToken();
432 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000433 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000434 SkipUntil(tok::semi);
435 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000436 }
437
438 // Parse the second part of the for specifier.
439 if (Tok.getKind() == tok::semi) { // for (...;;
440 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000441 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000442 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000443 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000444 }
445
446 if (Tok.getKind() == tok::semi) {
447 ConsumeToken();
448 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000449 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000450 SkipUntil(tok::semi);
451 }
452
453 // Parse the third part of the for specifier.
454 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
455 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000456 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000457 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000458 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000459 }
460
Chris Lattner4564bc12006-08-10 23:14:52 +0000461 // Match the ')'.
462 MatchRHSPunctuation(tok::r_paren, LParenLoc, "(", diag::err_expected_rparen);
Chris Lattner9075bd72006-08-10 04:59:57 +0000463
464 // Read the body statement.
465 ParseStatement();
466}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000467
Chris Lattner503fadc2006-08-10 05:45:44 +0000468/// ParseGotoStatement
469/// jump-statement:
470/// 'goto' identifier ';'
471/// [GNU] 'goto' '*' expression ';'
472///
473/// Note: this lets the caller parse the end ';'.
474///
475void Parser::ParseGotoStatement() {
476 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
477 ConsumeToken(); // eat the 'goto'.
478
479 if (Tok.getKind() == tok::identifier) {
480 ConsumeToken();
481 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
482 // GNU indirect goto extension.
483 Diag(Tok, diag::ext_gnu_indirect_goto);
484 ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000485 ExprResult R = ParseExpression();
486 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
487 SkipUntil(tok::semi, false, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000488 }
489}
490
491/// ParseReturnStatement
492/// jump-statement:
493/// 'return' expression[opt] ';'
494void Parser::ParseReturnStatement() {
495 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
496 ConsumeToken(); // eat the 'return'.
497
Chris Lattnera0927ce2006-08-12 16:59:03 +0000498 if (Tok.getKind() != tok::semi) {
499 ExprResult R = ParseExpression();
500 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
501 SkipUntil(tok::semi, false, true);
502 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000503}