blob: 1eac2549d7666953adaea8cc8a2cb020060dd07c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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
Fariborz Jahanianc385c902008-01-29 18:21:32 +000039/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000040/// [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 @
Chris Lattner516bd462007-12-27 20:29:42 +000090 if (Tok.isObjCAtKeyword(tok::objc_try))
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 return ParseObjCTryStmt(AtLoc);
Chris Lattner516bd462007-12-27 20:29:42 +000092 else if (Tok.isObjCAtKeyword(tok::objc_throw))
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000093 return ParseObjCThrowStmt(AtLoc);
Fariborz Jahanianc385c902008-01-29 18:21:32 +000094 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
95 return ParseObjCSynchronizedStmt(AtLoc);
Fariborz Jahanianb384d322007-10-04 20:19:06 +000096 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
97 if (Res.isInvalid) {
98 // If the expression is invalid, skip ahead to the next semicolon. Not
99 // doing this opens us up to the possibility of infinite loops if
100 // ParseExpression does not consume any tokens.
101 SkipUntil(tok::semi);
102 return true;
103 }
104 // Otherwise, eat the semicolon.
105 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
106 return Actions.ActOnExprStmt(Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000107 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 default:
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000110 if (!OnlyStatement && isDeclarationSpecifier()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000111 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000112 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 Diag(Tok, diag::err_expected_statement);
114 return true;
115 } else {
116 // expression[opt] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000117 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 if (Res.isInvalid) {
119 // If the expression is invalid, skip ahead to the next semicolon. Not
120 // doing this opens us up to the possibility of infinite loops if
121 // ParseExpression does not consume any tokens.
122 SkipUntil(tok::semi);
123 return true;
124 }
125 // Otherwise, eat the semicolon.
126 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000127 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 }
129
130 case tok::kw_case: // C99 6.8.1: labeled-statement
131 return ParseCaseStatement();
132 case tok::kw_default: // C99 6.8.1: labeled-statement
133 return ParseDefaultStatement();
134
135 case tok::l_brace: // C99 6.8.2: compound-statement
136 return ParseCompoundStatement();
137 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000138 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000139
140 case tok::kw_if: // C99 6.8.4.1: if-statement
141 return ParseIfStatement();
142 case tok::kw_switch: // C99 6.8.4.2: switch-statement
143 return ParseSwitchStatement();
144
145 case tok::kw_while: // C99 6.8.5.1: while-statement
146 return ParseWhileStatement();
147 case tok::kw_do: // C99 6.8.5.2: do-statement
148 Res = ParseDoStatement();
149 SemiError = "do/while loop";
150 break;
151 case tok::kw_for: // C99 6.8.5.3: for-statement
152 return ParseForStatement();
153
154 case tok::kw_goto: // C99 6.8.6.1: goto-statement
155 Res = ParseGotoStatement();
156 SemiError = "goto statement";
157 break;
158 case tok::kw_continue: // C99 6.8.6.2: continue-statement
159 Res = ParseContinueStatement();
160 SemiError = "continue statement";
161 break;
162 case tok::kw_break: // C99 6.8.6.3: break-statement
163 Res = ParseBreakStatement();
164 SemiError = "break statement";
165 break;
166 case tok::kw_return: // C99 6.8.6.4: return-statement
167 Res = ParseReturnStatement();
168 SemiError = "return statement";
169 break;
170
171 case tok::kw_asm:
172 Res = ParseAsmStatement();
173 SemiError = "asm statement";
174 break;
175 }
176
177 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000178 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 ConsumeToken();
180 } else {
181 Diag(Tok, diag::err_expected_semi_after, SemiError);
182 SkipUntil(tok::semi);
183 }
184 return Res;
185}
186
187/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
188/// have a bit of a quandry here. Reading the identifier is necessary to see if
189/// there is a ':' after it. If there is, this is a label, regardless of what
190/// else the identifier can mean. If not, this is either part of a declaration
191/// (if the identifier is a type-name) or part of an expression.
192///
193/// labeled-statement:
194/// identifier ':' statement
195/// [GNU] identifier ':' attributes[opt] statement
196/// declaration (if !OnlyStatement)
197/// expression[opt] ';'
198///
199Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000200 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 "Not an identifier!");
202
Chris Lattnerd2177732007-07-20 16:59:19 +0000203 Token IdentTok = Tok; // Save the whole token.
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 ConsumeToken(); // eat the identifier.
205
206 // identifier ':' statement
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000207 if (Tok.is(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 SourceLocation ColonLoc = ConsumeToken();
209
210 // Read label attributes, if present.
211 DeclTy *AttrList = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000212 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 // TODO: save these somewhere.
214 AttrList = ParseAttributes();
215
216 StmtResult SubStmt = ParseStatement();
217
218 // Broken substmt shouldn't prevent the label from being added to the AST.
219 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000220 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221
Steve Naroff1b273c42007-09-16 14:56:35 +0000222 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 IdentTok.getIdentifierInfo(),
224 ColonLoc, SubStmt.Val);
225 }
226
227 // Check to see if this is a declaration.
228 void *TypeRep;
229 if (!OnlyStatement &&
230 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
231 // Handle this. Warn/disable if in middle of block and !C99.
232 DeclSpec DS;
233
234 // Add the typedef name to the start of the decl-specs.
235 const char *PrevSpec = 0;
236 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
237 IdentTok.getLocation(), PrevSpec,
238 TypeRep);
239 assert(!isInvalid && "First declspec can't be invalid!");
Steve Narofff908a872007-10-30 02:23:23 +0000240 SourceLocation endProtoLoc;
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000241 if (Tok.is(tok::less)) {
242 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000243 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000244 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
245 new llvm::SmallVector<DeclTy *, 8>;
246 DS.setProtocolQualifiers(ProtocolDecl);
247 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
248 &ProtocolRefs[0], ProtocolRefs.size(),
249 *ProtocolDecl);
250 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000251
252 // ParseDeclarationSpecifiers will continue from there.
253 ParseDeclarationSpecifiers(DS);
254
255 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
256 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000257 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 // TODO: emit error on 'int;' or 'const enum foo;'.
259 // if (!DS.isMissingDeclaratorOk()) Diag(...);
260
261 ConsumeToken();
262 // FIXME: Return this as a type decl.
263 return 0;
264 }
265
266 // Parse all the declarators.
267 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
268 ParseDeclarator(DeclaratorInfo);
269
270 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff1b273c42007-09-16 14:56:35 +0000271 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 }
273
274 // Otherwise, this is an expression. Seed it with II and parse it.
275 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
276 if (Res.isInvalid) {
277 SkipUntil(tok::semi);
278 return true;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000279 } else if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 Diag(Tok, diag::err_expected_semi_after, "expression");
281 SkipUntil(tok::semi);
282 return true;
283 } else {
284 ConsumeToken();
285 // Convert expr to a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000286 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 }
288}
289
290/// ParseCaseStatement
291/// labeled-statement:
292/// 'case' constant-expression ':' statement
293/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
294///
295/// Note that this does not parse the 'statement' at the end.
296///
297Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000298 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
300
301 ExprResult LHS = ParseConstantExpression();
302 if (LHS.isInvalid) {
303 SkipUntil(tok::colon);
304 return true;
305 }
306
307 // GNU case range extension.
308 SourceLocation DotDotDotLoc;
309 ExprTy *RHSVal = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000310 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 Diag(Tok, diag::ext_gnu_case_range);
312 DotDotDotLoc = ConsumeToken();
313
314 ExprResult RHS = ParseConstantExpression();
315 if (RHS.isInvalid) {
316 SkipUntil(tok::colon);
317 return true;
318 }
319 RHSVal = RHS.Val;
320 }
321
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000322 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 Diag(Tok, diag::err_expected_colon_after, "'case'");
324 SkipUntil(tok::colon);
325 return true;
326 }
327
328 SourceLocation ColonLoc = ConsumeToken();
329
330 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000331 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 Diag(Tok, diag::err_label_end_of_compound_statement);
333 return true;
334 }
335
336 StmtResult SubStmt = ParseStatement();
337
338 // Broken substmt shouldn't prevent the case from being added to the AST.
339 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000340 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000341
Steve Naroff1b273c42007-09-16 14:56:35 +0000342 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 SubStmt.Val);
344}
345
346/// ParseDefaultStatement
347/// labeled-statement:
348/// 'default' ':' statement
349/// Note that this does not parse the 'statement' at the end.
350///
351Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000352 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
354
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000355 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 Diag(Tok, diag::err_expected_colon_after, "'default'");
357 SkipUntil(tok::colon);
358 return true;
359 }
360
361 SourceLocation ColonLoc = ConsumeToken();
362
363 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000364 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 Diag(Tok, diag::err_label_end_of_compound_statement);
366 return true;
367 }
368
369 StmtResult SubStmt = ParseStatement();
370 if (SubStmt.isInvalid)
371 return true;
372
Steve Naroff1b273c42007-09-16 14:56:35 +0000373 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000374}
375
376
377/// ParseCompoundStatement - Parse a "{}" block.
378///
379/// compound-statement: [C99 6.8.2]
380/// { block-item-list[opt] }
381/// [GNU] { label-declarations block-item-list } [TODO]
382///
383/// block-item-list:
384/// block-item
385/// block-item-list block-item
386///
387/// block-item:
388/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000389/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000390/// statement
391/// [OMP] openmp-directive [TODO]
392///
393/// [GNU] label-declarations:
394/// [GNU] label-declaration
395/// [GNU] label-declarations label-declaration
396///
397/// [GNU] label-declaration:
398/// [GNU] '__label__' identifier-list ';'
399///
400/// [OMP] openmp-directive: [TODO]
401/// [OMP] barrier-directive
402/// [OMP] flush-directive
403///
Chris Lattner98414c12007-08-31 21:49:55 +0000404Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000405 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000406
Chris Lattner31e05722007-08-26 06:24:45 +0000407 // Enter a scope to hold everything within the compound stmt. Compound
408 // statements can always hold declarations.
409 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000410
411 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000412 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000413
414 ExitScope();
415 return Body;
416}
417
418
419/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000420/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000421/// consume the '}' at the end of the block. It does not manipulate the scope
422/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000423Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
425
426 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000427 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000428
429 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000430 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000431 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000432 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000433 R = ParseStatementOrDeclaration(false);
434 } else {
435 // __extension__ can start declarations and it can also be a unary
436 // operator for expressions. Consume multiple __extension__ markers here
437 // until we can determine which is which.
438 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000439 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000440 ConsumeToken();
441
442 // If this is the start of a declaration, parse it as such.
443 if (isDeclarationSpecifier()) {
444 // FIXME: Save the __extension__ on the decl as a node somehow.
445 // FIXME: disable extwarns.
Steve Naroff1b273c42007-09-16 14:56:35 +0000446 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner45a566c2007-08-27 01:01:57 +0000447 } else {
448 // Otherwise this was a unary __extension__ marker. Parse the
449 // subexpression and add the __extension__ unary op.
450 // FIXME: disable extwarns.
451 ExprResult Res = ParseCastExpression(false);
452 if (Res.isInvalid) {
453 SkipUntil(tok::semi);
454 continue;
455 }
456
457 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000458 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000459 if (Res.isInvalid)
460 continue;
461
462 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
463 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000464 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000465 }
466 }
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 if (!R.isInvalid && R.Val)
469 Stmts.push_back(R.Val);
470 }
471
472 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000473 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 Diag(Tok, diag::err_expected_rbrace);
475 return 0;
476 }
477
478 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000479 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000480 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000481}
482
483/// ParseIfStatement
484/// if-statement: [C99 6.8.4.1]
485/// 'if' '(' expression ')' statement
486/// 'if' '(' expression ')' statement 'else' statement
487///
488Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000489 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
491
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000492 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 Diag(Tok, diag::err_expected_lparen_after, "if");
494 SkipUntil(tok::semi);
495 return true;
496 }
497
Chris Lattner22153252007-08-26 23:08:06 +0000498 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
499 // the case for C90.
500 if (getLang().C99)
501 EnterScope(Scope::DeclScope);
502
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 // Parse the condition.
504 ExprResult CondExp = ParseSimpleParenExpression();
505 if (CondExp.isInvalid) {
506 SkipUntil(tok::semi);
Chris Lattner22153252007-08-26 23:08:06 +0000507 if (getLang().C99)
508 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 return true;
510 }
511
Chris Lattner0ecea032007-08-22 05:28:50 +0000512 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000513 // there is no compound stmt. C90 does not have this clause. We only do this
514 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000515 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000516 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000517
Chris Lattnerb96728d2007-10-29 05:08:52 +0000518 // Read the 'then' stmt.
519 SourceLocation ThenStmtLoc = Tok.getLocation();
520 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000521
Chris Lattnera36ce712007-08-22 05:16:28 +0000522 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000523 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000524
525 // If it has an else, parse it.
526 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000527 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000529
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000530 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000532
Chris Lattner0ecea032007-08-22 05:28:50 +0000533 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000534 // there is no compound stmt. C90 does not have this clause. We only do
535 // this if the body isn't a compound statement to avoid push/pop in common
536 // cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000537 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000538 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000539
Chris Lattnerb96728d2007-10-29 05:08:52 +0000540 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000542
543 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000544 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 }
546
Chris Lattner22153252007-08-26 23:08:06 +0000547 if (getLang().C99)
548 ExitScope();
549
Chris Lattnerb96728d2007-10-29 05:08:52 +0000550 // If the then or else stmt is invalid and the other is valid (and present),
551 // make turn the invalid one into a null stmt to avoid dropping the other
552 // part. If both are invalid, return error.
553 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
554 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
555 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
556 // Both invalid, or one is invalid and other is non-present: delete cond and
557 // return error.
558 Actions.DeleteExpr(CondExp.Val);
559 return true;
560 }
561
562 // Now if either are invalid, replace with a ';'.
563 if (ThenStmt.isInvalid)
564 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
565 if (ElseStmt.isInvalid)
566 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
567
568
569
570 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 ElseLoc, ElseStmt.Val);
572}
573
574/// ParseSwitchStatement
575/// switch-statement:
576/// 'switch' '(' expression ')' statement
577Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000578 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
580
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000581 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 Diag(Tok, diag::err_expected_lparen_after, "switch");
583 SkipUntil(tok::semi);
584 return true;
585 }
Chris Lattner22153252007-08-26 23:08:06 +0000586
587 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
588 // not the case for C90. Start the switch scope.
589 if (getLang().C99)
590 EnterScope(Scope::BreakScope|Scope::DeclScope);
591 else
592 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000593
594 // Parse the condition.
595 ExprResult Cond = ParseSimpleParenExpression();
596
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000597 if (Cond.isInvalid) {
598 ExitScope();
599 return true;
600 }
601
Steve Naroff1b273c42007-09-16 14:56:35 +0000602 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000603
Chris Lattner0ecea032007-08-22 05:28:50 +0000604 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000605 // there is no compound stmt. C90 does not have this clause. We only do this
606 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000607 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000608 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 // Read the body statement.
611 StmtResult Body = ParseStatement();
612
Chris Lattner0ecea032007-08-22 05:28:50 +0000613 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000614 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000615
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000616 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000617 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000618 // FIXME: Remove the case statement list from the Switch statement.
619 }
620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 ExitScope();
622
Steve Naroff1b273c42007-09-16 14:56:35 +0000623 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624}
625
626/// ParseWhileStatement
627/// while-statement: [C99 6.8.5.1]
628/// 'while' '(' expression ')' statement
629Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000630 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 SourceLocation WhileLoc = Tok.getLocation();
632 ConsumeToken(); // eat the 'while'.
633
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000634 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 Diag(Tok, diag::err_expected_lparen_after, "while");
636 SkipUntil(tok::semi);
637 return true;
638 }
639
Chris Lattner22153252007-08-26 23:08:06 +0000640 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
641 // the case for C90. Start the loop scope.
642 if (getLang().C99)
643 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
644 else
645 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000646
647 // Parse the condition.
648 ExprResult Cond = ParseSimpleParenExpression();
649
Chris Lattner0ecea032007-08-22 05:28:50 +0000650 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000651 // there is no compound stmt. C90 does not have this clause. We only do this
652 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000653 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000654 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 // Read the body statement.
657 StmtResult Body = ParseStatement();
658
Chris Lattner0ecea032007-08-22 05:28:50 +0000659 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000660 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 ExitScope();
663
664 if (Cond.isInvalid || Body.isInvalid) return true;
665
Steve Naroff1b273c42007-09-16 14:56:35 +0000666 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000667}
668
669/// ParseDoStatement
670/// do-statement: [C99 6.8.5.2]
671/// 'do' statement 'while' '(' expression ')' ';'
672/// Note: this lets the caller parse the end ';'.
673Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000674 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
676
Chris Lattner22153252007-08-26 23:08:06 +0000677 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
678 // the case for C90. Start the loop scope.
679 if (getLang().C99)
680 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
681 else
682 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000683
Chris Lattner0ecea032007-08-22 05:28:50 +0000684 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000685 // there is no compound stmt. C90 does not have this clause. We only do this
686 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000687 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000688 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 // Read the body statement.
691 StmtResult Body = ParseStatement();
692
Chris Lattner0ecea032007-08-22 05:28:50 +0000693 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000694 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000695
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000696 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 ExitScope();
698 Diag(Tok, diag::err_expected_while);
699 Diag(DoLoc, diag::err_matching, "do");
700 SkipUntil(tok::semi);
701 return true;
702 }
703 SourceLocation WhileLoc = ConsumeToken();
704
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000705 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 ExitScope();
707 Diag(Tok, diag::err_expected_lparen_after, "do/while");
708 SkipUntil(tok::semi);
709 return true;
710 }
711
712 // Parse the condition.
713 ExprResult Cond = ParseSimpleParenExpression();
714
715 ExitScope();
716
717 if (Cond.isInvalid || Body.isInvalid) return true;
718
Steve Naroff1b273c42007-09-16 14:56:35 +0000719 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}
721
722/// ParseForStatement
723/// for-statement: [C99 6.8.5.3]
724/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
725/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000726/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
727/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000728Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000729 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
731
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000732 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 Diag(Tok, diag::err_expected_lparen_after, "for");
734 SkipUntil(tok::semi);
735 return true;
736 }
737
Chris Lattner22153252007-08-26 23:08:06 +0000738 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
739 // the case for C90. Start the loop scope.
740 if (getLang().C99)
741 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
742 else
743 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000744
745 SourceLocation LParenLoc = ConsumeParen();
746 ExprResult Value;
747
748 StmtTy *FirstPart = 0;
749 ExprTy *SecondPart = 0;
750 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000751 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000752
753 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000754 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 // no first part, eat the ';'.
756 ConsumeToken();
757 } else if (isDeclarationSpecifier()) { // for (int X = 4;
758 // Parse declaration, which eats the ';'.
759 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
760 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
761 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff1b273c42007-09-16 14:56:35 +0000762 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000764 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000765 ConsumeToken(); // consume 'in'
766 Value = ParseExpression();
767 if (!Value.isInvalid)
768 SecondPart = Value.Val;
769 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 } else {
771 Value = ParseExpression();
772
773 // Turn the expression into a stmt.
774 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000775 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 if (!R.isInvalid)
777 FirstPart = R.Val;
778 }
779
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000780 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000782 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000783 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000784 ConsumeToken(); // consume 'in'
785 Value = ParseExpression();
786 if (!Value.isInvalid)
787 SecondPart = Value.Val;
788 }
789 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
791 SkipUntil(tok::semi);
792 }
793 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000794 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000795 // Parse the second part of the for specifier.
796 if (Tok.is(tok::semi)) { // for (...;;
797 // no second part.
798 Value = ExprResult();
799 } else {
800 Value = ParseExpression();
801 if (!Value.isInvalid)
802 SecondPart = Value.Val;
803 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000804
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000805 if (Tok.is(tok::semi)) {
806 ConsumeToken();
807 } else {
808 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
809 SkipUntil(tok::semi);
810 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000811
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000812 // Parse the third part of the for specifier.
813 if (Tok.is(tok::r_paren)) { // for (...;...;)
814 // no third part.
815 Value = ExprResult();
816 } else {
817 Value = ParseExpression();
818 if (!Value.isInvalid) {
819 // Turn the expression into a stmt.
820 StmtResult R = Actions.ActOnExprStmt(Value.Val);
821 if (!R.isInvalid)
822 ThirdPart = R.Val;
823 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 }
825 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 // Match the ')'.
827 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
828
Chris Lattner0ecea032007-08-22 05:28:50 +0000829 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000830 // there is no compound stmt. C90 does not have this clause. We only do this
831 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000832 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000833 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 // Read the body statement.
836 StmtResult Body = ParseStatement();
837
Chris Lattner0ecea032007-08-22 05:28:50 +0000838 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000839 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000840
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 // Leave the for-scope.
842 ExitScope();
843
844 if (Body.isInvalid)
845 return Body;
846
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000847 if (!ForEach)
848 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
849 SecondPart, ThirdPart, RParenLoc, Body.Val);
850 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000852 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000853}
854
855/// ParseGotoStatement
856/// jump-statement:
857/// 'goto' identifier ';'
858/// [GNU] 'goto' '*' expression ';'
859///
860/// Note: this lets the caller parse the end ';'.
861///
862Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000863 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
865
866 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000867 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000868 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 Tok.getIdentifierInfo());
870 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000871 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 // GNU indirect goto extension.
873 Diag(Tok, diag::ext_gnu_indirect_goto);
874 SourceLocation StarLoc = ConsumeToken();
875 ExprResult R = ParseExpression();
876 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
877 SkipUntil(tok::semi, false, true);
878 return true;
879 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000880 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000881 } else {
882 Diag(Tok, diag::err_expected_ident);
883 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 return Res;
887}
888
889/// ParseContinueStatement
890/// jump-statement:
891/// 'continue' ';'
892///
893/// Note: this lets the caller parse the end ';'.
894///
895Parser::StmtResult Parser::ParseContinueStatement() {
896 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000897 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000898}
899
900/// ParseBreakStatement
901/// jump-statement:
902/// 'break' ';'
903///
904/// Note: this lets the caller parse the end ';'.
905///
906Parser::StmtResult Parser::ParseBreakStatement() {
907 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000908 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000909}
910
911/// ParseReturnStatement
912/// jump-statement:
913/// 'return' expression[opt] ';'
914Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000915 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
917
918 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000919 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 R = ParseExpression();
921 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
922 SkipUntil(tok::semi, false, true);
923 return true;
924 }
925 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000926 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927}
928
929/// ParseAsmStatement - Parse a GNU extended asm statement.
930/// [GNU] asm-statement:
931/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
932///
933/// [GNU] asm-argument:
934/// asm-string-literal
935/// asm-string-literal ':' asm-operands[opt]
936/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
937/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
938/// ':' asm-clobbers
939///
940/// [GNU] asm-clobbers:
941/// asm-string-literal
942/// asm-clobbers ',' asm-string-literal
943///
944Parser::StmtResult Parser::ParseAsmStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000945 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +0000946 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000947
948 DeclSpec DS;
949 SourceLocation Loc = Tok.getLocation();
950 ParseTypeQualifierListOpt(DS);
951
952 // GNU asms accept, but warn, about type-qualifiers other than volatile.
953 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
954 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
955 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
956 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
957
958 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +0000959 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Reid Spencer5f016e22007-07-11 17:01:13 +0000960
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000961 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 Diag(Tok, diag::err_expected_lparen_after, "asm");
963 SkipUntil(tok::r_paren);
964 return true;
965 }
966 Loc = ConsumeParen();
967
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000968 ExprResult AsmString = ParseAsmStringLiteral();
969 if (AsmString.isInvalid)
970 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +0000971
972 llvm::SmallVector<std::string, 4> Names;
973 llvm::SmallVector<ExprTy*, 4> Constraints;
974 llvm::SmallVector<ExprTy*, 4> Exprs;
975
976 // Parse Outputs, if present.
977 ParseAsmOperandsOpt(Names, Constraints, Exprs);
978
979 unsigned NumOutputs = Names.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
981 // Parse Inputs, if present.
Anders Carlssonb235fc22007-11-22 01:36:19 +0000982 ParseAsmOperandsOpt(Names, Constraints, Exprs);
983 assert(Names.size() == Constraints.size() &&
984 Constraints.size() == Exprs.size()
985 && "Input operand size mismatch!");
986
987 unsigned NumInputs = Names.size() - NumOutputs;
988
989 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +0000990
991 // Parse the clobbers, if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000992 if (Tok.is(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 ConsumeToken();
994
Anders Carlssoneecf8472007-11-21 23:27:34 +0000995 // Parse the asm-string list for clobbers.
996 while (1) {
Anders Carlssonb235fc22007-11-22 01:36:19 +0000997 ExprResult Clobber = ParseAsmStringLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000998
Anders Carlssonb235fc22007-11-22 01:36:19 +0000999 if (Clobber.isInvalid)
1000 break;
1001
1002 Clobbers.push_back(Clobber.Val);
1003
Anders Carlssoneecf8472007-11-21 23:27:34 +00001004 if (Tok.isNot(tok::comma)) break;
1005 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 }
1007 }
1008
Chris Lattnerfe795952007-10-29 04:04:16 +00001009 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001010
Anders Carlsson39c47b52007-11-23 23:12:25 +00001011 return Actions.ActOnAsmStmt(AsmLoc, isVolatile, NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001012 &Names[0], &Constraints[0], &Exprs[0],
1013 AsmString.Val,
1014 Clobbers.size(), &Clobbers[0],
1015 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016}
1017
1018/// ParseAsmOperands - Parse the asm-operands production as used by
1019/// asm-statement. We also parse a leading ':' token. If the leading colon is
1020/// not present, we do not parse anything.
1021///
1022/// [GNU] asm-operands:
1023/// asm-operand
1024/// asm-operands ',' asm-operand
1025///
1026/// [GNU] asm-operand:
1027/// asm-string-literal '(' expression ')'
1028/// '[' identifier ']' asm-string-literal '(' expression ')'
1029///
Anders Carlssonb235fc22007-11-22 01:36:19 +00001030void Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
1031 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1032 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 // Only do anything if this operand is present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001034 if (Tok.isNot(tok::colon)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 ConsumeToken();
1036
1037 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001038 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 return;
1040
Anders Carlssonb235fc22007-11-22 01:36:19 +00001041 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001043 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 SourceLocation Loc = ConsumeBracket();
1045
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001046 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 Diag(Tok, diag::err_expected_ident);
1048 SkipUntil(tok::r_paren);
1049 return;
1050 }
Chris Lattner69efba72007-10-29 04:06:22 +00001051
Anders Carlssonb235fc22007-11-22 01:36:19 +00001052 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001053 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001054
1055 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001057 } else
1058 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001059
Anders Carlssonb235fc22007-11-22 01:36:19 +00001060 ExprResult Constraint = ParseAsmStringLiteral();
1061 if (Constraint.isInvalid) {
1062 SkipUntil(tok::r_paren);
1063 return;
1064 }
1065 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001066
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001067 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1069 SkipUntil(tok::r_paren);
1070 return;
1071 }
1072
1073 // Read the parenthesized expression.
1074 ExprResult Res = ParseSimpleParenExpression();
1075 if (Res.isInvalid) {
1076 SkipUntil(tok::r_paren);
1077 return;
1078 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001079 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 // Eat the comma and continue parsing if it exists.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001081 if (Tok.isNot(tok::comma)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 ConsumeToken();
1083 }
1084}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001085
1086Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1087 SourceLocation L, SourceLocation R) {
1088 // Do not enter a scope for the brace, as the arguments are in the same scope
1089 // (the function body) as the body itself. Instead, just read the statement
1090 // list and put it into a CompoundStmt for safe keeping.
1091 StmtResult FnBody = ParseCompoundStatementBody();
1092
1093 // If the function body could not be parsed, make a bogus compoundstmt.
1094 if (FnBody.isInvalid)
1095 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1096
1097 // Leave the function body scope.
1098 ExitScope();
1099
Steve Naroffd6d054d2007-11-11 23:20:51 +00001100 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001101}