blob: fb4fe1e339e90c5385cc846231900c97795d439a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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()) {
Steve Naroff1b273c42007-09-16 14:56:35 +000087 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Reid Spencer5f016e22007-07-11 17:01:13 +000088 } else if (Tok.getKind() == tok::r_brace) {
89 Diag(Tok, diag::err_expected_statement);
90 return true;
91 } else {
92 // expression[opt] ';'
93 ExprResult Res = ParseExpression();
94 if (Res.isInvalid) {
95 // If the expression is invalid, skip ahead to the next semicolon. Not
96 // doing this opens us up to the possibility of infinite loops if
97 // ParseExpression does not consume any tokens.
98 SkipUntil(tok::semi);
99 return true;
100 }
101 // Otherwise, eat the semicolon.
102 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000103 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 }
105
106 case tok::kw_case: // C99 6.8.1: labeled-statement
107 return ParseCaseStatement();
108 case tok::kw_default: // C99 6.8.1: labeled-statement
109 return ParseDefaultStatement();
110
111 case tok::l_brace: // C99 6.8.2: compound-statement
112 return ParseCompoundStatement();
113 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000114 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000115
116 case tok::kw_if: // C99 6.8.4.1: if-statement
117 return ParseIfStatement();
118 case tok::kw_switch: // C99 6.8.4.2: switch-statement
119 return ParseSwitchStatement();
120
121 case tok::kw_while: // C99 6.8.5.1: while-statement
122 return ParseWhileStatement();
123 case tok::kw_do: // C99 6.8.5.2: do-statement
124 Res = ParseDoStatement();
125 SemiError = "do/while loop";
126 break;
127 case tok::kw_for: // C99 6.8.5.3: for-statement
128 return ParseForStatement();
129
130 case tok::kw_goto: // C99 6.8.6.1: goto-statement
131 Res = ParseGotoStatement();
132 SemiError = "goto statement";
133 break;
134 case tok::kw_continue: // C99 6.8.6.2: continue-statement
135 Res = ParseContinueStatement();
136 SemiError = "continue statement";
137 break;
138 case tok::kw_break: // C99 6.8.6.3: break-statement
139 Res = ParseBreakStatement();
140 SemiError = "break statement";
141 break;
142 case tok::kw_return: // C99 6.8.6.4: return-statement
143 Res = ParseReturnStatement();
144 SemiError = "return statement";
145 break;
146
147 case tok::kw_asm:
148 Res = ParseAsmStatement();
149 SemiError = "asm statement";
150 break;
151 }
152
153 // If we reached this code, the statement must end in a semicolon.
154 if (Tok.getKind() == tok::semi) {
155 ConsumeToken();
156 } else {
157 Diag(Tok, diag::err_expected_semi_after, SemiError);
158 SkipUntil(tok::semi);
159 }
160 return Res;
161}
162
163/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
164/// have a bit of a quandry here. Reading the identifier is necessary to see if
165/// there is a ':' after it. If there is, this is a label, regardless of what
166/// else the identifier can mean. If not, this is either part of a declaration
167/// (if the identifier is a type-name) or part of an expression.
168///
169/// labeled-statement:
170/// identifier ':' statement
171/// [GNU] identifier ':' attributes[opt] statement
172/// declaration (if !OnlyStatement)
173/// expression[opt] ';'
174///
175Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
176 assert(Tok.getKind() == tok::identifier && Tok.getIdentifierInfo() &&
177 "Not an identifier!");
178
Chris Lattnerd2177732007-07-20 16:59:19 +0000179 Token IdentTok = Tok; // Save the whole token.
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 ConsumeToken(); // eat the identifier.
181
182 // identifier ':' statement
183 if (Tok.getKind() == tok::colon) {
184 SourceLocation ColonLoc = ConsumeToken();
185
186 // Read label attributes, if present.
187 DeclTy *AttrList = 0;
188 if (Tok.getKind() == tok::kw___attribute)
189 // TODO: save these somewhere.
190 AttrList = ParseAttributes();
191
192 StmtResult SubStmt = ParseStatement();
193
194 // Broken substmt shouldn't prevent the label from being added to the AST.
195 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000196 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197
Steve Naroff1b273c42007-09-16 14:56:35 +0000198 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 IdentTok.getIdentifierInfo(),
200 ColonLoc, SubStmt.Val);
201 }
202
203 // Check to see if this is a declaration.
204 void *TypeRep;
205 if (!OnlyStatement &&
206 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
207 // Handle this. Warn/disable if in middle of block and !C99.
208 DeclSpec DS;
209
210 // Add the typedef name to the start of the decl-specs.
211 const char *PrevSpec = 0;
212 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
213 IdentTok.getLocation(), PrevSpec,
214 TypeRep);
215 assert(!isInvalid && "First declspec can't be invalid!");
216
217 // ParseDeclarationSpecifiers will continue from there.
218 ParseDeclarationSpecifiers(DS);
219
220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
222 if (Tok.getKind() == tok::semi) {
223 // TODO: emit error on 'int;' or 'const enum foo;'.
224 // if (!DS.isMissingDeclaratorOk()) Diag(...);
225
226 ConsumeToken();
227 // FIXME: Return this as a type decl.
228 return 0;
229 }
230
231 // Parse all the declarators.
232 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
233 ParseDeclarator(DeclaratorInfo);
234
235 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff1b273c42007-09-16 14:56:35 +0000236 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 }
238
239 // Otherwise, this is an expression. Seed it with II and parse it.
240 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
241 if (Res.isInvalid) {
242 SkipUntil(tok::semi);
243 return true;
244 } else if (Tok.getKind() != tok::semi) {
245 Diag(Tok, diag::err_expected_semi_after, "expression");
246 SkipUntil(tok::semi);
247 return true;
248 } else {
249 ConsumeToken();
250 // Convert expr to a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000251 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 }
253}
254
255/// ParseCaseStatement
256/// labeled-statement:
257/// 'case' constant-expression ':' statement
258/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
259///
260/// Note that this does not parse the 'statement' at the end.
261///
262Parser::StmtResult Parser::ParseCaseStatement() {
263 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
264 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
265
266 ExprResult LHS = ParseConstantExpression();
267 if (LHS.isInvalid) {
268 SkipUntil(tok::colon);
269 return true;
270 }
271
272 // GNU case range extension.
273 SourceLocation DotDotDotLoc;
274 ExprTy *RHSVal = 0;
275 if (Tok.getKind() == tok::ellipsis) {
276 Diag(Tok, diag::ext_gnu_case_range);
277 DotDotDotLoc = ConsumeToken();
278
279 ExprResult RHS = ParseConstantExpression();
280 if (RHS.isInvalid) {
281 SkipUntil(tok::colon);
282 return true;
283 }
284 RHSVal = RHS.Val;
285 }
286
287 if (Tok.getKind() != tok::colon) {
288 Diag(Tok, diag::err_expected_colon_after, "'case'");
289 SkipUntil(tok::colon);
290 return true;
291 }
292
293 SourceLocation ColonLoc = ConsumeToken();
294
295 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
296 if (Tok.getKind() == tok::r_brace) {
297 Diag(Tok, diag::err_label_end_of_compound_statement);
298 return true;
299 }
300
301 StmtResult SubStmt = ParseStatement();
302
303 // Broken substmt shouldn't prevent the case from being added to the AST.
304 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000305 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000306
Steve Naroff1b273c42007-09-16 14:56:35 +0000307 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 SubStmt.Val);
309}
310
311/// ParseDefaultStatement
312/// labeled-statement:
313/// 'default' ':' statement
314/// Note that this does not parse the 'statement' at the end.
315///
316Parser::StmtResult Parser::ParseDefaultStatement() {
317 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
318 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
319
320 if (Tok.getKind() != tok::colon) {
321 Diag(Tok, diag::err_expected_colon_after, "'default'");
322 SkipUntil(tok::colon);
323 return true;
324 }
325
326 SourceLocation ColonLoc = ConsumeToken();
327
328 // Diagnose the common error "switch (X) {... default: }", which is not valid.
329 if (Tok.getKind() == tok::r_brace) {
330 Diag(Tok, diag::err_label_end_of_compound_statement);
331 return true;
332 }
333
334 StmtResult SubStmt = ParseStatement();
335 if (SubStmt.isInvalid)
336 return true;
337
Steve Naroff1b273c42007-09-16 14:56:35 +0000338 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339}
340
341
342/// ParseCompoundStatement - Parse a "{}" block.
343///
344/// compound-statement: [C99 6.8.2]
345/// { block-item-list[opt] }
346/// [GNU] { label-declarations block-item-list } [TODO]
347///
348/// block-item-list:
349/// block-item
350/// block-item-list block-item
351///
352/// block-item:
353/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000354/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000355/// statement
356/// [OMP] openmp-directive [TODO]
357///
358/// [GNU] label-declarations:
359/// [GNU] label-declaration
360/// [GNU] label-declarations label-declaration
361///
362/// [GNU] label-declaration:
363/// [GNU] '__label__' identifier-list ';'
364///
365/// [OMP] openmp-directive: [TODO]
366/// [OMP] barrier-directive
367/// [OMP] flush-directive
368///
Chris Lattner98414c12007-08-31 21:49:55 +0000369Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
371
Chris Lattner31e05722007-08-26 06:24:45 +0000372 // Enter a scope to hold everything within the compound stmt. Compound
373 // statements can always hold declarations.
374 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375
376 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000377 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000378
379 ExitScope();
380 return Body;
381}
382
383
384/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000385/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000386/// consume the '}' at the end of the block. It does not manipulate the scope
387/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000388Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
390
391 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000392 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000393
394 llvm::SmallVector<StmtTy*, 32> Stmts;
395 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000396 StmtResult R;
397 if (Tok.getKind() != tok::kw___extension__) {
398 R = ParseStatementOrDeclaration(false);
399 } else {
400 // __extension__ can start declarations and it can also be a unary
401 // operator for expressions. Consume multiple __extension__ markers here
402 // until we can determine which is which.
403 SourceLocation ExtLoc = ConsumeToken();
404 while (Tok.getKind() == tok::kw___extension__)
405 ConsumeToken();
406
407 // If this is the start of a declaration, parse it as such.
408 if (isDeclarationSpecifier()) {
409 // FIXME: Save the __extension__ on the decl as a node somehow.
410 // FIXME: disable extwarns.
Steve Naroff1b273c42007-09-16 14:56:35 +0000411 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner45a566c2007-08-27 01:01:57 +0000412 } else {
413 // Otherwise this was a unary __extension__ marker. Parse the
414 // subexpression and add the __extension__ unary op.
415 // FIXME: disable extwarns.
416 ExprResult Res = ParseCastExpression(false);
417 if (Res.isInvalid) {
418 SkipUntil(tok::semi);
419 continue;
420 }
421
422 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000423 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000424 if (Res.isInvalid)
425 continue;
426
427 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
428 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000429 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000430 }
431 }
432
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 if (!R.isInvalid && R.Val)
434 Stmts.push_back(R.Val);
435 }
436
437 // We broke out of the while loop because we found a '}' or EOF.
438 if (Tok.getKind() != tok::r_brace) {
439 Diag(Tok, diag::err_expected_rbrace);
440 return 0;
441 }
442
443 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000444 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000445 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000446}
447
448/// ParseIfStatement
449/// if-statement: [C99 6.8.4.1]
450/// 'if' '(' expression ')' statement
451/// 'if' '(' expression ')' statement 'else' statement
452///
453Parser::StmtResult Parser::ParseIfStatement() {
454 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
455 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
456
457 if (Tok.getKind() != tok::l_paren) {
458 Diag(Tok, diag::err_expected_lparen_after, "if");
459 SkipUntil(tok::semi);
460 return true;
461 }
462
Chris Lattner22153252007-08-26 23:08:06 +0000463 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
464 // the case for C90.
465 if (getLang().C99)
466 EnterScope(Scope::DeclScope);
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 // Parse the condition.
469 ExprResult CondExp = ParseSimpleParenExpression();
470 if (CondExp.isInvalid) {
471 SkipUntil(tok::semi);
Chris Lattner22153252007-08-26 23:08:06 +0000472 if (getLang().C99)
473 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 return true;
475 }
476
Chris Lattner0ecea032007-08-22 05:28:50 +0000477 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000478 // there is no compound stmt. C90 does not have this clause. We only do this
479 // if the body isn't a compound statement to avoid push/pop in common cases.
480 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000481 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000482
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 // Read the if condition.
484 StmtResult CondStmt = ParseStatement();
485
486 // Broken substmt shouldn't prevent the label from being added to the AST.
487 if (CondStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000488 CondStmt = Actions.ActOnNullStmt(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000489
Chris Lattnera36ce712007-08-22 05:16:28 +0000490 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000491 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000492
493 // If it has an else, parse it.
494 SourceLocation ElseLoc;
495 StmtResult ElseStmt(false);
496 if (Tok.getKind() == tok::kw_else) {
497 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000498
Chris Lattner0ecea032007-08-22 05:28:50 +0000499 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000500 // there is no compound stmt. C90 does not have this clause. We only do
501 // this if the body isn't a compound statement to avoid push/pop in common
502 // cases.
503 NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000504 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000505
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000507
508 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000509 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000510
511 if (ElseStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000512 ElseStmt = Actions.ActOnNullStmt(ElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 }
514
Chris Lattner22153252007-08-26 23:08:06 +0000515 if (getLang().C99)
516 ExitScope();
517
Steve Naroff1b273c42007-09-16 14:56:35 +0000518 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 ElseLoc, ElseStmt.Val);
520}
521
522/// ParseSwitchStatement
523/// switch-statement:
524/// 'switch' '(' expression ')' statement
525Parser::StmtResult Parser::ParseSwitchStatement() {
526 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
527 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
528
529 if (Tok.getKind() != tok::l_paren) {
530 Diag(Tok, diag::err_expected_lparen_after, "switch");
531 SkipUntil(tok::semi);
532 return true;
533 }
Chris Lattner22153252007-08-26 23:08:06 +0000534
535 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
536 // not the case for C90. Start the switch scope.
537 if (getLang().C99)
538 EnterScope(Scope::BreakScope|Scope::DeclScope);
539 else
540 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000541
542 // Parse the condition.
543 ExprResult Cond = ParseSimpleParenExpression();
544
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000545 if (Cond.isInvalid) {
546 ExitScope();
547 return true;
548 }
549
Steve Naroff1b273c42007-09-16 14:56:35 +0000550 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000551
Chris Lattner0ecea032007-08-22 05:28:50 +0000552 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000553 // there is no compound stmt. C90 does not have this clause. We only do this
554 // if the body isn't a compound statement to avoid push/pop in common cases.
555 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000556 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000557
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 // Read the body statement.
559 StmtResult Body = ParseStatement();
560
Chris Lattner0ecea032007-08-22 05:28:50 +0000561 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000562 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000563
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000564 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000565 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000566 // FIXME: Remove the case statement list from the Switch statement.
567 }
568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 ExitScope();
570
Steve Naroff1b273c42007-09-16 14:56:35 +0000571 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000572}
573
574/// ParseWhileStatement
575/// while-statement: [C99 6.8.5.1]
576/// 'while' '(' expression ')' statement
577Parser::StmtResult Parser::ParseWhileStatement() {
578 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
579 SourceLocation WhileLoc = Tok.getLocation();
580 ConsumeToken(); // eat the 'while'.
581
582 if (Tok.getKind() != tok::l_paren) {
583 Diag(Tok, diag::err_expected_lparen_after, "while");
584 SkipUntil(tok::semi);
585 return true;
586 }
587
Chris Lattner22153252007-08-26 23:08:06 +0000588 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
589 // the case for C90. Start the loop scope.
590 if (getLang().C99)
591 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
592 else
593 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594
595 // Parse the condition.
596 ExprResult Cond = ParseSimpleParenExpression();
597
Chris Lattner0ecea032007-08-22 05:28:50 +0000598 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000599 // there is no compound stmt. C90 does not have this clause. We only do this
600 // if the body isn't a compound statement to avoid push/pop in common cases.
601 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000602 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 // Read the body statement.
605 StmtResult Body = ParseStatement();
606
Chris Lattner0ecea032007-08-22 05:28:50 +0000607 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000608 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 ExitScope();
611
612 if (Cond.isInvalid || Body.isInvalid) return true;
613
Steve Naroff1b273c42007-09-16 14:56:35 +0000614 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000615}
616
617/// ParseDoStatement
618/// do-statement: [C99 6.8.5.2]
619/// 'do' statement 'while' '(' expression ')' ';'
620/// Note: this lets the caller parse the end ';'.
621Parser::StmtResult Parser::ParseDoStatement() {
622 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
623 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
624
Chris Lattner22153252007-08-26 23:08:06 +0000625 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
626 // the case for C90. Start the loop scope.
627 if (getLang().C99)
628 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
629 else
630 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000631
Chris Lattner0ecea032007-08-22 05:28:50 +0000632 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000633 // there is no compound stmt. C90 does not have this clause. We only do this
634 // if the body isn't a compound statement to avoid push/pop in common cases.
635 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000636 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000637
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 // Read the body statement.
639 StmtResult Body = ParseStatement();
640
Chris Lattner0ecea032007-08-22 05:28:50 +0000641 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000642 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 if (Tok.getKind() != tok::kw_while) {
645 ExitScope();
646 Diag(Tok, diag::err_expected_while);
647 Diag(DoLoc, diag::err_matching, "do");
648 SkipUntil(tok::semi);
649 return true;
650 }
651 SourceLocation WhileLoc = ConsumeToken();
652
653 if (Tok.getKind() != tok::l_paren) {
654 ExitScope();
655 Diag(Tok, diag::err_expected_lparen_after, "do/while");
656 SkipUntil(tok::semi);
657 return true;
658 }
659
660 // Parse the condition.
661 ExprResult Cond = ParseSimpleParenExpression();
662
663 ExitScope();
664
665 if (Cond.isInvalid || Body.isInvalid) return true;
666
Steve Naroff1b273c42007-09-16 14:56:35 +0000667 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000668}
669
670/// ParseForStatement
671/// for-statement: [C99 6.8.5.3]
672/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
673/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
674Parser::StmtResult Parser::ParseForStatement() {
675 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
676 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
677
678 if (Tok.getKind() != tok::l_paren) {
679 Diag(Tok, diag::err_expected_lparen_after, "for");
680 SkipUntil(tok::semi);
681 return true;
682 }
683
Chris Lattner22153252007-08-26 23:08:06 +0000684 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
685 // the case for C90. Start the loop scope.
686 if (getLang().C99)
687 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
688 else
689 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
691 SourceLocation LParenLoc = ConsumeParen();
692 ExprResult Value;
693
694 StmtTy *FirstPart = 0;
695 ExprTy *SecondPart = 0;
696 StmtTy *ThirdPart = 0;
697
698 // Parse the first part of the for specifier.
699 if (Tok.getKind() == tok::semi) { // for (;
700 // no first part, eat the ';'.
701 ConsumeToken();
702 } else if (isDeclarationSpecifier()) { // for (int X = 4;
703 // Parse declaration, which eats the ';'.
704 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
705 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
706 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff1b273c42007-09-16 14:56:35 +0000707 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
709 } else {
710 Value = ParseExpression();
711
712 // Turn the expression into a stmt.
713 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000714 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 if (!R.isInvalid)
716 FirstPart = R.Val;
717 }
718
719 if (Tok.getKind() == tok::semi) {
720 ConsumeToken();
721 } else {
722 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
723 SkipUntil(tok::semi);
724 }
725 }
726
727 // Parse the second part of the for specifier.
728 if (Tok.getKind() == tok::semi) { // for (...;;
729 // no second part.
730 Value = ExprResult();
731 } else {
732 Value = ParseExpression();
733 if (!Value.isInvalid)
734 SecondPart = Value.Val;
735 }
736
737 if (Tok.getKind() == tok::semi) {
738 ConsumeToken();
739 } else {
740 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
741 SkipUntil(tok::semi);
742 }
743
744 // Parse the third part of the for specifier.
745 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
746 // no third part.
747 Value = ExprResult();
748 } else {
749 Value = ParseExpression();
750 if (!Value.isInvalid) {
751 // Turn the expression into a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000752 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 if (!R.isInvalid)
754 ThirdPart = R.Val;
755 }
756 }
757
758 // Match the ')'.
759 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
760
Chris Lattner0ecea032007-08-22 05:28:50 +0000761 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000762 // there is no compound stmt. C90 does not have this clause. We only do this
763 // if the body isn't a compound statement to avoid push/pop in common cases.
764 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000765 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000766
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 // Read the body statement.
768 StmtResult Body = ParseStatement();
769
Chris Lattner0ecea032007-08-22 05:28:50 +0000770 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000771 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000772
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 // Leave the for-scope.
774 ExitScope();
775
776 if (Body.isInvalid)
777 return Body;
778
Steve Naroff1b273c42007-09-16 14:56:35 +0000779 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 ThirdPart, RParenLoc, Body.Val);
781}
782
783/// ParseGotoStatement
784/// jump-statement:
785/// 'goto' identifier ';'
786/// [GNU] 'goto' '*' expression ';'
787///
788/// Note: this lets the caller parse the end ';'.
789///
790Parser::StmtResult Parser::ParseGotoStatement() {
791 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
792 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
793
794 StmtResult Res;
795 if (Tok.getKind() == tok::identifier) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000796 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 Tok.getIdentifierInfo());
798 ConsumeToken();
799 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
800 // GNU indirect goto extension.
801 Diag(Tok, diag::ext_gnu_indirect_goto);
802 SourceLocation StarLoc = ConsumeToken();
803 ExprResult R = ParseExpression();
804 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
805 SkipUntil(tok::semi, false, true);
806 return true;
807 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000808 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000809 } else {
810 Diag(Tok, diag::err_expected_ident);
811 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 return Res;
815}
816
817/// ParseContinueStatement
818/// jump-statement:
819/// 'continue' ';'
820///
821/// Note: this lets the caller parse the end ';'.
822///
823Parser::StmtResult Parser::ParseContinueStatement() {
824 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000825 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000826}
827
828/// ParseBreakStatement
829/// jump-statement:
830/// 'break' ';'
831///
832/// Note: this lets the caller parse the end ';'.
833///
834Parser::StmtResult Parser::ParseBreakStatement() {
835 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000836 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000837}
838
839/// ParseReturnStatement
840/// jump-statement:
841/// 'return' expression[opt] ';'
842Parser::StmtResult Parser::ParseReturnStatement() {
843 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
844 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
845
846 ExprResult R(0);
847 if (Tok.getKind() != tok::semi) {
848 R = ParseExpression();
849 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
850 SkipUntil(tok::semi, false, true);
851 return true;
852 }
853 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000854 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000855}
856
857/// ParseAsmStatement - Parse a GNU extended asm statement.
858/// [GNU] asm-statement:
859/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
860///
861/// [GNU] asm-argument:
862/// asm-string-literal
863/// asm-string-literal ':' asm-operands[opt]
864/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
865/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
866/// ':' asm-clobbers
867///
868/// [GNU] asm-clobbers:
869/// asm-string-literal
870/// asm-clobbers ',' asm-string-literal
871///
872Parser::StmtResult Parser::ParseAsmStatement() {
873 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
874 ConsumeToken();
875
876 DeclSpec DS;
877 SourceLocation Loc = Tok.getLocation();
878 ParseTypeQualifierListOpt(DS);
879
880 // GNU asms accept, but warn, about type-qualifiers other than volatile.
881 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
882 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
883 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
884 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
885
886 // Remember if this was a volatile asm.
887 //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
888
889 if (Tok.getKind() != tok::l_paren) {
890 Diag(Tok, diag::err_expected_lparen_after, "asm");
891 SkipUntil(tok::r_paren);
892 return true;
893 }
894 Loc = ConsumeParen();
895
896 ParseAsmStringLiteral();
897
898 // Parse Outputs, if present.
899 ParseAsmOperandsOpt();
900
901 // Parse Inputs, if present.
902 ParseAsmOperandsOpt();
903
904 // Parse the clobbers, if present.
905 if (Tok.getKind() == tok::colon) {
906 ConsumeToken();
907
908 if (isTokenStringLiteral()) {
909 // Parse the asm-string list for clobbers.
910 while (1) {
911 ParseAsmStringLiteral();
912
913 if (Tok.getKind() != tok::comma) break;
914 ConsumeToken();
915 }
916 }
917 }
918
919 MatchRHSPunctuation(tok::r_paren, Loc);
920
921 // FIXME: Implement action for asm parsing.
922 return false;
923}
924
925/// ParseAsmOperands - Parse the asm-operands production as used by
926/// asm-statement. We also parse a leading ':' token. If the leading colon is
927/// not present, we do not parse anything.
928///
929/// [GNU] asm-operands:
930/// asm-operand
931/// asm-operands ',' asm-operand
932///
933/// [GNU] asm-operand:
934/// asm-string-literal '(' expression ')'
935/// '[' identifier ']' asm-string-literal '(' expression ')'
936///
937void Parser::ParseAsmOperandsOpt() {
938 // Only do anything if this operand is present.
939 if (Tok.getKind() != tok::colon) return;
940 ConsumeToken();
941
942 // 'asm-operands' isn't present?
943 if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square)
944 return;
945
946 while (1) {
947 // Read the [id] if present.
948 if (Tok.getKind() == tok::l_square) {
949 SourceLocation Loc = ConsumeBracket();
950
951 if (Tok.getKind() != tok::identifier) {
952 Diag(Tok, diag::err_expected_ident);
953 SkipUntil(tok::r_paren);
954 return;
955 }
956 MatchRHSPunctuation(tok::r_square, Loc);
957 }
958
959 ParseAsmStringLiteral();
960
961 if (Tok.getKind() != tok::l_paren) {
962 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
963 SkipUntil(tok::r_paren);
964 return;
965 }
966
967 // Read the parenthesized expression.
968 ExprResult Res = ParseSimpleParenExpression();
969 if (Res.isInvalid) {
970 SkipUntil(tok::r_paren);
971 return;
972 }
973
974 // Eat the comma and continue parsing if it exists.
975 if (Tok.getKind() != tok::comma) return;
976 ConsumeToken();
977 }
978}