blob: 1eac2549d7666953adaea8cc8a2cb020060dd07c [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//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +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"
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
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000039/// [OBC] objc-synchronized-statement
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 @
Chris Lattner11221032007-12-27 20:29:42 +000090 if (Tok.isObjCAtKeyword(tok::objc_try))
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000091 return ParseObjCTryStmt(AtLoc);
Chris Lattner11221032007-12-27 20:29:42 +000092 else if (Tok.isObjCAtKeyword(tok::objc_throw))
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000093 return ParseObjCThrowStmt(AtLoc);
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000094 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
95 return ParseObjCSynchronizedStmt(AtLoc);
Fariborz Jahanian90814572007-10-04 20:19:06 +000096 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
97 if (Res.isInvalid) {
98 // If the expression is invalid, skip ahead to the next semicolon. Not
99 // doing this opens us up to the possibility of infinite loops if
100 // ParseExpression does not consume any tokens.
101 SkipUntil(tok::semi);
102 return true;
103 }
104 // Otherwise, eat the semicolon.
105 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
106 return Actions.ActOnExprStmt(Res.Val);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000107 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000108
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000109 default:
Fariborz Jahanian90814572007-10-04 20:19:06 +0000110 if (!OnlyStatement && isDeclarationSpecifier()) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000111 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000112 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000113 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +0000114 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +0000115 } else {
116 // expression[opt] ';'
Fariborz Jahanian90814572007-10-04 20:19:06 +0000117 ExprResult Res = ParseExpression();
Chris Lattner89c50c62006-08-11 06:41:18 +0000118 if (Res.isInvalid) {
119 // If the expression is invalid, skip ahead to the next semicolon. Not
120 // doing this opens us up to the possibility of infinite loops if
121 // ParseExpression does not consume any tokens.
122 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000123 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000124 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000125 // Otherwise, eat the semicolon.
126 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000127 return Actions.ActOnExprStmt(Res.Val);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000128 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000129
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000130 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000131 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000132 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000133 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000134
135 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000136 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000137 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff66356bd2007-09-16 14:56:35 +0000138 return Actions.ActOnNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000139
Chris Lattner9075bd72006-08-10 04:59:57 +0000140 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000141 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000142 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000143 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000144
Chris Lattner9075bd72006-08-10 04:59:57 +0000145 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000146 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000147 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000148 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000149 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000150 break;
151 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000152 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000153
154 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000155 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000156 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000157 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000158 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000159 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000160 SemiError = "continue statement";
161 break;
162 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000163 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000164 SemiError = "break statement";
165 break;
166 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000167 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000168 SemiError = "return statement";
169 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000170
171 case tok::kw_asm:
Chris Lattner30f910e2006-10-16 05:52:41 +0000172 Res = ParseAsmStatement();
Chris Lattner0116c472006-08-15 06:03:28 +0000173 SemiError = "asm statement";
174 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000175 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000176
177 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000178 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000179 ConsumeToken();
180 } else {
181 Diag(Tok, diag::err_expected_semi_after, SemiError);
182 SkipUntil(tok::semi);
183 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000184 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000185}
186
Chris Lattnerf8afb622006-08-10 18:26:31 +0000187/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
188/// have a bit of a quandry here. Reading the identifier is necessary to see if
189/// there is a ':' after it. If there is, this is a label, regardless of what
190/// else the identifier can mean. If not, this is either part of a declaration
191/// (if the identifier is a type-name) or part of an expression.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000192///
193/// labeled-statement:
194/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000195/// [GNU] identifier ':' attributes[opt] statement
Chris Lattner6dfd9782006-08-10 18:31:37 +0000196/// declaration (if !OnlyStatement)
197/// expression[opt] ';'
198///
Chris Lattner30f910e2006-10-16 05:52:41 +0000199Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000200 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Chris Lattner0663d2a2006-11-05 18:39:59 +0000201 "Not an identifier!");
Chris Lattner6dfd9782006-08-10 18:31:37 +0000202
Chris Lattner146762e2007-07-20 16:59:19 +0000203 Token IdentTok = Tok; // Save the whole token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000204 ConsumeToken(); // eat the identifier.
205
206 // identifier ':' statement
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000207 if (Tok.is(tok::colon)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000208 SourceLocation ColonLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000209
210 // Read label attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000211 DeclTy *AttrList = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000212 if (Tok.is(tok::kw___attribute))
Chris Lattner30f910e2006-10-16 05:52:41 +0000213 // TODO: save these somewhere.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000214 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +0000215
Chris Lattner30f910e2006-10-16 05:52:41 +0000216 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000217
218 // Broken substmt shouldn't prevent the label from being added to the AST.
219 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000220 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000221
Steve Naroff66356bd2007-09-16 14:56:35 +0000222 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000223 IdentTok.getIdentifierInfo(),
224 ColonLoc, SubStmt.Val);
Chris Lattner6dfd9782006-08-10 18:31:37 +0000225 }
226
Chris Lattner30f910e2006-10-16 05:52:41 +0000227 // Check to see if this is a declaration.
Chris Lattner49252eb2007-01-27 19:04:39 +0000228 void *TypeRep;
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000229 if (!OnlyStatement &&
Chris Lattner49252eb2007-01-27 19:04:39 +0000230 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000231 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000232 DeclSpec DS;
233
Chris Lattner49252eb2007-01-27 19:04:39 +0000234 // Add the typedef name to the start of the decl-specs.
235 const char *PrevSpec = 0;
236 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
237 IdentTok.getLocation(), PrevSpec,
238 TypeRep);
239 assert(!isInvalid && "First declspec can't be invalid!");
Steve Naroffc5484042007-10-30 02:23:23 +0000240 SourceLocation endProtoLoc;
Fariborz Jahaniand7971132007-10-11 18:08:47 +0000241 if (Tok.is(tok::less)) {
242 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffc5484042007-10-30 02:23:23 +0000243 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahaniand7971132007-10-11 18:08:47 +0000244 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
245 new llvm::SmallVector<DeclTy *, 8>;
246 DS.setProtocolQualifiers(ProtocolDecl);
247 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
248 &ProtocolRefs[0], ProtocolRefs.size(),
249 *ProtocolDecl);
250 }
Chris Lattner0663d2a2006-11-05 18:39:59 +0000251
Chris Lattner2f9980e2006-08-10 18:39:24 +0000252 // ParseDeclarationSpecifiers will continue from there.
253 ParseDeclarationSpecifiers(DS);
254
Chris Lattner0e894622006-08-13 19:58:17 +0000255 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
256 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000257 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000258 // TODO: emit error on 'int;' or 'const enum foo;'.
259 // if (!DS.isMissingDeclaratorOk()) Diag(...);
260
261 ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000262 // FIXME: Return this as a type decl.
263 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000264 }
265
Chris Lattner2f9980e2006-08-10 18:39:24 +0000266 // Parse all the declarators.
267 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
268 ParseDeclarator(DeclaratorInfo);
269
Chris Lattner436806a2007-07-10 05:03:31 +0000270 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff66356bd2007-09-16 14:56:35 +0000271 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000272 }
273
Chris Lattner0c6c0342006-08-12 18:12:45 +0000274 // Otherwise, this is an expression. Seed it with II and parse it.
275 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
Chris Lattner30f910e2006-10-16 05:52:41 +0000276 if (Res.isInvalid) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000277 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000278 return true;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000279 } else if (Tok.isNot(tok::semi)) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000280 Diag(Tok, diag::err_expected_semi_after, "expression");
281 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000282 return true;
283 } else {
284 ConsumeToken();
Chris Lattnercd68f642007-06-27 01:06:29 +0000285 // Convert expr to a stmt.
Steve Naroff66356bd2007-09-16 14:56:35 +0000286 return Actions.ActOnExprStmt(Res.Val);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000287 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000288}
289
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000290/// ParseCaseStatement
291/// labeled-statement:
292/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000293/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000294///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000295/// Note that this does not parse the 'statement' at the end.
296///
Chris Lattner30f910e2006-10-16 05:52:41 +0000297Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000298 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000299 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000300
Chris Lattner30f910e2006-10-16 05:52:41 +0000301 ExprResult LHS = ParseConstantExpression();
302 if (LHS.isInvalid) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000303 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000304 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000305 }
306
307 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000308 SourceLocation DotDotDotLoc;
309 ExprTy *RHSVal = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000310 if (Tok.is(tok::ellipsis)) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000311 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000312 DotDotDotLoc = ConsumeToken();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000313
314 ExprResult RHS = ParseConstantExpression();
315 if (RHS.isInvalid) {
316 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000317 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000318 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000319 RHSVal = RHS.Val;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000320 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000321
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000322 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000323 Diag(Tok, diag::err_expected_colon_after, "'case'");
324 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000325 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000326 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000327
Chris Lattneraf635312006-10-16 06:06:51 +0000328 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000329
330 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000331 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000332 Diag(Tok, diag::err_label_end_of_compound_statement);
333 return true;
334 }
335
336 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000337
338 // Broken substmt shouldn't prevent the case from being added to the AST.
Chris Lattner30f910e2006-10-16 05:52:41 +0000339 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000340 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000341
Steve Naroff66356bd2007-09-16 14:56:35 +0000342 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Chris Lattner30f910e2006-10-16 05:52:41 +0000343 SubStmt.Val);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000344}
345
346/// ParseDefaultStatement
347/// labeled-statement:
348/// 'default' ':' statement
349/// Note that this does not parse the 'statement' at the end.
350///
Chris Lattner30f910e2006-10-16 05:52:41 +0000351Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000352 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000353 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000354
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000355 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000356 Diag(Tok, diag::err_expected_colon_after, "'default'");
357 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000358 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000359 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000360
Chris Lattneraf635312006-10-16 06:06:51 +0000361 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000362
363 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000364 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000365 Diag(Tok, diag::err_label_end_of_compound_statement);
366 return true;
367 }
368
369 StmtResult SubStmt = ParseStatement();
370 if (SubStmt.isInvalid)
371 return true;
372
Steve Naroff66356bd2007-09-16 14:56:35 +0000373 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000374}
375
376
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000377/// ParseCompoundStatement - Parse a "{}" block.
378///
379/// compound-statement: [C99 6.8.2]
380/// { block-item-list[opt] }
381/// [GNU] { label-declarations block-item-list } [TODO]
382///
383/// block-item-list:
384/// block-item
385/// block-item-list block-item
386///
387/// block-item:
388/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000389/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000390/// statement
391/// [OMP] openmp-directive [TODO]
392///
393/// [GNU] label-declarations:
394/// [GNU] label-declaration
395/// [GNU] label-declarations label-declaration
396///
397/// [GNU] label-declaration:
398/// [GNU] '__label__' identifier-list ';'
399///
400/// [OMP] openmp-directive: [TODO]
401/// [OMP] barrier-directive
402/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000403///
Chris Lattnercac27a52007-08-31 21:49:55 +0000404Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000405 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000406
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000407 // Enter a scope to hold everything within the compound stmt. Compound
408 // statements can always hold declarations.
409 EnterScope(Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000410
411 // Parse the statements in the body.
Chris Lattnercac27a52007-08-31 21:49:55 +0000412 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000413
414 ExitScope();
415 return Body;
416}
417
418
419/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000420/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000421/// consume the '}' at the end of the block. It does not manipulate the scope
422/// stack.
Chris Lattnercac27a52007-08-31 21:49:55 +0000423Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerf2978802007-01-21 06:52:16 +0000424 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
425
Chris Lattner010015a2007-05-28 07:23:54 +0000426 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000427 // only allowed at the start of a compound stmt regardless of the language.
Chris Lattner010015a2007-05-28 07:23:54 +0000428
Chris Lattner23b7eb62007-06-15 23:05:46 +0000429 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000430 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000431 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000432 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000433 R = ParseStatementOrDeclaration(false);
434 } else {
435 // __extension__ can start declarations and it can also be a unary
436 // operator for expressions. Consume multiple __extension__ markers here
437 // until we can determine which is which.
438 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000439 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000440 ConsumeToken();
441
442 // If this is the start of a declaration, parse it as such.
443 if (isDeclarationSpecifier()) {
444 // FIXME: Save the __extension__ on the decl as a node somehow.
445 // FIXME: disable extwarns.
Steve Naroff66356bd2007-09-16 14:56:35 +0000446 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000447 } else {
448 // Otherwise this was a unary __extension__ marker. Parse the
449 // subexpression and add the __extension__ unary op.
450 // FIXME: disable extwarns.
451 ExprResult Res = ParseCastExpression(false);
452 if (Res.isInvalid) {
453 SkipUntil(tok::semi);
454 continue;
455 }
456
457 // Add the __extension__ node to the AST.
Steve Naroff83895f72007-09-16 03:34:24 +0000458 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000459 if (Res.isInvalid)
460 continue;
461
462 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
463 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000464 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000465 }
466 }
467
Chris Lattner30f910e2006-10-16 05:52:41 +0000468 if (!R.isInvalid && R.Val)
469 Stmts.push_back(R.Val);
470 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000471
472 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000473 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000474 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner30f910e2006-10-16 05:52:41 +0000475 return 0;
476 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000477
Chris Lattner04132372006-10-16 06:12:55 +0000478 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff66356bd2007-09-16 14:56:35 +0000479 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattnercac27a52007-08-31 21:49:55 +0000480 &Stmts[0], Stmts.size(), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000481}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000482
483/// ParseIfStatement
484/// if-statement: [C99 6.8.4.1]
485/// 'if' '(' expression ')' statement
486/// 'if' '(' expression ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000487///
488Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000489 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000490 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000491
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000492 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000493 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000494 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000495 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000496 }
497
Chris Lattner2dd1b722007-08-26 23:08:06 +0000498 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
499 // the case for C90.
500 if (getLang().C99)
501 EnterScope(Scope::DeclScope);
502
Chris Lattnerc951dae2006-08-10 04:23:57 +0000503 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000504 ExprResult CondExp = ParseSimpleParenExpression();
505 if (CondExp.isInvalid) {
506 SkipUntil(tok::semi);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000507 if (getLang().C99)
508 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000509 return true;
510 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000511
Chris Lattner8fb26252007-08-22 05:28:50 +0000512 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000513 // there is no compound stmt. C90 does not have this clause. We only do this
514 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000515 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000516 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000517
Chris Lattner5c5808a2007-10-29 05:08:52 +0000518 // Read the 'then' stmt.
519 SourceLocation ThenStmtLoc = Tok.getLocation();
520 StmtResult ThenStmt = ParseStatement();
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;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000527 SourceLocation ElseStmtLoc;
Chris Lattner30f910e2006-10-16 05:52:41 +0000528 StmtResult ElseStmt(false);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000529
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000530 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000531 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000532
Chris Lattner8fb26252007-08-22 05:28:50 +0000533 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000534 // there is no compound stmt. C90 does not have this clause. We only do
535 // this if the body isn't a compound statement to avoid push/pop in common
536 // cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000537 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000538 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000539
Chris Lattner5c5808a2007-10-29 05:08:52 +0000540 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000541 ElseStmt = ParseStatement();
Chris Lattner37e54f42007-08-22 05:16:28 +0000542
543 // Pop the 'else' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000544 if (NeedsInnerScope) ExitScope();
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
Chris Lattner5c5808a2007-10-29 05:08:52 +0000550 // If the then or else stmt is invalid and the other is valid (and present),
551 // make turn the invalid one into a null stmt to avoid dropping the other
552 // part. If both are invalid, return error.
553 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
554 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
555 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
556 // Both invalid, or one is invalid and other is non-present: delete cond and
557 // return error.
558 Actions.DeleteExpr(CondExp.Val);
559 return true;
560 }
561
562 // Now if either are invalid, replace with a ';'.
563 if (ThenStmt.isInvalid)
564 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
565 if (ElseStmt.isInvalid)
566 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
567
568
569
570 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Chris Lattner30f910e2006-10-16 05:52:41 +0000571 ElseLoc, ElseStmt.Val);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000572}
573
Chris Lattner9075bd72006-08-10 04:59:57 +0000574/// ParseSwitchStatement
575/// switch-statement:
576/// 'switch' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000577Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000578 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000579 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000580
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000581 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000582 Diag(Tok, diag::err_expected_lparen_after, "switch");
583 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000584 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000585 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000586
587 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
588 // not the case for C90. Start the switch scope.
589 if (getLang().C99)
590 EnterScope(Scope::BreakScope|Scope::DeclScope);
591 else
592 EnterScope(Scope::BreakScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000593
Chris Lattner9075bd72006-08-10 04:59:57 +0000594 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000595 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000596
Anders Carlsson51873c22007-07-22 07:07:56 +0000597 if (Cond.isInvalid) {
598 ExitScope();
599 return true;
600 }
601
Steve Naroff66356bd2007-09-16 14:56:35 +0000602 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlsson51873c22007-07-22 07:07:56 +0000603
Chris Lattner8fb26252007-08-22 05:28:50 +0000604 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000605 // there is no compound stmt. C90 does not have this clause. We only do this
606 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000607 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000608 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000609
Chris Lattner9075bd72006-08-10 04:59:57 +0000610 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000611 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000612
Chris Lattner8fb26252007-08-22 05:28:50 +0000613 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000614 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000615
Anders Carlsson51873c22007-07-22 07:07:56 +0000616 if (Body.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000617 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000618 // FIXME: Remove the case statement list from the Switch statement.
619 }
620
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000621 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000622
Steve Naroff66356bd2007-09-16 14:56:35 +0000623 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000624}
625
626/// ParseWhileStatement
627/// while-statement: [C99 6.8.5.1]
628/// 'while' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000629Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000630 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000631 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000632 ConsumeToken(); // eat the 'while'.
633
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000634 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000635 Diag(Tok, diag::err_expected_lparen_after, "while");
636 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000637 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000638 }
639
Chris Lattner2dd1b722007-08-26 23:08:06 +0000640 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
641 // the case for C90. Start the loop scope.
642 if (getLang().C99)
643 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
644 else
645 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000646
Chris Lattner9075bd72006-08-10 04:59:57 +0000647 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000648 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000649
Chris Lattner8fb26252007-08-22 05:28:50 +0000650 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000651 // there is no compound stmt. C90 does not have this clause. We only do this
652 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000653 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000654 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000655
Chris Lattner9075bd72006-08-10 04:59:57 +0000656 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000657 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000658
Chris Lattner8fb26252007-08-22 05:28:50 +0000659 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000660 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000661
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000662 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000663
664 if (Cond.isInvalid || Body.isInvalid) return true;
665
Steve Naroff66356bd2007-09-16 14:56:35 +0000666 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000667}
668
669/// ParseDoStatement
670/// do-statement: [C99 6.8.5.2]
671/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000672/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000673Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000674 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000675 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000676
Chris Lattner2dd1b722007-08-26 23:08:06 +0000677 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
678 // the case for C90. Start the loop scope.
679 if (getLang().C99)
680 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
681 else
682 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000683
Chris Lattner8fb26252007-08-22 05:28:50 +0000684 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000685 // there is no compound stmt. C90 does not have this clause. We only do this
686 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000687 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000688 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000689
Chris Lattner9075bd72006-08-10 04:59:57 +0000690 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000691 StmtResult Body = ParseStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000692
Chris Lattner8fb26252007-08-22 05:28:50 +0000693 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000694 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000695
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000696 if (Tok.isNot(tok::kw_while)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000697 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000698 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000699 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000700 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000701 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000702 }
Chris Lattneraf635312006-10-16 06:06:51 +0000703 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000704
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000705 if (Tok.isNot(tok::l_paren)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000706 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000707 Diag(Tok, diag::err_expected_lparen_after, "do/while");
708 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000709 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000710 }
711
712 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000713 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000714
715 ExitScope();
716
Chris Lattner30f910e2006-10-16 05:52:41 +0000717 if (Cond.isInvalid || Body.isInvalid) return true;
718
Steve Naroff66356bd2007-09-16 14:56:35 +0000719 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000720}
721
722/// ParseForStatement
723/// for-statement: [C99 6.8.5.3]
724/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
725/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000726/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
727/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000728Parser::StmtResult Parser::ParseForStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000729 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000730 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000731
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000732 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000733 Diag(Tok, diag::err_expected_lparen_after, "for");
734 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000735 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000736 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000737
Chris Lattner2dd1b722007-08-26 23:08:06 +0000738 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
739 // the case for C90. Start the loop scope.
740 if (getLang().C99)
741 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
742 else
743 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner9075bd72006-08-10 04:59:57 +0000744
Chris Lattner04132372006-10-16 06:12:55 +0000745 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000746 ExprResult Value;
747
Chris Lattner71e23ce2006-11-04 20:18:38 +0000748 StmtTy *FirstPart = 0;
Chris Lattnercd68f642007-06-27 01:06:29 +0000749 ExprTy *SecondPart = 0;
750 StmtTy *ThirdPart = 0;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000751 bool ForEach = false;
Chris Lattner71e23ce2006-11-04 20:18:38 +0000752
Chris Lattner9075bd72006-08-10 04:59:57 +0000753 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000754 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000755 // no first part, eat the ';'.
756 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000757 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000758 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000759 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
760 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000761 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff66356bd2007-09-16 14:56:35 +0000762 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000763 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000764 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000765 ConsumeToken(); // consume 'in'
766 Value = ParseExpression();
767 if (!Value.isInvalid)
768 SecondPart = Value.Val;
769 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000770 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000771 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000772
Chris Lattnercd68f642007-06-27 01:06:29 +0000773 // Turn the expression into a stmt.
774 if (!Value.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000775 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattnercd68f642007-06-27 01:06:29 +0000776 if (!R.isInvalid)
777 FirstPart = R.Val;
778 }
Steve Naroff9992bba2007-05-30 16:27:15 +0000779
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000780 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000781 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000782 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000783 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000784 ConsumeToken(); // consume 'in'
785 Value = ParseExpression();
786 if (!Value.isInvalid)
787 SecondPart = Value.Val;
788 }
789 else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000790 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000791 SkipUntil(tok::semi);
792 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000793 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000794 if (!ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000795 // Parse the second part of the for specifier.
796 if (Tok.is(tok::semi)) { // for (...;;
797 // no second part.
798 Value = ExprResult();
799 } else {
800 Value = ParseExpression();
801 if (!Value.isInvalid)
802 SecondPart = Value.Val;
803 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000804
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000805 if (Tok.is(tok::semi)) {
806 ConsumeToken();
807 } else {
808 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
809 SkipUntil(tok::semi);
810 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000811
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000812 // Parse the third part of the for specifier.
813 if (Tok.is(tok::r_paren)) { // for (...;...;)
814 // no third part.
815 Value = ExprResult();
816 } else {
817 Value = ParseExpression();
818 if (!Value.isInvalid) {
819 // Turn the expression into a stmt.
820 StmtResult R = Actions.ActOnExprStmt(Value.Val);
821 if (!R.isInvalid)
822 ThirdPart = R.Val;
823 }
Chris Lattnercd68f642007-06-27 01:06:29 +0000824 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000825 }
Chris Lattner4564bc12006-08-10 23:14:52 +0000826 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000827 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000828
Chris Lattner8fb26252007-08-22 05:28:50 +0000829 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000830 // there is no compound stmt. C90 does not have this clause. We only do this
831 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000832 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000833 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000834
Chris Lattner9075bd72006-08-10 04:59:57 +0000835 // Read the body statement.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000836 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000837
Chris Lattner8fb26252007-08-22 05:28:50 +0000838 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000839 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000840
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000841 // Leave the for-scope.
842 ExitScope();
843
Chris Lattner71e23ce2006-11-04 20:18:38 +0000844 if (Body.isInvalid)
845 return Body;
Chris Lattner30f910e2006-10-16 05:52:41 +0000846
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000847 if (!ForEach)
848 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
849 SecondPart, ThirdPart, RParenLoc, Body.Val);
850 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000851 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000852 SecondPart, RParenLoc, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000853}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000854
Chris Lattner503fadc2006-08-10 05:45:44 +0000855/// ParseGotoStatement
856/// jump-statement:
857/// 'goto' identifier ';'
858/// [GNU] 'goto' '*' expression ';'
859///
860/// Note: this lets the caller parse the end ';'.
861///
Chris Lattner30f910e2006-10-16 05:52:41 +0000862Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000863 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000864 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000865
Chris Lattner30f910e2006-10-16 05:52:41 +0000866 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000867 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000868 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000869 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000870 ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000871 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000872 // GNU indirect goto extension.
873 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000874 SourceLocation StarLoc = ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000875 ExprResult R = ParseExpression();
Chris Lattner30f910e2006-10-16 05:52:41 +0000876 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000877 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000878 return true;
879 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000880 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattnere34b2c22007-07-22 04:13:33 +0000881 } else {
882 Diag(Tok, diag::err_expected_ident);
883 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000884 }
Chris Lattnere34b2c22007-07-22 04:13:33 +0000885
Chris Lattner30f910e2006-10-16 05:52:41 +0000886 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000887}
888
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000889/// ParseContinueStatement
890/// jump-statement:
891/// 'continue' ';'
892///
893/// Note: this lets the caller parse the end ';'.
894///
895Parser::StmtResult Parser::ParseContinueStatement() {
896 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000897 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000898}
899
900/// ParseBreakStatement
901/// jump-statement:
902/// 'break' ';'
903///
904/// Note: this lets the caller parse the end ';'.
905///
906Parser::StmtResult Parser::ParseBreakStatement() {
907 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000908 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000909}
910
Chris Lattner503fadc2006-08-10 05:45:44 +0000911/// ParseReturnStatement
912/// jump-statement:
913/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +0000914Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000915 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000916 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000917
Chris Lattner30f910e2006-10-16 05:52:41 +0000918 ExprResult R(0);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000919 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000920 R = ParseExpression();
921 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000922 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000923 return true;
924 }
Chris Lattnera0927ce2006-08-12 16:59:03 +0000925 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000926 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Chris Lattner503fadc2006-08-10 05:45:44 +0000927}
Chris Lattner0116c472006-08-15 06:03:28 +0000928
929/// ParseAsmStatement - Parse a GNU extended asm statement.
930/// [GNU] asm-statement:
931/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
932///
933/// [GNU] asm-argument:
934/// asm-string-literal
935/// asm-string-literal ':' asm-operands[opt]
936/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
937/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
938/// ':' asm-clobbers
939///
940/// [GNU] asm-clobbers:
941/// asm-string-literal
942/// asm-clobbers ',' asm-string-literal
943///
Chris Lattner30f910e2006-10-16 05:52:41 +0000944Parser::StmtResult Parser::ParseAsmStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000945 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +0000946 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner0116c472006-08-15 06:03:28 +0000947
948 DeclSpec DS;
949 SourceLocation Loc = Tok.getLocation();
950 ParseTypeQualifierListOpt(DS);
951
952 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +0000953 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner0116c472006-08-15 06:03:28 +0000954 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
Chris Lattnera925dc62006-11-28 04:33:46 +0000955 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner0116c472006-08-15 06:03:28 +0000956 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
957
958 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +0000959 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner0116c472006-08-15 06:03:28 +0000960
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000961 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +0000962 Diag(Tok, diag::err_expected_lparen_after, "asm");
963 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +0000964 return true;
Chris Lattner0116c472006-08-15 06:03:28 +0000965 }
Chris Lattner04132372006-10-16 06:12:55 +0000966 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +0000967
Anders Carlsson81a5a692007-11-20 19:21:03 +0000968 ExprResult AsmString = ParseAsmStringLiteral();
969 if (AsmString.isInvalid)
970 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000971
972 llvm::SmallVector<std::string, 4> Names;
973 llvm::SmallVector<ExprTy*, 4> Constraints;
974 llvm::SmallVector<ExprTy*, 4> Exprs;
975
976 // Parse Outputs, if present.
977 ParseAsmOperandsOpt(Names, Constraints, Exprs);
978
979 unsigned NumOutputs = Names.size();
Chris Lattner0116c472006-08-15 06:03:28 +0000980
981 // Parse Inputs, if present.
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000982 ParseAsmOperandsOpt(Names, Constraints, Exprs);
983 assert(Names.size() == Constraints.size() &&
984 Constraints.size() == Exprs.size()
985 && "Input operand size mismatch!");
986
987 unsigned NumInputs = Names.size() - NumOutputs;
988
989 llvm::SmallVector<ExprTy*, 4> Clobbers;
Chris Lattner0116c472006-08-15 06:03:28 +0000990
991 // Parse the clobbers, if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000992 if (Tok.is(tok::colon)) {
Chris Lattner0116c472006-08-15 06:03:28 +0000993 ConsumeToken();
994
Anders Carlsson091a0592007-11-21 23:27:34 +0000995 // Parse the asm-string list for clobbers.
996 while (1) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000997 ExprResult Clobber = ParseAsmStringLiteral();
Chris Lattner0116c472006-08-15 06:03:28 +0000998
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000999 if (Clobber.isInvalid)
1000 break;
1001
1002 Clobbers.push_back(Clobber.Val);
1003
Anders Carlsson091a0592007-11-21 23:27:34 +00001004 if (Tok.isNot(tok::comma)) break;
1005 ConsumeToken();
Chris Lattner0116c472006-08-15 06:03:28 +00001006 }
1007 }
1008
Chris Lattner73c56c02007-10-29 04:04:16 +00001009 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner30f910e2006-10-16 05:52:41 +00001010
Anders Carlsson660bdd12007-11-23 23:12:25 +00001011 return Actions.ActOnAsmStmt(AsmLoc, isVolatile, NumOutputs, NumInputs,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001012 &Names[0], &Constraints[0], &Exprs[0],
1013 AsmString.Val,
1014 Clobbers.size(), &Clobbers[0],
1015 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001016}
1017
1018/// ParseAsmOperands - Parse the asm-operands production as used by
1019/// asm-statement. We also parse a leading ':' token. If the leading colon is
1020/// not present, we do not parse anything.
1021///
1022/// [GNU] asm-operands:
1023/// asm-operand
1024/// asm-operands ',' asm-operand
1025///
1026/// [GNU] asm-operand:
1027/// asm-string-literal '(' expression ')'
1028/// '[' identifier ']' asm-string-literal '(' expression ')'
1029///
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001030void Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
1031 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1032 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001033 // Only do anything if this operand is present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001034 if (Tok.isNot(tok::colon)) return;
Chris Lattner0116c472006-08-15 06:03:28 +00001035 ConsumeToken();
1036
1037 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001038 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Chris Lattner0116c472006-08-15 06:03:28 +00001039 return;
1040
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001041 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001042 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001043 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001044 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +00001045
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001046 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001047 Diag(Tok, diag::err_expected_ident);
1048 SkipUntil(tok::r_paren);
1049 return;
1050 }
Chris Lattner645ff3f2007-10-29 04:06:22 +00001051
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001052 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001053 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001054
1055 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner0116c472006-08-15 06:03:28 +00001056 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001057 } else
1058 Names.push_back(std::string());
Chris Lattner0116c472006-08-15 06:03:28 +00001059
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001060 ExprResult Constraint = ParseAsmStringLiteral();
1061 if (Constraint.isInvalid) {
1062 SkipUntil(tok::r_paren);
1063 return;
1064 }
1065 Constraints.push_back(Constraint.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001066
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001067 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001068 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1069 SkipUntil(tok::r_paren);
1070 return;
1071 }
1072
1073 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001074 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +00001075 if (Res.isInvalid) {
1076 SkipUntil(tok::r_paren);
1077 return;
1078 }
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001079 Exprs.push_back(Res.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001080 // Eat the comma and continue parsing if it exists.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001081 if (Tok.isNot(tok::comma)) return;
Chris Lattner0116c472006-08-15 06:03:28 +00001082 ConsumeToken();
1083 }
1084}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001085
1086Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1087 SourceLocation L, SourceLocation R) {
1088 // Do not enter a scope for the brace, as the arguments are in the same scope
1089 // (the function body) as the body itself. Instead, just read the statement
1090 // list and put it into a CompoundStmt for safe keeping.
1091 StmtResult FnBody = ParseCompoundStatementBody();
1092
1093 // If the function body could not be parsed, make a bogus compoundstmt.
1094 if (FnBody.isInvalid)
1095 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1096
1097 // Leave the function body scope.
1098 ExitScope();
1099
Steve Naroffb313fc32007-11-11 23:20:51 +00001100 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001101}