blob: f8ae4a05585067cd3f384d0a8c4248b44c4ef405 [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()) {
Steve Naroff1b273c42007-09-16 14:56:35 +000096 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner4e1d99a2007-10-09 17:41:39 +000097 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000098 Diag(Tok, diag::err_expected_statement);
99 return true;
100 } else {
101 // expression[opt] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000102 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 if (Res.isInvalid) {
104 // If the expression is invalid, skip ahead to the next semicolon. Not
105 // doing this opens us up to the possibility of infinite loops if
106 // ParseExpression does not consume any tokens.
107 SkipUntil(tok::semi);
108 return true;
109 }
110 // Otherwise, eat the semicolon.
111 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000112 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 }
114
115 case tok::kw_case: // C99 6.8.1: labeled-statement
116 return ParseCaseStatement();
117 case tok::kw_default: // C99 6.8.1: labeled-statement
118 return ParseDefaultStatement();
119
120 case tok::l_brace: // C99 6.8.2: compound-statement
121 return ParseCompoundStatement();
122 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000123 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
125 case tok::kw_if: // C99 6.8.4.1: if-statement
126 return ParseIfStatement();
127 case tok::kw_switch: // C99 6.8.4.2: switch-statement
128 return ParseSwitchStatement();
129
130 case tok::kw_while: // C99 6.8.5.1: while-statement
131 return ParseWhileStatement();
132 case tok::kw_do: // C99 6.8.5.2: do-statement
133 Res = ParseDoStatement();
134 SemiError = "do/while loop";
135 break;
136 case tok::kw_for: // C99 6.8.5.3: for-statement
137 return ParseForStatement();
138
139 case tok::kw_goto: // C99 6.8.6.1: goto-statement
140 Res = ParseGotoStatement();
141 SemiError = "goto statement";
142 break;
143 case tok::kw_continue: // C99 6.8.6.2: continue-statement
144 Res = ParseContinueStatement();
145 SemiError = "continue statement";
146 break;
147 case tok::kw_break: // C99 6.8.6.3: break-statement
148 Res = ParseBreakStatement();
149 SemiError = "break statement";
150 break;
151 case tok::kw_return: // C99 6.8.6.4: return-statement
152 Res = ParseReturnStatement();
153 SemiError = "return statement";
154 break;
155
156 case tok::kw_asm:
Steve Naroffd62701b2008-02-07 03:50:06 +0000157 bool msAsm = false;
158 Res = ParseAsmStatement(msAsm);
159 if (msAsm) return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 SemiError = "asm statement";
161 break;
162 }
163
164 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000165 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 ConsumeToken();
167 } else {
168 Diag(Tok, diag::err_expected_semi_after, SemiError);
169 SkipUntil(tok::semi);
170 }
171 return Res;
172}
173
174/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
175/// have a bit of a quandry here. Reading the identifier is necessary to see if
176/// there is a ':' after it. If there is, this is a label, regardless of what
177/// else the identifier can mean. If not, this is either part of a declaration
178/// (if the identifier is a type-name) or part of an expression.
179///
180/// labeled-statement:
181/// identifier ':' statement
182/// [GNU] identifier ':' attributes[opt] statement
183/// declaration (if !OnlyStatement)
184/// expression[opt] ';'
185///
186Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000187 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 "Not an identifier!");
189
Chris Lattnerd2177732007-07-20 16:59:19 +0000190 Token IdentTok = Tok; // Save the whole token.
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 ConsumeToken(); // eat the identifier.
192
193 // identifier ':' statement
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000194 if (Tok.is(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 SourceLocation ColonLoc = ConsumeToken();
196
197 // Read label attributes, if present.
198 DeclTy *AttrList = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000199 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 // TODO: save these somewhere.
201 AttrList = ParseAttributes();
202
203 StmtResult SubStmt = ParseStatement();
204
205 // Broken substmt shouldn't prevent the label from being added to the AST.
206 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000207 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
Steve Naroff1b273c42007-09-16 14:56:35 +0000209 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 IdentTok.getIdentifierInfo(),
211 ColonLoc, SubStmt.Val);
212 }
213
214 // Check to see if this is a declaration.
215 void *TypeRep;
216 if (!OnlyStatement &&
217 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
218 // Handle this. Warn/disable if in middle of block and !C99.
219 DeclSpec DS;
220
221 // Add the typedef name to the start of the decl-specs.
222 const char *PrevSpec = 0;
223 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
224 IdentTok.getLocation(), PrevSpec,
225 TypeRep);
226 assert(!isInvalid && "First declspec can't be invalid!");
Steve Narofff908a872007-10-30 02:23:23 +0000227 SourceLocation endProtoLoc;
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000228 if (Tok.is(tok::less)) {
229 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000230 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000231 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
232 new llvm::SmallVector<DeclTy *, 8>;
233 DS.setProtocolQualifiers(ProtocolDecl);
234 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
235 &ProtocolRefs[0], ProtocolRefs.size(),
236 *ProtocolDecl);
237 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000238
239 // ParseDeclarationSpecifiers will continue from there.
240 ParseDeclarationSpecifiers(DS);
241
242 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
243 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000244 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 // TODO: emit error on 'int;' or 'const enum foo;'.
246 // if (!DS.isMissingDeclaratorOk()) Diag(...);
247
248 ConsumeToken();
249 // FIXME: Return this as a type decl.
250 return 0;
251 }
252
253 // Parse all the declarators.
254 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
255 ParseDeclarator(DeclaratorInfo);
256
257 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff1b273c42007-09-16 14:56:35 +0000258 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 }
260
261 // Otherwise, this is an expression. Seed it with II and parse it.
262 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
263 if (Res.isInvalid) {
264 SkipUntil(tok::semi);
265 return true;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000266 } else if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 Diag(Tok, diag::err_expected_semi_after, "expression");
268 SkipUntil(tok::semi);
269 return true;
270 } else {
271 ConsumeToken();
272 // Convert expr to a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000273 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 }
275}
276
277/// ParseCaseStatement
278/// labeled-statement:
279/// 'case' constant-expression ':' statement
280/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
281///
282/// Note that this does not parse the 'statement' at the end.
283///
284Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000285 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
287
288 ExprResult LHS = ParseConstantExpression();
289 if (LHS.isInvalid) {
290 SkipUntil(tok::colon);
291 return true;
292 }
293
294 // GNU case range extension.
295 SourceLocation DotDotDotLoc;
296 ExprTy *RHSVal = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000297 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 Diag(Tok, diag::ext_gnu_case_range);
299 DotDotDotLoc = ConsumeToken();
300
301 ExprResult RHS = ParseConstantExpression();
302 if (RHS.isInvalid) {
303 SkipUntil(tok::colon);
304 return true;
305 }
306 RHSVal = RHS.Val;
307 }
308
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000309 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 Diag(Tok, diag::err_expected_colon_after, "'case'");
311 SkipUntil(tok::colon);
312 return true;
313 }
314
315 SourceLocation ColonLoc = ConsumeToken();
316
317 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000318 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 Diag(Tok, diag::err_label_end_of_compound_statement);
320 return true;
321 }
322
323 StmtResult SubStmt = ParseStatement();
324
325 // Broken substmt shouldn't prevent the case from being added to the AST.
326 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000327 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
Steve Naroff1b273c42007-09-16 14:56:35 +0000329 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 SubStmt.Val);
331}
332
333/// ParseDefaultStatement
334/// labeled-statement:
335/// 'default' ':' statement
336/// Note that this does not parse the 'statement' at the end.
337///
338Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000339 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
341
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000342 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 Diag(Tok, diag::err_expected_colon_after, "'default'");
344 SkipUntil(tok::colon);
345 return true;
346 }
347
348 SourceLocation ColonLoc = ConsumeToken();
349
350 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000351 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 Diag(Tok, diag::err_label_end_of_compound_statement);
353 return true;
354 }
355
356 StmtResult SubStmt = ParseStatement();
357 if (SubStmt.isInvalid)
358 return true;
359
Steve Naroff1b273c42007-09-16 14:56:35 +0000360 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000361}
362
363
364/// ParseCompoundStatement - Parse a "{}" block.
365///
366/// compound-statement: [C99 6.8.2]
367/// { block-item-list[opt] }
368/// [GNU] { label-declarations block-item-list } [TODO]
369///
370/// block-item-list:
371/// block-item
372/// block-item-list block-item
373///
374/// block-item:
375/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000376/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000377/// statement
378/// [OMP] openmp-directive [TODO]
379///
380/// [GNU] label-declarations:
381/// [GNU] label-declaration
382/// [GNU] label-declarations label-declaration
383///
384/// [GNU] label-declaration:
385/// [GNU] '__label__' identifier-list ';'
386///
387/// [OMP] openmp-directive: [TODO]
388/// [OMP] barrier-directive
389/// [OMP] flush-directive
390///
Chris Lattner98414c12007-08-31 21:49:55 +0000391Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000392 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000393
Chris Lattner31e05722007-08-26 06:24:45 +0000394 // Enter a scope to hold everything within the compound stmt. Compound
395 // statements can always hold declarations.
396 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397
398 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000399 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400
401 ExitScope();
402 return Body;
403}
404
405
406/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000407/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000408/// consume the '}' at the end of the block. It does not manipulate the scope
409/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000410Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
412
413 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000414 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000415
416 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000417 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000418 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000419 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000420 R = ParseStatementOrDeclaration(false);
421 } else {
422 // __extension__ can start declarations and it can also be a unary
423 // operator for expressions. Consume multiple __extension__ markers here
424 // until we can determine which is which.
425 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000426 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000427 ConsumeToken();
428
429 // If this is the start of a declaration, parse it as such.
430 if (isDeclarationSpecifier()) {
431 // FIXME: Save the __extension__ on the decl as a node somehow.
432 // FIXME: disable extwarns.
Steve Naroff1b273c42007-09-16 14:56:35 +0000433 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner45a566c2007-08-27 01:01:57 +0000434 } else {
435 // Otherwise this was a unary __extension__ marker. Parse the
436 // subexpression and add the __extension__ unary op.
437 // FIXME: disable extwarns.
438 ExprResult Res = ParseCastExpression(false);
439 if (Res.isInvalid) {
440 SkipUntil(tok::semi);
441 continue;
442 }
443
444 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000445 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000446 if (Res.isInvalid)
447 continue;
448
449 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
450 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000451 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000452 }
453 }
454
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 if (!R.isInvalid && R.Val)
456 Stmts.push_back(R.Val);
457 }
458
459 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000460 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000462 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 }
464
465 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000466 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000467 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000468}
469
470/// ParseIfStatement
471/// if-statement: [C99 6.8.4.1]
472/// 'if' '(' expression ')' statement
473/// 'if' '(' expression ')' statement 'else' statement
474///
475Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000476 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
478
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000479 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 Diag(Tok, diag::err_expected_lparen_after, "if");
481 SkipUntil(tok::semi);
482 return true;
483 }
484
Chris Lattner22153252007-08-26 23:08:06 +0000485 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
486 // the case for C90.
487 if (getLang().C99)
488 EnterScope(Scope::DeclScope);
489
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 // Parse the condition.
491 ExprResult CondExp = ParseSimpleParenExpression();
492 if (CondExp.isInvalid) {
493 SkipUntil(tok::semi);
Chris Lattner22153252007-08-26 23:08:06 +0000494 if (getLang().C99)
495 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 return true;
497 }
498
Chris Lattner0ecea032007-08-22 05:28:50 +0000499 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000500 // there is no compound stmt. C90 does not have this clause. We only do this
501 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000502 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000503 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000504
Chris Lattnerb96728d2007-10-29 05:08:52 +0000505 // Read the 'then' stmt.
506 SourceLocation ThenStmtLoc = Tok.getLocation();
507 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000508
Chris Lattnera36ce712007-08-22 05:16:28 +0000509 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000510 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000511
512 // If it has an else, parse it.
513 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000514 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000516
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000517 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000519
Chris Lattner0ecea032007-08-22 05:28:50 +0000520 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000521 // there is no compound stmt. C90 does not have this clause. We only do
522 // this if the body isn't a compound statement to avoid push/pop in common
523 // cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000524 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000525 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000526
Chris Lattnerb96728d2007-10-29 05:08:52 +0000527 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000529
530 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000531 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 }
533
Chris Lattner22153252007-08-26 23:08:06 +0000534 if (getLang().C99)
535 ExitScope();
536
Chris Lattnerb96728d2007-10-29 05:08:52 +0000537 // If the then or else stmt is invalid and the other is valid (and present),
538 // make turn the invalid one into a null stmt to avoid dropping the other
539 // part. If both are invalid, return error.
540 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
541 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
542 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
543 // Both invalid, or one is invalid and other is non-present: delete cond and
544 // return error.
545 Actions.DeleteExpr(CondExp.Val);
546 return true;
547 }
548
549 // Now if either are invalid, replace with a ';'.
550 if (ThenStmt.isInvalid)
551 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
552 if (ElseStmt.isInvalid)
553 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
554
Chris Lattnerb96728d2007-10-29 05:08:52 +0000555 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 ElseLoc, ElseStmt.Val);
557}
558
559/// ParseSwitchStatement
560/// switch-statement:
561/// 'switch' '(' expression ')' statement
562Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000563 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
565
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000566 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 Diag(Tok, diag::err_expected_lparen_after, "switch");
568 SkipUntil(tok::semi);
569 return true;
570 }
Chris Lattner22153252007-08-26 23:08:06 +0000571
572 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
573 // not the case for C90. Start the switch scope.
574 if (getLang().C99)
575 EnterScope(Scope::BreakScope|Scope::DeclScope);
576 else
577 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
579 // Parse the condition.
580 ExprResult Cond = ParseSimpleParenExpression();
581
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000582 if (Cond.isInvalid) {
583 ExitScope();
584 return true;
585 }
586
Steve Naroff1b273c42007-09-16 14:56:35 +0000587 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000588
Chris Lattner0ecea032007-08-22 05:28:50 +0000589 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000590 // there is no compound stmt. C90 does not have this clause. We only do this
591 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000592 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000593 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 // Read the body statement.
596 StmtResult Body = ParseStatement();
597
Chris Lattner0ecea032007-08-22 05:28:50 +0000598 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000599 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000600
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000601 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000602 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000603 // FIXME: Remove the case statement list from the Switch statement.
604 }
605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 ExitScope();
607
Steve Naroff1b273c42007-09-16 14:56:35 +0000608 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000609}
610
611/// ParseWhileStatement
612/// while-statement: [C99 6.8.5.1]
613/// 'while' '(' expression ')' statement
614Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000615 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 SourceLocation WhileLoc = Tok.getLocation();
617 ConsumeToken(); // eat the 'while'.
618
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000619 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 Diag(Tok, diag::err_expected_lparen_after, "while");
621 SkipUntil(tok::semi);
622 return true;
623 }
624
Chris Lattner22153252007-08-26 23:08:06 +0000625 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
626 // the case for C90. Start the loop scope.
627 if (getLang().C99)
628 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
629 else
630 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000631
632 // Parse the condition.
633 ExprResult Cond = ParseSimpleParenExpression();
634
Chris Lattner0ecea032007-08-22 05:28:50 +0000635 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000636 // there is no compound stmt. C90 does not have this clause. We only do this
637 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000638 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000639 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // Read the body statement.
642 StmtResult Body = ParseStatement();
643
Chris Lattner0ecea032007-08-22 05:28:50 +0000644 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000645 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000646
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 ExitScope();
648
649 if (Cond.isInvalid || Body.isInvalid) return true;
650
Steve Naroff1b273c42007-09-16 14:56:35 +0000651 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000652}
653
654/// ParseDoStatement
655/// do-statement: [C99 6.8.5.2]
656/// 'do' statement 'while' '(' expression ')' ';'
657/// Note: this lets the caller parse the end ';'.
658Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000659 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
661
Chris Lattner22153252007-08-26 23:08:06 +0000662 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
663 // the case for C90. Start the loop scope.
664 if (getLang().C99)
665 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
666 else
667 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000668
Chris Lattner0ecea032007-08-22 05:28:50 +0000669 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000670 // there is no compound stmt. C90 does not have this clause. We only do this
671 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000672 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000673 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 // Read the body statement.
676 StmtResult Body = ParseStatement();
677
Chris Lattner0ecea032007-08-22 05:28:50 +0000678 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000679 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000680
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000681 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 ExitScope();
683 Diag(Tok, diag::err_expected_while);
684 Diag(DoLoc, diag::err_matching, "do");
685 SkipUntil(tok::semi);
686 return true;
687 }
688 SourceLocation WhileLoc = ConsumeToken();
689
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000690 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 ExitScope();
692 Diag(Tok, diag::err_expected_lparen_after, "do/while");
693 SkipUntil(tok::semi);
694 return true;
695 }
696
697 // Parse the condition.
698 ExprResult Cond = ParseSimpleParenExpression();
699
700 ExitScope();
701
702 if (Cond.isInvalid || Body.isInvalid) return true;
703
Steve Naroff1b273c42007-09-16 14:56:35 +0000704 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705}
706
707/// ParseForStatement
708/// for-statement: [C99 6.8.5.3]
709/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
710/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000711/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
712/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000713Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000714 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
716
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000717 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 Diag(Tok, diag::err_expected_lparen_after, "for");
719 SkipUntil(tok::semi);
720 return true;
721 }
722
Chris Lattner22153252007-08-26 23:08:06 +0000723 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
724 // the case for C90. Start the loop scope.
725 if (getLang().C99)
726 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
727 else
728 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000729
730 SourceLocation LParenLoc = ConsumeParen();
731 ExprResult Value;
732
733 StmtTy *FirstPart = 0;
734 ExprTy *SecondPart = 0;
735 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000736 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000737
738 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000739 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 // no first part, eat the ';'.
741 ConsumeToken();
742 } else if (isDeclarationSpecifier()) { // for (int X = 4;
743 // Parse declaration, which eats the ';'.
744 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
745 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
746 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff1b273c42007-09-16 14:56:35 +0000747 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000749 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000750 ConsumeToken(); // consume 'in'
751 Value = ParseExpression();
752 if (!Value.isInvalid)
753 SecondPart = Value.Val;
754 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 } else {
756 Value = ParseExpression();
757
758 // Turn the expression into a stmt.
759 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000760 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 if (!R.isInvalid)
762 FirstPart = R.Val;
763 }
764
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000765 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000767 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000768 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000769 ConsumeToken(); // consume 'in'
770 Value = ParseExpression();
771 if (!Value.isInvalid)
772 SecondPart = Value.Val;
773 }
774 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
776 SkipUntil(tok::semi);
777 }
778 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000779 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000780 // Parse the second part of the for specifier.
781 if (Tok.is(tok::semi)) { // for (...;;
782 // no second part.
783 Value = ExprResult();
784 } else {
785 Value = ParseExpression();
786 if (!Value.isInvalid)
787 SecondPart = Value.Val;
788 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000789
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000790 if (Tok.is(tok::semi)) {
791 ConsumeToken();
792 } else {
793 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
794 SkipUntil(tok::semi);
795 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000796
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000797 // Parse the third part of the for specifier.
798 if (Tok.is(tok::r_paren)) { // for (...;...;)
799 // no third part.
800 Value = ExprResult();
801 } else {
802 Value = ParseExpression();
803 if (!Value.isInvalid) {
804 // Turn the expression into a stmt.
805 StmtResult R = Actions.ActOnExprStmt(Value.Val);
806 if (!R.isInvalid)
807 ThirdPart = R.Val;
808 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 }
810 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 // Match the ')'.
812 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
813
Chris Lattner0ecea032007-08-22 05:28:50 +0000814 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000815 // there is no compound stmt. C90 does not have this clause. We only do this
816 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000817 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000818 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 // Read the body statement.
821 StmtResult Body = ParseStatement();
822
Chris Lattner0ecea032007-08-22 05:28:50 +0000823 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000824 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000825
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 // Leave the for-scope.
827 ExitScope();
828
829 if (Body.isInvalid)
830 return Body;
831
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000832 if (!ForEach)
833 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
834 SecondPart, ThirdPart, RParenLoc, Body.Val);
835 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000836 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000837 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000838}
839
840/// ParseGotoStatement
841/// jump-statement:
842/// 'goto' identifier ';'
843/// [GNU] 'goto' '*' expression ';'
844///
845/// Note: this lets the caller parse the end ';'.
846///
847Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000848 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
850
851 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000852 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000853 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 Tok.getIdentifierInfo());
855 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000856 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 // GNU indirect goto extension.
858 Diag(Tok, diag::ext_gnu_indirect_goto);
859 SourceLocation StarLoc = ConsumeToken();
860 ExprResult R = ParseExpression();
861 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
862 SkipUntil(tok::semi, false, true);
863 return true;
864 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000865 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000866 } else {
867 Diag(Tok, diag::err_expected_ident);
868 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000870
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 return Res;
872}
873
874/// ParseContinueStatement
875/// jump-statement:
876/// 'continue' ';'
877///
878/// Note: this lets the caller parse the end ';'.
879///
880Parser::StmtResult Parser::ParseContinueStatement() {
881 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000882 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883}
884
885/// ParseBreakStatement
886/// jump-statement:
887/// 'break' ';'
888///
889/// Note: this lets the caller parse the end ';'.
890///
891Parser::StmtResult Parser::ParseBreakStatement() {
892 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000893 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000894}
895
896/// ParseReturnStatement
897/// jump-statement:
898/// 'return' expression[opt] ';'
899Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000900 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
902
903 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000904 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 R = ParseExpression();
906 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
907 SkipUntil(tok::semi, false, true);
908 return true;
909 }
910 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000911 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000912}
913
Steve Naroffd62701b2008-02-07 03:50:06 +0000914Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +0000915 if (Tok.is(tok::l_brace)) {
916 unsigned short savedBraceCount = BraceCount;
917 do {
918 ConsumeAnyToken();
919 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
920 } else {
921 // From the MS website: If used without braces, the __asm keyword means
922 // that the rest of the line is an assembly-language statement.
923 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +0000924 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +0000925 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
926 do {
927 ConsumeAnyToken();
928 TokLoc = Tok.getLocation();
929 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
930 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
931 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +0000932 }
Steve Naroffd62701b2008-02-07 03:50:06 +0000933 return false;
934}
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936/// ParseAsmStatement - Parse a GNU extended asm statement.
937/// [GNU] asm-statement:
938/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
939///
940/// [GNU] asm-argument:
941/// asm-string-literal
942/// asm-string-literal ':' asm-operands[opt]
943/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
944/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
945/// ':' asm-clobbers
946///
947/// [GNU] asm-clobbers:
948/// asm-string-literal
949/// asm-clobbers ',' asm-string-literal
950///
Steve Naroffd62701b2008-02-07 03:50:06 +0000951Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000952 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +0000953 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000954
Steve Naroffb746ce82008-02-07 23:24:32 +0000955 if (getLang().Microsoft && Tok.isNot(tok::l_paren)) {
Steve Naroffd62701b2008-02-07 03:50:06 +0000956 msAsm = true;
957 return FuzzyParseMicrosoftAsmStatement();
958 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 DeclSpec DS;
960 SourceLocation Loc = Tok.getLocation();
961 ParseTypeQualifierListOpt(DS);
962
963 // GNU asms accept, but warn, about type-qualifiers other than volatile.
964 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
965 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
966 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
967 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
968
969 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +0000970 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +0000971 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000972 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 Diag(Tok, diag::err_expected_lparen_after, "asm");
974 SkipUntil(tok::r_paren);
975 return true;
976 }
977 Loc = ConsumeParen();
978
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000979 ExprResult AsmString = ParseAsmStringLiteral();
980 if (AsmString.isInvalid)
981 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +0000982
983 llvm::SmallVector<std::string, 4> Names;
984 llvm::SmallVector<ExprTy*, 4> Constraints;
985 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlssonb235fc22007-11-22 01:36:19 +0000986 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +0000987
Anders Carlssondfab34a2008-02-05 23:03:50 +0000988 unsigned NumInputs = 0, NumOutputs = 0;
989
990 SourceLocation RParenLoc;
991 if (Tok.is(tok::r_paren)) {
992 // We have a simple asm expression
993 isSimple = true;
994
995 RParenLoc = ConsumeParen();
996 } else {
997 // Parse Outputs, if present.
998 ParseAsmOperandsOpt(Names, Constraints, Exprs);
999
1000 NumOutputs = Names.size();
1001
1002 // Parse Inputs, if present.
1003 ParseAsmOperandsOpt(Names, Constraints, Exprs);
1004 assert(Names.size() == Constraints.size() &&
1005 Constraints.size() == Exprs.size()
1006 && "Input operand size mismatch!");
1007
1008 NumInputs = Names.size() - NumOutputs;
1009
1010 // Parse the clobbers, if present.
1011 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001012 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +00001013
1014 // Parse the asm-string list for clobbers.
1015 while (1) {
1016 ExprResult Clobber = ParseAsmStringLiteral();
1017
1018 if (Clobber.isInvalid)
1019 break;
1020
1021 Clobbers.push_back(Clobber.Val);
1022
1023 if (Tok.isNot(tok::comma)) break;
1024 ConsumeToken();
1025 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 }
Anders Carlssondfab34a2008-02-05 23:03:50 +00001027
1028 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 }
1030
Anders Carlssondfab34a2008-02-05 23:03:50 +00001031 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1032 NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001033 &Names[0], &Constraints[0], &Exprs[0],
1034 AsmString.Val,
1035 Clobbers.size(), &Clobbers[0],
1036 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001037}
1038
1039/// ParseAsmOperands - Parse the asm-operands production as used by
1040/// asm-statement. We also parse a leading ':' token. If the leading colon is
1041/// not present, we do not parse anything.
1042///
1043/// [GNU] asm-operands:
1044/// asm-operand
1045/// asm-operands ',' asm-operand
1046///
1047/// [GNU] asm-operand:
1048/// asm-string-literal '(' expression ')'
1049/// '[' identifier ']' asm-string-literal '(' expression ')'
1050///
Anders Carlssonb235fc22007-11-22 01:36:19 +00001051void Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
1052 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1053 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 // Only do anything if this operand is present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001055 if (Tok.isNot(tok::colon)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 ConsumeToken();
1057
1058 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001059 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 return;
1061
Anders Carlssonb235fc22007-11-22 01:36:19 +00001062 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001064 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 SourceLocation Loc = ConsumeBracket();
1066
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001067 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 Diag(Tok, diag::err_expected_ident);
1069 SkipUntil(tok::r_paren);
1070 return;
1071 }
Chris Lattner69efba72007-10-29 04:06:22 +00001072
Anders Carlssonb235fc22007-11-22 01:36:19 +00001073 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001074 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001075
1076 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001078 } else
1079 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001080
Anders Carlssonb235fc22007-11-22 01:36:19 +00001081 ExprResult Constraint = ParseAsmStringLiteral();
1082 if (Constraint.isInvalid) {
1083 SkipUntil(tok::r_paren);
1084 return;
1085 }
1086 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001087
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001088 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1090 SkipUntil(tok::r_paren);
1091 return;
1092 }
1093
1094 // Read the parenthesized expression.
1095 ExprResult Res = ParseSimpleParenExpression();
1096 if (Res.isInvalid) {
1097 SkipUntil(tok::r_paren);
1098 return;
1099 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001100 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 // Eat the comma and continue parsing if it exists.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001102 if (Tok.isNot(tok::comma)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 ConsumeToken();
1104 }
1105}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001106
1107Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1108 SourceLocation L, SourceLocation R) {
1109 // Do not enter a scope for the brace, as the arguments are in the same scope
1110 // (the function body) as the body itself. Instead, just read the statement
1111 // list and put it into a CompoundStmt for safe keeping.
1112 StmtResult FnBody = ParseCompoundStatementBody();
1113
1114 // If the function body could not be parsed, make a bogus compoundstmt.
1115 if (FnBody.isInvalid)
1116 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1117
1118 // Leave the function body scope.
1119 ExitScope();
1120
Steve Naroffd6d054d2007-11-11 23:20:51 +00001121 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001122}