blob: e0d527ebf607001e6384cc87a2f51ab0b1808f75 [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
357/// [GNU] '__extension__' declaration [TODO]
358/// 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
375 // Enter a scope to hold everything within the compound stmt.
376 EnterScope(0);
377
378 // Parse the statements in the body.
379 StmtResult Body = ParseCompoundStatementBody();
380
381 ExitScope();
382 return Body;
383}
384
385
386/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
387/// ParseCompoundStmt action. This expects the '{' to be the current token, and
388/// consume the '}' at the end of the block. It does not manipulate the scope
389/// stack.
390Parser::StmtResult Parser::ParseCompoundStatementBody() {
391 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
392
393 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
394 // only allowed at the start of a compound stmt.
395
396 llvm::SmallVector<StmtTy*, 32> Stmts;
397 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) {
398 StmtResult R = ParseStatementOrDeclaration(false);
399 if (!R.isInvalid && R.Val)
400 Stmts.push_back(R.Val);
401 }
402
403 // We broke out of the while loop because we found a '}' or EOF.
404 if (Tok.getKind() != tok::r_brace) {
405 Diag(Tok, diag::err_expected_rbrace);
406 return 0;
407 }
408
409 SourceLocation RBraceLoc = ConsumeBrace();
410 return Actions.ParseCompoundStmt(LBraceLoc, RBraceLoc,
411 &Stmts[0], Stmts.size());
412}
413
414/// ParseIfStatement
415/// if-statement: [C99 6.8.4.1]
416/// 'if' '(' expression ')' statement
417/// 'if' '(' expression ')' statement 'else' statement
418///
419Parser::StmtResult Parser::ParseIfStatement() {
420 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
421 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
422
423 if (Tok.getKind() != tok::l_paren) {
424 Diag(Tok, diag::err_expected_lparen_after, "if");
425 SkipUntil(tok::semi);
426 return true;
427 }
428
429 // Parse the condition.
430 ExprResult CondExp = ParseSimpleParenExpression();
431 if (CondExp.isInvalid) {
432 SkipUntil(tok::semi);
433 return true;
434 }
435
Chris Lattnerf446f722007-08-22 05:28:50 +0000436 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000437 // there is no compound stmt. C90 does not have this clause. We only do this
438 // if the body isn't a compound statement to avoid push/pop in common cases.
439 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
440 if (NeedsInnerScope) EnterScope(0);
Chris Lattnerd190ac22007-08-22 05:16:28 +0000441
Chris Lattner4b009652007-07-25 00:24:17 +0000442 // Read the if condition.
443 StmtResult CondStmt = ParseStatement();
444
445 // Broken substmt shouldn't prevent the label from being added to the AST.
446 if (CondStmt.isInvalid)
447 CondStmt = Actions.ParseNullStmt(Tok.getLocation());
448
Chris Lattnerd190ac22007-08-22 05:16:28 +0000449 // Pop the 'if' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000450 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000451
452 // If it has an else, parse it.
453 SourceLocation ElseLoc;
454 StmtResult ElseStmt(false);
455 if (Tok.getKind() == tok::kw_else) {
456 ElseLoc = ConsumeToken();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000457
Chris Lattnerf446f722007-08-22 05:28:50 +0000458 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000459 // there is no compound stmt. C90 does not have this clause. We only do
460 // this if the body isn't a compound statement to avoid push/pop in common
461 // cases.
462 NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
463 if (NeedsInnerScope) EnterScope(0);
Chris Lattnerd190ac22007-08-22 05:16:28 +0000464
Chris Lattner4b009652007-07-25 00:24:17 +0000465 ElseStmt = ParseStatement();
Chris Lattnerd190ac22007-08-22 05:16:28 +0000466
467 // Pop the 'else' scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000468 if (NeedsInnerScope) ExitScope();
Chris Lattner4b009652007-07-25 00:24:17 +0000469
470 if (ElseStmt.isInvalid)
471 ElseStmt = Actions.ParseNullStmt(ElseLoc);
472 }
473
474 return Actions.ParseIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
475 ElseLoc, ElseStmt.Val);
476}
477
478/// ParseSwitchStatement
479/// switch-statement:
480/// 'switch' '(' expression ')' statement
481Parser::StmtResult Parser::ParseSwitchStatement() {
482 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
483 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
484
485 if (Tok.getKind() != tok::l_paren) {
486 Diag(Tok, diag::err_expected_lparen_after, "switch");
487 SkipUntil(tok::semi);
488 return true;
489 }
490
491 // Start the switch scope.
492 EnterScope(Scope::BreakScope);
493
494 // Parse the condition.
495 ExprResult Cond = ParseSimpleParenExpression();
496
497 if (Cond.isInvalid) {
498 ExitScope();
499 return true;
500 }
501
502 StmtResult Switch = Actions.StartSwitchStmt(Cond.Val);
503
Chris Lattnerf446f722007-08-22 05:28:50 +0000504 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000505 // there is no compound stmt. C90 does not have this clause. We only do this
506 // if the body isn't a compound statement to avoid push/pop in common cases.
507 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
508 if (NeedsInnerScope) EnterScope(0);
Chris Lattnerf446f722007-08-22 05:28:50 +0000509
Chris Lattner4b009652007-07-25 00:24:17 +0000510 // Read the body statement.
511 StmtResult Body = ParseStatement();
512
Chris Lattnerf446f722007-08-22 05:28:50 +0000513 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000514 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000515
Chris Lattner4b009652007-07-25 00:24:17 +0000516 if (Body.isInvalid) {
517 Body = Actions.ParseNullStmt(Tok.getLocation());
518 // FIXME: Remove the case statement list from the Switch statement.
519 }
520
521 ExitScope();
522
523 return Actions.FinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
524}
525
526/// ParseWhileStatement
527/// while-statement: [C99 6.8.5.1]
528/// 'while' '(' expression ')' statement
529Parser::StmtResult Parser::ParseWhileStatement() {
530 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
531 SourceLocation WhileLoc = Tok.getLocation();
532 ConsumeToken(); // eat the 'while'.
533
534 if (Tok.getKind() != tok::l_paren) {
535 Diag(Tok, diag::err_expected_lparen_after, "while");
536 SkipUntil(tok::semi);
537 return true;
538 }
539
540 // Start the loop scope.
541 EnterScope(Scope::BreakScope | Scope::ContinueScope);
542
543 // Parse the condition.
544 ExprResult Cond = ParseSimpleParenExpression();
545
Chris Lattnerf446f722007-08-22 05:28:50 +0000546 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000547 // there is no compound stmt. C90 does not have this clause. We only do this
548 // if the body isn't a compound statement to avoid push/pop in common cases.
549 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
550 if (NeedsInnerScope) EnterScope(0);
Chris Lattnerf446f722007-08-22 05:28:50 +0000551
Chris Lattner4b009652007-07-25 00:24:17 +0000552 // Read the body statement.
553 StmtResult Body = ParseStatement();
554
Chris Lattnerf446f722007-08-22 05:28:50 +0000555 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000556 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000557
Chris Lattner4b009652007-07-25 00:24:17 +0000558 ExitScope();
559
560 if (Cond.isInvalid || Body.isInvalid) return true;
561
562 return Actions.ParseWhileStmt(WhileLoc, Cond.Val, Body.Val);
563}
564
565/// ParseDoStatement
566/// do-statement: [C99 6.8.5.2]
567/// 'do' statement 'while' '(' expression ')' ';'
568/// Note: this lets the caller parse the end ';'.
569Parser::StmtResult Parser::ParseDoStatement() {
570 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
571 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
572
573 // Start the loop scope.
574 EnterScope(Scope::BreakScope | Scope::ContinueScope);
575
Chris Lattnerf446f722007-08-22 05:28:50 +0000576 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000577 // there is no compound stmt. C90 does not have this clause. We only do this
578 // if the body isn't a compound statement to avoid push/pop in common cases.
579 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
580 if (NeedsInnerScope) EnterScope(0);
Chris Lattnerf446f722007-08-22 05:28:50 +0000581
Chris Lattner4b009652007-07-25 00:24:17 +0000582 // Read the body statement.
583 StmtResult Body = ParseStatement();
584
Chris Lattnerf446f722007-08-22 05:28:50 +0000585 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000586 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000587
Chris Lattner4b009652007-07-25 00:24:17 +0000588 if (Tok.getKind() != tok::kw_while) {
589 ExitScope();
590 Diag(Tok, diag::err_expected_while);
591 Diag(DoLoc, diag::err_matching, "do");
592 SkipUntil(tok::semi);
593 return true;
594 }
595 SourceLocation WhileLoc = ConsumeToken();
596
597 if (Tok.getKind() != tok::l_paren) {
598 ExitScope();
599 Diag(Tok, diag::err_expected_lparen_after, "do/while");
600 SkipUntil(tok::semi);
601 return true;
602 }
603
604 // Parse the condition.
605 ExprResult Cond = ParseSimpleParenExpression();
606
607 ExitScope();
608
609 if (Cond.isInvalid || Body.isInvalid) return true;
610
611 return Actions.ParseDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
612}
613
614/// ParseForStatement
615/// for-statement: [C99 6.8.5.3]
616/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
617/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
618Parser::StmtResult Parser::ParseForStatement() {
619 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
620 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
621
622 if (Tok.getKind() != tok::l_paren) {
623 Diag(Tok, diag::err_expected_lparen_after, "for");
624 SkipUntil(tok::semi);
625 return true;
626 }
627
628 EnterScope(Scope::BreakScope | Scope::ContinueScope);
629
630 SourceLocation LParenLoc = ConsumeParen();
631 ExprResult Value;
632
633 StmtTy *FirstPart = 0;
634 ExprTy *SecondPart = 0;
635 StmtTy *ThirdPart = 0;
636
637 // Parse the first part of the for specifier.
638 if (Tok.getKind() == tok::semi) { // for (;
639 // no first part, eat the ';'.
640 ConsumeToken();
641 } else if (isDeclarationSpecifier()) { // for (int X = 4;
642 // Parse declaration, which eats the ';'.
643 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
644 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
645 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
646 StmtResult stmtResult = Actions.ParseDeclStmt(aBlockVarDecl);
647 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
648 } else {
649 Value = ParseExpression();
650
651 // Turn the expression into a stmt.
652 if (!Value.isInvalid) {
653 StmtResult R = Actions.ParseExprStmt(Value.Val);
654 if (!R.isInvalid)
655 FirstPart = R.Val;
656 }
657
658 if (Tok.getKind() == tok::semi) {
659 ConsumeToken();
660 } else {
661 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
662 SkipUntil(tok::semi);
663 }
664 }
665
666 // Parse the second part of the for specifier.
667 if (Tok.getKind() == tok::semi) { // for (...;;
668 // no second part.
669 Value = ExprResult();
670 } else {
671 Value = ParseExpression();
672 if (!Value.isInvalid)
673 SecondPart = Value.Val;
674 }
675
676 if (Tok.getKind() == tok::semi) {
677 ConsumeToken();
678 } else {
679 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
680 SkipUntil(tok::semi);
681 }
682
683 // Parse the third part of the for specifier.
684 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
685 // no third part.
686 Value = ExprResult();
687 } else {
688 Value = ParseExpression();
689 if (!Value.isInvalid) {
690 // Turn the expression into a stmt.
691 StmtResult R = Actions.ParseExprStmt(Value.Val);
692 if (!R.isInvalid)
693 ThirdPart = R.Val;
694 }
695 }
696
697 // Match the ')'.
698 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
699
Chris Lattnerf446f722007-08-22 05:28:50 +0000700 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000701 // there is no compound stmt. C90 does not have this clause. We only do this
702 // if the body isn't a compound statement to avoid push/pop in common cases.
703 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
704 if (NeedsInnerScope) EnterScope(0);
Chris Lattnerf446f722007-08-22 05:28:50 +0000705
Chris Lattner4b009652007-07-25 00:24:17 +0000706 // Read the body statement.
707 StmtResult Body = ParseStatement();
708
Chris Lattnerf446f722007-08-22 05:28:50 +0000709 // Pop the body scope if needed.
Chris Lattner59ed6e22007-08-22 05:33:11 +0000710 if (NeedsInnerScope) ExitScope();
Chris Lattnerf446f722007-08-22 05:28:50 +0000711
Chris Lattner4b009652007-07-25 00:24:17 +0000712 // Leave the for-scope.
713 ExitScope();
714
715 if (Body.isInvalid)
716 return Body;
717
718 return Actions.ParseForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
719 ThirdPart, RParenLoc, Body.Val);
720}
721
722/// ParseGotoStatement
723/// jump-statement:
724/// 'goto' identifier ';'
725/// [GNU] 'goto' '*' expression ';'
726///
727/// Note: this lets the caller parse the end ';'.
728///
729Parser::StmtResult Parser::ParseGotoStatement() {
730 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
731 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
732
733 StmtResult Res;
734 if (Tok.getKind() == tok::identifier) {
735 Res = Actions.ParseGotoStmt(GotoLoc, Tok.getLocation(),
736 Tok.getIdentifierInfo());
737 ConsumeToken();
738 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
739 // GNU indirect goto extension.
740 Diag(Tok, diag::ext_gnu_indirect_goto);
741 SourceLocation StarLoc = ConsumeToken();
742 ExprResult R = ParseExpression();
743 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
744 SkipUntil(tok::semi, false, true);
745 return true;
746 }
747 Res = Actions.ParseIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
748 } else {
749 Diag(Tok, diag::err_expected_ident);
750 return true;
751 }
752
753 return Res;
754}
755
756/// ParseContinueStatement
757/// jump-statement:
758/// 'continue' ';'
759///
760/// Note: this lets the caller parse the end ';'.
761///
762Parser::StmtResult Parser::ParseContinueStatement() {
763 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
764 return Actions.ParseContinueStmt(ContinueLoc, CurScope);
765}
766
767/// ParseBreakStatement
768/// jump-statement:
769/// 'break' ';'
770///
771/// Note: this lets the caller parse the end ';'.
772///
773Parser::StmtResult Parser::ParseBreakStatement() {
774 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
775 return Actions.ParseBreakStmt(BreakLoc, CurScope);
776}
777
778/// ParseReturnStatement
779/// jump-statement:
780/// 'return' expression[opt] ';'
781Parser::StmtResult Parser::ParseReturnStatement() {
782 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
783 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
784
785 ExprResult R(0);
786 if (Tok.getKind() != tok::semi) {
787 R = ParseExpression();
788 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
789 SkipUntil(tok::semi, false, true);
790 return true;
791 }
792 }
793 return Actions.ParseReturnStmt(ReturnLoc, R.Val);
794}
795
796/// ParseAsmStatement - Parse a GNU extended asm statement.
797/// [GNU] asm-statement:
798/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
799///
800/// [GNU] asm-argument:
801/// asm-string-literal
802/// asm-string-literal ':' asm-operands[opt]
803/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
804/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
805/// ':' asm-clobbers
806///
807/// [GNU] asm-clobbers:
808/// asm-string-literal
809/// asm-clobbers ',' asm-string-literal
810///
811Parser::StmtResult Parser::ParseAsmStatement() {
812 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
813 ConsumeToken();
814
815 DeclSpec DS;
816 SourceLocation Loc = Tok.getLocation();
817 ParseTypeQualifierListOpt(DS);
818
819 // GNU asms accept, but warn, about type-qualifiers other than volatile.
820 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
821 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
822 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
823 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
824
825 // Remember if this was a volatile asm.
826 //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
827
828 if (Tok.getKind() != tok::l_paren) {
829 Diag(Tok, diag::err_expected_lparen_after, "asm");
830 SkipUntil(tok::r_paren);
831 return true;
832 }
833 Loc = ConsumeParen();
834
835 ParseAsmStringLiteral();
836
837 // Parse Outputs, if present.
838 ParseAsmOperandsOpt();
839
840 // Parse Inputs, if present.
841 ParseAsmOperandsOpt();
842
843 // Parse the clobbers, if present.
844 if (Tok.getKind() == tok::colon) {
845 ConsumeToken();
846
847 if (isTokenStringLiteral()) {
848 // Parse the asm-string list for clobbers.
849 while (1) {
850 ParseAsmStringLiteral();
851
852 if (Tok.getKind() != tok::comma) break;
853 ConsumeToken();
854 }
855 }
856 }
857
858 MatchRHSPunctuation(tok::r_paren, Loc);
859
860 // FIXME: Implement action for asm parsing.
861 return false;
862}
863
864/// ParseAsmOperands - Parse the asm-operands production as used by
865/// asm-statement. We also parse a leading ':' token. If the leading colon is
866/// not present, we do not parse anything.
867///
868/// [GNU] asm-operands:
869/// asm-operand
870/// asm-operands ',' asm-operand
871///
872/// [GNU] asm-operand:
873/// asm-string-literal '(' expression ')'
874/// '[' identifier ']' asm-string-literal '(' expression ')'
875///
876void Parser::ParseAsmOperandsOpt() {
877 // Only do anything if this operand is present.
878 if (Tok.getKind() != tok::colon) return;
879 ConsumeToken();
880
881 // 'asm-operands' isn't present?
882 if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square)
883 return;
884
885 while (1) {
886 // Read the [id] if present.
887 if (Tok.getKind() == tok::l_square) {
888 SourceLocation Loc = ConsumeBracket();
889
890 if (Tok.getKind() != tok::identifier) {
891 Diag(Tok, diag::err_expected_ident);
892 SkipUntil(tok::r_paren);
893 return;
894 }
895 MatchRHSPunctuation(tok::r_square, Loc);
896 }
897
898 ParseAsmStringLiteral();
899
900 if (Tok.getKind() != tok::l_paren) {
901 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
902 SkipUntil(tok::r_paren);
903 return;
904 }
905
906 // Read the parenthesized expression.
907 ExprResult Res = ParseSimpleParenExpression();
908 if (Res.isInvalid) {
909 SkipUntil(tok::r_paren);
910 return;
911 }
912
913 // Eat the comma and continue parsing if it exists.
914 if (Tok.getKind() != tok::comma) return;
915 ConsumeToken();
916 }
917}