blob: f9800219ed5b15f9371be8b54cf49ad395ddc289 [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"
Steve Naroff4e79d342008-02-07 23:24:32 +000017#include "clang/Basic/SourceManager.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner33ad2ca2006-11-05 23:47:55 +000019#include "clang/Parse/Scope.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.8: Statements and Blocks.
24//===----------------------------------------------------------------------===//
25
26/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
27/// StatementOrDeclaration:
28/// statement
29/// declaration
30///
31/// statement:
32/// labeled-statement
33/// compound-statement
34/// expression-statement
35/// selection-statement
36/// iteration-statement
37/// jump-statement
Fariborz Jahanian90814572007-10-04 20:19:06 +000038/// [OBC] objc-throw-statement
39/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000040/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000041/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000042/// [OMP] openmp-construct [TODO]
43///
44/// labeled-statement:
45/// identifier ':' statement
46/// 'case' constant-expression ':' statement
47/// 'default' ':' statement
48///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000049/// selection-statement:
50/// if-statement
51/// switch-statement
52///
53/// iteration-statement:
54/// while-statement
55/// do-statement
56/// for-statement
57///
Chris Lattner9075bd72006-08-10 04:59:57 +000058/// expression-statement:
59/// expression[opt] ';'
60///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000061/// jump-statement:
62/// 'goto' identifier ';'
63/// 'continue' ';'
64/// 'break' ';'
65/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000066/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000067///
Fariborz Jahanian90814572007-10-04 20:19:06 +000068/// [OBC] objc-throw-statement:
69/// [OBC] '@' 'throw' expression ';'
70/// [OBC] '@' 'throw' ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000071///
Chris Lattner30f910e2006-10-16 05:52:41 +000072Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000073 const char *SemiError = 0;
Chris Lattner30f910e2006-10-16 05:52:41 +000074 Parser::StmtResult Res;
Chris Lattner503fadc2006-08-10 05:45:44 +000075
76 // Cases in this switch statement should fall through if the parser expects
77 // the token to end in a semicolon (in which case SemiError should be set),
78 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000079 tok::TokenKind Kind = Tok.getKind();
80 SourceLocation AtLoc;
81 switch (Kind) {
Chris Lattnerf8afb622006-08-10 18:26:31 +000082 case tok::identifier: // C99 6.8.1: labeled-statement
83 // identifier ':' statement
84 // declaration (if !OnlyStatement)
85 // expression[opt] ';'
86 return ParseIdentifierStatement(OnlyStatement);
87
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000088 case tok::at: // May be a @try or @throw statement
89 {
90 AtLoc = ConsumeToken(); // consume @
Steve Naroffe6016792008-02-05 21:27:35 +000091 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000092 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000093
Chris Lattner0ccd51e2006-08-09 05:47:47 +000094 default:
Fariborz Jahanian90814572007-10-04 20:19:06 +000095 if (!OnlyStatement && isDeclarationSpecifier()) {
Chris Lattner2e232092008-03-13 06:29:04 +000096 SourceLocation DeclStart = Tok.getLocation();
97 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
98 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerb943aa82008-03-13 06:29:54 +000099 return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000100 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000101 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +0000102 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +0000103 } else {
104 // expression[opt] ';'
Fariborz Jahanian90814572007-10-04 20:19:06 +0000105 ExprResult Res = ParseExpression();
Chris Lattner89c50c62006-08-11 06:41:18 +0000106 if (Res.isInvalid) {
107 // If the expression is invalid, skip ahead to the next semicolon. Not
108 // doing this opens us up to the possibility of infinite loops if
109 // ParseExpression does not consume any tokens.
110 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000111 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000112 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000113 // Otherwise, eat the semicolon.
114 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000115 return Actions.ActOnExprStmt(Res.Val);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000116 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000117
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000118 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000119 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000120 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000121 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000122
123 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000124 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000125 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff66356bd2007-09-16 14:56:35 +0000126 return Actions.ActOnNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000127
Chris Lattner9075bd72006-08-10 04:59:57 +0000128 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000129 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000130 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000131 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000132
Chris Lattner9075bd72006-08-10 04:59:57 +0000133 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000134 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000135 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000136 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000137 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000138 break;
139 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000140 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000141
142 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000143 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000144 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000145 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000146 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000147 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000148 SemiError = "continue statement";
149 break;
150 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000151 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000152 SemiError = "break statement";
153 break;
154 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000155 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000156 SemiError = "return statement";
157 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000158
159 case tok::kw_asm:
Steve Naroffb2c80c72008-02-07 03:50:06 +0000160 bool msAsm = false;
161 Res = ParseAsmStatement(msAsm);
162 if (msAsm) return Res;
Chris Lattner0116c472006-08-15 06:03:28 +0000163 SemiError = "asm statement";
164 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000165 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000166
167 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000168 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000169 ConsumeToken();
170 } else {
171 Diag(Tok, diag::err_expected_semi_after, SemiError);
172 SkipUntil(tok::semi);
173 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000174 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000175}
176
Chris Lattnerf8afb622006-08-10 18:26:31 +0000177/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
178/// have a bit of a quandry here. Reading the identifier is necessary to see if
179/// there is a ':' after it. If there is, this is a label, regardless of what
180/// else the identifier can mean. If not, this is either part of a declaration
181/// (if the identifier is a type-name) or part of an expression.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000182///
183/// labeled-statement:
184/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000185/// [GNU] identifier ':' attributes[opt] statement
Chris Lattner6dfd9782006-08-10 18:31:37 +0000186/// declaration (if !OnlyStatement)
187/// expression[opt] ';'
188///
Chris Lattner30f910e2006-10-16 05:52:41 +0000189Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000190 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Chris Lattner0663d2a2006-11-05 18:39:59 +0000191 "Not an identifier!");
Chris Lattner6dfd9782006-08-10 18:31:37 +0000192
Chris Lattner146762e2007-07-20 16:59:19 +0000193 Token IdentTok = Tok; // Save the whole token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000194 ConsumeToken(); // eat the identifier.
195
196 // identifier ':' statement
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000197 if (Tok.is(tok::colon)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000198 SourceLocation ColonLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000199
200 // Read label attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000201 DeclTy *AttrList = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000202 if (Tok.is(tok::kw___attribute))
Chris Lattner30f910e2006-10-16 05:52:41 +0000203 // TODO: save these somewhere.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000204 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +0000205
Chris Lattner30f910e2006-10-16 05:52:41 +0000206 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000207
208 // Broken substmt shouldn't prevent the label from being added to the AST.
209 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000210 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000211
Steve Naroff66356bd2007-09-16 14:56:35 +0000212 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000213 IdentTok.getIdentifierInfo(),
214 ColonLoc, SubStmt.Val);
Chris Lattner6dfd9782006-08-10 18:31:37 +0000215 }
216
Chris Lattner30f910e2006-10-16 05:52:41 +0000217 // Check to see if this is a declaration.
Chris Lattner49252eb2007-01-27 19:04:39 +0000218 void *TypeRep;
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000219 if (!OnlyStatement &&
Chris Lattner49252eb2007-01-27 19:04:39 +0000220 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000221 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000222 DeclSpec DS;
223
Chris Lattner49252eb2007-01-27 19:04:39 +0000224 // Add the typedef name to the start of the decl-specs.
225 const char *PrevSpec = 0;
226 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
227 IdentTok.getLocation(), PrevSpec,
228 TypeRep);
229 assert(!isInvalid && "First declspec can't be invalid!");
Steve Naroffc5484042007-10-30 02:23:23 +0000230 SourceLocation endProtoLoc;
Fariborz Jahaniand7971132007-10-11 18:08:47 +0000231 if (Tok.is(tok::less)) {
232 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffc5484042007-10-30 02:23:23 +0000233 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahaniand7971132007-10-11 18:08:47 +0000234 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
235 new llvm::SmallVector<DeclTy *, 8>;
236 DS.setProtocolQualifiers(ProtocolDecl);
237 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
238 &ProtocolRefs[0], ProtocolRefs.size(),
239 *ProtocolDecl);
240 }
Chris Lattner0663d2a2006-11-05 18:39:59 +0000241
Chris Lattner2f9980e2006-08-10 18:39:24 +0000242 // ParseDeclarationSpecifiers will continue from there.
243 ParseDeclarationSpecifiers(DS);
244
Chris Lattner0e894622006-08-13 19:58:17 +0000245 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
246 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000247 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000248 // TODO: emit error on 'int;' or 'const enum foo;'.
249 // if (!DS.isMissingDeclaratorOk()) Diag(...);
250
251 ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000252 // FIXME: Return this as a type decl.
253 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000254 }
255
Chris Lattner2f9980e2006-08-10 18:39:24 +0000256 // Parse all the declarators.
257 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
258 ParseDeclarator(DeclaratorInfo);
259
Chris Lattner436806a2007-07-10 05:03:31 +0000260 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner2e232092008-03-13 06:29:04 +0000261 if (!Decl) return 0;
262 return Actions.ActOnDeclStmt(Decl, DS.getSourceRange().getBegin(),
263 DeclaratorInfo.getSourceRange().getEnd());
Chris Lattner6dfd9782006-08-10 18:31:37 +0000264 }
265
Chris Lattner0c6c0342006-08-12 18:12:45 +0000266 // Otherwise, this is an expression. Seed it with II and parse it.
267 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
Chris Lattner30f910e2006-10-16 05:52:41 +0000268 if (Res.isInvalid) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000269 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000270 return true;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000271 } else if (Tok.isNot(tok::semi)) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000272 Diag(Tok, diag::err_expected_semi_after, "expression");
273 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000274 return true;
275 } else {
276 ConsumeToken();
Chris Lattnercd68f642007-06-27 01:06:29 +0000277 // Convert expr to a stmt.
Steve Naroff66356bd2007-09-16 14:56:35 +0000278 return Actions.ActOnExprStmt(Res.Val);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000279 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000280}
281
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000282/// ParseCaseStatement
283/// labeled-statement:
284/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000285/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000286///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000287/// Note that this does not parse the 'statement' at the end.
288///
Chris Lattner30f910e2006-10-16 05:52:41 +0000289Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000290 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000291 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000292
Chris Lattner30f910e2006-10-16 05:52:41 +0000293 ExprResult LHS = ParseConstantExpression();
294 if (LHS.isInvalid) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000295 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000296 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000297 }
298
299 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000300 SourceLocation DotDotDotLoc;
301 ExprTy *RHSVal = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000302 if (Tok.is(tok::ellipsis)) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000303 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000304 DotDotDotLoc = ConsumeToken();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000305
306 ExprResult RHS = ParseConstantExpression();
307 if (RHS.isInvalid) {
308 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000309 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000310 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000311 RHSVal = RHS.Val;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000312 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000313
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000314 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000315 Diag(Tok, diag::err_expected_colon_after, "'case'");
316 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000317 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000318 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000319
Chris Lattneraf635312006-10-16 06:06:51 +0000320 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000321
322 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000323 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000324 Diag(Tok, diag::err_label_end_of_compound_statement);
325 return true;
326 }
327
328 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000329
330 // Broken substmt shouldn't prevent the case from being added to the AST.
Chris Lattner30f910e2006-10-16 05:52:41 +0000331 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000332 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000333
Steve Naroff66356bd2007-09-16 14:56:35 +0000334 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Chris Lattner30f910e2006-10-16 05:52:41 +0000335 SubStmt.Val);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000336}
337
338/// ParseDefaultStatement
339/// labeled-statement:
340/// 'default' ':' statement
341/// Note that this does not parse the 'statement' at the end.
342///
Chris Lattner30f910e2006-10-16 05:52:41 +0000343Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000344 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000345 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000346
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000347 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000348 Diag(Tok, diag::err_expected_colon_after, "'default'");
349 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000350 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000351 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000352
Chris Lattneraf635312006-10-16 06:06:51 +0000353 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000354
355 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000356 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000357 Diag(Tok, diag::err_label_end_of_compound_statement);
358 return true;
359 }
360
361 StmtResult SubStmt = ParseStatement();
362 if (SubStmt.isInvalid)
363 return true;
364
Steve Naroff66356bd2007-09-16 14:56:35 +0000365 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000366}
367
368
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000369/// ParseCompoundStatement - Parse a "{}" block.
370///
371/// compound-statement: [C99 6.8.2]
372/// { block-item-list[opt] }
373/// [GNU] { label-declarations block-item-list } [TODO]
374///
375/// block-item-list:
376/// block-item
377/// block-item-list block-item
378///
379/// block-item:
380/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000381/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000382/// statement
383/// [OMP] openmp-directive [TODO]
384///
385/// [GNU] label-declarations:
386/// [GNU] label-declaration
387/// [GNU] label-declarations label-declaration
388///
389/// [GNU] label-declaration:
390/// [GNU] '__label__' identifier-list ';'
391///
392/// [OMP] openmp-directive: [TODO]
393/// [OMP] barrier-directive
394/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000395///
Chris Lattnercac27a52007-08-31 21:49:55 +0000396Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000397 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000398
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000399 // Enter a scope to hold everything within the compound stmt. Compound
400 // statements can always hold declarations.
401 EnterScope(Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000402
403 // Parse the statements in the body.
Chris Lattnercac27a52007-08-31 21:49:55 +0000404 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000405
406 ExitScope();
407 return Body;
408}
409
410
411/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000412/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000413/// consume the '}' at the end of the block. It does not manipulate the scope
414/// stack.
Chris Lattnercac27a52007-08-31 21:49:55 +0000415Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerf2978802007-01-21 06:52:16 +0000416 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
417
Chris Lattner010015a2007-05-28 07:23:54 +0000418 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000419 // only allowed at the start of a compound stmt regardless of the language.
Chris Lattner010015a2007-05-28 07:23:54 +0000420
Chris Lattner23b7eb62007-06-15 23:05:46 +0000421 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000422 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000423 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000424 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000425 R = ParseStatementOrDeclaration(false);
426 } else {
427 // __extension__ can start declarations and it can also be a unary
428 // operator for expressions. Consume multiple __extension__ markers here
429 // until we can determine which is which.
430 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000431 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000432 ConsumeToken();
433
Chris Lattnerfdc07482008-03-13 06:32:11 +0000434 // __extension__ silences extension warnings in the subexpression.
435 bool SavedExtWarn = Diags.getWarnOnExtensions();
436 Diags.setWarnOnExtensions(false);
437
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000438 // If this is the start of a declaration, parse it as such.
439 if (isDeclarationSpecifier()) {
440 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner2e232092008-03-13 06:29:04 +0000441 SourceLocation DeclStart = Tok.getLocation();
442 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
443 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerb943aa82008-03-13 06:29:54 +0000444 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattnerfdc07482008-03-13 06:32:11 +0000445
446 Diags.setWarnOnExtensions(SavedExtWarn);
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.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000450 ExprResult Res = ParseCastExpression(false);
Chris Lattnerfdc07482008-03-13 06:32:11 +0000451 Diags.setWarnOnExtensions(SavedExtWarn);
452
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000453 if (Res.isInvalid) {
454 SkipUntil(tok::semi);
455 continue;
456 }
457
458 // Add the __extension__ node to the AST.
Steve Naroff83895f72007-09-16 03:34:24 +0000459 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000460 if (Res.isInvalid)
461 continue;
462
463 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
464 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000465 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000466 }
467 }
468
Chris Lattner30f910e2006-10-16 05:52:41 +0000469 if (!R.isInvalid && R.Val)
470 Stmts.push_back(R.Val);
471 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000472
473 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000474 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000475 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffe97c4ab2008-01-31 18:29:10 +0000476 return true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000477 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000478
Chris Lattner04132372006-10-16 06:12:55 +0000479 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff66356bd2007-09-16 14:56:35 +0000480 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattnercac27a52007-08-31 21:49:55 +0000481 &Stmts[0], Stmts.size(), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000482}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000483
484/// ParseIfStatement
485/// if-statement: [C99 6.8.4.1]
486/// 'if' '(' expression ')' statement
487/// 'if' '(' expression ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000488///
489Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000490 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000491 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000492
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000493 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000494 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000495 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000496 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000497 }
498
Chris Lattner2dd1b722007-08-26 23:08:06 +0000499 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
500 // the case for C90.
501 if (getLang().C99)
502 EnterScope(Scope::DeclScope);
503
Chris Lattnerc951dae2006-08-10 04:23:57 +0000504 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000505 ExprResult CondExp = ParseSimpleParenExpression();
506 if (CondExp.isInvalid) {
507 SkipUntil(tok::semi);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000508 if (getLang().C99)
509 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000510 return true;
511 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000512
Chris Lattner8fb26252007-08-22 05:28:50 +0000513 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000514 // there is no compound stmt. C90 does not have this clause. We only do this
515 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000516 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000517 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000518
Chris Lattner5c5808a2007-10-29 05:08:52 +0000519 // Read the 'then' stmt.
520 SourceLocation ThenStmtLoc = Tok.getLocation();
521 StmtResult ThenStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000522
Chris Lattner37e54f42007-08-22 05:16:28 +0000523 // Pop the 'if' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000524 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000525
526 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000527 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000528 SourceLocation ElseStmtLoc;
Chris Lattner30f910e2006-10-16 05:52:41 +0000529 StmtResult ElseStmt(false);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000530
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000531 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000532 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000533
Chris Lattner8fb26252007-08-22 05:28:50 +0000534 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000535 // there is no compound stmt. C90 does not have this clause. We only do
536 // this if the body isn't a compound statement to avoid push/pop in common
537 // cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000538 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000539 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000540
Chris Lattner5c5808a2007-10-29 05:08:52 +0000541 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000542 ElseStmt = ParseStatement();
Chris Lattner37e54f42007-08-22 05:16:28 +0000543
544 // Pop the 'else' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000545 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000546 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000547
Chris Lattner2dd1b722007-08-26 23:08:06 +0000548 if (getLang().C99)
549 ExitScope();
550
Chris Lattner5c5808a2007-10-29 05:08:52 +0000551 // If the then or else stmt is invalid and the other is valid (and present),
552 // make turn the invalid one into a null stmt to avoid dropping the other
553 // part. If both are invalid, return error.
554 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
555 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
556 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
557 // Both invalid, or one is invalid and other is non-present: delete cond and
558 // return error.
559 Actions.DeleteExpr(CondExp.Val);
560 return true;
561 }
562
563 // Now if either are invalid, replace with a ';'.
564 if (ThenStmt.isInvalid)
565 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
566 if (ElseStmt.isInvalid)
567 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
568
Chris Lattner5c5808a2007-10-29 05:08:52 +0000569 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Chris Lattner30f910e2006-10-16 05:52:41 +0000570 ElseLoc, ElseStmt.Val);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000571}
572
Chris Lattner9075bd72006-08-10 04:59:57 +0000573/// ParseSwitchStatement
574/// switch-statement:
575/// 'switch' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000576Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000577 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000578 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000579
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000580 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000581 Diag(Tok, diag::err_expected_lparen_after, "switch");
582 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000583 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000584 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000585
586 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
587 // not the case for C90. Start the switch scope.
588 if (getLang().C99)
589 EnterScope(Scope::BreakScope|Scope::DeclScope);
590 else
591 EnterScope(Scope::BreakScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000592
Chris Lattner9075bd72006-08-10 04:59:57 +0000593 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000594 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000595
Anders Carlsson51873c22007-07-22 07:07:56 +0000596 if (Cond.isInvalid) {
597 ExitScope();
598 return true;
599 }
600
Steve Naroff66356bd2007-09-16 14:56:35 +0000601 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlsson51873c22007-07-22 07:07:56 +0000602
Chris Lattner8fb26252007-08-22 05:28:50 +0000603 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000604 // there is no compound stmt. C90 does not have this clause. We only do this
605 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000606 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000607 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000608
Chris Lattner9075bd72006-08-10 04:59:57 +0000609 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000610 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000611
Chris Lattner8fb26252007-08-22 05:28:50 +0000612 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000613 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000614
Anders Carlsson51873c22007-07-22 07:07:56 +0000615 if (Body.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000616 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000617 // FIXME: Remove the case statement list from the Switch statement.
618 }
619
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000620 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000621
Steve Naroff66356bd2007-09-16 14:56:35 +0000622 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000623}
624
625/// ParseWhileStatement
626/// while-statement: [C99 6.8.5.1]
627/// 'while' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000628Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000629 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000630 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000631 ConsumeToken(); // eat the 'while'.
632
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000633 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000634 Diag(Tok, diag::err_expected_lparen_after, "while");
635 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000636 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000637 }
638
Chris Lattner2dd1b722007-08-26 23:08:06 +0000639 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
640 // the case for C90. Start the loop scope.
641 if (getLang().C99)
642 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
643 else
644 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000645
Chris Lattner9075bd72006-08-10 04:59:57 +0000646 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000647 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000648
Chris Lattner8fb26252007-08-22 05:28:50 +0000649 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000650 // there is no compound stmt. C90 does not have this clause. We only do this
651 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000652 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000653 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000654
Chris Lattner9075bd72006-08-10 04:59:57 +0000655 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000656 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000657
Chris Lattner8fb26252007-08-22 05:28:50 +0000658 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000659 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000660
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000661 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000662
663 if (Cond.isInvalid || Body.isInvalid) return true;
664
Steve Naroff66356bd2007-09-16 14:56:35 +0000665 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000666}
667
668/// ParseDoStatement
669/// do-statement: [C99 6.8.5.2]
670/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000671/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000672Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000673 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000674 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000675
Chris Lattner2dd1b722007-08-26 23:08:06 +0000676 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
677 // the case for C90. Start the loop scope.
678 if (getLang().C99)
679 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
680 else
681 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000682
Chris Lattner8fb26252007-08-22 05:28:50 +0000683 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000684 // there is no compound stmt. C90 does not have this clause. We only do this
685 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000686 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000687 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000688
Chris Lattner9075bd72006-08-10 04:59:57 +0000689 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000690 StmtResult Body = ParseStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000691
Chris Lattner8fb26252007-08-22 05:28:50 +0000692 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000693 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000694
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000695 if (Tok.isNot(tok::kw_while)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000696 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000697 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000698 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000699 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000700 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000701 }
Chris Lattneraf635312006-10-16 06:06:51 +0000702 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000703
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000704 if (Tok.isNot(tok::l_paren)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000705 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000706 Diag(Tok, diag::err_expected_lparen_after, "do/while");
707 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000708 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000709 }
710
711 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000712 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000713
714 ExitScope();
715
Chris Lattner30f910e2006-10-16 05:52:41 +0000716 if (Cond.isInvalid || Body.isInvalid) return true;
717
Steve Naroff66356bd2007-09-16 14:56:35 +0000718 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000719}
720
721/// ParseForStatement
722/// for-statement: [C99 6.8.5.3]
723/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
724/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000725/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
726/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000727Parser::StmtResult Parser::ParseForStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000728 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000729 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000730
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000731 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000732 Diag(Tok, diag::err_expected_lparen_after, "for");
733 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000734 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000735 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000736
Chris Lattner2dd1b722007-08-26 23:08:06 +0000737 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
738 // the case for C90. Start the loop scope.
739 if (getLang().C99)
740 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
741 else
742 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner9075bd72006-08-10 04:59:57 +0000743
Chris Lattner04132372006-10-16 06:12:55 +0000744 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000745 ExprResult Value;
746
Chris Lattner71e23ce2006-11-04 20:18:38 +0000747 StmtTy *FirstPart = 0;
Chris Lattnercd68f642007-06-27 01:06:29 +0000748 ExprTy *SecondPart = 0;
749 StmtTy *ThirdPart = 0;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000750 bool ForEach = false;
Chris Lattner71e23ce2006-11-04 20:18:38 +0000751
Chris Lattner9075bd72006-08-10 04:59:57 +0000752 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000753 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000754 // no first part, eat the ';'.
755 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000756 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000757 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000758 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
759 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner2e232092008-03-13 06:29:04 +0000760
761 SourceLocation DeclStart = Tok.getLocation();
Steve Naroff2a8ad182007-05-29 22:59:26 +0000762 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Chris Lattner2e232092008-03-13 06:29:04 +0000763 // FIXME: Pass in the right location for the end of the declstmt.
764 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattnerb943aa82008-03-13 06:29:54 +0000765 DeclStart);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000766 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000767 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000768 ConsumeToken(); // consume 'in'
769 Value = ParseExpression();
770 if (!Value.isInvalid)
771 SecondPart = Value.Val;
772 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000773 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000774 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000775
Chris Lattnercd68f642007-06-27 01:06:29 +0000776 // Turn the expression into a stmt.
777 if (!Value.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000778 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattnercd68f642007-06-27 01:06:29 +0000779 if (!R.isInvalid)
780 FirstPart = R.Val;
781 }
Steve Naroff9992bba2007-05-30 16:27:15 +0000782
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000783 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000784 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000785 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000786 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000787 ConsumeToken(); // consume 'in'
788 Value = ParseExpression();
789 if (!Value.isInvalid)
790 SecondPart = Value.Val;
791 }
792 else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000793 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000794 SkipUntil(tok::semi);
795 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000796 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000797 if (!ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000798 // Parse the second part of the for specifier.
799 if (Tok.is(tok::semi)) { // for (...;;
800 // no second part.
801 Value = ExprResult();
802 } else {
803 Value = ParseExpression();
804 if (!Value.isInvalid)
805 SecondPart = Value.Val;
806 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000807
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000808 if (Tok.is(tok::semi)) {
809 ConsumeToken();
810 } else {
811 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
812 SkipUntil(tok::semi);
813 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000814
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000815 // Parse the third part of the for specifier.
816 if (Tok.is(tok::r_paren)) { // for (...;...;)
817 // no third part.
818 Value = ExprResult();
819 } else {
820 Value = ParseExpression();
821 if (!Value.isInvalid) {
822 // Turn the expression into a stmt.
823 StmtResult R = Actions.ActOnExprStmt(Value.Val);
824 if (!R.isInvalid)
825 ThirdPart = R.Val;
826 }
Chris Lattnercd68f642007-06-27 01:06:29 +0000827 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000828 }
Chris Lattner4564bc12006-08-10 23:14:52 +0000829 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000830 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000831
Chris Lattner8fb26252007-08-22 05:28:50 +0000832 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000833 // there is no compound stmt. C90 does not have this clause. We only do this
834 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000835 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000836 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000837
Chris Lattner9075bd72006-08-10 04:59:57 +0000838 // Read the body statement.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000839 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000840
Chris Lattner8fb26252007-08-22 05:28:50 +0000841 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000842 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000843
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000844 // Leave the for-scope.
845 ExitScope();
846
Chris Lattner71e23ce2006-11-04 20:18:38 +0000847 if (Body.isInvalid)
848 return Body;
Chris Lattner30f910e2006-10-16 05:52:41 +0000849
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000850 if (!ForEach)
851 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
852 SecondPart, ThirdPart, RParenLoc, Body.Val);
853 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000854 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000855 SecondPart, RParenLoc, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000856}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000857
Chris Lattner503fadc2006-08-10 05:45:44 +0000858/// ParseGotoStatement
859/// jump-statement:
860/// 'goto' identifier ';'
861/// [GNU] 'goto' '*' expression ';'
862///
863/// Note: this lets the caller parse the end ';'.
864///
Chris Lattner30f910e2006-10-16 05:52:41 +0000865Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000866 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000867 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000868
Chris Lattner30f910e2006-10-16 05:52:41 +0000869 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000870 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000871 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000872 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000873 ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000874 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000875 // GNU indirect goto extension.
876 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000877 SourceLocation StarLoc = ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000878 ExprResult R = ParseExpression();
Chris Lattner30f910e2006-10-16 05:52:41 +0000879 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000880 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000881 return true;
882 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000883 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattnere34b2c22007-07-22 04:13:33 +0000884 } else {
885 Diag(Tok, diag::err_expected_ident);
886 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000887 }
Chris Lattnere34b2c22007-07-22 04:13:33 +0000888
Chris Lattner30f910e2006-10-16 05:52:41 +0000889 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000890}
891
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000892/// ParseContinueStatement
893/// jump-statement:
894/// 'continue' ';'
895///
896/// Note: this lets the caller parse the end ';'.
897///
898Parser::StmtResult Parser::ParseContinueStatement() {
899 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000900 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000901}
902
903/// ParseBreakStatement
904/// jump-statement:
905/// 'break' ';'
906///
907/// Note: this lets the caller parse the end ';'.
908///
909Parser::StmtResult Parser::ParseBreakStatement() {
910 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000911 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000912}
913
Chris Lattner503fadc2006-08-10 05:45:44 +0000914/// ParseReturnStatement
915/// jump-statement:
916/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +0000917Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000918 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000919 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000920
Chris Lattner30f910e2006-10-16 05:52:41 +0000921 ExprResult R(0);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000922 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000923 R = ParseExpression();
924 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000925 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000926 return true;
927 }
Chris Lattnera0927ce2006-08-12 16:59:03 +0000928 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000929 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Chris Lattner503fadc2006-08-10 05:45:44 +0000930}
Chris Lattner0116c472006-08-15 06:03:28 +0000931
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000932/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
933/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffb2c80c72008-02-07 03:50:06 +0000934Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +0000935 if (Tok.is(tok::l_brace)) {
936 unsigned short savedBraceCount = BraceCount;
937 do {
938 ConsumeAnyToken();
939 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
940 } else {
941 // From the MS website: If used without braces, the __asm keyword means
942 // that the rest of the line is an assembly-language statement.
943 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +0000944 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +0000945 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
946 do {
947 ConsumeAnyToken();
948 TokLoc = Tok.getLocation();
949 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
950 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
951 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +0000952 }
Steve Naroffb2c80c72008-02-07 03:50:06 +0000953 return false;
954}
955
Chris Lattner0116c472006-08-15 06:03:28 +0000956/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000957/// asm-statement:
958/// gnu-asm-statement
959/// ms-asm-statement
960///
961/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +0000962/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
963///
964/// [GNU] asm-argument:
965/// asm-string-literal
966/// asm-string-literal ':' asm-operands[opt]
967/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
968/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
969/// ':' asm-clobbers
970///
971/// [GNU] asm-clobbers:
972/// asm-string-literal
973/// asm-clobbers ',' asm-string-literal
974///
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000975/// [MS] ms-asm-statement:
976/// '__asm' assembly-instruction ';'[opt]
977/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
978///
979/// [MS] assembly-instruction-list:
980/// assembly-instruction ';'[opt]
981/// assembly-instruction-list ';' assembly-instruction ';'[opt]
982///
Steve Naroffb2c80c72008-02-07 03:50:06 +0000983Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000984 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +0000985 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner0116c472006-08-15 06:03:28 +0000986
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000987 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +0000988 msAsm = true;
989 return FuzzyParseMicrosoftAsmStatement();
990 }
Chris Lattner0116c472006-08-15 06:03:28 +0000991 DeclSpec DS;
992 SourceLocation Loc = Tok.getLocation();
993 ParseTypeQualifierListOpt(DS);
994
995 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +0000996 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner0116c472006-08-15 06:03:28 +0000997 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
Chris Lattnera925dc62006-11-28 04:33:46 +0000998 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner0116c472006-08-15 06:03:28 +0000999 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
1000
1001 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001002 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001003 bool isSimple = false;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001004 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001005 Diag(Tok, diag::err_expected_lparen_after, "asm");
1006 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +00001007 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001008 }
Chris Lattner04132372006-10-16 06:12:55 +00001009 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +00001010
Anders Carlsson81a5a692007-11-20 19:21:03 +00001011 ExprResult AsmString = ParseAsmStringLiteral();
1012 if (AsmString.isInvalid)
1013 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001014
1015 llvm::SmallVector<std::string, 4> Names;
1016 llvm::SmallVector<ExprTy*, 4> Constraints;
1017 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001018 llvm::SmallVector<ExprTy*, 4> Clobbers;
Chris Lattner0116c472006-08-15 06:03:28 +00001019
Anders Carlsson19fe1162008-02-05 23:03:50 +00001020 unsigned NumInputs = 0, NumOutputs = 0;
1021
1022 SourceLocation RParenLoc;
1023 if (Tok.is(tok::r_paren)) {
1024 // We have a simple asm expression
1025 isSimple = true;
1026
1027 RParenLoc = ConsumeParen();
1028 } else {
1029 // Parse Outputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001030 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1031 return true;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001032
1033 NumOutputs = Names.size();
1034
1035 // Parse Inputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001036 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1037 return true;
1038
Anders Carlsson19fe1162008-02-05 23:03:50 +00001039 assert(Names.size() == Constraints.size() &&
1040 Constraints.size() == Exprs.size()
1041 && "Input operand size mismatch!");
1042
1043 NumInputs = Names.size() - NumOutputs;
1044
1045 // Parse the clobbers, if present.
1046 if (Tok.is(tok::colon)) {
Anders Carlsson091a0592007-11-21 23:27:34 +00001047 ConsumeToken();
Anders Carlsson19fe1162008-02-05 23:03:50 +00001048
1049 // Parse the asm-string list for clobbers.
1050 while (1) {
1051 ExprResult Clobber = ParseAsmStringLiteral();
1052
1053 if (Clobber.isInvalid)
1054 break;
1055
1056 Clobbers.push_back(Clobber.Val);
1057
1058 if (Tok.isNot(tok::comma)) break;
1059 ConsumeToken();
1060 }
Chris Lattner0116c472006-08-15 06:03:28 +00001061 }
Anders Carlsson19fe1162008-02-05 23:03:50 +00001062
1063 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner0116c472006-08-15 06:03:28 +00001064 }
1065
Anders Carlsson19fe1162008-02-05 23:03:50 +00001066 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1067 NumOutputs, NumInputs,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001068 &Names[0], &Constraints[0], &Exprs[0],
1069 AsmString.Val,
1070 Clobbers.size(), &Clobbers[0],
1071 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001072}
1073
1074/// ParseAsmOperands - Parse the asm-operands production as used by
1075/// asm-statement. We also parse a leading ':' token. If the leading colon is
1076/// not present, we do not parse anything.
1077///
1078/// [GNU] asm-operands:
1079/// asm-operand
1080/// asm-operands ',' asm-operand
1081///
1082/// [GNU] asm-operand:
1083/// asm-string-literal '(' expression ')'
1084/// '[' identifier ']' asm-string-literal '(' expression ')'
1085///
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001086bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001087 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1088 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001089 // Only do anything if this operand is present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001090 if (Tok.isNot(tok::colon)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001091 ConsumeToken();
1092
1093 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001094 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001095 return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001096
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001097 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001098 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001099 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001100 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +00001101
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001102 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001103 Diag(Tok, diag::err_expected_ident);
1104 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001105 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001106 }
Chris Lattner645ff3f2007-10-29 04:06:22 +00001107
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001108 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001109 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001110
1111 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner0116c472006-08-15 06:03:28 +00001112 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001113 } else
1114 Names.push_back(std::string());
Chris Lattner0116c472006-08-15 06:03:28 +00001115
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001116 ExprResult Constraint = ParseAsmStringLiteral();
1117 if (Constraint.isInvalid) {
1118 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001119 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001120 }
1121 Constraints.push_back(Constraint.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001122
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001123 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001124 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1125 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001126 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001127 }
1128
1129 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001130 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +00001131 if (Res.isInvalid) {
1132 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001133 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001134 }
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001135 Exprs.push_back(Res.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001136 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001137 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001138 ConsumeToken();
1139 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001140
1141 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001142}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001143
1144Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1145 SourceLocation L, SourceLocation R) {
1146 // Do not enter a scope for the brace, as the arguments are in the same scope
1147 // (the function body) as the body itself. Instead, just read the statement
1148 // list and put it into a CompoundStmt for safe keeping.
1149 StmtResult FnBody = ParseCompoundStatementBody();
1150
1151 // If the function body could not be parsed, make a bogus compoundstmt.
1152 if (FnBody.isInvalid)
1153 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1154
1155 // Leave the function body scope.
1156 ExitScope();
1157
Steve Naroffb313fc32007-11-11 23:20:51 +00001158 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001159}