blob: 670181e2245bf2588688c537bdb5b7a0b314670d [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.
Chris Lattner691a38b2008-03-13 06:29:54 +000099 return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
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
Chris Lattner043a0b52008-03-13 06:32:11 +0000434 // __extension__ silences extension warnings in the subexpression.
435 bool SavedExtWarn = Diags.getWarnOnExtensions();
436 Diags.setWarnOnExtensions(false);
437
Chris Lattner45a566c2007-08-27 01:01:57 +0000438 // If this is the start of a declaration, parse it as such.
439 if (isDeclarationSpecifier()) {
440 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000441 SourceLocation DeclStart = Tok.getLocation();
442 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
443 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000444 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner043a0b52008-03-13 06:32:11 +0000445
446 Diags.setWarnOnExtensions(SavedExtWarn);
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.
Chris Lattner45a566c2007-08-27 01:01:57 +0000450 ExprResult Res = ParseCastExpression(false);
Chris Lattner043a0b52008-03-13 06:32:11 +0000451 Diags.setWarnOnExtensions(SavedExtWarn);
452
Chris Lattner45a566c2007-08-27 01:01:57 +0000453 if (Res.isInvalid) {
454 SkipUntil(tok::semi);
455 continue;
456 }
457
458 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000459 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000460 if (Res.isInvalid)
461 continue;
462
463 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
464 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000465 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000466 }
467 }
468
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 if (!R.isInvalid && R.Val)
470 Stmts.push_back(R.Val);
471 }
472
473 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000474 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000476 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 }
478
479 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000480 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000481 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482}
483
484/// ParseIfStatement
485/// if-statement: [C99 6.8.4.1]
486/// 'if' '(' expression ')' statement
487/// 'if' '(' expression ')' statement 'else' statement
488///
489Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000490 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
492
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000493 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 Diag(Tok, diag::err_expected_lparen_after, "if");
495 SkipUntil(tok::semi);
496 return true;
497 }
498
Chris Lattner22153252007-08-26 23:08:06 +0000499 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
500 // the case for C90.
501 if (getLang().C99)
502 EnterScope(Scope::DeclScope);
503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 // Parse the condition.
505 ExprResult CondExp = ParseSimpleParenExpression();
506 if (CondExp.isInvalid) {
507 SkipUntil(tok::semi);
Chris Lattner22153252007-08-26 23:08:06 +0000508 if (getLang().C99)
509 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 return true;
511 }
512
Chris Lattner0ecea032007-08-22 05:28:50 +0000513 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000514 // there is no compound stmt. C90 does not have this clause. We only do this
515 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000516 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000517 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000518
Chris Lattnerb96728d2007-10-29 05:08:52 +0000519 // Read the 'then' stmt.
520 SourceLocation ThenStmtLoc = Tok.getLocation();
521 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000522
Chris Lattnera36ce712007-08-22 05:16:28 +0000523 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000524 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525
526 // If it has an else, parse it.
527 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000528 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000530
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000531 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000533
Chris Lattner0ecea032007-08-22 05:28:50 +0000534 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000535 // there is no compound stmt. C90 does not have this clause. We only do
536 // this if the body isn't a compound statement to avoid push/pop in common
537 // cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000538 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000539 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000540
Chris Lattnerb96728d2007-10-29 05:08:52 +0000541 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000543
544 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000545 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 }
547
Chris Lattner22153252007-08-26 23:08:06 +0000548 if (getLang().C99)
549 ExitScope();
550
Chris Lattnerb96728d2007-10-29 05:08:52 +0000551 // If the then or else stmt is invalid and the other is valid (and present),
552 // make turn the invalid one into a null stmt to avoid dropping the other
553 // part. If both are invalid, return error.
554 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
555 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
556 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
557 // Both invalid, or one is invalid and other is non-present: delete cond and
558 // return error.
559 Actions.DeleteExpr(CondExp.Val);
560 return true;
561 }
562
563 // Now if either are invalid, replace with a ';'.
564 if (ThenStmt.isInvalid)
565 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
566 if (ElseStmt.isInvalid)
567 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
568
Chris Lattnerb96728d2007-10-29 05:08:52 +0000569 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 ElseLoc, ElseStmt.Val);
571}
572
573/// ParseSwitchStatement
574/// switch-statement:
575/// 'switch' '(' expression ')' statement
576Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000577 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
579
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000580 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 Diag(Tok, diag::err_expected_lparen_after, "switch");
582 SkipUntil(tok::semi);
583 return true;
584 }
Chris Lattner22153252007-08-26 23:08:06 +0000585
586 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
587 // not the case for C90. Start the switch scope.
588 if (getLang().C99)
589 EnterScope(Scope::BreakScope|Scope::DeclScope);
590 else
591 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000592
593 // Parse the condition.
594 ExprResult Cond = ParseSimpleParenExpression();
595
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000596 if (Cond.isInvalid) {
597 ExitScope();
598 return true;
599 }
600
Steve Naroff1b273c42007-09-16 14:56:35 +0000601 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000602
Chris Lattner0ecea032007-08-22 05:28:50 +0000603 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000604 // there is no compound stmt. C90 does not have this clause. We only do this
605 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000606 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000607 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000608
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 // Read the body statement.
610 StmtResult Body = ParseStatement();
611
Chris Lattner0ecea032007-08-22 05:28:50 +0000612 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000613 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000614
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000615 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000616 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000617 // FIXME: Remove the case statement list from the Switch statement.
618 }
619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 ExitScope();
621
Steve Naroff1b273c42007-09-16 14:56:35 +0000622 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000623}
624
625/// ParseWhileStatement
626/// while-statement: [C99 6.8.5.1]
627/// 'while' '(' expression ')' statement
628Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000629 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 SourceLocation WhileLoc = Tok.getLocation();
631 ConsumeToken(); // eat the 'while'.
632
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000633 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 Diag(Tok, diag::err_expected_lparen_after, "while");
635 SkipUntil(tok::semi);
636 return true;
637 }
638
Chris Lattner22153252007-08-26 23:08:06 +0000639 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
640 // the case for C90. Start the loop scope.
641 if (getLang().C99)
642 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
643 else
644 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000645
646 // Parse the condition.
647 ExprResult Cond = ParseSimpleParenExpression();
648
Chris Lattner0ecea032007-08-22 05:28:50 +0000649 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000650 // there is no compound stmt. C90 does not have this clause. We only do this
651 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000652 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000653 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 // Read the body statement.
656 StmtResult Body = ParseStatement();
657
Chris Lattner0ecea032007-08-22 05:28:50 +0000658 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000659 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 ExitScope();
662
663 if (Cond.isInvalid || Body.isInvalid) return true;
664
Steve Naroff1b273c42007-09-16 14:56:35 +0000665 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000666}
667
668/// ParseDoStatement
669/// do-statement: [C99 6.8.5.2]
670/// 'do' statement 'while' '(' expression ')' ';'
671/// Note: this lets the caller parse the end ';'.
672Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000673 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
675
Chris Lattner22153252007-08-26 23:08:06 +0000676 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
677 // the case for C90. Start the loop scope.
678 if (getLang().C99)
679 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
680 else
681 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000682
Chris Lattner0ecea032007-08-22 05:28:50 +0000683 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000684 // there is no compound stmt. C90 does not have this clause. We only do this
685 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000686 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000687 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // Read the body statement.
690 StmtResult Body = ParseStatement();
691
Chris Lattner0ecea032007-08-22 05:28:50 +0000692 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000693 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000694
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000695 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 ExitScope();
697 Diag(Tok, diag::err_expected_while);
698 Diag(DoLoc, diag::err_matching, "do");
699 SkipUntil(tok::semi);
700 return true;
701 }
702 SourceLocation WhileLoc = ConsumeToken();
703
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000704 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 ExitScope();
706 Diag(Tok, diag::err_expected_lparen_after, "do/while");
707 SkipUntil(tok::semi);
708 return true;
709 }
710
711 // Parse the condition.
712 ExprResult Cond = ParseSimpleParenExpression();
713
714 ExitScope();
715
716 if (Cond.isInvalid || Body.isInvalid) return true;
717
Steve Naroff1b273c42007-09-16 14:56:35 +0000718 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000719}
720
721/// ParseForStatement
722/// for-statement: [C99 6.8.5.3]
723/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
724/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000725/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
726/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000727Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000728 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
730
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000731 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 Diag(Tok, diag::err_expected_lparen_after, "for");
733 SkipUntil(tok::semi);
734 return true;
735 }
736
Chris Lattner22153252007-08-26 23:08:06 +0000737 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
738 // the case for C90. Start the loop scope.
739 if (getLang().C99)
740 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
741 else
742 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000743
744 SourceLocation LParenLoc = ConsumeParen();
745 ExprResult Value;
746
747 StmtTy *FirstPart = 0;
748 ExprTy *SecondPart = 0;
749 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000750 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000751
752 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000753 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 // no first part, eat the ';'.
755 ConsumeToken();
756 } else if (isDeclarationSpecifier()) { // for (int X = 4;
757 // Parse declaration, which eats the ';'.
758 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
759 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000760
761 SourceLocation DeclStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000763 // FIXME: Pass in the right location for the end of the declstmt.
764 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattner691a38b2008-03-13 06:29:54 +0000765 DeclStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000767 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000768 ConsumeToken(); // consume 'in'
769 Value = ParseExpression();
770 if (!Value.isInvalid)
771 SecondPart = Value.Val;
772 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 } else {
774 Value = ParseExpression();
775
776 // Turn the expression into a stmt.
777 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000778 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 if (!R.isInvalid)
780 FirstPart = R.Val;
781 }
782
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000783 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000785 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000786 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000787 ConsumeToken(); // consume 'in'
788 Value = ParseExpression();
789 if (!Value.isInvalid)
790 SecondPart = Value.Val;
791 }
792 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
794 SkipUntil(tok::semi);
795 }
796 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000797 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000798 // Parse the second part of the for specifier.
799 if (Tok.is(tok::semi)) { // for (...;;
800 // no second part.
801 Value = ExprResult();
802 } else {
803 Value = ParseExpression();
804 if (!Value.isInvalid)
805 SecondPart = Value.Val;
806 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000807
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000808 if (Tok.is(tok::semi)) {
809 ConsumeToken();
810 } else {
811 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
812 SkipUntil(tok::semi);
813 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000814
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000815 // Parse the third part of the for specifier.
816 if (Tok.is(tok::r_paren)) { // for (...;...;)
817 // no third part.
818 Value = ExprResult();
819 } else {
820 Value = ParseExpression();
821 if (!Value.isInvalid) {
822 // Turn the expression into a stmt.
823 StmtResult R = Actions.ActOnExprStmt(Value.Val);
824 if (!R.isInvalid)
825 ThirdPart = R.Val;
826 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 }
828 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 // Match the ')'.
830 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
831
Chris Lattner0ecea032007-08-22 05:28:50 +0000832 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000833 // there is no compound stmt. C90 does not have this clause. We only do this
834 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000835 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000836 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000837
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 // Read the body statement.
839 StmtResult Body = ParseStatement();
840
Chris Lattner0ecea032007-08-22 05:28:50 +0000841 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000842 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000843
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 // Leave the for-scope.
845 ExitScope();
846
847 if (Body.isInvalid)
848 return Body;
849
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000850 if (!ForEach)
851 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
852 SecondPart, ThirdPart, RParenLoc, Body.Val);
853 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000854 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000855 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000856}
857
858/// ParseGotoStatement
859/// jump-statement:
860/// 'goto' identifier ';'
861/// [GNU] 'goto' '*' expression ';'
862///
863/// Note: this lets the caller parse the end ';'.
864///
865Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000866 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
868
869 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000870 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000871 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 Tok.getIdentifierInfo());
873 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000874 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 // GNU indirect goto extension.
876 Diag(Tok, diag::ext_gnu_indirect_goto);
877 SourceLocation StarLoc = ConsumeToken();
878 ExprResult R = ParseExpression();
879 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
880 SkipUntil(tok::semi, false, true);
881 return true;
882 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000883 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000884 } else {
885 Diag(Tok, diag::err_expected_ident);
886 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000888
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 return Res;
890}
891
892/// ParseContinueStatement
893/// jump-statement:
894/// 'continue' ';'
895///
896/// Note: this lets the caller parse the end ';'.
897///
898Parser::StmtResult Parser::ParseContinueStatement() {
899 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000900 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000901}
902
903/// ParseBreakStatement
904/// jump-statement:
905/// 'break' ';'
906///
907/// Note: this lets the caller parse the end ';'.
908///
909Parser::StmtResult Parser::ParseBreakStatement() {
910 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000911 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000912}
913
914/// ParseReturnStatement
915/// jump-statement:
916/// 'return' expression[opt] ';'
917Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000918 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
920
921 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000922 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 R = ParseExpression();
924 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
925 SkipUntil(tok::semi, false, true);
926 return true;
927 }
928 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000929 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000930}
931
Steve Naroff5f8aa692008-02-11 23:15:56 +0000932/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
933/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +0000934Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +0000935 if (Tok.is(tok::l_brace)) {
936 unsigned short savedBraceCount = BraceCount;
937 do {
938 ConsumeAnyToken();
939 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
940 } else {
941 // From the MS website: If used without braces, the __asm keyword means
942 // that the rest of the line is an assembly-language statement.
943 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +0000944 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +0000945 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
946 do {
947 ConsumeAnyToken();
948 TokLoc = Tok.getLocation();
949 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
950 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
951 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +0000952 }
Steve Naroffd77bc282008-04-07 21:06:54 +0000953 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +0000954}
955
Reid Spencer5f016e22007-07-11 17:01:13 +0000956/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +0000957/// asm-statement:
958/// gnu-asm-statement
959/// ms-asm-statement
960///
961/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +0000962/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
963///
964/// [GNU] asm-argument:
965/// asm-string-literal
966/// asm-string-literal ':' asm-operands[opt]
967/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
968/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
969/// ':' asm-clobbers
970///
971/// [GNU] asm-clobbers:
972/// asm-string-literal
973/// asm-clobbers ',' asm-string-literal
974///
Steve Naroff5f8aa692008-02-11 23:15:56 +0000975/// [MS] ms-asm-statement:
976/// '__asm' assembly-instruction ';'[opt]
977/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
978///
979/// [MS] assembly-instruction-list:
980/// assembly-instruction ';'[opt]
981/// assembly-instruction-list ';' assembly-instruction ';'[opt]
982///
Steve Naroffd62701b2008-02-07 03:50:06 +0000983Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000984 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +0000985 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000986
Steve Naroff5f8aa692008-02-11 23:15:56 +0000987 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +0000988 msAsm = true;
989 return FuzzyParseMicrosoftAsmStatement();
990 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 DeclSpec DS;
992 SourceLocation Loc = Tok.getLocation();
993 ParseTypeQualifierListOpt(DS);
994
995 // GNU asms accept, but warn, about type-qualifiers other than volatile.
996 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
997 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
998 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
999 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
1000
1001 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001002 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001003 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001004 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 Diag(Tok, diag::err_expected_lparen_after, "asm");
1006 SkipUntil(tok::r_paren);
1007 return true;
1008 }
1009 Loc = ConsumeParen();
1010
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001011 ExprResult AsmString = ParseAsmStringLiteral();
1012 if (AsmString.isInvalid)
1013 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001014
1015 llvm::SmallVector<std::string, 4> Names;
1016 llvm::SmallVector<ExprTy*, 4> Constraints;
1017 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001018 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001019
Anders Carlssondfab34a2008-02-05 23:03:50 +00001020 unsigned NumInputs = 0, NumOutputs = 0;
1021
1022 SourceLocation RParenLoc;
1023 if (Tok.is(tok::r_paren)) {
1024 // We have a simple asm expression
1025 isSimple = true;
1026
1027 RParenLoc = ConsumeParen();
1028 } else {
1029 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001030 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1031 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001032
1033 NumOutputs = Names.size();
1034
1035 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001036 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1037 return true;
1038
Anders Carlssondfab34a2008-02-05 23:03:50 +00001039 assert(Names.size() == Constraints.size() &&
1040 Constraints.size() == Exprs.size()
1041 && "Input operand size mismatch!");
1042
1043 NumInputs = Names.size() - NumOutputs;
1044
1045 // Parse the clobbers, if present.
1046 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001047 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +00001048
1049 // Parse the asm-string list for clobbers.
1050 while (1) {
1051 ExprResult Clobber = ParseAsmStringLiteral();
1052
1053 if (Clobber.isInvalid)
1054 break;
1055
1056 Clobbers.push_back(Clobber.Val);
1057
1058 if (Tok.isNot(tok::comma)) break;
1059 ConsumeToken();
1060 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 }
Anders Carlssondfab34a2008-02-05 23:03:50 +00001062
1063 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 }
1065
Anders Carlssondfab34a2008-02-05 23:03:50 +00001066 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1067 NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001068 &Names[0], &Constraints[0], &Exprs[0],
1069 AsmString.Val,
1070 Clobbers.size(), &Clobbers[0],
1071 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072}
1073
1074/// ParseAsmOperands - Parse the asm-operands production as used by
1075/// asm-statement. We also parse a leading ':' token. If the leading colon is
1076/// not present, we do not parse anything.
1077///
1078/// [GNU] asm-operands:
1079/// asm-operand
1080/// asm-operands ',' asm-operand
1081///
1082/// [GNU] asm-operand:
1083/// asm-string-literal '(' expression ')'
1084/// '[' identifier ']' asm-string-literal '(' expression ')'
1085///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001086bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001087 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1088 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001090 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 ConsumeToken();
1092
1093 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001094 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001095 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001096
Anders Carlssonb235fc22007-11-22 01:36:19 +00001097 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001099 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 SourceLocation Loc = ConsumeBracket();
1101
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001102 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 Diag(Tok, diag::err_expected_ident);
1104 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001105 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 }
Chris Lattner69efba72007-10-29 04:06:22 +00001107
Anders Carlssonb235fc22007-11-22 01:36:19 +00001108 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001109 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001110
1111 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001113 } else
1114 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001115
Anders Carlssonb235fc22007-11-22 01:36:19 +00001116 ExprResult Constraint = ParseAsmStringLiteral();
1117 if (Constraint.isInvalid) {
1118 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001119 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001120 }
1121 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001122
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001123 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1125 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001126 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 }
1128
1129 // Read the parenthesized expression.
1130 ExprResult Res = ParseSimpleParenExpression();
1131 if (Res.isInvalid) {
1132 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001133 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001135 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001137 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 ConsumeToken();
1139 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001140
1141 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001142}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001143
1144Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1145 SourceLocation L, SourceLocation R) {
1146 // Do not enter a scope for the brace, as the arguments are in the same scope
1147 // (the function body) as the body itself. Instead, just read the statement
1148 // list and put it into a CompoundStmt for safe keeping.
1149 StmtResult FnBody = ParseCompoundStatementBody();
1150
1151 // If the function body could not be parsed, make a bogus compoundstmt.
1152 if (FnBody.isInvalid)
1153 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1154
1155 // Leave the function body scope.
1156 ExitScope();
1157
Steve Naroffd6d054d2007-11-11 23:20:51 +00001158 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001159}