blob: 7dc6a62e84abef19ec89f48aae44c7d766a16a9c [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
Fariborz Jahanianb384d322007-10-04 20:19:06 +000037/// [OBC] objc-throw-statement
38/// [OBC] objc-try-catch-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000039/// [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///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000067/// [OBC] objc-throw-statement:
68/// [OBC] '@' 'throw' expression ';'
69/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000070///
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.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000078 tok::TokenKind Kind = Tok.getKind();
79 SourceLocation AtLoc;
80 switch (Kind) {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 case tok::identifier: // C99 6.8.1: labeled-statement
82 // identifier ':' statement
83 // declaration (if !OnlyStatement)
84 // expression[opt] ';'
85 return ParseIdentifierStatement(OnlyStatement);
86
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
90 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_try)
91 return ParseObjCTryStmt(AtLoc);
92 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_throw)
93 return ParseObjCThrowStmt(AtLoc);
Fariborz Jahanianb384d322007-10-04 20:19:06 +000094 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
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.ActOnExprStmt(Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000105 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 default:
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000108 if (!OnlyStatement && isDeclarationSpecifier()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000109 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 } else if (Tok.getKind() == tok::r_brace) {
111 Diag(Tok, diag::err_expected_statement);
112 return true;
113 } else {
114 // expression[opt] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000115 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 if (Res.isInvalid) {
117 // If the expression is invalid, skip ahead to the next semicolon. Not
118 // doing this opens us up to the possibility of infinite loops if
119 // ParseExpression does not consume any tokens.
120 SkipUntil(tok::semi);
121 return true;
122 }
123 // Otherwise, eat the semicolon.
124 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000125 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127
128 case tok::kw_case: // C99 6.8.1: labeled-statement
129 return ParseCaseStatement();
130 case tok::kw_default: // C99 6.8.1: labeled-statement
131 return ParseDefaultStatement();
132
133 case tok::l_brace: // C99 6.8.2: compound-statement
134 return ParseCompoundStatement();
135 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000136 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000137
138 case tok::kw_if: // C99 6.8.4.1: if-statement
139 return ParseIfStatement();
140 case tok::kw_switch: // C99 6.8.4.2: switch-statement
141 return ParseSwitchStatement();
142
143 case tok::kw_while: // C99 6.8.5.1: while-statement
144 return ParseWhileStatement();
145 case tok::kw_do: // C99 6.8.5.2: do-statement
146 Res = ParseDoStatement();
147 SemiError = "do/while loop";
148 break;
149 case tok::kw_for: // C99 6.8.5.3: for-statement
150 return ParseForStatement();
151
152 case tok::kw_goto: // C99 6.8.6.1: goto-statement
153 Res = ParseGotoStatement();
154 SemiError = "goto statement";
155 break;
156 case tok::kw_continue: // C99 6.8.6.2: continue-statement
157 Res = ParseContinueStatement();
158 SemiError = "continue statement";
159 break;
160 case tok::kw_break: // C99 6.8.6.3: break-statement
161 Res = ParseBreakStatement();
162 SemiError = "break statement";
163 break;
164 case tok::kw_return: // C99 6.8.6.4: return-statement
165 Res = ParseReturnStatement();
166 SemiError = "return statement";
167 break;
168
169 case tok::kw_asm:
170 Res = ParseAsmStatement();
171 SemiError = "asm statement";
172 break;
173 }
174
175 // If we reached this code, the statement must end in a semicolon.
176 if (Tok.getKind() == tok::semi) {
177 ConsumeToken();
178 } else {
179 Diag(Tok, diag::err_expected_semi_after, SemiError);
180 SkipUntil(tok::semi);
181 }
182 return Res;
183}
184
185/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
186/// have a bit of a quandry here. Reading the identifier is necessary to see if
187/// there is a ':' after it. If there is, this is a label, regardless of what
188/// else the identifier can mean. If not, this is either part of a declaration
189/// (if the identifier is a type-name) or part of an expression.
190///
191/// labeled-statement:
192/// identifier ':' statement
193/// [GNU] identifier ':' attributes[opt] statement
194/// declaration (if !OnlyStatement)
195/// expression[opt] ';'
196///
197Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
198 assert(Tok.getKind() == tok::identifier && Tok.getIdentifierInfo() &&
199 "Not an identifier!");
200
Chris Lattnerd2177732007-07-20 16:59:19 +0000201 Token IdentTok = Tok; // Save the whole token.
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 ConsumeToken(); // eat the identifier.
203
204 // identifier ':' statement
205 if (Tok.getKind() == tok::colon) {
206 SourceLocation ColonLoc = ConsumeToken();
207
208 // Read label attributes, if present.
209 DeclTy *AttrList = 0;
210 if (Tok.getKind() == tok::kw___attribute)
211 // TODO: save these somewhere.
212 AttrList = ParseAttributes();
213
214 StmtResult SubStmt = ParseStatement();
215
216 // Broken substmt shouldn't prevent the label from being added to the AST.
217 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000218 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
Steve Naroff1b273c42007-09-16 14:56:35 +0000220 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 IdentTok.getIdentifierInfo(),
222 ColonLoc, SubStmt.Val);
223 }
224
225 // Check to see if this is a declaration.
226 void *TypeRep;
227 if (!OnlyStatement &&
228 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
229 // Handle this. Warn/disable if in middle of block and !C99.
230 DeclSpec DS;
231
232 // Add the typedef name to the start of the decl-specs.
233 const char *PrevSpec = 0;
234 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
235 IdentTok.getLocation(), PrevSpec,
236 TypeRep);
237 assert(!isInvalid && "First declspec can't be invalid!");
238
239 // ParseDeclarationSpecifiers will continue from there.
240 ParseDeclarationSpecifiers(DS);
241
242 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
243 // declaration-specifiers init-declarator-list[opt] ';'
244 if (Tok.getKind() == tok::semi) {
245 // TODO: emit error on 'int;' or 'const enum foo;'.
246 // if (!DS.isMissingDeclaratorOk()) Diag(...);
247
248 ConsumeToken();
249 // FIXME: Return this as a type decl.
250 return 0;
251 }
252
253 // Parse all the declarators.
254 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
255 ParseDeclarator(DeclaratorInfo);
256
257 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff1b273c42007-09-16 14:56:35 +0000258 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 }
260
261 // Otherwise, this is an expression. Seed it with II and parse it.
262 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
263 if (Res.isInvalid) {
264 SkipUntil(tok::semi);
265 return true;
266 } else if (Tok.getKind() != tok::semi) {
267 Diag(Tok, diag::err_expected_semi_after, "expression");
268 SkipUntil(tok::semi);
269 return true;
270 } else {
271 ConsumeToken();
272 // Convert expr to a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000273 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 }
275}
276
277/// ParseCaseStatement
278/// labeled-statement:
279/// 'case' constant-expression ':' statement
280/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
281///
282/// Note that this does not parse the 'statement' at the end.
283///
284Parser::StmtResult Parser::ParseCaseStatement() {
285 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
286 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
287
288 ExprResult LHS = ParseConstantExpression();
289 if (LHS.isInvalid) {
290 SkipUntil(tok::colon);
291 return true;
292 }
293
294 // GNU case range extension.
295 SourceLocation DotDotDotLoc;
296 ExprTy *RHSVal = 0;
297 if (Tok.getKind() == tok::ellipsis) {
298 Diag(Tok, diag::ext_gnu_case_range);
299 DotDotDotLoc = ConsumeToken();
300
301 ExprResult RHS = ParseConstantExpression();
302 if (RHS.isInvalid) {
303 SkipUntil(tok::colon);
304 return true;
305 }
306 RHSVal = RHS.Val;
307 }
308
309 if (Tok.getKind() != tok::colon) {
310 Diag(Tok, diag::err_expected_colon_after, "'case'");
311 SkipUntil(tok::colon);
312 return true;
313 }
314
315 SourceLocation ColonLoc = ConsumeToken();
316
317 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
318 if (Tok.getKind() == tok::r_brace) {
319 Diag(Tok, diag::err_label_end_of_compound_statement);
320 return true;
321 }
322
323 StmtResult SubStmt = ParseStatement();
324
325 // Broken substmt shouldn't prevent the case from being added to the AST.
326 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000327 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
Steve Naroff1b273c42007-09-16 14:56:35 +0000329 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 SubStmt.Val);
331}
332
333/// ParseDefaultStatement
334/// labeled-statement:
335/// 'default' ':' statement
336/// Note that this does not parse the 'statement' at the end.
337///
338Parser::StmtResult Parser::ParseDefaultStatement() {
339 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
340 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
341
342 if (Tok.getKind() != tok::colon) {
343 Diag(Tok, diag::err_expected_colon_after, "'default'");
344 SkipUntil(tok::colon);
345 return true;
346 }
347
348 SourceLocation ColonLoc = ConsumeToken();
349
350 // Diagnose the common error "switch (X) {... default: }", which is not valid.
351 if (Tok.getKind() == tok::r_brace) {
352 Diag(Tok, diag::err_label_end_of_compound_statement);
353 return true;
354 }
355
356 StmtResult SubStmt = ParseStatement();
357 if (SubStmt.isInvalid)
358 return true;
359
Steve Naroff1b273c42007-09-16 14:56:35 +0000360 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000361}
362
363
364/// ParseCompoundStatement - Parse a "{}" block.
365///
366/// compound-statement: [C99 6.8.2]
367/// { block-item-list[opt] }
368/// [GNU] { label-declarations block-item-list } [TODO]
369///
370/// block-item-list:
371/// block-item
372/// block-item-list block-item
373///
374/// block-item:
375/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000376/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000377/// statement
378/// [OMP] openmp-directive [TODO]
379///
380/// [GNU] label-declarations:
381/// [GNU] label-declaration
382/// [GNU] label-declarations label-declaration
383///
384/// [GNU] label-declaration:
385/// [GNU] '__label__' identifier-list ';'
386///
387/// [OMP] openmp-directive: [TODO]
388/// [OMP] barrier-directive
389/// [OMP] flush-directive
390///
Chris Lattner98414c12007-08-31 21:49:55 +0000391Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
393
Chris Lattner31e05722007-08-26 06:24:45 +0000394 // Enter a scope to hold everything within the compound stmt. Compound
395 // statements can always hold declarations.
396 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397
398 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000399 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400
401 ExitScope();
402 return Body;
403}
404
405
406/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000407/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000408/// consume the '}' at the end of the block. It does not manipulate the scope
409/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000410Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
412
413 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000414 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000415
416 llvm::SmallVector<StmtTy*, 32> Stmts;
417 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000418 StmtResult R;
419 if (Tok.getKind() != tok::kw___extension__) {
420 R = ParseStatementOrDeclaration(false);
421 } else {
422 // __extension__ can start declarations and it can also be a unary
423 // operator for expressions. Consume multiple __extension__ markers here
424 // until we can determine which is which.
425 SourceLocation ExtLoc = ConsumeToken();
426 while (Tok.getKind() == tok::kw___extension__)
427 ConsumeToken();
428
429 // If this is the start of a declaration, parse it as such.
430 if (isDeclarationSpecifier()) {
431 // FIXME: Save the __extension__ on the decl as a node somehow.
432 // FIXME: disable extwarns.
Steve Naroff1b273c42007-09-16 14:56:35 +0000433 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner45a566c2007-08-27 01:01:57 +0000434 } else {
435 // Otherwise this was a unary __extension__ marker. Parse the
436 // subexpression and add the __extension__ unary op.
437 // FIXME: disable extwarns.
438 ExprResult Res = ParseCastExpression(false);
439 if (Res.isInvalid) {
440 SkipUntil(tok::semi);
441 continue;
442 }
443
444 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000445 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000446 if (Res.isInvalid)
447 continue;
448
449 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
450 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000451 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000452 }
453 }
454
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 if (!R.isInvalid && R.Val)
456 Stmts.push_back(R.Val);
457 }
458
459 // We broke out of the while loop because we found a '}' or EOF.
460 if (Tok.getKind() != tok::r_brace) {
461 Diag(Tok, diag::err_expected_rbrace);
462 return 0;
463 }
464
465 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000466 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000467 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000468}
469
470/// ParseIfStatement
471/// if-statement: [C99 6.8.4.1]
472/// 'if' '(' expression ')' statement
473/// 'if' '(' expression ')' statement 'else' statement
474///
475Parser::StmtResult Parser::ParseIfStatement() {
476 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
477 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
478
479 if (Tok.getKind() != tok::l_paren) {
480 Diag(Tok, diag::err_expected_lparen_after, "if");
481 SkipUntil(tok::semi);
482 return true;
483 }
484
Chris Lattner22153252007-08-26 23:08:06 +0000485 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
486 // the case for C90.
487 if (getLang().C99)
488 EnterScope(Scope::DeclScope);
489
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 // Parse the condition.
491 ExprResult CondExp = ParseSimpleParenExpression();
492 if (CondExp.isInvalid) {
493 SkipUntil(tok::semi);
Chris Lattner22153252007-08-26 23:08:06 +0000494 if (getLang().C99)
495 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 return true;
497 }
498
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 this
501 // if the body isn't a compound statement to avoid push/pop in common cases.
502 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000503 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 // Read the if condition.
506 StmtResult CondStmt = ParseStatement();
507
508 // Broken substmt shouldn't prevent the label from being added to the AST.
509 if (CondStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000510 CondStmt = Actions.ActOnNullStmt(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000511
Chris Lattnera36ce712007-08-22 05:16:28 +0000512 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000513 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000514
515 // If it has an else, parse it.
516 SourceLocation ElseLoc;
517 StmtResult ElseStmt(false);
518 if (Tok.getKind() == tok::kw_else) {
519 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000520
Chris Lattner0ecea032007-08-22 05:28:50 +0000521 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000522 // there is no compound stmt. C90 does not have this clause. We only do
523 // this if the body isn't a compound statement to avoid push/pop in common
524 // cases.
525 NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000526 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000527
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000529
530 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000531 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532
533 if (ElseStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000534 ElseStmt = Actions.ActOnNullStmt(ElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 }
536
Chris Lattner22153252007-08-26 23:08:06 +0000537 if (getLang().C99)
538 ExitScope();
539
Steve Naroff1b273c42007-09-16 14:56:35 +0000540 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 ElseLoc, ElseStmt.Val);
542}
543
544/// ParseSwitchStatement
545/// switch-statement:
546/// 'switch' '(' expression ')' statement
547Parser::StmtResult Parser::ParseSwitchStatement() {
548 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
549 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
550
551 if (Tok.getKind() != tok::l_paren) {
552 Diag(Tok, diag::err_expected_lparen_after, "switch");
553 SkipUntil(tok::semi);
554 return true;
555 }
Chris Lattner22153252007-08-26 23:08:06 +0000556
557 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
558 // not the case for C90. Start the switch scope.
559 if (getLang().C99)
560 EnterScope(Scope::BreakScope|Scope::DeclScope);
561 else
562 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
564 // Parse the condition.
565 ExprResult Cond = ParseSimpleParenExpression();
566
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000567 if (Cond.isInvalid) {
568 ExitScope();
569 return true;
570 }
571
Steve Naroff1b273c42007-09-16 14:56:35 +0000572 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000573
Chris Lattner0ecea032007-08-22 05:28:50 +0000574 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000575 // there is no compound stmt. C90 does not have this clause. We only do this
576 // if the body isn't a compound statement to avoid push/pop in common cases.
577 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000578 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000579
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 // Read the body statement.
581 StmtResult Body = ParseStatement();
582
Chris Lattner0ecea032007-08-22 05:28:50 +0000583 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000584 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000585
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000586 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000587 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000588 // FIXME: Remove the case statement list from the Switch statement.
589 }
590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 ExitScope();
592
Steve Naroff1b273c42007-09-16 14:56:35 +0000593 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594}
595
596/// ParseWhileStatement
597/// while-statement: [C99 6.8.5.1]
598/// 'while' '(' expression ')' statement
599Parser::StmtResult Parser::ParseWhileStatement() {
600 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
601 SourceLocation WhileLoc = Tok.getLocation();
602 ConsumeToken(); // eat the 'while'.
603
604 if (Tok.getKind() != tok::l_paren) {
605 Diag(Tok, diag::err_expected_lparen_after, "while");
606 SkipUntil(tok::semi);
607 return true;
608 }
609
Chris Lattner22153252007-08-26 23:08:06 +0000610 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
611 // the case for C90. Start the loop scope.
612 if (getLang().C99)
613 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
614 else
615 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000616
617 // Parse the condition.
618 ExprResult Cond = ParseSimpleParenExpression();
619
Chris Lattner0ecea032007-08-22 05:28:50 +0000620 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000621 // there is no compound stmt. C90 does not have this clause. We only do this
622 // if the body isn't a compound statement to avoid push/pop in common cases.
623 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000624 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // Read the body statement.
627 StmtResult Body = ParseStatement();
628
Chris Lattner0ecea032007-08-22 05:28:50 +0000629 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000630 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000631
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 ExitScope();
633
634 if (Cond.isInvalid || Body.isInvalid) return true;
635
Steve Naroff1b273c42007-09-16 14:56:35 +0000636 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637}
638
639/// ParseDoStatement
640/// do-statement: [C99 6.8.5.2]
641/// 'do' statement 'while' '(' expression ')' ';'
642/// Note: this lets the caller parse the end ';'.
643Parser::StmtResult Parser::ParseDoStatement() {
644 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
645 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
646
Chris Lattner22153252007-08-26 23:08:06 +0000647 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
648 // the case for C90. Start the loop scope.
649 if (getLang().C99)
650 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
651 else
652 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653
Chris Lattner0ecea032007-08-22 05:28:50 +0000654 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000655 // there is no compound stmt. C90 does not have this clause. We only do this
656 // if the body isn't a compound statement to avoid push/pop in common cases.
657 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000658 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000659
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 // Read the body statement.
661 StmtResult Body = ParseStatement();
662
Chris Lattner0ecea032007-08-22 05:28:50 +0000663 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000664 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000665
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 if (Tok.getKind() != tok::kw_while) {
667 ExitScope();
668 Diag(Tok, diag::err_expected_while);
669 Diag(DoLoc, diag::err_matching, "do");
670 SkipUntil(tok::semi);
671 return true;
672 }
673 SourceLocation WhileLoc = ConsumeToken();
674
675 if (Tok.getKind() != tok::l_paren) {
676 ExitScope();
677 Diag(Tok, diag::err_expected_lparen_after, "do/while");
678 SkipUntil(tok::semi);
679 return true;
680 }
681
682 // Parse the condition.
683 ExprResult Cond = ParseSimpleParenExpression();
684
685 ExitScope();
686
687 if (Cond.isInvalid || Body.isInvalid) return true;
688
Steve Naroff1b273c42007-09-16 14:56:35 +0000689 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000690}
691
692/// ParseForStatement
693/// for-statement: [C99 6.8.5.3]
694/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
695/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
696Parser::StmtResult Parser::ParseForStatement() {
697 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
698 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
699
700 if (Tok.getKind() != tok::l_paren) {
701 Diag(Tok, diag::err_expected_lparen_after, "for");
702 SkipUntil(tok::semi);
703 return true;
704 }
705
Chris Lattner22153252007-08-26 23:08:06 +0000706 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
707 // the case for C90. Start the loop scope.
708 if (getLang().C99)
709 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
710 else
711 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712
713 SourceLocation LParenLoc = ConsumeParen();
714 ExprResult Value;
715
716 StmtTy *FirstPart = 0;
717 ExprTy *SecondPart = 0;
718 StmtTy *ThirdPart = 0;
719
720 // Parse the first part of the for specifier.
721 if (Tok.getKind() == tok::semi) { // for (;
722 // no first part, eat the ';'.
723 ConsumeToken();
724 } else if (isDeclarationSpecifier()) { // for (int X = 4;
725 // Parse declaration, which eats the ';'.
726 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
727 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
728 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff1b273c42007-09-16 14:56:35 +0000729 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
731 } else {
732 Value = ParseExpression();
733
734 // Turn the expression into a stmt.
735 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000736 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 if (!R.isInvalid)
738 FirstPart = R.Val;
739 }
740
741 if (Tok.getKind() == tok::semi) {
742 ConsumeToken();
743 } else {
744 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
745 SkipUntil(tok::semi);
746 }
747 }
748
749 // Parse the second part of the for specifier.
750 if (Tok.getKind() == tok::semi) { // for (...;;
751 // no second part.
752 Value = ExprResult();
753 } else {
754 Value = ParseExpression();
755 if (!Value.isInvalid)
756 SecondPart = Value.Val;
757 }
758
759 if (Tok.getKind() == tok::semi) {
760 ConsumeToken();
761 } else {
762 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
763 SkipUntil(tok::semi);
764 }
765
766 // Parse the third part of the for specifier.
767 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
768 // no third part.
769 Value = ExprResult();
770 } else {
771 Value = ParseExpression();
772 if (!Value.isInvalid) {
773 // Turn the expression into a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000774 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 if (!R.isInvalid)
776 ThirdPart = R.Val;
777 }
778 }
779
780 // Match the ')'.
781 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
782
Chris Lattner0ecea032007-08-22 05:28:50 +0000783 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000784 // there is no compound stmt. C90 does not have this clause. We only do this
785 // if the body isn't a compound statement to avoid push/pop in common cases.
786 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner31e05722007-08-26 06:24:45 +0000787 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000788
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 // Read the body statement.
790 StmtResult Body = ParseStatement();
791
Chris Lattner0ecea032007-08-22 05:28:50 +0000792 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000793 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 // Leave the for-scope.
796 ExitScope();
797
798 if (Body.isInvalid)
799 return Body;
800
Steve Naroff1b273c42007-09-16 14:56:35 +0000801 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 ThirdPart, RParenLoc, Body.Val);
803}
804
805/// ParseGotoStatement
806/// jump-statement:
807/// 'goto' identifier ';'
808/// [GNU] 'goto' '*' expression ';'
809///
810/// Note: this lets the caller parse the end ';'.
811///
812Parser::StmtResult Parser::ParseGotoStatement() {
813 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
814 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
815
816 StmtResult Res;
817 if (Tok.getKind() == tok::identifier) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000818 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 Tok.getIdentifierInfo());
820 ConsumeToken();
821 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
822 // GNU indirect goto extension.
823 Diag(Tok, diag::ext_gnu_indirect_goto);
824 SourceLocation StarLoc = ConsumeToken();
825 ExprResult R = ParseExpression();
826 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
827 SkipUntil(tok::semi, false, true);
828 return true;
829 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000830 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000831 } else {
832 Diag(Tok, diag::err_expected_ident);
833 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000835
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 return Res;
837}
838
839/// ParseContinueStatement
840/// jump-statement:
841/// 'continue' ';'
842///
843/// Note: this lets the caller parse the end ';'.
844///
845Parser::StmtResult Parser::ParseContinueStatement() {
846 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000847 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848}
849
850/// ParseBreakStatement
851/// jump-statement:
852/// 'break' ';'
853///
854/// Note: this lets the caller parse the end ';'.
855///
856Parser::StmtResult Parser::ParseBreakStatement() {
857 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000858 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859}
860
861/// ParseReturnStatement
862/// jump-statement:
863/// 'return' expression[opt] ';'
864Parser::StmtResult Parser::ParseReturnStatement() {
865 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
866 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
867
868 ExprResult R(0);
869 if (Tok.getKind() != tok::semi) {
870 R = ParseExpression();
871 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
872 SkipUntil(tok::semi, false, true);
873 return true;
874 }
875 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000876 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000877}
878
879/// ParseAsmStatement - Parse a GNU extended asm statement.
880/// [GNU] asm-statement:
881/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
882///
883/// [GNU] asm-argument:
884/// asm-string-literal
885/// asm-string-literal ':' asm-operands[opt]
886/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
887/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
888/// ':' asm-clobbers
889///
890/// [GNU] asm-clobbers:
891/// asm-string-literal
892/// asm-clobbers ',' asm-string-literal
893///
894Parser::StmtResult Parser::ParseAsmStatement() {
895 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
896 ConsumeToken();
897
898 DeclSpec DS;
899 SourceLocation Loc = Tok.getLocation();
900 ParseTypeQualifierListOpt(DS);
901
902 // GNU asms accept, but warn, about type-qualifiers other than volatile.
903 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
904 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
905 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
906 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
907
908 // Remember if this was a volatile asm.
909 //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
910
911 if (Tok.getKind() != tok::l_paren) {
912 Diag(Tok, diag::err_expected_lparen_after, "asm");
913 SkipUntil(tok::r_paren);
914 return true;
915 }
916 Loc = ConsumeParen();
917
918 ParseAsmStringLiteral();
919
920 // Parse Outputs, if present.
921 ParseAsmOperandsOpt();
922
923 // Parse Inputs, if present.
924 ParseAsmOperandsOpt();
925
926 // Parse the clobbers, if present.
927 if (Tok.getKind() == tok::colon) {
928 ConsumeToken();
929
930 if (isTokenStringLiteral()) {
931 // Parse the asm-string list for clobbers.
932 while (1) {
933 ParseAsmStringLiteral();
934
935 if (Tok.getKind() != tok::comma) break;
936 ConsumeToken();
937 }
938 }
939 }
940
941 MatchRHSPunctuation(tok::r_paren, Loc);
942
943 // FIXME: Implement action for asm parsing.
944 return false;
945}
946
947/// ParseAsmOperands - Parse the asm-operands production as used by
948/// asm-statement. We also parse a leading ':' token. If the leading colon is
949/// not present, we do not parse anything.
950///
951/// [GNU] asm-operands:
952/// asm-operand
953/// asm-operands ',' asm-operand
954///
955/// [GNU] asm-operand:
956/// asm-string-literal '(' expression ')'
957/// '[' identifier ']' asm-string-literal '(' expression ')'
958///
959void Parser::ParseAsmOperandsOpt() {
960 // Only do anything if this operand is present.
961 if (Tok.getKind() != tok::colon) return;
962 ConsumeToken();
963
964 // 'asm-operands' isn't present?
965 if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square)
966 return;
967
968 while (1) {
969 // Read the [id] if present.
970 if (Tok.getKind() == tok::l_square) {
971 SourceLocation Loc = ConsumeBracket();
972
973 if (Tok.getKind() != tok::identifier) {
974 Diag(Tok, diag::err_expected_ident);
975 SkipUntil(tok::r_paren);
976 return;
977 }
978 MatchRHSPunctuation(tok::r_square, Loc);
979 }
980
981 ParseAsmStringLiteral();
982
983 if (Tok.getKind() != tok::l_paren) {
984 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
985 SkipUntil(tok::r_paren);
986 return;
987 }
988
989 // Read the parenthesized expression.
990 ExprResult Res = ParseSimpleParenExpression();
991 if (Res.isInvalid) {
992 SkipUntil(tok::r_paren);
993 return;
994 }
995
996 // Eat the comma and continue parsing if it exists.
997 if (Tok.getKind() != tok::comma) return;
998 ConsumeToken();
999 }
1000}