blob: 160701ee9c78ee48d4144898005fe50c390e08bd [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"
Steve Naroffb746ce82008-02-07 23:24:32 +000017#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Parse/DeclSpec.h"
19#include "clang/Parse/Scope.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.8: Statements and Blocks.
24//===----------------------------------------------------------------------===//
25
26/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
27/// StatementOrDeclaration:
28/// statement
29/// declaration
30///
31/// statement:
32/// labeled-statement
33/// compound-statement
34/// expression-statement
35/// selection-statement
36/// iteration-statement
37/// jump-statement
Fariborz Jahanianb384d322007-10-04 20:19:06 +000038/// [OBC] objc-throw-statement
39/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000040/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000041/// [GNU] asm-statement
42/// [OMP] openmp-construct [TODO]
43///
44/// labeled-statement:
45/// identifier ':' statement
46/// 'case' constant-expression ':' statement
47/// 'default' ':' statement
48///
49/// selection-statement:
50/// if-statement
51/// switch-statement
52///
53/// iteration-statement:
54/// while-statement
55/// do-statement
56/// for-statement
57///
58/// expression-statement:
59/// expression[opt] ';'
60///
61/// jump-statement:
62/// 'goto' identifier ';'
63/// 'continue' ';'
64/// 'break' ';'
65/// 'return' expression[opt] ';'
66/// [GNU] 'goto' '*' expression ';'
67///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000068/// [OBC] objc-throw-statement:
69/// [OBC] '@' 'throw' expression ';'
70/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000071///
72Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
73 const char *SemiError = 0;
74 Parser::StmtResult Res;
75
76 // Cases in this switch statement should fall through if the parser expects
77 // the token to end in a semicolon (in which case SemiError should be set),
78 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000079 tok::TokenKind Kind = Tok.getKind();
80 SourceLocation AtLoc;
81 switch (Kind) {
Reid Spencer5f016e22007-07-11 17:01:13 +000082 case tok::identifier: // C99 6.8.1: labeled-statement
83 // identifier ':' statement
84 // declaration (if !OnlyStatement)
85 // expression[opt] ';'
86 return ParseIdentifierStatement(OnlyStatement);
87
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000088 case tok::at: // May be a @try or @throw statement
89 {
90 AtLoc = ConsumeToken(); // consume @
Steve Naroff64515f32008-02-05 21:27:35 +000091 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 default:
Fariborz Jahanianb384d322007-10-04 20:19:06 +000095 if (!OnlyStatement && isDeclarationSpecifier()) {
Chris Lattner81c018d2008-03-13 06:29:04 +000096 SourceLocation DeclStart = Tok.getLocation();
97 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
98 // FIXME: Pass in the right location for the end of the declstmt.
99 return Actions.ActOnDeclStmt(Res, DeclStart, SourceLocation());
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000100 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 Diag(Tok, diag::err_expected_statement);
102 return true;
103 } else {
104 // expression[opt] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000105 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 if (Res.isInvalid) {
107 // If the expression is invalid, skip ahead to the next semicolon. Not
108 // doing this opens us up to the possibility of infinite loops if
109 // ParseExpression does not consume any tokens.
110 SkipUntil(tok::semi);
111 return true;
112 }
113 // Otherwise, eat the semicolon.
114 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000115 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 }
117
118 case tok::kw_case: // C99 6.8.1: labeled-statement
119 return ParseCaseStatement();
120 case tok::kw_default: // C99 6.8.1: labeled-statement
121 return ParseDefaultStatement();
122
123 case tok::l_brace: // C99 6.8.2: compound-statement
124 return ParseCompoundStatement();
125 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000126 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000127
128 case tok::kw_if: // C99 6.8.4.1: if-statement
129 return ParseIfStatement();
130 case tok::kw_switch: // C99 6.8.4.2: switch-statement
131 return ParseSwitchStatement();
132
133 case tok::kw_while: // C99 6.8.5.1: while-statement
134 return ParseWhileStatement();
135 case tok::kw_do: // C99 6.8.5.2: do-statement
136 Res = ParseDoStatement();
137 SemiError = "do/while loop";
138 break;
139 case tok::kw_for: // C99 6.8.5.3: for-statement
140 return ParseForStatement();
141
142 case tok::kw_goto: // C99 6.8.6.1: goto-statement
143 Res = ParseGotoStatement();
144 SemiError = "goto statement";
145 break;
146 case tok::kw_continue: // C99 6.8.6.2: continue-statement
147 Res = ParseContinueStatement();
148 SemiError = "continue statement";
149 break;
150 case tok::kw_break: // C99 6.8.6.3: break-statement
151 Res = ParseBreakStatement();
152 SemiError = "break statement";
153 break;
154 case tok::kw_return: // C99 6.8.6.4: return-statement
155 Res = ParseReturnStatement();
156 SemiError = "return statement";
157 break;
158
159 case tok::kw_asm:
Steve Naroffd62701b2008-02-07 03:50:06 +0000160 bool msAsm = false;
161 Res = ParseAsmStatement(msAsm);
162 if (msAsm) return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 SemiError = "asm statement";
164 break;
165 }
166
167 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000168 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 ConsumeToken();
170 } else {
171 Diag(Tok, diag::err_expected_semi_after, SemiError);
172 SkipUntil(tok::semi);
173 }
174 return Res;
175}
176
177/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
178/// have a bit of a quandry here. Reading the identifier is necessary to see if
179/// there is a ':' after it. If there is, this is a label, regardless of what
180/// else the identifier can mean. If not, this is either part of a declaration
181/// (if the identifier is a type-name) or part of an expression.
182///
183/// labeled-statement:
184/// identifier ':' statement
185/// [GNU] identifier ':' attributes[opt] statement
186/// declaration (if !OnlyStatement)
187/// expression[opt] ';'
188///
189Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000190 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 "Not an identifier!");
192
Chris Lattnerd2177732007-07-20 16:59:19 +0000193 Token IdentTok = Tok; // Save the whole token.
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 ConsumeToken(); // eat the identifier.
195
196 // identifier ':' statement
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000197 if (Tok.is(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 SourceLocation ColonLoc = ConsumeToken();
199
200 // Read label attributes, if present.
201 DeclTy *AttrList = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000202 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 // TODO: save these somewhere.
204 AttrList = ParseAttributes();
205
206 StmtResult SubStmt = ParseStatement();
207
208 // Broken substmt shouldn't prevent the label from being added to the AST.
209 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000210 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211
Steve Naroff1b273c42007-09-16 14:56:35 +0000212 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 IdentTok.getIdentifierInfo(),
214 ColonLoc, SubStmt.Val);
215 }
216
217 // Check to see if this is a declaration.
218 void *TypeRep;
219 if (!OnlyStatement &&
220 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
221 // Handle this. Warn/disable if in middle of block and !C99.
222 DeclSpec DS;
223
224 // Add the typedef name to the start of the decl-specs.
225 const char *PrevSpec = 0;
226 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
227 IdentTok.getLocation(), PrevSpec,
228 TypeRep);
229 assert(!isInvalid && "First declspec can't be invalid!");
Steve Narofff908a872007-10-30 02:23:23 +0000230 SourceLocation endProtoLoc;
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000231 if (Tok.is(tok::less)) {
232 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000233 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000234 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
235 new llvm::SmallVector<DeclTy *, 8>;
236 DS.setProtocolQualifiers(ProtocolDecl);
237 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
238 &ProtocolRefs[0], ProtocolRefs.size(),
239 *ProtocolDecl);
240 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000241
242 // ParseDeclarationSpecifiers will continue from there.
243 ParseDeclarationSpecifiers(DS);
244
245 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
246 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000247 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // TODO: emit error on 'int;' or 'const enum foo;'.
249 // if (!DS.isMissingDeclaratorOk()) Diag(...);
250
251 ConsumeToken();
252 // FIXME: Return this as a type decl.
253 return 0;
254 }
255
256 // Parse all the declarators.
257 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
258 ParseDeclarator(DeclaratorInfo);
259
260 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner81c018d2008-03-13 06:29:04 +0000261 if (!Decl) return 0;
262 return Actions.ActOnDeclStmt(Decl, DS.getSourceRange().getBegin(),
263 DeclaratorInfo.getSourceRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 }
265
266 // Otherwise, this is an expression. Seed it with II and parse it.
267 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
268 if (Res.isInvalid) {
269 SkipUntil(tok::semi);
270 return true;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000271 } else if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 Diag(Tok, diag::err_expected_semi_after, "expression");
273 SkipUntil(tok::semi);
274 return true;
275 } else {
276 ConsumeToken();
277 // Convert expr to a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000278 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 }
280}
281
282/// ParseCaseStatement
283/// labeled-statement:
284/// 'case' constant-expression ':' statement
285/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
286///
287/// Note that this does not parse the 'statement' at the end.
288///
289Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000290 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
292
293 ExprResult LHS = ParseConstantExpression();
294 if (LHS.isInvalid) {
295 SkipUntil(tok::colon);
296 return true;
297 }
298
299 // GNU case range extension.
300 SourceLocation DotDotDotLoc;
301 ExprTy *RHSVal = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000302 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 Diag(Tok, diag::ext_gnu_case_range);
304 DotDotDotLoc = ConsumeToken();
305
306 ExprResult RHS = ParseConstantExpression();
307 if (RHS.isInvalid) {
308 SkipUntil(tok::colon);
309 return true;
310 }
311 RHSVal = RHS.Val;
312 }
313
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000314 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 Diag(Tok, diag::err_expected_colon_after, "'case'");
316 SkipUntil(tok::colon);
317 return true;
318 }
319
320 SourceLocation ColonLoc = ConsumeToken();
321
322 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000323 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 Diag(Tok, diag::err_label_end_of_compound_statement);
325 return true;
326 }
327
328 StmtResult SubStmt = ParseStatement();
329
330 // Broken substmt shouldn't prevent the case from being added to the AST.
331 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000332 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000333
Steve Naroff1b273c42007-09-16 14:56:35 +0000334 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 SubStmt.Val);
336}
337
338/// ParseDefaultStatement
339/// labeled-statement:
340/// 'default' ':' statement
341/// Note that this does not parse the 'statement' at the end.
342///
343Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000344 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
346
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000347 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 Diag(Tok, diag::err_expected_colon_after, "'default'");
349 SkipUntil(tok::colon);
350 return true;
351 }
352
353 SourceLocation ColonLoc = ConsumeToken();
354
355 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000356 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 Diag(Tok, diag::err_label_end_of_compound_statement);
358 return true;
359 }
360
361 StmtResult SubStmt = ParseStatement();
362 if (SubStmt.isInvalid)
363 return true;
364
Steve Naroff1b273c42007-09-16 14:56:35 +0000365 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000366}
367
368
369/// ParseCompoundStatement - Parse a "{}" block.
370///
371/// compound-statement: [C99 6.8.2]
372/// { block-item-list[opt] }
373/// [GNU] { label-declarations block-item-list } [TODO]
374///
375/// block-item-list:
376/// block-item
377/// block-item-list block-item
378///
379/// block-item:
380/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000381/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000382/// statement
383/// [OMP] openmp-directive [TODO]
384///
385/// [GNU] label-declarations:
386/// [GNU] label-declaration
387/// [GNU] label-declarations label-declaration
388///
389/// [GNU] label-declaration:
390/// [GNU] '__label__' identifier-list ';'
391///
392/// [OMP] openmp-directive: [TODO]
393/// [OMP] barrier-directive
394/// [OMP] flush-directive
395///
Chris Lattner98414c12007-08-31 21:49:55 +0000396Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000397 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000398
Chris Lattner31e05722007-08-26 06:24:45 +0000399 // Enter a scope to hold everything within the compound stmt. Compound
400 // statements can always hold declarations.
401 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000402
403 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000404 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000405
406 ExitScope();
407 return Body;
408}
409
410
411/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000412/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000413/// consume the '}' at the end of the block. It does not manipulate the scope
414/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000415Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
417
418 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000419 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000420
421 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000422 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000423 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000424 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000425 R = ParseStatementOrDeclaration(false);
426 } else {
427 // __extension__ can start declarations and it can also be a unary
428 // operator for expressions. Consume multiple __extension__ markers here
429 // until we can determine which is which.
430 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000431 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000432 ConsumeToken();
433
434 // If this is the start of a declaration, parse it as such.
435 if (isDeclarationSpecifier()) {
436 // FIXME: Save the __extension__ on the decl as a node somehow.
437 // FIXME: disable extwarns.
Chris Lattner81c018d2008-03-13 06:29:04 +0000438 SourceLocation DeclStart = Tok.getLocation();
439 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
440 // FIXME: Pass in the right location for the end of the declstmt.
441 R = Actions.ActOnDeclStmt(Res, DeclStart, SourceLocation());
Chris Lattner45a566c2007-08-27 01:01:57 +0000442 } else {
443 // Otherwise this was a unary __extension__ marker. Parse the
444 // subexpression and add the __extension__ unary op.
445 // FIXME: disable extwarns.
446 ExprResult Res = ParseCastExpression(false);
447 if (Res.isInvalid) {
448 SkipUntil(tok::semi);
449 continue;
450 }
451
452 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000453 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000454 if (Res.isInvalid)
455 continue;
456
457 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
458 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000459 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000460 }
461 }
462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 if (!R.isInvalid && R.Val)
464 Stmts.push_back(R.Val);
465 }
466
467 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000468 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000470 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 }
472
473 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000474 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000475 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000476}
477
478/// ParseIfStatement
479/// if-statement: [C99 6.8.4.1]
480/// 'if' '(' expression ')' statement
481/// 'if' '(' expression ')' statement 'else' statement
482///
483Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000484 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
486
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000487 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 Diag(Tok, diag::err_expected_lparen_after, "if");
489 SkipUntil(tok::semi);
490 return true;
491 }
492
Chris Lattner22153252007-08-26 23:08:06 +0000493 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
494 // the case for C90.
495 if (getLang().C99)
496 EnterScope(Scope::DeclScope);
497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 // Parse the condition.
499 ExprResult CondExp = ParseSimpleParenExpression();
500 if (CondExp.isInvalid) {
501 SkipUntil(tok::semi);
Chris Lattner22153252007-08-26 23:08:06 +0000502 if (getLang().C99)
503 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 return true;
505 }
506
Chris Lattner0ecea032007-08-22 05:28:50 +0000507 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000508 // there is no compound stmt. C90 does not have this clause. We only do this
509 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000510 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000511 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000512
Chris Lattnerb96728d2007-10-29 05:08:52 +0000513 // Read the 'then' stmt.
514 SourceLocation ThenStmtLoc = Tok.getLocation();
515 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000516
Chris Lattnera36ce712007-08-22 05:16:28 +0000517 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000518 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
520 // If it has an else, parse it.
521 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000522 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000524
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000525 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000527
Chris Lattner0ecea032007-08-22 05:28:50 +0000528 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000529 // there is no compound stmt. C90 does not have this clause. We only do
530 // this if the body isn't a compound statement to avoid push/pop in common
531 // cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000532 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000533 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000534
Chris Lattnerb96728d2007-10-29 05:08:52 +0000535 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000537
538 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000539 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 }
541
Chris Lattner22153252007-08-26 23:08:06 +0000542 if (getLang().C99)
543 ExitScope();
544
Chris Lattnerb96728d2007-10-29 05:08:52 +0000545 // If the then or else stmt is invalid and the other is valid (and present),
546 // make turn the invalid one into a null stmt to avoid dropping the other
547 // part. If both are invalid, return error.
548 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
549 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
550 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
551 // Both invalid, or one is invalid and other is non-present: delete cond and
552 // return error.
553 Actions.DeleteExpr(CondExp.Val);
554 return true;
555 }
556
557 // Now if either are invalid, replace with a ';'.
558 if (ThenStmt.isInvalid)
559 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
560 if (ElseStmt.isInvalid)
561 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
562
Chris Lattnerb96728d2007-10-29 05:08:52 +0000563 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 ElseLoc, ElseStmt.Val);
565}
566
567/// ParseSwitchStatement
568/// switch-statement:
569/// 'switch' '(' expression ')' statement
570Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000571 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
573
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000574 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 Diag(Tok, diag::err_expected_lparen_after, "switch");
576 SkipUntil(tok::semi);
577 return true;
578 }
Chris Lattner22153252007-08-26 23:08:06 +0000579
580 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
581 // not the case for C90. Start the switch scope.
582 if (getLang().C99)
583 EnterScope(Scope::BreakScope|Scope::DeclScope);
584 else
585 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000586
587 // Parse the condition.
588 ExprResult Cond = ParseSimpleParenExpression();
589
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000590 if (Cond.isInvalid) {
591 ExitScope();
592 return true;
593 }
594
Steve Naroff1b273c42007-09-16 14:56:35 +0000595 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000596
Chris Lattner0ecea032007-08-22 05:28:50 +0000597 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000598 // there is no compound stmt. C90 does not have this clause. We only do this
599 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000600 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000601 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 // Read the body statement.
604 StmtResult Body = ParseStatement();
605
Chris Lattner0ecea032007-08-22 05:28:50 +0000606 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000607 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000608
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000609 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000610 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000611 // FIXME: Remove the case statement list from the Switch statement.
612 }
613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 ExitScope();
615
Steve Naroff1b273c42007-09-16 14:56:35 +0000616 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}
618
619/// ParseWhileStatement
620/// while-statement: [C99 6.8.5.1]
621/// 'while' '(' expression ')' statement
622Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000623 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 SourceLocation WhileLoc = Tok.getLocation();
625 ConsumeToken(); // eat the 'while'.
626
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000627 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 Diag(Tok, diag::err_expected_lparen_after, "while");
629 SkipUntil(tok::semi);
630 return true;
631 }
632
Chris Lattner22153252007-08-26 23:08:06 +0000633 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
634 // the case for C90. Start the loop scope.
635 if (getLang().C99)
636 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
637 else
638 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639
640 // Parse the condition.
641 ExprResult Cond = ParseSimpleParenExpression();
642
Chris Lattner0ecea032007-08-22 05:28:50 +0000643 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000644 // there is no compound stmt. C90 does not have this clause. We only do this
645 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000646 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000647 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 // Read the body statement.
650 StmtResult Body = ParseStatement();
651
Chris Lattner0ecea032007-08-22 05:28:50 +0000652 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000653 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 ExitScope();
656
657 if (Cond.isInvalid || Body.isInvalid) return true;
658
Steve Naroff1b273c42007-09-16 14:56:35 +0000659 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000660}
661
662/// ParseDoStatement
663/// do-statement: [C99 6.8.5.2]
664/// 'do' statement 'while' '(' expression ')' ';'
665/// Note: this lets the caller parse the end ';'.
666Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000667 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
669
Chris Lattner22153252007-08-26 23:08:06 +0000670 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
671 // the case for C90. Start the loop scope.
672 if (getLang().C99)
673 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
674 else
675 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000676
Chris Lattner0ecea032007-08-22 05:28:50 +0000677 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000678 // there is no compound stmt. C90 does not have this clause. We only do this
679 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000680 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000681 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 // Read the body statement.
684 StmtResult Body = ParseStatement();
685
Chris Lattner0ecea032007-08-22 05:28:50 +0000686 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000687 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000688
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000689 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 ExitScope();
691 Diag(Tok, diag::err_expected_while);
692 Diag(DoLoc, diag::err_matching, "do");
693 SkipUntil(tok::semi);
694 return true;
695 }
696 SourceLocation WhileLoc = ConsumeToken();
697
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000698 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 ExitScope();
700 Diag(Tok, diag::err_expected_lparen_after, "do/while");
701 SkipUntil(tok::semi);
702 return true;
703 }
704
705 // Parse the condition.
706 ExprResult Cond = ParseSimpleParenExpression();
707
708 ExitScope();
709
710 if (Cond.isInvalid || Body.isInvalid) return true;
711
Steve Naroff1b273c42007-09-16 14:56:35 +0000712 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000713}
714
715/// ParseForStatement
716/// for-statement: [C99 6.8.5.3]
717/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
718/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000719/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
720/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000721Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000722 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
724
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000725 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 Diag(Tok, diag::err_expected_lparen_after, "for");
727 SkipUntil(tok::semi);
728 return true;
729 }
730
Chris Lattner22153252007-08-26 23:08:06 +0000731 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
732 // the case for C90. Start the loop scope.
733 if (getLang().C99)
734 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
735 else
736 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737
738 SourceLocation LParenLoc = ConsumeParen();
739 ExprResult Value;
740
741 StmtTy *FirstPart = 0;
742 ExprTy *SecondPart = 0;
743 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000744 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000745
746 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000747 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 // no first part, eat the ';'.
749 ConsumeToken();
750 } else if (isDeclarationSpecifier()) { // for (int X = 4;
751 // Parse declaration, which eats the ';'.
752 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
753 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000754
755 SourceLocation DeclStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000757 // FIXME: Pass in the right location for the end of the declstmt.
758 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
759 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000761 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000762 ConsumeToken(); // consume 'in'
763 Value = ParseExpression();
764 if (!Value.isInvalid)
765 SecondPart = Value.Val;
766 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 } else {
768 Value = ParseExpression();
769
770 // Turn the expression into a stmt.
771 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000772 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 if (!R.isInvalid)
774 FirstPart = R.Val;
775 }
776
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000777 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000779 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000780 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000781 ConsumeToken(); // consume 'in'
782 Value = ParseExpression();
783 if (!Value.isInvalid)
784 SecondPart = Value.Val;
785 }
786 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
788 SkipUntil(tok::semi);
789 }
790 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000791 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000792 // Parse the second part of the for specifier.
793 if (Tok.is(tok::semi)) { // for (...;;
794 // no second part.
795 Value = ExprResult();
796 } else {
797 Value = ParseExpression();
798 if (!Value.isInvalid)
799 SecondPart = Value.Val;
800 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000801
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000802 if (Tok.is(tok::semi)) {
803 ConsumeToken();
804 } else {
805 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
806 SkipUntil(tok::semi);
807 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000808
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000809 // Parse the third part of the for specifier.
810 if (Tok.is(tok::r_paren)) { // for (...;...;)
811 // no third part.
812 Value = ExprResult();
813 } else {
814 Value = ParseExpression();
815 if (!Value.isInvalid) {
816 // Turn the expression into a stmt.
817 StmtResult R = Actions.ActOnExprStmt(Value.Val);
818 if (!R.isInvalid)
819 ThirdPart = R.Val;
820 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 }
822 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 // Match the ')'.
824 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
825
Chris Lattner0ecea032007-08-22 05:28:50 +0000826 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000827 // there is no compound stmt. C90 does not have this clause. We only do this
828 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000829 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000830 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 // Read the body statement.
833 StmtResult Body = ParseStatement();
834
Chris Lattner0ecea032007-08-22 05:28:50 +0000835 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000836 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000837
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 // Leave the for-scope.
839 ExitScope();
840
841 if (Body.isInvalid)
842 return Body;
843
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000844 if (!ForEach)
845 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
846 SecondPart, ThirdPart, RParenLoc, Body.Val);
847 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000848 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000849 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000850}
851
852/// ParseGotoStatement
853/// jump-statement:
854/// 'goto' identifier ';'
855/// [GNU] 'goto' '*' expression ';'
856///
857/// Note: this lets the caller parse the end ';'.
858///
859Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000860 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
862
863 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000864 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000865 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 Tok.getIdentifierInfo());
867 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000868 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 // GNU indirect goto extension.
870 Diag(Tok, diag::ext_gnu_indirect_goto);
871 SourceLocation StarLoc = ConsumeToken();
872 ExprResult R = ParseExpression();
873 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
874 SkipUntil(tok::semi, false, true);
875 return true;
876 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000877 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000878 } else {
879 Diag(Tok, diag::err_expected_ident);
880 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000882
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 return Res;
884}
885
886/// ParseContinueStatement
887/// jump-statement:
888/// 'continue' ';'
889///
890/// Note: this lets the caller parse the end ';'.
891///
892Parser::StmtResult Parser::ParseContinueStatement() {
893 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000894 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000895}
896
897/// ParseBreakStatement
898/// jump-statement:
899/// 'break' ';'
900///
901/// Note: this lets the caller parse the end ';'.
902///
903Parser::StmtResult Parser::ParseBreakStatement() {
904 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000905 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906}
907
908/// ParseReturnStatement
909/// jump-statement:
910/// 'return' expression[opt] ';'
911Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000912 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
914
915 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000916 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 R = ParseExpression();
918 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
919 SkipUntil(tok::semi, false, true);
920 return true;
921 }
922 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000923 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000924}
925
Steve Naroff5f8aa692008-02-11 23:15:56 +0000926/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
927/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +0000928Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +0000929 if (Tok.is(tok::l_brace)) {
930 unsigned short savedBraceCount = BraceCount;
931 do {
932 ConsumeAnyToken();
933 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
934 } else {
935 // From the MS website: If used without braces, the __asm keyword means
936 // that the rest of the line is an assembly-language statement.
937 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +0000938 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +0000939 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
940 do {
941 ConsumeAnyToken();
942 TokLoc = Tok.getLocation();
943 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
944 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
945 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +0000946 }
Steve Naroffd62701b2008-02-07 03:50:06 +0000947 return false;
948}
949
Reid Spencer5f016e22007-07-11 17:01:13 +0000950/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +0000951/// asm-statement:
952/// gnu-asm-statement
953/// ms-asm-statement
954///
955/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +0000956/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
957///
958/// [GNU] asm-argument:
959/// asm-string-literal
960/// asm-string-literal ':' asm-operands[opt]
961/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
962/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
963/// ':' asm-clobbers
964///
965/// [GNU] asm-clobbers:
966/// asm-string-literal
967/// asm-clobbers ',' asm-string-literal
968///
Steve Naroff5f8aa692008-02-11 23:15:56 +0000969/// [MS] ms-asm-statement:
970/// '__asm' assembly-instruction ';'[opt]
971/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
972///
973/// [MS] assembly-instruction-list:
974/// assembly-instruction ';'[opt]
975/// assembly-instruction-list ';' assembly-instruction ';'[opt]
976///
Steve Naroffd62701b2008-02-07 03:50:06 +0000977Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000978 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +0000979 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
Steve Naroff5f8aa692008-02-11 23:15:56 +0000981 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +0000982 msAsm = true;
983 return FuzzyParseMicrosoftAsmStatement();
984 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 DeclSpec DS;
986 SourceLocation Loc = Tok.getLocation();
987 ParseTypeQualifierListOpt(DS);
988
989 // GNU asms accept, but warn, about type-qualifiers other than volatile.
990 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
991 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
992 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
993 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
994
995 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +0000996 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +0000997 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000998 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 Diag(Tok, diag::err_expected_lparen_after, "asm");
1000 SkipUntil(tok::r_paren);
1001 return true;
1002 }
1003 Loc = ConsumeParen();
1004
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001005 ExprResult AsmString = ParseAsmStringLiteral();
1006 if (AsmString.isInvalid)
1007 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001008
1009 llvm::SmallVector<std::string, 4> Names;
1010 llvm::SmallVector<ExprTy*, 4> Constraints;
1011 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001012 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001013
Anders Carlssondfab34a2008-02-05 23:03:50 +00001014 unsigned NumInputs = 0, NumOutputs = 0;
1015
1016 SourceLocation RParenLoc;
1017 if (Tok.is(tok::r_paren)) {
1018 // We have a simple asm expression
1019 isSimple = true;
1020
1021 RParenLoc = ConsumeParen();
1022 } else {
1023 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001024 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1025 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001026
1027 NumOutputs = Names.size();
1028
1029 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001030 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1031 return true;
1032
Anders Carlssondfab34a2008-02-05 23:03:50 +00001033 assert(Names.size() == Constraints.size() &&
1034 Constraints.size() == Exprs.size()
1035 && "Input operand size mismatch!");
1036
1037 NumInputs = Names.size() - NumOutputs;
1038
1039 // Parse the clobbers, if present.
1040 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001041 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +00001042
1043 // Parse the asm-string list for clobbers.
1044 while (1) {
1045 ExprResult Clobber = ParseAsmStringLiteral();
1046
1047 if (Clobber.isInvalid)
1048 break;
1049
1050 Clobbers.push_back(Clobber.Val);
1051
1052 if (Tok.isNot(tok::comma)) break;
1053 ConsumeToken();
1054 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 }
Anders Carlssondfab34a2008-02-05 23:03:50 +00001056
1057 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 }
1059
Anders Carlssondfab34a2008-02-05 23:03:50 +00001060 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1061 NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001062 &Names[0], &Constraints[0], &Exprs[0],
1063 AsmString.Val,
1064 Clobbers.size(), &Clobbers[0],
1065 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001066}
1067
1068/// ParseAsmOperands - Parse the asm-operands production as used by
1069/// asm-statement. We also parse a leading ':' token. If the leading colon is
1070/// not present, we do not parse anything.
1071///
1072/// [GNU] asm-operands:
1073/// asm-operand
1074/// asm-operands ',' asm-operand
1075///
1076/// [GNU] asm-operand:
1077/// asm-string-literal '(' expression ')'
1078/// '[' identifier ']' asm-string-literal '(' expression ')'
1079///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001080bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001081 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1082 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001084 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 ConsumeToken();
1086
1087 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001088 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001089 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001090
Anders Carlssonb235fc22007-11-22 01:36:19 +00001091 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001093 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 SourceLocation Loc = ConsumeBracket();
1095
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001096 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 Diag(Tok, diag::err_expected_ident);
1098 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001099 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 }
Chris Lattner69efba72007-10-29 04:06:22 +00001101
Anders Carlssonb235fc22007-11-22 01:36:19 +00001102 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001103 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001104
1105 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001107 } else
1108 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001109
Anders Carlssonb235fc22007-11-22 01:36:19 +00001110 ExprResult Constraint = ParseAsmStringLiteral();
1111 if (Constraint.isInvalid) {
1112 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001113 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001114 }
1115 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001116
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001117 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1119 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001120 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 }
1122
1123 // Read the parenthesized expression.
1124 ExprResult Res = ParseSimpleParenExpression();
1125 if (Res.isInvalid) {
1126 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001127 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001129 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001131 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 ConsumeToken();
1133 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001134
1135 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001136}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001137
1138Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1139 SourceLocation L, SourceLocation R) {
1140 // Do not enter a scope for the brace, as the arguments are in the same scope
1141 // (the function body) as the body itself. Instead, just read the statement
1142 // list and put it into a CompoundStmt for safe keeping.
1143 StmtResult FnBody = ParseCompoundStatementBody();
1144
1145 // If the function body could not be parsed, make a bogus compoundstmt.
1146 if (FnBody.isInvalid)
1147 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1148
1149 // Leave the function body scope.
1150 ExitScope();
1151
Steve Naroffd6d054d2007-11-11 23:20:51 +00001152 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001153}