blob: 68781c1be4c9961488e8da8e400896c52efa61d4 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
16#include "clang/Basic/Diagnostic.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner33ad2ca2006-11-05 23:47:55 +000018#include "clang/Parse/Scope.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.8: Statements and Blocks.
23//===----------------------------------------------------------------------===//
24
25/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
26/// StatementOrDeclaration:
27/// statement
28/// declaration
29///
30/// statement:
31/// labeled-statement
32/// compound-statement
33/// expression-statement
34/// selection-statement
35/// iteration-statement
36/// jump-statement
Fariborz Jahanian90814572007-10-04 20:19:06 +000037/// [OBC] objc-throw-statement
38/// [OBC] objc-try-catch-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000039/// [OBC] objc-synchronized-statement [TODO]
Chris Lattner0116c472006-08-15 06:03:28 +000040/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000041/// [OMP] openmp-construct [TODO]
42///
43/// labeled-statement:
44/// identifier ':' statement
45/// 'case' constant-expression ':' statement
46/// 'default' ':' statement
47///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// selection-statement:
49/// if-statement
50/// switch-statement
51///
52/// iteration-statement:
53/// while-statement
54/// do-statement
55/// for-statement
56///
Chris Lattner9075bd72006-08-10 04:59:57 +000057/// expression-statement:
58/// expression[opt] ';'
59///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000060/// jump-statement:
61/// 'goto' identifier ';'
62/// 'continue' ';'
63/// 'break' ';'
64/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000065/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000066///
Fariborz Jahanian90814572007-10-04 20:19:06 +000067/// [OBC] objc-throw-statement:
68/// [OBC] '@' 'throw' expression ';'
69/// [OBC] '@' 'throw' ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000070///
Chris Lattner30f910e2006-10-16 05:52:41 +000071Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000072 const char *SemiError = 0;
Chris Lattner30f910e2006-10-16 05:52:41 +000073 Parser::StmtResult Res;
Chris Lattner503fadc2006-08-10 05:45:44 +000074
75 // Cases in this switch statement should fall through if the parser expects
76 // the token to end in a semicolon (in which case SemiError should be set),
77 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000078 tok::TokenKind Kind = Tok.getKind();
79 SourceLocation AtLoc;
80 switch (Kind) {
Chris Lattnerf8afb622006-08-10 18:26:31 +000081 case tok::identifier: // C99 6.8.1: labeled-statement
82 // identifier ':' statement
83 // declaration (if !OnlyStatement)
84 // expression[opt] ';'
85 return ParseIdentifierStatement(OnlyStatement);
86
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
90 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_try)
91 return ParseObjCTryStmt(AtLoc);
92 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_throw)
93 return ParseObjCThrowStmt(AtLoc);
Fariborz Jahanian90814572007-10-04 20:19:06 +000094 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
95 if (Res.isInvalid) {
96 // If the expression is invalid, skip ahead to the next semicolon. Not
97 // doing this opens us up to the possibility of infinite loops if
98 // ParseExpression does not consume any tokens.
99 SkipUntil(tok::semi);
100 return true;
101 }
102 // Otherwise, eat the semicolon.
103 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
104 return Actions.ActOnExprStmt(Res.Val);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000105 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000106
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000107 default:
Fariborz Jahanian90814572007-10-04 20:19:06 +0000108 if (!OnlyStatement && isDeclarationSpecifier()) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000109 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000110 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000111 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +0000112 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +0000113 } else {
114 // expression[opt] ';'
Fariborz Jahanian90814572007-10-04 20:19:06 +0000115 ExprResult Res = ParseExpression();
Chris Lattner89c50c62006-08-11 06:41:18 +0000116 if (Res.isInvalid) {
117 // If the expression is invalid, skip ahead to the next semicolon. Not
118 // doing this opens us up to the possibility of infinite loops if
119 // ParseExpression does not consume any tokens.
120 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000121 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000122 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000123 // Otherwise, eat the semicolon.
124 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000125 return Actions.ActOnExprStmt(Res.Val);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000126 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000127
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000128 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000129 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000130 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000131 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000132
133 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000134 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000135 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff66356bd2007-09-16 14:56:35 +0000136 return Actions.ActOnNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000137
Chris Lattner9075bd72006-08-10 04:59:57 +0000138 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000139 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000140 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000141 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000142
Chris Lattner9075bd72006-08-10 04:59:57 +0000143 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000144 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000145 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000146 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000147 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000148 break;
149 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000150 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000151
152 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000153 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000154 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000155 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000156 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000157 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000158 SemiError = "continue statement";
159 break;
160 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000161 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000162 SemiError = "break statement";
163 break;
164 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000165 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000166 SemiError = "return statement";
167 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000168
169 case tok::kw_asm:
Chris Lattner30f910e2006-10-16 05:52:41 +0000170 Res = ParseAsmStatement();
Chris Lattner0116c472006-08-15 06:03:28 +0000171 SemiError = "asm statement";
172 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000173 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000174
175 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000176 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000177 ConsumeToken();
178 } else {
179 Diag(Tok, diag::err_expected_semi_after, SemiError);
180 SkipUntil(tok::semi);
181 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000182 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000183}
184
Chris Lattnerf8afb622006-08-10 18:26:31 +0000185/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
186/// have a bit of a quandry here. Reading the identifier is necessary to see if
187/// there is a ':' after it. If there is, this is a label, regardless of what
188/// else the identifier can mean. If not, this is either part of a declaration
189/// (if the identifier is a type-name) or part of an expression.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000190///
191/// labeled-statement:
192/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000193/// [GNU] identifier ':' attributes[opt] statement
Chris Lattner6dfd9782006-08-10 18:31:37 +0000194/// declaration (if !OnlyStatement)
195/// expression[opt] ';'
196///
Chris Lattner30f910e2006-10-16 05:52:41 +0000197Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000198 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Chris Lattner0663d2a2006-11-05 18:39:59 +0000199 "Not an identifier!");
Chris Lattner6dfd9782006-08-10 18:31:37 +0000200
Chris Lattner146762e2007-07-20 16:59:19 +0000201 Token IdentTok = Tok; // Save the whole token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000202 ConsumeToken(); // eat the identifier.
203
204 // identifier ':' statement
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000205 if (Tok.is(tok::colon)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000206 SourceLocation ColonLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000207
208 // Read label attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000209 DeclTy *AttrList = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000210 if (Tok.is(tok::kw___attribute))
Chris Lattner30f910e2006-10-16 05:52:41 +0000211 // TODO: save these somewhere.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000212 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +0000213
Chris Lattner30f910e2006-10-16 05:52:41 +0000214 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000215
216 // Broken substmt shouldn't prevent the label from being added to the AST.
217 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000218 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000219
Steve Naroff66356bd2007-09-16 14:56:35 +0000220 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000221 IdentTok.getIdentifierInfo(),
222 ColonLoc, SubStmt.Val);
Chris Lattner6dfd9782006-08-10 18:31:37 +0000223 }
224
Chris Lattner30f910e2006-10-16 05:52:41 +0000225 // Check to see if this is a declaration.
Chris Lattner49252eb2007-01-27 19:04:39 +0000226 void *TypeRep;
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000227 if (!OnlyStatement &&
Chris Lattner49252eb2007-01-27 19:04:39 +0000228 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000229 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000230 DeclSpec DS;
231
Chris Lattner49252eb2007-01-27 19:04:39 +0000232 // Add the typedef name to the start of the decl-specs.
233 const char *PrevSpec = 0;
234 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
235 IdentTok.getLocation(), PrevSpec,
236 TypeRep);
237 assert(!isInvalid && "First declspec can't be invalid!");
Fariborz Jahaniand7971132007-10-11 18:08:47 +0000238 if (Tok.is(tok::less)) {
239 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
240 ParseObjCProtocolReferences(ProtocolRefs);
241 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
242 new llvm::SmallVector<DeclTy *, 8>;
243 DS.setProtocolQualifiers(ProtocolDecl);
244 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
245 &ProtocolRefs[0], ProtocolRefs.size(),
246 *ProtocolDecl);
247 }
Chris Lattner0663d2a2006-11-05 18:39:59 +0000248
Chris Lattner2f9980e2006-08-10 18:39:24 +0000249 // ParseDeclarationSpecifiers will continue from there.
250 ParseDeclarationSpecifiers(DS);
251
Chris Lattner0e894622006-08-13 19:58:17 +0000252 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
253 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000254 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000255 // TODO: emit error on 'int;' or 'const enum foo;'.
256 // if (!DS.isMissingDeclaratorOk()) Diag(...);
257
258 ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000259 // FIXME: Return this as a type decl.
260 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000261 }
262
Chris Lattner2f9980e2006-08-10 18:39:24 +0000263 // Parse all the declarators.
264 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
265 ParseDeclarator(DeclaratorInfo);
266
Chris Lattner436806a2007-07-10 05:03:31 +0000267 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff66356bd2007-09-16 14:56:35 +0000268 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000269 }
270
Chris Lattner0c6c0342006-08-12 18:12:45 +0000271 // Otherwise, this is an expression. Seed it with II and parse it.
272 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
Chris Lattner30f910e2006-10-16 05:52:41 +0000273 if (Res.isInvalid) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000274 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000275 return true;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000276 } else if (Tok.isNot(tok::semi)) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000277 Diag(Tok, diag::err_expected_semi_after, "expression");
278 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000279 return true;
280 } else {
281 ConsumeToken();
Chris Lattnercd68f642007-06-27 01:06:29 +0000282 // Convert expr to a stmt.
Steve Naroff66356bd2007-09-16 14:56:35 +0000283 return Actions.ActOnExprStmt(Res.Val);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000284 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000285}
286
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000287/// ParseCaseStatement
288/// labeled-statement:
289/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000290/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000291///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000292/// Note that this does not parse the 'statement' at the end.
293///
Chris Lattner30f910e2006-10-16 05:52:41 +0000294Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000295 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000296 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000297
Chris Lattner30f910e2006-10-16 05:52:41 +0000298 ExprResult LHS = ParseConstantExpression();
299 if (LHS.isInvalid) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000300 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000301 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000302 }
303
304 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000305 SourceLocation DotDotDotLoc;
306 ExprTy *RHSVal = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000307 if (Tok.is(tok::ellipsis)) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000308 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000309 DotDotDotLoc = ConsumeToken();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000310
311 ExprResult RHS = ParseConstantExpression();
312 if (RHS.isInvalid) {
313 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000314 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000315 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000316 RHSVal = RHS.Val;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000317 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000318
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000319 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000320 Diag(Tok, diag::err_expected_colon_after, "'case'");
321 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000322 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000323 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000324
Chris Lattneraf635312006-10-16 06:06:51 +0000325 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000326
327 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000328 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000329 Diag(Tok, diag::err_label_end_of_compound_statement);
330 return true;
331 }
332
333 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000334
335 // Broken substmt shouldn't prevent the case from being added to the AST.
Chris Lattner30f910e2006-10-16 05:52:41 +0000336 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000337 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000338
Steve Naroff66356bd2007-09-16 14:56:35 +0000339 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Chris Lattner30f910e2006-10-16 05:52:41 +0000340 SubStmt.Val);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000341}
342
343/// ParseDefaultStatement
344/// labeled-statement:
345/// 'default' ':' statement
346/// Note that this does not parse the 'statement' at the end.
347///
Chris Lattner30f910e2006-10-16 05:52:41 +0000348Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000349 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000350 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000351
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000352 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000353 Diag(Tok, diag::err_expected_colon_after, "'default'");
354 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000355 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000356 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000357
Chris Lattneraf635312006-10-16 06:06:51 +0000358 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000359
360 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000361 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000362 Diag(Tok, diag::err_label_end_of_compound_statement);
363 return true;
364 }
365
366 StmtResult SubStmt = ParseStatement();
367 if (SubStmt.isInvalid)
368 return true;
369
Steve Naroff66356bd2007-09-16 14:56:35 +0000370 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000371}
372
373
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000374/// ParseCompoundStatement - Parse a "{}" block.
375///
376/// compound-statement: [C99 6.8.2]
377/// { block-item-list[opt] }
378/// [GNU] { label-declarations block-item-list } [TODO]
379///
380/// block-item-list:
381/// block-item
382/// block-item-list block-item
383///
384/// block-item:
385/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000386/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000387/// statement
388/// [OMP] openmp-directive [TODO]
389///
390/// [GNU] label-declarations:
391/// [GNU] label-declaration
392/// [GNU] label-declarations label-declaration
393///
394/// [GNU] label-declaration:
395/// [GNU] '__label__' identifier-list ';'
396///
397/// [OMP] openmp-directive: [TODO]
398/// [OMP] barrier-directive
399/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000400///
Chris Lattnercac27a52007-08-31 21:49:55 +0000401Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000402 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000403
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000404 // Enter a scope to hold everything within the compound stmt. Compound
405 // statements can always hold declarations.
406 EnterScope(Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000407
408 // Parse the statements in the body.
Chris Lattnercac27a52007-08-31 21:49:55 +0000409 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000410
411 ExitScope();
412 return Body;
413}
414
415
416/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000417/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000418/// consume the '}' at the end of the block. It does not manipulate the scope
419/// stack.
Chris Lattnercac27a52007-08-31 21:49:55 +0000420Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerf2978802007-01-21 06:52:16 +0000421 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
422
Chris Lattner010015a2007-05-28 07:23:54 +0000423 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000424 // only allowed at the start of a compound stmt regardless of the language.
Chris Lattner010015a2007-05-28 07:23:54 +0000425
Chris Lattner23b7eb62007-06-15 23:05:46 +0000426 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000427 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000428 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000429 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000430 R = ParseStatementOrDeclaration(false);
431 } else {
432 // __extension__ can start declarations and it can also be a unary
433 // operator for expressions. Consume multiple __extension__ markers here
434 // until we can determine which is which.
435 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000436 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000437 ConsumeToken();
438
439 // If this is the start of a declaration, parse it as such.
440 if (isDeclarationSpecifier()) {
441 // FIXME: Save the __extension__ on the decl as a node somehow.
442 // FIXME: disable extwarns.
Steve Naroff66356bd2007-09-16 14:56:35 +0000443 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000444 } else {
445 // Otherwise this was a unary __extension__ marker. Parse the
446 // subexpression and add the __extension__ unary op.
447 // FIXME: disable extwarns.
448 ExprResult Res = ParseCastExpression(false);
449 if (Res.isInvalid) {
450 SkipUntil(tok::semi);
451 continue;
452 }
453
454 // Add the __extension__ node to the AST.
Steve Naroff83895f72007-09-16 03:34:24 +0000455 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000456 if (Res.isInvalid)
457 continue;
458
459 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
460 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000461 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000462 }
463 }
464
Chris Lattner30f910e2006-10-16 05:52:41 +0000465 if (!R.isInvalid && R.Val)
466 Stmts.push_back(R.Val);
467 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000468
469 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000470 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000471 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner30f910e2006-10-16 05:52:41 +0000472 return 0;
473 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000474
Chris Lattner04132372006-10-16 06:12:55 +0000475 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff66356bd2007-09-16 14:56:35 +0000476 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattnercac27a52007-08-31 21:49:55 +0000477 &Stmts[0], Stmts.size(), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000478}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000479
480/// ParseIfStatement
481/// if-statement: [C99 6.8.4.1]
482/// 'if' '(' expression ')' statement
483/// 'if' '(' expression ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000484///
485Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000486 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000487 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000488
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000489 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000490 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000491 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000492 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000493 }
494
Chris Lattner2dd1b722007-08-26 23:08:06 +0000495 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
496 // the case for C90.
497 if (getLang().C99)
498 EnterScope(Scope::DeclScope);
499
Chris Lattnerc951dae2006-08-10 04:23:57 +0000500 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000501 ExprResult CondExp = ParseSimpleParenExpression();
502 if (CondExp.isInvalid) {
503 SkipUntil(tok::semi);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000504 if (getLang().C99)
505 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000506 return true;
507 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000508
Chris Lattner8fb26252007-08-22 05:28:50 +0000509 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000510 // there is no compound stmt. C90 does not have this clause. We only do this
511 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000512 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000513 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000514
Chris Lattnerc951dae2006-08-10 04:23:57 +0000515 // Read the if condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000516 StmtResult CondStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000517
518 // Broken substmt shouldn't prevent the label from being added to the AST.
519 if (CondStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000520 CondStmt = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000521
Chris Lattner37e54f42007-08-22 05:16:28 +0000522 // Pop the 'if' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000523 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000524
525 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000526 SourceLocation ElseLoc;
527 StmtResult ElseStmt(false);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000528 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000529 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000530
Chris Lattner8fb26252007-08-22 05:28:50 +0000531 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000532 // there is no compound stmt. C90 does not have this clause. We only do
533 // this if the body isn't a compound statement to avoid push/pop in common
534 // cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000535 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000536 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000537
Chris Lattner30f910e2006-10-16 05:52:41 +0000538 ElseStmt = ParseStatement();
Chris Lattner37e54f42007-08-22 05:16:28 +0000539
540 // Pop the 'else' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000541 if (NeedsInnerScope) ExitScope();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000542
543 if (ElseStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000544 ElseStmt = Actions.ActOnNullStmt(ElseLoc);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000545 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000546
Chris Lattner2dd1b722007-08-26 23:08:06 +0000547 if (getLang().C99)
548 ExitScope();
549
Steve Naroff66356bd2007-09-16 14:56:35 +0000550 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
Chris Lattner30f910e2006-10-16 05:52:41 +0000551 ElseLoc, ElseStmt.Val);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000552}
553
Chris Lattner9075bd72006-08-10 04:59:57 +0000554/// ParseSwitchStatement
555/// switch-statement:
556/// 'switch' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000557Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000558 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000559 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000560
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000561 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000562 Diag(Tok, diag::err_expected_lparen_after, "switch");
563 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000564 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000565 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000566
567 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
568 // not the case for C90. Start the switch scope.
569 if (getLang().C99)
570 EnterScope(Scope::BreakScope|Scope::DeclScope);
571 else
572 EnterScope(Scope::BreakScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000573
Chris Lattner9075bd72006-08-10 04:59:57 +0000574 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000575 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000576
Anders Carlsson51873c22007-07-22 07:07:56 +0000577 if (Cond.isInvalid) {
578 ExitScope();
579 return true;
580 }
581
Steve Naroff66356bd2007-09-16 14:56:35 +0000582 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlsson51873c22007-07-22 07:07:56 +0000583
Chris Lattner8fb26252007-08-22 05:28:50 +0000584 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000585 // there is no compound stmt. C90 does not have this clause. We only do this
586 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000587 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000588 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000589
Chris Lattner9075bd72006-08-10 04:59:57 +0000590 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000591 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000592
Chris Lattner8fb26252007-08-22 05:28:50 +0000593 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000594 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000595
Anders Carlsson51873c22007-07-22 07:07:56 +0000596 if (Body.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000597 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000598 // FIXME: Remove the case statement list from the Switch statement.
599 }
600
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000601 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000602
Steve Naroff66356bd2007-09-16 14:56:35 +0000603 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000604}
605
606/// ParseWhileStatement
607/// while-statement: [C99 6.8.5.1]
608/// 'while' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000609Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000610 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000611 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000612 ConsumeToken(); // eat the 'while'.
613
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000614 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000615 Diag(Tok, diag::err_expected_lparen_after, "while");
616 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000617 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000618 }
619
Chris Lattner2dd1b722007-08-26 23:08:06 +0000620 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
621 // the case for C90. Start the loop scope.
622 if (getLang().C99)
623 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
624 else
625 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000626
Chris Lattner9075bd72006-08-10 04:59:57 +0000627 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000628 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000629
Chris Lattner8fb26252007-08-22 05:28:50 +0000630 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000631 // there is no compound stmt. C90 does not have this clause. We only do this
632 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000633 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000634 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000635
Chris Lattner9075bd72006-08-10 04:59:57 +0000636 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000637 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000638
Chris Lattner8fb26252007-08-22 05:28:50 +0000639 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000640 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000641
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000642 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000643
644 if (Cond.isInvalid || Body.isInvalid) return true;
645
Steve Naroff66356bd2007-09-16 14:56:35 +0000646 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000647}
648
649/// ParseDoStatement
650/// do-statement: [C99 6.8.5.2]
651/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000652/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000653Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000654 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000655 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000656
Chris Lattner2dd1b722007-08-26 23:08:06 +0000657 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
658 // the case for C90. Start the loop scope.
659 if (getLang().C99)
660 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
661 else
662 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000663
Chris Lattner8fb26252007-08-22 05:28:50 +0000664 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000665 // there is no compound stmt. C90 does not have this clause. We only do this
666 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000667 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000668 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000669
Chris Lattner9075bd72006-08-10 04:59:57 +0000670 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000671 StmtResult Body = ParseStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000672
Chris Lattner8fb26252007-08-22 05:28:50 +0000673 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000674 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000675
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000676 if (Tok.isNot(tok::kw_while)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000677 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000678 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000679 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000680 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000681 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000682 }
Chris Lattneraf635312006-10-16 06:06:51 +0000683 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000684
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000685 if (Tok.isNot(tok::l_paren)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000686 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000687 Diag(Tok, diag::err_expected_lparen_after, "do/while");
688 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000689 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000690 }
691
692 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000693 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000694
695 ExitScope();
696
Chris Lattner30f910e2006-10-16 05:52:41 +0000697 if (Cond.isInvalid || Body.isInvalid) return true;
698
Steve Naroff66356bd2007-09-16 14:56:35 +0000699 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000700}
701
702/// ParseForStatement
703/// for-statement: [C99 6.8.5.3]
704/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
705/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000706Parser::StmtResult Parser::ParseForStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000707 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000708 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000709
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000710 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000711 Diag(Tok, diag::err_expected_lparen_after, "for");
712 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000713 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000714 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000715
Chris Lattner2dd1b722007-08-26 23:08:06 +0000716 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
717 // the case for C90. Start the loop scope.
718 if (getLang().C99)
719 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
720 else
721 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner9075bd72006-08-10 04:59:57 +0000722
Chris Lattner04132372006-10-16 06:12:55 +0000723 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000724 ExprResult Value;
725
Chris Lattner71e23ce2006-11-04 20:18:38 +0000726 StmtTy *FirstPart = 0;
Chris Lattnercd68f642007-06-27 01:06:29 +0000727 ExprTy *SecondPart = 0;
728 StmtTy *ThirdPart = 0;
Chris Lattner71e23ce2006-11-04 20:18:38 +0000729
Chris Lattner9075bd72006-08-10 04:59:57 +0000730 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000731 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000732 // no first part, eat the ';'.
733 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000734 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000735 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000736 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
737 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000738 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff66356bd2007-09-16 14:56:35 +0000739 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000740 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Chris Lattner9075bd72006-08-10 04:59:57 +0000741 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000742 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000743
Chris Lattnercd68f642007-06-27 01:06:29 +0000744 // Turn the expression into a stmt.
745 if (!Value.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000746 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattnercd68f642007-06-27 01:06:29 +0000747 if (!R.isInvalid)
748 FirstPart = R.Val;
749 }
Steve Naroff9992bba2007-05-30 16:27:15 +0000750
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000751 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000752 ConsumeToken();
753 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000754 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000755 SkipUntil(tok::semi);
756 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000757 }
758
759 // Parse the second part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000760 if (Tok.is(tok::semi)) { // for (...;;
Chris Lattner9075bd72006-08-10 04:59:57 +0000761 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000762 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000763 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000764 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000765 if (!Value.isInvalid)
766 SecondPart = Value.Val;
Chris Lattner9075bd72006-08-10 04:59:57 +0000767 }
768
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000769 if (Tok.is(tok::semi)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000770 ConsumeToken();
771 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000772 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000773 SkipUntil(tok::semi);
774 }
775
776 // Parse the third part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000777 if (Tok.is(tok::r_paren)) { // for (...;...;)
Chris Lattner9075bd72006-08-10 04:59:57 +0000778 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000779 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000780 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000781 Value = ParseExpression();
Chris Lattnercd68f642007-06-27 01:06:29 +0000782 if (!Value.isInvalid) {
783 // Turn the expression into a stmt.
Steve Naroff66356bd2007-09-16 14:56:35 +0000784 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattnercd68f642007-06-27 01:06:29 +0000785 if (!R.isInvalid)
786 ThirdPart = R.Val;
787 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000788 }
789
Chris Lattner4564bc12006-08-10 23:14:52 +0000790 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000791 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000792
Chris Lattner8fb26252007-08-22 05:28:50 +0000793 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000794 // there is no compound stmt. C90 does not have this clause. We only do this
795 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000796 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000797 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000798
Chris Lattner9075bd72006-08-10 04:59:57 +0000799 // Read the body statement.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000800 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000801
Chris Lattner8fb26252007-08-22 05:28:50 +0000802 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000803 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000804
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000805 // Leave the for-scope.
806 ExitScope();
807
Chris Lattner71e23ce2006-11-04 20:18:38 +0000808 if (Body.isInvalid)
809 return Body;
Chris Lattner30f910e2006-10-16 05:52:41 +0000810
Steve Naroff66356bd2007-09-16 14:56:35 +0000811 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
Chris Lattner71e23ce2006-11-04 20:18:38 +0000812 ThirdPart, RParenLoc, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000813}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000814
Chris Lattner503fadc2006-08-10 05:45:44 +0000815/// ParseGotoStatement
816/// jump-statement:
817/// 'goto' identifier ';'
818/// [GNU] 'goto' '*' expression ';'
819///
820/// Note: this lets the caller parse the end ';'.
821///
Chris Lattner30f910e2006-10-16 05:52:41 +0000822Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000823 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000824 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000825
Chris Lattner30f910e2006-10-16 05:52:41 +0000826 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000827 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000828 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000829 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000830 ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000831 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000832 // GNU indirect goto extension.
833 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000834 SourceLocation StarLoc = ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000835 ExprResult R = ParseExpression();
Chris Lattner30f910e2006-10-16 05:52:41 +0000836 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000837 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000838 return true;
839 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000840 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattnere34b2c22007-07-22 04:13:33 +0000841 } else {
842 Diag(Tok, diag::err_expected_ident);
843 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000844 }
Chris Lattnere34b2c22007-07-22 04:13:33 +0000845
Chris Lattner30f910e2006-10-16 05:52:41 +0000846 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000847}
848
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000849/// ParseContinueStatement
850/// jump-statement:
851/// 'continue' ';'
852///
853/// Note: this lets the caller parse the end ';'.
854///
855Parser::StmtResult Parser::ParseContinueStatement() {
856 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000857 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000858}
859
860/// ParseBreakStatement
861/// jump-statement:
862/// 'break' ';'
863///
864/// Note: this lets the caller parse the end ';'.
865///
866Parser::StmtResult Parser::ParseBreakStatement() {
867 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000868 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000869}
870
Chris Lattner503fadc2006-08-10 05:45:44 +0000871/// ParseReturnStatement
872/// jump-statement:
873/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +0000874Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000875 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000876 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000877
Chris Lattner30f910e2006-10-16 05:52:41 +0000878 ExprResult R(0);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000879 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000880 R = ParseExpression();
881 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000882 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000883 return true;
884 }
Chris Lattnera0927ce2006-08-12 16:59:03 +0000885 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000886 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Chris Lattner503fadc2006-08-10 05:45:44 +0000887}
Chris Lattner0116c472006-08-15 06:03:28 +0000888
889/// ParseAsmStatement - Parse a GNU extended asm statement.
890/// [GNU] asm-statement:
891/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
892///
893/// [GNU] asm-argument:
894/// asm-string-literal
895/// asm-string-literal ':' asm-operands[opt]
896/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
897/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
898/// ':' asm-clobbers
899///
900/// [GNU] asm-clobbers:
901/// asm-string-literal
902/// asm-clobbers ',' asm-string-literal
903///
Chris Lattner30f910e2006-10-16 05:52:41 +0000904Parser::StmtResult Parser::ParseAsmStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000905 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +0000906 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner0116c472006-08-15 06:03:28 +0000907
908 DeclSpec DS;
909 SourceLocation Loc = Tok.getLocation();
910 ParseTypeQualifierListOpt(DS);
911
912 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +0000913 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner0116c472006-08-15 06:03:28 +0000914 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
Chris Lattnera925dc62006-11-28 04:33:46 +0000915 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner0116c472006-08-15 06:03:28 +0000916 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
917
918 // Remember if this was a volatile asm.
Chris Lattner1f496802006-10-18 04:02:28 +0000919 //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
Chris Lattner0116c472006-08-15 06:03:28 +0000920
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000921 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +0000922 Diag(Tok, diag::err_expected_lparen_after, "asm");
923 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +0000924 return true;
Chris Lattner0116c472006-08-15 06:03:28 +0000925 }
Chris Lattner04132372006-10-16 06:12:55 +0000926 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +0000927
928 ParseAsmStringLiteral();
929
930 // Parse Outputs, if present.
931 ParseAsmOperandsOpt();
932
933 // Parse Inputs, if present.
934 ParseAsmOperandsOpt();
935
936 // Parse the clobbers, if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000937 if (Tok.is(tok::colon)) {
Chris Lattner0116c472006-08-15 06:03:28 +0000938 ConsumeToken();
939
Chris Lattnerd3e98952006-10-06 05:22:26 +0000940 if (isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000941 // Parse the asm-string list for clobbers.
942 while (1) {
943 ParseAsmStringLiteral();
944
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000945 if (Tok.isNot(tok::comma)) break;
Chris Lattner0116c472006-08-15 06:03:28 +0000946 ConsumeToken();
947 }
948 }
949 }
950
Chris Lattner73c56c02007-10-29 04:04:16 +0000951 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000952
Chris Lattner73c56c02007-10-29 04:04:16 +0000953 // FIXME: Pass all the details down to the action.
954 return Actions.ActOnAsmStmt(AsmLoc, RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +0000955}
956
957/// ParseAsmOperands - Parse the asm-operands production as used by
958/// asm-statement. We also parse a leading ':' token. If the leading colon is
959/// not present, we do not parse anything.
960///
961/// [GNU] asm-operands:
962/// asm-operand
963/// asm-operands ',' asm-operand
964///
965/// [GNU] asm-operand:
966/// asm-string-literal '(' expression ')'
967/// '[' identifier ']' asm-string-literal '(' expression ')'
968///
969void Parser::ParseAsmOperandsOpt() {
970 // Only do anything if this operand is present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000971 if (Tok.isNot(tok::colon)) return;
Chris Lattner0116c472006-08-15 06:03:28 +0000972 ConsumeToken();
973
974 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000975 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Chris Lattner0116c472006-08-15 06:03:28 +0000976 return;
977
978 while (1) {
979 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000980 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +0000981 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +0000982
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000983 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +0000984 Diag(Tok, diag::err_expected_ident);
985 SkipUntil(tok::r_paren);
986 return;
987 }
988 MatchRHSPunctuation(tok::r_square, Loc);
989 }
990
991 ParseAsmStringLiteral();
992
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000993 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +0000994 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
995 SkipUntil(tok::r_paren);
996 return;
997 }
998
999 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001000 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +00001001 if (Res.isInvalid) {
1002 SkipUntil(tok::r_paren);
1003 return;
1004 }
1005
1006 // Eat the comma and continue parsing if it exists.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001007 if (Tok.isNot(tok::comma)) return;
Chris Lattner0116c472006-08-15 06:03:28 +00001008 ConsumeToken();
1009 }
1010}