blob: cae056aa8eb2c9760ed29d2410049403b99f3227 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseStmt.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"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
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
41/// [OMP] openmp-construct [TODO]
42///
43/// labeled-statement:
44/// identifier ':' statement
45/// 'case' constant-expression ':' statement
46/// 'default' ':' statement
47///
48/// selection-statement:
49/// if-statement
50/// switch-statement
51///
52/// iteration-statement:
53/// while-statement
54/// do-statement
55/// for-statement
56///
57/// expression-statement:
58/// expression[opt] ';'
59///
60/// jump-statement:
61/// 'goto' identifier ';'
62/// 'continue' ';'
63/// 'break' ';'
64/// 'return' expression[opt] ';'
65/// [GNU] 'goto' '*' expression ';'
66///
67/// [OBC] objc-throw-statement: [TODO]
68/// [OBC] '@' 'throw' expression ';' [TODO]
69/// [OBC] '@' 'throw' ';' [TODO]
70///
71Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
72 const char *SemiError = 0;
73 Parser::StmtResult Res;
74
75 // 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.
78 switch (Tok.getKind()) {
79 case tok::identifier: // C99 6.8.1: labeled-statement
80 // identifier ':' statement
81 // declaration (if !OnlyStatement)
82 // expression[opt] ';'
83 return ParseIdentifierStatement(OnlyStatement);
84
85 default:
86 if (!OnlyStatement && isDeclarationSpecifier()) {
87 // TODO: warn/disable if declaration is in the middle of a block and !C99.
88 return Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext));
89 } else if (Tok.getKind() == tok::r_brace) {
90 Diag(Tok, diag::err_expected_statement);
91 return true;
92 } else {
93 // expression[opt] ';'
94 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 return true;
101 }
102 // Otherwise, eat the semicolon.
103 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
104 return Actions.ParseExprStmt(Res.Val);
105 }
106
107 case tok::kw_case: // C99 6.8.1: labeled-statement
108 return ParseCaseStatement();
109 case tok::kw_default: // C99 6.8.1: labeled-statement
110 return ParseDefaultStatement();
111
112 case tok::l_brace: // C99 6.8.2: compound-statement
113 return ParseCompoundStatement();
114 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
115 return Actions.ParseNullStmt(ConsumeToken());
116
117 case tok::kw_if: // C99 6.8.4.1: if-statement
118 return ParseIfStatement();
119 case tok::kw_switch: // C99 6.8.4.2: switch-statement
120 return ParseSwitchStatement();
121
122 case tok::kw_while: // C99 6.8.5.1: while-statement
123 return ParseWhileStatement();
124 case tok::kw_do: // C99 6.8.5.2: do-statement
125 Res = ParseDoStatement();
126 SemiError = "do/while loop";
127 break;
128 case tok::kw_for: // C99 6.8.5.3: for-statement
129 return ParseForStatement();
130
131 case tok::kw_goto: // C99 6.8.6.1: goto-statement
132 Res = ParseGotoStatement();
133 SemiError = "goto statement";
134 break;
135 case tok::kw_continue: // C99 6.8.6.2: continue-statement
136 Res = ParseContinueStatement();
137 SemiError = "continue statement";
138 break;
139 case tok::kw_break: // C99 6.8.6.3: break-statement
140 Res = ParseBreakStatement();
141 SemiError = "break statement";
142 break;
143 case tok::kw_return: // C99 6.8.6.4: return-statement
144 Res = ParseReturnStatement();
145 SemiError = "return statement";
146 break;
147
148 case tok::kw_asm:
149 Res = ParseAsmStatement();
150 SemiError = "asm statement";
151 break;
152 }
153
154 // If we reached this code, the statement must end in a semicolon.
155 if (Tok.getKind() == tok::semi) {
156 ConsumeToken();
157 } else {
158 Diag(Tok, diag::err_expected_semi_after, SemiError);
159 SkipUntil(tok::semi);
160 }
161 return Res;
162}
163
164/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
165/// have a bit of a quandry here. Reading the identifier is necessary to see if
166/// there is a ':' after it. If there is, this is a label, regardless of what
167/// else the identifier can mean. If not, this is either part of a declaration
168/// (if the identifier is a type-name) or part of an expression.
169///
170/// labeled-statement:
171/// identifier ':' statement
172/// [GNU] identifier ':' attributes[opt] statement
173/// declaration (if !OnlyStatement)
174/// expression[opt] ';'
175///
176Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
177 assert(Tok.getKind() == tok::identifier && Tok.getIdentifierInfo() &&
178 "Not an identifier!");
179
180 Token IdentTok = Tok; // Save the whole token.
181 ConsumeToken(); // eat the identifier.
182
183 // identifier ':' statement
184 if (Tok.getKind() == tok::colon) {
185 SourceLocation ColonLoc = ConsumeToken();
186
187 // Read label attributes, if present.
188 DeclTy *AttrList = 0;
189 if (Tok.getKind() == tok::kw___attribute)
190 // TODO: save these somewhere.
191 AttrList = ParseAttributes();
192
193 StmtResult SubStmt = ParseStatement();
194
195 // Broken substmt shouldn't prevent the label from being added to the AST.
196 if (SubStmt.isInvalid)
197 SubStmt = Actions.ParseNullStmt(ColonLoc);
198
199 return Actions.ParseLabelStmt(IdentTok.getLocation(),
200 IdentTok.getIdentifierInfo(),
201 ColonLoc, SubStmt.Val);
202 }
203
204 // Check to see if this is a declaration.
205 void *TypeRep;
206 if (!OnlyStatement &&
207 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
208 // Handle this. Warn/disable if in middle of block and !C99.
209 DeclSpec DS;
210
211 // Add the typedef name to the start of the decl-specs.
212 const char *PrevSpec = 0;
213 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
214 IdentTok.getLocation(), PrevSpec,
215 TypeRep);
216 assert(!isInvalid && "First declspec can't be invalid!");
217
218 // ParseDeclarationSpecifiers will continue from there.
219 ParseDeclarationSpecifiers(DS);
220
221 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
222 // declaration-specifiers init-declarator-list[opt] ';'
223 if (Tok.getKind() == tok::semi) {
224 // TODO: emit error on 'int;' or 'const enum foo;'.
225 // if (!DS.isMissingDeclaratorOk()) Diag(...);
226
227 ConsumeToken();
228 // FIXME: Return this as a type decl.
229 return 0;
230 }
231
232 // Parse all the declarators.
233 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
234 ParseDeclarator(DeclaratorInfo);
235
236 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
237 return Decl ? Actions.ParseDeclStmt(Decl) : 0;
238 }
239
240 // Otherwise, this is an expression. Seed it with II and parse it.
241 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
242 if (Res.isInvalid) {
243 SkipUntil(tok::semi);
244 return true;
245 } else if (Tok.getKind() != tok::semi) {
246 Diag(Tok, diag::err_expected_semi_after, "expression");
247 SkipUntil(tok::semi);
248 return true;
249 } else {
250 ConsumeToken();
251 // Convert expr to a stmt.
252 return Actions.ParseExprStmt(Res.Val);
253 }
254}
255
256/// ParseCaseStatement
257/// labeled-statement:
258/// 'case' constant-expression ':' statement
259/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
260///
261/// Note that this does not parse the 'statement' at the end.
262///
263Parser::StmtResult Parser::ParseCaseStatement() {
264 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
265 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
266
267 ExprResult LHS = ParseConstantExpression();
268 if (LHS.isInvalid) {
269 SkipUntil(tok::colon);
270 return true;
271 }
272
273 // GNU case range extension.
274 SourceLocation DotDotDotLoc;
275 ExprTy *RHSVal = 0;
276 if (Tok.getKind() == tok::ellipsis) {
277 Diag(Tok, diag::ext_gnu_case_range);
278 DotDotDotLoc = ConsumeToken();
279
280 ExprResult RHS = ParseConstantExpression();
281 if (RHS.isInvalid) {
282 SkipUntil(tok::colon);
283 return true;
284 }
285 RHSVal = RHS.Val;
286 }
287
288 if (Tok.getKind() != tok::colon) {
289 Diag(Tok, diag::err_expected_colon_after, "'case'");
290 SkipUntil(tok::colon);
291 return true;
292 }
293
294 SourceLocation ColonLoc = ConsumeToken();
295
296 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
297 if (Tok.getKind() == tok::r_brace) {
298 Diag(Tok, diag::err_label_end_of_compound_statement);
299 return true;
300 }
301
302 StmtResult SubStmt = ParseStatement();
303
304 // Broken substmt shouldn't prevent the case from being added to the AST.
305 if (SubStmt.isInvalid)
306 SubStmt = Actions.ParseNullStmt(ColonLoc);
307
308 // TODO: look up enclosing switch stmt.
309 return Actions.ParseCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
310 SubStmt.Val);
311}
312
313/// ParseDefaultStatement
314/// labeled-statement:
315/// 'default' ':' statement
316/// Note that this does not parse the 'statement' at the end.
317///
318Parser::StmtResult Parser::ParseDefaultStatement() {
319 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
320 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
321
322 if (Tok.getKind() != tok::colon) {
323 Diag(Tok, diag::err_expected_colon_after, "'default'");
324 SkipUntil(tok::colon);
325 return true;
326 }
327
328 SourceLocation ColonLoc = ConsumeToken();
329
330 // Diagnose the common error "switch (X) {... default: }", which is not valid.
331 if (Tok.getKind() == tok::r_brace) {
332 Diag(Tok, diag::err_label_end_of_compound_statement);
333 return true;
334 }
335
336 StmtResult SubStmt = ParseStatement();
337 if (SubStmt.isInvalid)
338 return true;
339
340 // TODO: look up enclosing switch stmt.
341 return Actions.ParseDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
342}
343
344
345/// ParseCompoundStatement - Parse a "{}" block.
346///
347/// compound-statement: [C99 6.8.2]
348/// { block-item-list[opt] }
349/// [GNU] { label-declarations block-item-list } [TODO]
350///
351/// block-item-list:
352/// block-item
353/// block-item-list block-item
354///
355/// block-item:
356/// declaration
Chris Lattner81417722007-08-27 01:01:57 +0000357/// [GNU] '__extension__' declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000358/// statement
359/// [OMP] openmp-directive [TODO]
360///
361/// [GNU] label-declarations:
362/// [GNU] label-declaration
363/// [GNU] label-declarations label-declaration
364///
365/// [GNU] label-declaration:
366/// [GNU] '__label__' identifier-list ';'
367///
368/// [OMP] openmp-directive: [TODO]
369/// [OMP] barrier-directive
370/// [OMP] flush-directive
371///
372Parser::StmtResult Parser::ParseCompoundStatement() {
373 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
374
Chris Lattnera7549902007-08-26 06:24:45 +0000375 // Enter a scope to hold everything within the compound stmt. Compound
376 // statements can always hold declarations.
377 EnterScope(Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000378
379 // Parse the statements in the body.
380 StmtResult Body = ParseCompoundStatementBody();
381
382 ExitScope();
383 return Body;
384}
385
386
387/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
388/// ParseCompoundStmt action. This expects the '{' to be the current token, and
389/// consume the '}' at the end of the block. It does not manipulate the scope
390/// stack.
391Parser::StmtResult Parser::ParseCompoundStatementBody() {
392 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
393
394 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner81417722007-08-27 01:01:57 +0000395 // only allowed at the start of a compound stmt regardless of the language.
Chris Lattner4b009652007-07-25 00:24:17 +0000396
397 llvm::SmallVector<StmtTy*, 32> Stmts;
398 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) {
Chris Lattner81417722007-08-27 01:01:57 +0000399 StmtResult R;
400 if (Tok.getKind() != tok::kw___extension__) {
401 R = ParseStatementOrDeclaration(false);
402 } else {
403 // __extension__ can start declarations and it can also be a unary
404 // operator for expressions. Consume multiple __extension__ markers here
405 // until we can determine which is which.
406 SourceLocation ExtLoc = ConsumeToken();
407 while (Tok.getKind() == tok::kw___extension__)
408 ConsumeToken();
409
410 // If this is the start of a declaration, parse it as such.
411 if (isDeclarationSpecifier()) {
412 // FIXME: Save the __extension__ on the decl as a node somehow.
413 // FIXME: disable extwarns.
414 R = Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext));
415 } else {
416 // Otherwise this was a unary __extension__ marker. Parse the
417 // subexpression and add the __extension__ unary op.
418 // FIXME: disable extwarns.
419 ExprResult Res = ParseCastExpression(false);
420 if (Res.isInvalid) {
421 SkipUntil(tok::semi);
422 continue;
423 }
424
425 // Add the __extension__ node to the AST.
426 Res = Actions.ParseUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
427 if (Res.isInvalid)
428 continue;
429
430 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
431 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
432 R = Actions.ParseExprStmt(Res.Val);
433 }
434 }
435
Chris Lattner4b009652007-07-25 00:24:17 +0000436 if (!R.isInvalid && R.Val)
437 Stmts.push_back(R.Val);
438 }
439
440 // We broke out of the while loop because we found a '}' or EOF.
441 if (Tok.getKind() != tok::r_brace) {
442 Diag(Tok, diag::err_expected_rbrace);
443 return 0;
444 }
445
446 SourceLocation RBraceLoc = ConsumeBrace();
447 return Actions.ParseCompoundStmt(LBraceLoc, RBraceLoc,
448 &Stmts[0], Stmts.size());
449}
450
451/// ParseIfStatement
452/// if-statement: [C99 6.8.4.1]
453/// 'if' '(' expression ')' statement
454/// 'if' '(' expression ')' statement 'else' statement
455///
456Parser::StmtResult Parser::ParseIfStatement() {
457 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
458 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
459
460 if (Tok.getKind() != tok::l_paren) {
461 Diag(Tok, diag::err_expected_lparen_after, "if");
462 SkipUntil(tok::semi);
463 return true;
464 }
465
Chris Lattnere0cc5082007-08-26 23:08:06 +0000466 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
467 // the case for C90.
468 if (getLang().C99)
469 EnterScope(Scope::DeclScope);
470
Chris Lattner4b009652007-07-25 00:24:17 +0000471 // Parse the condition.
472 ExprResult CondExp = ParseSimpleParenExpression();
473 if (CondExp.isInvalid) {
474 SkipUntil(tok::semi);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000475 if (getLang().C99)
476 ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000477 return true;
478 }
479
Chris Lattnerf446f722007-08-22 05:28:50 +0000480 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000481 // there is no compound stmt. C90 does not have this clause. We only do this
482 // if the body isn't a compound statement to avoid push/pop in common cases.
483 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattnera7549902007-08-26 06:24:45 +0000484 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerd190ac22007-08-22 05:16:28 +0000485
Chris Lattner4b009652007-07-25 00:24:17 +0000486 // Read the if condition.
487 StmtResult CondStmt = ParseStatement();
488
489 // Broken substmt shouldn't prevent the label from being added to the AST.
490 if (CondStmt.isInvalid)
491 CondStmt = Actions.ParseNullStmt(Tok.getLocation());
492
Chris Lattnerd190ac22007-08-22 05:16:28 +0000493 // Pop the 'if' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000494 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000495
496 // If it has an else, parse it.
497 SourceLocation ElseLoc;
498 StmtResult ElseStmt(false);
499 if (Tok.getKind() == tok::kw_else) {
500 ElseLoc = ConsumeToken();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000501
Chris Lattnerf446f722007-08-22 05:28:50 +0000502 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000503 // there is no compound stmt. C90 does not have this clause. We only do
504 // this if the body isn't a compound statement to avoid push/pop in common
505 // cases.
506 NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattnera7549902007-08-26 06:24:45 +0000507 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerd190ac22007-08-22 05:16:28 +0000508
Chris Lattner4b009652007-07-25 00:24:17 +0000509 ElseStmt = ParseStatement();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000510
511 // Pop the 'else' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000512 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000513
514 if (ElseStmt.isInvalid)
515 ElseStmt = Actions.ParseNullStmt(ElseLoc);
516 }
517
Chris Lattnere0cc5082007-08-26 23:08:06 +0000518 if (getLang().C99)
519 ExitScope();
520
Chris Lattner4b009652007-07-25 00:24:17 +0000521 return Actions.ParseIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
522 ElseLoc, ElseStmt.Val);
523}
524
525/// ParseSwitchStatement
526/// switch-statement:
527/// 'switch' '(' expression ')' statement
528Parser::StmtResult Parser::ParseSwitchStatement() {
529 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
530 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
531
532 if (Tok.getKind() != tok::l_paren) {
533 Diag(Tok, diag::err_expected_lparen_after, "switch");
534 SkipUntil(tok::semi);
535 return true;
536 }
Chris Lattnere0cc5082007-08-26 23:08:06 +0000537
538 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
539 // not the case for C90. Start the switch scope.
540 if (getLang().C99)
541 EnterScope(Scope::BreakScope|Scope::DeclScope);
542 else
543 EnterScope(Scope::BreakScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000544
545 // Parse the condition.
546 ExprResult Cond = ParseSimpleParenExpression();
547
548 if (Cond.isInvalid) {
549 ExitScope();
550 return true;
551 }
552
553 StmtResult Switch = Actions.StartSwitchStmt(Cond.Val);
554
Chris Lattnerf446f722007-08-22 05:28:50 +0000555 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000556 // there is no compound stmt. C90 does not have this clause. We only do this
557 // if the body isn't a compound statement to avoid push/pop in common cases.
558 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattnera7549902007-08-26 06:24:45 +0000559 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000560
Chris Lattner4b009652007-07-25 00:24:17 +0000561 // Read the body statement.
562 StmtResult Body = ParseStatement();
563
Chris Lattnerf446f722007-08-22 05:28:50 +0000564 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000565 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000566
Chris Lattner4b009652007-07-25 00:24:17 +0000567 if (Body.isInvalid) {
568 Body = Actions.ParseNullStmt(Tok.getLocation());
569 // FIXME: Remove the case statement list from the Switch statement.
570 }
571
572 ExitScope();
573
574 return Actions.FinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
575}
576
577/// ParseWhileStatement
578/// while-statement: [C99 6.8.5.1]
579/// 'while' '(' expression ')' statement
580Parser::StmtResult Parser::ParseWhileStatement() {
581 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
582 SourceLocation WhileLoc = Tok.getLocation();
583 ConsumeToken(); // eat the 'while'.
584
585 if (Tok.getKind() != tok::l_paren) {
586 Diag(Tok, diag::err_expected_lparen_after, "while");
587 SkipUntil(tok::semi);
588 return true;
589 }
590
Chris Lattnere0cc5082007-08-26 23:08:06 +0000591 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
592 // the case for C90. Start the loop scope.
593 if (getLang().C99)
594 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
595 else
596 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000597
598 // Parse the condition.
599 ExprResult Cond = ParseSimpleParenExpression();
600
Chris Lattnerf446f722007-08-22 05:28:50 +0000601 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000602 // there is no compound stmt. C90 does not have this clause. We only do this
603 // if the body isn't a compound statement to avoid push/pop in common cases.
604 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattnera7549902007-08-26 06:24:45 +0000605 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000606
Chris Lattner4b009652007-07-25 00:24:17 +0000607 // Read the body statement.
608 StmtResult Body = ParseStatement();
609
Chris Lattnerf446f722007-08-22 05:28:50 +0000610 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000611 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000612
Chris Lattner4b009652007-07-25 00:24:17 +0000613 ExitScope();
614
615 if (Cond.isInvalid || Body.isInvalid) return true;
616
617 return Actions.ParseWhileStmt(WhileLoc, Cond.Val, Body.Val);
618}
619
620/// ParseDoStatement
621/// do-statement: [C99 6.8.5.2]
622/// 'do' statement 'while' '(' expression ')' ';'
623/// Note: this lets the caller parse the end ';'.
624Parser::StmtResult Parser::ParseDoStatement() {
625 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
626 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
627
Chris Lattnere0cc5082007-08-26 23:08:06 +0000628 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
629 // the case for C90. Start the loop scope.
630 if (getLang().C99)
631 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
632 else
633 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000634
Chris Lattnerf446f722007-08-22 05:28:50 +0000635 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000636 // there is no compound stmt. C90 does not have this clause. We only do this
637 // if the body isn't a compound statement to avoid push/pop in common cases.
638 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattnera7549902007-08-26 06:24:45 +0000639 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000640
Chris Lattner4b009652007-07-25 00:24:17 +0000641 // Read the body statement.
642 StmtResult Body = ParseStatement();
643
Chris Lattnerf446f722007-08-22 05:28:50 +0000644 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000645 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000646
Chris Lattner4b009652007-07-25 00:24:17 +0000647 if (Tok.getKind() != tok::kw_while) {
648 ExitScope();
649 Diag(Tok, diag::err_expected_while);
650 Diag(DoLoc, diag::err_matching, "do");
651 SkipUntil(tok::semi);
652 return true;
653 }
654 SourceLocation WhileLoc = ConsumeToken();
655
656 if (Tok.getKind() != tok::l_paren) {
657 ExitScope();
658 Diag(Tok, diag::err_expected_lparen_after, "do/while");
659 SkipUntil(tok::semi);
660 return true;
661 }
662
663 // Parse the condition.
664 ExprResult Cond = ParseSimpleParenExpression();
665
666 ExitScope();
667
668 if (Cond.isInvalid || Body.isInvalid) return true;
669
670 return Actions.ParseDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
671}
672
673/// ParseForStatement
674/// for-statement: [C99 6.8.5.3]
675/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
676/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
677Parser::StmtResult Parser::ParseForStatement() {
678 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
679 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
680
681 if (Tok.getKind() != tok::l_paren) {
682 Diag(Tok, diag::err_expected_lparen_after, "for");
683 SkipUntil(tok::semi);
684 return true;
685 }
686
Chris Lattnere0cc5082007-08-26 23:08:06 +0000687 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
688 // the case for C90. Start the loop scope.
689 if (getLang().C99)
690 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
691 else
692 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000693
694 SourceLocation LParenLoc = ConsumeParen();
695 ExprResult Value;
696
697 StmtTy *FirstPart = 0;
698 ExprTy *SecondPart = 0;
699 StmtTy *ThirdPart = 0;
700
701 // Parse the first part of the for specifier.
702 if (Tok.getKind() == tok::semi) { // for (;
703 // no first part, eat the ';'.
704 ConsumeToken();
705 } else if (isDeclarationSpecifier()) { // for (int X = 4;
706 // Parse declaration, which eats the ';'.
707 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
708 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
709 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
710 StmtResult stmtResult = Actions.ParseDeclStmt(aBlockVarDecl);
711 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
712 } else {
713 Value = ParseExpression();
714
715 // Turn the expression into a stmt.
716 if (!Value.isInvalid) {
717 StmtResult R = Actions.ParseExprStmt(Value.Val);
718 if (!R.isInvalid)
719 FirstPart = R.Val;
720 }
721
722 if (Tok.getKind() == tok::semi) {
723 ConsumeToken();
724 } else {
725 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
726 SkipUntil(tok::semi);
727 }
728 }
729
730 // Parse the second part of the for specifier.
731 if (Tok.getKind() == tok::semi) { // for (...;;
732 // no second part.
733 Value = ExprResult();
734 } else {
735 Value = ParseExpression();
736 if (!Value.isInvalid)
737 SecondPart = Value.Val;
738 }
739
740 if (Tok.getKind() == tok::semi) {
741 ConsumeToken();
742 } else {
743 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
744 SkipUntil(tok::semi);
745 }
746
747 // Parse the third part of the for specifier.
748 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
749 // no third part.
750 Value = ExprResult();
751 } else {
752 Value = ParseExpression();
753 if (!Value.isInvalid) {
754 // Turn the expression into a stmt.
755 StmtResult R = Actions.ParseExprStmt(Value.Val);
756 if (!R.isInvalid)
757 ThirdPart = R.Val;
758 }
759 }
760
761 // Match the ')'.
762 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
763
Chris Lattnerf446f722007-08-22 05:28:50 +0000764 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000765 // there is no compound stmt. C90 does not have this clause. We only do this
766 // if the body isn't a compound statement to avoid push/pop in common cases.
767 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattnera7549902007-08-26 06:24:45 +0000768 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnerf446f722007-08-22 05:28:50 +0000769
Chris Lattner4b009652007-07-25 00:24:17 +0000770 // Read the body statement.
771 StmtResult Body = ParseStatement();
772
Chris Lattnerf446f722007-08-22 05:28:50 +0000773 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000774 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000775
Chris Lattner4b009652007-07-25 00:24:17 +0000776 // Leave the for-scope.
777 ExitScope();
778
779 if (Body.isInvalid)
780 return Body;
781
782 return Actions.ParseForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
783 ThirdPart, RParenLoc, Body.Val);
784}
785
786/// ParseGotoStatement
787/// jump-statement:
788/// 'goto' identifier ';'
789/// [GNU] 'goto' '*' expression ';'
790///
791/// Note: this lets the caller parse the end ';'.
792///
793Parser::StmtResult Parser::ParseGotoStatement() {
794 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
795 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
796
797 StmtResult Res;
798 if (Tok.getKind() == tok::identifier) {
799 Res = Actions.ParseGotoStmt(GotoLoc, Tok.getLocation(),
800 Tok.getIdentifierInfo());
801 ConsumeToken();
802 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
803 // GNU indirect goto extension.
804 Diag(Tok, diag::ext_gnu_indirect_goto);
805 SourceLocation StarLoc = ConsumeToken();
806 ExprResult R = ParseExpression();
807 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
808 SkipUntil(tok::semi, false, true);
809 return true;
810 }
811 Res = Actions.ParseIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
812 } else {
813 Diag(Tok, diag::err_expected_ident);
814 return true;
815 }
816
817 return Res;
818}
819
820/// ParseContinueStatement
821/// jump-statement:
822/// 'continue' ';'
823///
824/// Note: this lets the caller parse the end ';'.
825///
826Parser::StmtResult Parser::ParseContinueStatement() {
827 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
828 return Actions.ParseContinueStmt(ContinueLoc, CurScope);
829}
830
831/// ParseBreakStatement
832/// jump-statement:
833/// 'break' ';'
834///
835/// Note: this lets the caller parse the end ';'.
836///
837Parser::StmtResult Parser::ParseBreakStatement() {
838 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
839 return Actions.ParseBreakStmt(BreakLoc, CurScope);
840}
841
842/// ParseReturnStatement
843/// jump-statement:
844/// 'return' expression[opt] ';'
845Parser::StmtResult Parser::ParseReturnStatement() {
846 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
847 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
848
849 ExprResult R(0);
850 if (Tok.getKind() != tok::semi) {
851 R = ParseExpression();
852 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
853 SkipUntil(tok::semi, false, true);
854 return true;
855 }
856 }
857 return Actions.ParseReturnStmt(ReturnLoc, R.Val);
858}
859
860/// ParseAsmStatement - Parse a GNU extended asm statement.
861/// [GNU] asm-statement:
862/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
863///
864/// [GNU] asm-argument:
865/// asm-string-literal
866/// asm-string-literal ':' asm-operands[opt]
867/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
868/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
869/// ':' asm-clobbers
870///
871/// [GNU] asm-clobbers:
872/// asm-string-literal
873/// asm-clobbers ',' asm-string-literal
874///
875Parser::StmtResult Parser::ParseAsmStatement() {
876 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
877 ConsumeToken();
878
879 DeclSpec DS;
880 SourceLocation Loc = Tok.getLocation();
881 ParseTypeQualifierListOpt(DS);
882
883 // GNU asms accept, but warn, about type-qualifiers other than volatile.
884 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
885 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
886 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
887 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
888
889 // Remember if this was a volatile asm.
890 //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
891
892 if (Tok.getKind() != tok::l_paren) {
893 Diag(Tok, diag::err_expected_lparen_after, "asm");
894 SkipUntil(tok::r_paren);
895 return true;
896 }
897 Loc = ConsumeParen();
898
899 ParseAsmStringLiteral();
900
901 // Parse Outputs, if present.
902 ParseAsmOperandsOpt();
903
904 // Parse Inputs, if present.
905 ParseAsmOperandsOpt();
906
907 // Parse the clobbers, if present.
908 if (Tok.getKind() == tok::colon) {
909 ConsumeToken();
910
911 if (isTokenStringLiteral()) {
912 // Parse the asm-string list for clobbers.
913 while (1) {
914 ParseAsmStringLiteral();
915
916 if (Tok.getKind() != tok::comma) break;
917 ConsumeToken();
918 }
919 }
920 }
921
922 MatchRHSPunctuation(tok::r_paren, Loc);
923
924 // FIXME: Implement action for asm parsing.
925 return false;
926}
927
928/// ParseAsmOperands - Parse the asm-operands production as used by
929/// asm-statement. We also parse a leading ':' token. If the leading colon is
930/// not present, we do not parse anything.
931///
932/// [GNU] asm-operands:
933/// asm-operand
934/// asm-operands ',' asm-operand
935///
936/// [GNU] asm-operand:
937/// asm-string-literal '(' expression ')'
938/// '[' identifier ']' asm-string-literal '(' expression ')'
939///
940void Parser::ParseAsmOperandsOpt() {
941 // Only do anything if this operand is present.
942 if (Tok.getKind() != tok::colon) return;
943 ConsumeToken();
944
945 // 'asm-operands' isn't present?
946 if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square)
947 return;
948
949 while (1) {
950 // Read the [id] if present.
951 if (Tok.getKind() == tok::l_square) {
952 SourceLocation Loc = ConsumeBracket();
953
954 if (Tok.getKind() != tok::identifier) {
955 Diag(Tok, diag::err_expected_ident);
956 SkipUntil(tok::r_paren);
957 return;
958 }
959 MatchRHSPunctuation(tok::r_square, Loc);
960 }
961
962 ParseAsmStringLiteral();
963
964 if (Tok.getKind() != tok::l_paren) {
965 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
966 SkipUntil(tok::r_paren);
967 return;
968 }
969
970 // Read the parenthesized expression.
971 ExprResult Res = ParseSimpleParenExpression();
972 if (Res.isInvalid) {
973 SkipUntil(tok::r_paren);
974 return;
975 }
976
977 // Eat the comma and continue parsing if it exists.
978 if (Tok.getKind() != tok::comma) return;
979 ConsumeToken();
980 }
981}