blob: 263079f1380a74d2abe0b71cc333a674214d3133 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner39146d62008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroffb746ce82008-02-07 23:24:32 +000019#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Parse/DeclSpec.h"
21#include "clang/Parse/Scope.h"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Fariborz Jahanianb384d322007-10-04 20:19:06 +000041/// [OBC] objc-throw-statement
42/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000043/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000044/// [GNU] asm-statement
45/// [OMP] openmp-construct [TODO]
46///
47/// labeled-statement:
48/// identifier ':' statement
49/// 'case' constant-expression ':' statement
50/// 'default' ':' statement
51///
52/// selection-statement:
53/// if-statement
54/// switch-statement
55///
56/// iteration-statement:
57/// while-statement
58/// do-statement
59/// for-statement
60///
61/// expression-statement:
62/// expression[opt] ';'
63///
64/// jump-statement:
65/// 'goto' identifier ';'
66/// 'continue' ';'
67/// 'break' ';'
68/// 'return' expression[opt] ';'
69/// [GNU] 'goto' '*' expression ';'
70///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000071/// [OBC] objc-throw-statement:
72/// [OBC] '@' 'throw' expression ';'
73/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000074///
Sebastian Redl61364dd2008-12-11 19:30:53 +000075Parser::OwningStmtResult
76Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000077 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000078 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000079
Reid Spencer5f016e22007-07-11 17:01:13 +000080 // Cases in this switch statement should fall through if the parser expects
81 // the token to end in a semicolon (in which case SemiError should be set),
82 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000083 tok::TokenKind Kind = Tok.getKind();
84 SourceLocation AtLoc;
85 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000086 case tok::at: // May be a @try or @throw statement
87 {
88 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000089 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000090 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +000092 case tok::identifier:
93 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
94 // identifier ':' statement
95 return ParseLabeledStatement();
96 }
97 // PASS THROUGH.
98
Reid Spencer5f016e22007-07-11 17:01:13 +000099 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000100 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000101 SourceLocation DeclStart = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000102 DeclTy *Decl = ParseDeclaration(Declarator::BlockContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000103 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000104 return Owned(Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart));
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000105 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000107 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 } else {
109 // expression[opt] ';'
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000110 OwningExprResult Expr(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000111 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // If the expression is invalid, skip ahead to the next semicolon. Not
113 // doing this opens us up to the possibility of infinite loops if
114 // ParseExpression does not consume any tokens.
115 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000116 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 }
118 // Otherwise, eat the semicolon.
119 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000120 return Owned(Actions.ActOnExprStmt(Expr.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000122
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 case tok::kw_case: // C99 6.8.1: labeled-statement
124 return ParseCaseStatement();
125 case tok::kw_default: // C99 6.8.1: labeled-statement
126 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 case tok::l_brace: // C99 6.8.2: compound-statement
129 return ParseCompoundStatement();
130 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl61364dd2008-12-11 19:30:53 +0000131 return Owned(Actions.ActOnNullStmt(ConsumeToken()));
132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 case tok::kw_if: // C99 6.8.4.1: if-statement
134 return ParseIfStatement();
135 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000136 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000139 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 case tok::kw_do: // C99 6.8.5.2: do-statement
141 Res = ParseDoStatement();
142 SemiError = "do/while loop";
143 break;
144 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000145 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000146
147 case tok::kw_goto: // C99 6.8.6.1: goto-statement
148 Res = ParseGotoStatement();
149 SemiError = "goto statement";
150 break;
151 case tok::kw_continue: // C99 6.8.6.2: continue-statement
152 Res = ParseContinueStatement();
153 SemiError = "continue statement";
154 break;
155 case tok::kw_break: // C99 6.8.6.3: break-statement
156 Res = ParseBreakStatement();
157 SemiError = "break statement";
158 break;
159 case tok::kw_return: // C99 6.8.6.4: return-statement
160 Res = ParseReturnStatement();
161 SemiError = "return statement";
162 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 case tok::kw_asm:
Steve Naroffd62701b2008-02-07 03:50:06 +0000165 bool msAsm = false;
166 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000167 if (msAsm) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 SemiError = "asm statement";
169 break;
170 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000173 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000175 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000176 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000177 // Skip until we see a } or ;, but don't eat it.
178 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000180 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181}
182
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000183/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000184///
185/// labeled-statement:
186/// identifier ':' statement
187/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000188///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000189Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000190 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
191 "Not an identifier!");
192
193 Token IdentTok = Tok; // Save the whole token.
194 ConsumeToken(); // eat the identifier.
195
196 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000197
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000198 // identifier ':' statement
199 SourceLocation ColonLoc = ConsumeToken();
200
201 // Read label attributes, if present.
202 DeclTy *AttrList = 0;
203 if (Tok.is(tok::kw___attribute))
204 // TODO: save these somewhere.
205 AttrList = ParseAttributes();
206
Sebastian Redl61364dd2008-12-11 19:30:53 +0000207 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000208
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000209 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000210 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000211 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000212
Sebastian Redl61364dd2008-12-11 19:30:53 +0000213 return Owned(Actions.ActOnLabelStmt(IdentTok.getLocation(),
214 IdentTok.getIdentifierInfo(),
215 ColonLoc, SubStmt.release()));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000216}
Reid Spencer5f016e22007-07-11 17:01:13 +0000217
218/// ParseCaseStatement
219/// labeled-statement:
220/// 'case' constant-expression ':' statement
221/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
222///
223/// Note that this does not parse the 'statement' at the end.
224///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000225Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000226 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
228
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000229 OwningExprResult LHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000230 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000232 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 // GNU case range extension.
236 SourceLocation DotDotDotLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000237 OwningExprResult RHS(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000238 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 Diag(Tok, diag::ext_gnu_case_range);
240 DotDotDotLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000241
242 RHS = ParseConstantExpression();
243 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000245 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000248
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000249 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000250 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000252 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000258 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000260 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000262
263 OwningStmtResult SubStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000264
265 // Broken substmt shouldn't prevent the case from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000266 if (SubStmt.isInvalid())
Steve Naroff1b273c42007-09-16 14:56:35 +0000267 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000268
269 return Owned(Actions.ActOnCaseStmt(CaseLoc, LHS.release(), DotDotDotLoc,
270 RHS.release(), ColonLoc,
271 SubStmt.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000272}
273
274/// ParseDefaultStatement
275/// labeled-statement:
276/// 'default' ':' statement
277/// Note that this does not parse the 'statement' at the end.
278///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000279Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000280 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
282
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000283 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000284 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000286 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000292 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000294 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 }
296
Sebastian Redl61364dd2008-12-11 19:30:53 +0000297 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000298 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000299 return StmtError();
300
301 return Owned(Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
302 SubStmt.release(), CurScope));
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
305
306/// ParseCompoundStatement - Parse a "{}" block.
307///
308/// compound-statement: [C99 6.8.2]
309/// { block-item-list[opt] }
310/// [GNU] { label-declarations block-item-list } [TODO]
311///
312/// block-item-list:
313/// block-item
314/// block-item-list block-item
315///
316/// block-item:
317/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000318/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000319/// statement
320/// [OMP] openmp-directive [TODO]
321///
322/// [GNU] label-declarations:
323/// [GNU] label-declaration
324/// [GNU] label-declarations label-declaration
325///
326/// [GNU] label-declaration:
327/// [GNU] '__label__' identifier-list ';'
328///
329/// [OMP] openmp-directive: [TODO]
330/// [OMP] barrier-directive
331/// [OMP] flush-directive
332///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000333Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000334 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000335
Chris Lattner31e05722007-08-26 06:24:45 +0000336 // Enter a scope to hold everything within the compound stmt. Compound
337 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000338 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
340 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000341 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
343
344
345/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000346/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000347/// consume the '}' at the end of the block. It does not manipulate the scope
348/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000349Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
351
352 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000353 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000354
355 typedef StmtVector StmtsTy;
356 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000357 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000358 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000359 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000360 R = ParseStatementOrDeclaration(false);
361 } else {
362 // __extension__ can start declarations and it can also be a unary
363 // operator for expressions. Consume multiple __extension__ markers here
364 // until we can determine which is which.
365 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000366 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000367 ConsumeToken();
368
Chris Lattner043a0b52008-03-13 06:32:11 +0000369 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000370 ExtensionRAIIObject O(Diags); // Use RAII to do this.
371
Chris Lattner45a566c2007-08-27 01:01:57 +0000372 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000373 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000374 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000375 SourceLocation DeclStart = Tok.getLocation();
376 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
377 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000378 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000379 } else {
380 // Otherwise this was a unary __extension__ marker. Parse the
381 // subexpression and add the __extension__ unary op.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000382 OwningExprResult Res(ParseCastExpression(false));
Chris Lattner043a0b52008-03-13 06:32:11 +0000383
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000384 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000385 SkipUntil(tok::semi);
386 continue;
387 }
388
389 // Add the __extension__ node to the AST.
Douglas Gregor74253732008-11-19 15:42:04 +0000390 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000391 Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000392 if (Res.isInvalid())
Chris Lattner45a566c2007-08-27 01:01:57 +0000393 continue;
394
Chris Lattner39146d62008-10-20 06:51:33 +0000395 // Eat the semicolon at the end of stmt and convert the expr into a
396 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000397 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000398 R = Actions.ActOnExprStmt(Res.release());
Chris Lattner45a566c2007-08-27 01:01:57 +0000399 }
400 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000401
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000402 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000403 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000407 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000409 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000413 return Owned(Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, Stmts.take(),
414 Stmts.size(), isStmtExpr));
Reid Spencer5f016e22007-07-11 17:01:13 +0000415}
416
417/// ParseIfStatement
418/// if-statement: [C99 6.8.4.1]
419/// 'if' '(' expression ')' statement
420/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000421/// [C++] 'if' '(' condition ')' statement
422/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000423///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000424Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000425 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
427
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000428 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000429 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000431 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000433
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000434 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
435
Chris Lattner22153252007-08-26 23:08:06 +0000436 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
437 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000438 //
439 // C++ 6.4p3:
440 // A name introduced by a declaration in a condition is in scope from its
441 // point of declaration until the end of the substatements controlled by the
442 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000443 // C++ 3.3.2p4:
444 // Names declared in the for-init-statement, and in the condition of if,
445 // while, for, and switch statements are local to the if, while, for, or
446 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000447 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000448 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000449
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 // Parse the condition.
Chris Lattner18914bc2008-12-12 06:19:11 +0000451 SourceLocation LParenLoc = ConsumeParen();
452
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000453 OwningExprResult CondExp(Actions);
Chris Lattner18914bc2008-12-12 06:19:11 +0000454 if (getLang().CPlusPlus)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000455 CondExp = ParseCXXCondition();
Chris Lattner18914bc2008-12-12 06:19:11 +0000456 else
457 CondExp = ParseExpression();
458
459 // If the parser was confused by the condition and we don't have a ')', try to
460 // recover by skipping ahead to a semi and bailing out. If condexp is
461 // semantically invalid but we have well formed code, keep going.
462 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 SkipUntil(tok::semi);
Chris Lattner18914bc2008-12-12 06:19:11 +0000464 // Skipping may have stopped if it found the containing ')'. If so, we can
465 // continue parsing the if statement.
466 if (Tok.isNot(tok::r_paren))
467 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 }
469
Chris Lattner18914bc2008-12-12 06:19:11 +0000470 // Otherwise the condition is valid or the rparen is present.
471 MatchRHSPunctuation(tok::r_paren, LParenLoc);
472
Chris Lattner0ecea032007-08-22 05:28:50 +0000473 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000474 // there is no compound stmt. C90 does not have this clause. We only do this
475 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000476 //
477 // C++ 6.4p1:
478 // The substatement in a selection-statement (each substatement, in the else
479 // form of the if statement) implicitly defines a local scope.
480 //
481 // For C++ we create a scope for the condition and a new scope for
482 // substatements because:
483 // -When the 'then' scope exits, we want the condition declaration to still be
484 // active for the 'else' scope too.
485 // -Sema will detect name clashes by considering declarations of a
486 // 'ControlScope' as part of its direct subscope.
487 // -If we wanted the condition and substatement to be in the same scope, we
488 // would have to notify ParseStatement not to create a new scope. It's
489 // simpler to let it create a new scope.
490 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000491 ParseScope InnerScope(this, Scope::DeclScope,
492 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000493
Chris Lattnerb96728d2007-10-29 05:08:52 +0000494 // Read the 'then' stmt.
495 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000496 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000497
Chris Lattnera36ce712007-08-22 05:16:28 +0000498 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000499 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000500
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 // If it has an else, parse it.
502 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000503 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000504 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000505
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000506 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000508
Chris Lattner0ecea032007-08-22 05:28:50 +0000509 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000510 // there is no compound stmt. C90 does not have this clause. We only do
511 // this if the body isn't a compound statement to avoid push/pop in common
512 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000513 //
514 // C++ 6.4p1:
515 // The substatement in a selection-statement (each substatement, in the else
516 // form of the if statement) implicitly defines a local scope.
517 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000518 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000519 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000520
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000521 bool WithinElse = CurScope->isWithinElse();
522 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000523 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000525 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000526
527 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000528 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000530
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000531 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000532
533 // If the condition was invalid, discard the if statement. We could recover
534 // better by replacing it with a valid expr, but don't do that yet.
535 if (CondExp.isInvalid())
536 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000537
Chris Lattnerb96728d2007-10-29 05:08:52 +0000538 // If the then or else stmt is invalid and the other is valid (and present),
539 // make turn the invalid one into a null stmt to avoid dropping the other
540 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000541 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
542 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
543 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000544 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000545 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000546 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000547
Chris Lattnerb96728d2007-10-29 05:08:52 +0000548 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000549 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000550 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000551 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000552 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000553
Sebastian Redl61364dd2008-12-11 19:30:53 +0000554 return Owned(Actions.ActOnIfStmt(IfLoc, CondExp.release(), ThenStmt.release(),
555 ElseLoc, ElseStmt.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000556}
557
558/// ParseSwitchStatement
559/// switch-statement:
560/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000561/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000562Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000563 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
565
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000566 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000567 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000569 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 }
Chris Lattner22153252007-08-26 23:08:06 +0000571
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000572 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
573
Chris Lattner22153252007-08-26 23:08:06 +0000574 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
575 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000576 //
577 // C++ 6.4p3:
578 // A name introduced by a declaration in a condition is in scope from its
579 // point of declaration until the end of the substatements controlled by the
580 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000581 // C++ 3.3.2p4:
582 // Names declared in the for-init-statement, and in the condition of if,
583 // while, for, and switch statements are local to the if, while, for, or
584 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000585 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000586 unsigned ScopeFlags
587 = C99orCXX? Scope::BreakScope | Scope::DeclScope | Scope::ControlScope
588 : Scope::BreakScope;
589 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000590
591 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000592 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000593 if (getLang().CPlusPlus) {
594 SourceLocation LParenLoc = ConsumeParen();
595 Cond = ParseCXXCondition();
596 MatchRHSPunctuation(tok::r_paren, LParenLoc);
597 } else {
598 Cond = ParseSimpleParenExpression();
599 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000600
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000601 if (Cond.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000602 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000603
Sebastian Redleffa8d12008-12-10 00:02:53 +0000604 OwningStmtResult Switch(Actions,
605 Actions.ActOnStartOfSwitchStmt(Cond.release()));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000606
Chris Lattner0ecea032007-08-22 05:28:50 +0000607 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000608 // there is no compound stmt. C90 does not have this clause. We only do this
609 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000610 //
611 // C++ 6.4p1:
612 // The substatement in a selection-statement (each substatement, in the else
613 // form of the if statement) implicitly defines a local scope.
614 //
615 // See comments in ParseIfStatement for why we create a scope for the
616 // condition and a new scope for substatement in C++.
617 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000618 ParseScope InnerScope(this, Scope::DeclScope,
619 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000622 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000623
Chris Lattner0ecea032007-08-22 05:28:50 +0000624 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000625 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000626
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000627 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000628 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000629 // FIXME: Remove the case statement list from the Switch statement.
630 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000631
632 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000633
Sebastian Redl9a920342008-12-11 19:48:14 +0000634 return Owned(Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.release(),
635 Body.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000636}
637
638/// ParseWhileStatement
639/// while-statement: [C99 6.8.5.1]
640/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000641/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000642Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000643 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 SourceLocation WhileLoc = Tok.getLocation();
645 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000646
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000647 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000648 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000650 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000652
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000653 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
654
Chris Lattner22153252007-08-26 23:08:06 +0000655 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
656 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000657 //
658 // C++ 6.4p3:
659 // A name introduced by a declaration in a condition is in scope from its
660 // point of declaration until the end of the substatements controlled by the
661 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000662 // C++ 3.3.2p4:
663 // Names declared in the for-init-statement, and in the condition of if,
664 // while, for, and switch statements are local to the if, while, for, or
665 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000666 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000667 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000668 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000669 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
670 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000671 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000672 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
673 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000674
675 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000676 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000677 if (getLang().CPlusPlus) {
678 SourceLocation LParenLoc = ConsumeParen();
679 Cond = ParseCXXCondition();
680 MatchRHSPunctuation(tok::r_paren, LParenLoc);
681 } else {
682 Cond = ParseSimpleParenExpression();
683 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000684
Chris Lattner0ecea032007-08-22 05:28:50 +0000685 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000686 // there is no compound stmt. C90 does not have this clause. We only do this
687 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000688 //
689 // C++ 6.5p2:
690 // The substatement in an iteration-statement implicitly defines a local scope
691 // which is entered and exited each time through the loop.
692 //
693 // See comments in ParseIfStatement for why we create a scope for the
694 // condition and a new scope for substatement in C++.
695 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000696 ParseScope InnerScope(this, Scope::DeclScope,
697 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000698
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000700 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000701
Chris Lattner0ecea032007-08-22 05:28:50 +0000702 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000703 InnerScope.Exit();
704 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000705
706 if (Cond.isInvalid() || Body.isInvalid())
707 return StmtError();
708
709 return Owned(Actions.ActOnWhileStmt(WhileLoc, Cond.release(),Body.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000710}
711
712/// ParseDoStatement
713/// do-statement: [C99 6.8.5.2]
714/// 'do' statement 'while' '(' expression ')' ';'
715/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000716Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000717 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000719
Chris Lattner22153252007-08-26 23:08:06 +0000720 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
721 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000722 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000723 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000724 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000725 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000726 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000727
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000728 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000729
Chris Lattner0ecea032007-08-22 05:28:50 +0000730 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000731 // there is no compound stmt. C90 does not have this clause. We only do this
732 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000733 //
734 // C++ 6.5p2:
735 // The substatement in an iteration-statement implicitly defines a local scope
736 // which is entered and exited each time through the loop.
737 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000738 ParseScope InnerScope(this, Scope::DeclScope,
739 (getLang().C99 || getLang().CPlusPlus) &&
740 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000741
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000743 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000744
Chris Lattner0ecea032007-08-22 05:28:50 +0000745 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000746 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000747
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000748 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000749 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000750 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000751 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000752 SkipUntil(tok::semi, false, true);
753 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000754 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
756 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000757
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000758 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000759 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000760 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000761 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000763
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 // Parse the condition.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000765 OwningExprResult Cond(ParseSimpleParenExpression());
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000766 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000767
Sebastian Redl9a920342008-12-11 19:48:14 +0000768 if (Cond.isInvalid() || Body.isInvalid())
769 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000770
Sebastian Redl9a920342008-12-11 19:48:14 +0000771 return Owned(Actions.ActOnDoStmt(DoLoc, Body.release(), WhileLoc,
772 Cond.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000773}
774
775/// ParseForStatement
776/// for-statement: [C99 6.8.5.3]
777/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
778/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000779/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
780/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000781/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
782/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000783///
784/// [C++] for-init-statement:
785/// [C++] expression-statement
786/// [C++] simple-declaration
787///
Sebastian Redl9a920342008-12-11 19:48:14 +0000788Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000789 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000791
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000792 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000793 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000795 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000797
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000798 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
799
Chris Lattner22153252007-08-26 23:08:06 +0000800 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
801 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000802 //
803 // C++ 6.4p3:
804 // A name introduced by a declaration in a condition is in scope from its
805 // point of declaration until the end of the substatements controlled by the
806 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000807 // C++ 3.3.2p4:
808 // Names declared in the for-init-statement, and in the condition of if,
809 // while, for, and switch statements are local to the if, while, for, or
810 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000811 // C++ 6.5.3p1:
812 // Names declared in the for-init-statement are in the same declarative-region
813 // as those declared in the condition.
814 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000815 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000816 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000817 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
818 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000819 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000820 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
821
822 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000823
824 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000825 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000826
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000827 bool ForEach = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000828 OwningStmtResult FirstPart(Actions), ThirdPart(Actions);
829 OwningExprResult SecondPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000830
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000832 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 // no first part, eat the ';'.
834 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000835 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000837 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000839
Chris Lattner81c018d2008-03-13 06:29:04 +0000840 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000841 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000842 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000843 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
844 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000845 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000846 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000847 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000848 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 } else {
850 Value = ParseExpression();
851
852 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000853 if (!Value.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000854 FirstPart = Actions.ActOnExprStmt(Value.release());
855
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000856 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000858 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000859 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000860 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000861 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000862 }
863 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000864 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 SkipUntil(tok::semi);
866 }
867 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000868 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000869 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000870 // Parse the second part of the for specifier.
871 if (Tok.is(tok::semi)) { // for (...;;
872 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000873 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000874 SecondPart = getLang().CPlusPlus ? ParseCXXCondition()
875 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000876 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000877
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000878 if (Tok.is(tok::semi)) {
879 ConsumeToken();
880 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000881 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000882 SkipUntil(tok::semi);
883 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000884
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000885 // Parse the third part of the for specifier.
886 if (Tok.is(tok::r_paren)) { // for (...;...;)
887 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000888 } else {
889 Value = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000890 if (!Value.isInvalid()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000891 // Turn the expression into a stmt.
Sebastian Redleffa8d12008-12-10 00:02:53 +0000892 ThirdPart = Actions.ActOnExprStmt(Value.release());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000893 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 }
895 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 // Match the ')'.
897 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000898
Chris Lattner0ecea032007-08-22 05:28:50 +0000899 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000900 // there is no compound stmt. C90 does not have this clause. We only do this
901 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000902 //
903 // C++ 6.5p2:
904 // The substatement in an iteration-statement implicitly defines a local scope
905 // which is entered and exited each time through the loop.
906 //
907 // See comments in ParseIfStatement for why we create a scope for
908 // for-init-statement/condition and a new scope for substatement in C++.
909 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000910 ParseScope InnerScope(this, Scope::DeclScope,
911 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000914 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000915
Chris Lattner0ecea032007-08-22 05:28:50 +0000916 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000917 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000918
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000920 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000921
922 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000923 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000924
925 if (!ForEach)
Sebastian Redl9a920342008-12-11 19:48:14 +0000926 return Owned(Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.release(),
927 SecondPart.release(), ThirdPart.release(),
928 RParenLoc, Body.release()));
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000929 else
Sebastian Redl9a920342008-12-11 19:48:14 +0000930 return Owned(Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
931 FirstPart.release(),
932 SecondPart.release(),
933 RParenLoc, Body.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000934}
935
936/// ParseGotoStatement
937/// jump-statement:
938/// 'goto' identifier ';'
939/// [GNU] 'goto' '*' expression ';'
940///
941/// Note: this lets the caller parse the end ';'.
942///
Sebastian Redl9a920342008-12-11 19:48:14 +0000943Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000944 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000946
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000947 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000948 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000949 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 Tok.getIdentifierInfo());
951 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000952 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 // GNU indirect goto extension.
954 Diag(Tok, diag::ext_gnu_indirect_goto);
955 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000956 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000957 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000959 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000961 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.release());
Chris Lattner95cfb852007-07-22 04:13:33 +0000962 } else {
963 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +0000964 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000966
Sebastian Redl9a920342008-12-11 19:48:14 +0000967 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000968}
969
970/// ParseContinueStatement
971/// jump-statement:
972/// 'continue' ';'
973///
974/// Note: this lets the caller parse the end ';'.
975///
Sebastian Redl9a920342008-12-11 19:48:14 +0000976Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000978 return Owned(Actions.ActOnContinueStmt(ContinueLoc, CurScope));
Reid Spencer5f016e22007-07-11 17:01:13 +0000979}
980
981/// ParseBreakStatement
982/// jump-statement:
983/// 'break' ';'
984///
985/// Note: this lets the caller parse the end ';'.
986///
Sebastian Redl9a920342008-12-11 19:48:14 +0000987Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000989 return Owned(Actions.ActOnBreakStmt(BreakLoc, CurScope));
Reid Spencer5f016e22007-07-11 17:01:13 +0000990}
991
992/// ParseReturnStatement
993/// jump-statement:
994/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +0000995Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000996 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000998
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000999 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001000 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001002 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001004 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 }
1006 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001007 return Owned(Actions.ActOnReturnStmt(ReturnLoc, R.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001008}
1009
Steve Naroff5f8aa692008-02-11 23:15:56 +00001010/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1011/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001012Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001013 if (Tok.is(tok::l_brace)) {
1014 unsigned short savedBraceCount = BraceCount;
1015 do {
1016 ConsumeAnyToken();
1017 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1018 } else {
1019 // From the MS website: If used without braces, the __asm keyword means
1020 // that the rest of the line is an assembly-language statement.
1021 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001022 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001023 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1024 do {
1025 ConsumeAnyToken();
1026 TokLoc = Tok.getLocation();
1027 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1028 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1029 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001030 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001031 return Owned(Actions.ActOnNullStmt(Tok.getLocation()));
Steve Naroffd62701b2008-02-07 03:50:06 +00001032}
1033
Reid Spencer5f016e22007-07-11 17:01:13 +00001034/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001035/// asm-statement:
1036/// gnu-asm-statement
1037/// ms-asm-statement
1038///
1039/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001040/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1041///
1042/// [GNU] asm-argument:
1043/// asm-string-literal
1044/// asm-string-literal ':' asm-operands[opt]
1045/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1046/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1047/// ':' asm-clobbers
1048///
1049/// [GNU] asm-clobbers:
1050/// asm-string-literal
1051/// asm-clobbers ',' asm-string-literal
1052///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001053/// [MS] ms-asm-statement:
1054/// '__asm' assembly-instruction ';'[opt]
1055/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1056///
1057/// [MS] assembly-instruction-list:
1058/// assembly-instruction ';'[opt]
1059/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1060///
Sebastian Redl9a920342008-12-11 19:48:14 +00001061Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001062 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001063 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001064
Steve Naroff5f8aa692008-02-11 23:15:56 +00001065 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001066 msAsm = true;
1067 return FuzzyParseMicrosoftAsmStatement();
1068 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 DeclSpec DS;
1070 SourceLocation Loc = Tok.getLocation();
1071 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001072
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1074 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001075 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001077 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001078
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001080 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001081 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001082 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001083 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001085 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 }
1087 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001088
Sebastian Redleffa8d12008-12-10 00:02:53 +00001089 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001090 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001091 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001092
Anders Carlssonb235fc22007-11-22 01:36:19 +00001093 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001094 ExprVector Constraints(Actions);
1095 ExprVector Exprs(Actions);
1096 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097
Anders Carlssondfab34a2008-02-05 23:03:50 +00001098 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001099
Anders Carlssondfab34a2008-02-05 23:03:50 +00001100 SourceLocation RParenLoc;
1101 if (Tok.is(tok::r_paren)) {
1102 // We have a simple asm expression
1103 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001104
Anders Carlssondfab34a2008-02-05 23:03:50 +00001105 RParenLoc = ConsumeParen();
1106 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001107 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001108 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001109 return StmtError();
1110
Anders Carlssondfab34a2008-02-05 23:03:50 +00001111 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001112
Anders Carlssondfab34a2008-02-05 23:03:50 +00001113 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001114 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001115 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001116
Anders Carlssondfab34a2008-02-05 23:03:50 +00001117 assert(Names.size() == Constraints.size() &&
1118 Constraints.size() == Exprs.size()
1119 && "Input operand size mismatch!");
1120
1121 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001122
Anders Carlssondfab34a2008-02-05 23:03:50 +00001123 // Parse the clobbers, if present.
1124 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001125 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001126
Anders Carlssondfab34a2008-02-05 23:03:50 +00001127 // Parse the asm-string list for clobbers.
1128 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001129 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001130
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001131 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001132 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001133
1134 Clobbers.push_back(Clobber.release());
1135
Anders Carlssondfab34a2008-02-05 23:03:50 +00001136 if (Tok.isNot(tok::comma)) break;
1137 ConsumeToken();
1138 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001140
Anders Carlssondfab34a2008-02-05 23:03:50 +00001141 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001143
Sebastian Redl9a920342008-12-11 19:48:14 +00001144 return Owned(Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1145 NumOutputs, NumInputs,
1146 &Names[0], Constraints.take(),
1147 Exprs.take(), AsmString.release(),
1148 Clobbers.size(), Clobbers.take(),
1149 RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001150}
1151
1152/// ParseAsmOperands - Parse the asm-operands production as used by
1153/// asm-statement. We also parse a leading ':' token. If the leading colon is
1154/// not present, we do not parse anything.
1155///
1156/// [GNU] asm-operands:
1157/// asm-operand
1158/// asm-operands ',' asm-operand
1159///
1160/// [GNU] asm-operand:
1161/// asm-string-literal '(' expression ')'
1162/// '[' identifier ']' asm-string-literal '(' expression ')'
1163///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001164bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001165 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1166 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001168 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 ConsumeToken();
1170
1171 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001172 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001173 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001174
Anders Carlssonb235fc22007-11-22 01:36:19 +00001175 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001177 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 SourceLocation Loc = ConsumeBracket();
1179
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001180 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 Diag(Tok, diag::err_expected_ident);
1182 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001183 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 }
Chris Lattner69efba72007-10-29 04:06:22 +00001185
Anders Carlssonb235fc22007-11-22 01:36:19 +00001186 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001187 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001188
1189 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001191 } else
1192 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001193
Sebastian Redleffa8d12008-12-10 00:02:53 +00001194 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001195 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001196 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001197 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001198 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001199 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001200
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001201 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001202 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001204 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001206
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001208 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001209 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001211 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001213 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001215 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 ConsumeToken();
1217 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001218
1219 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001220}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001221
1222Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1223 SourceLocation L, SourceLocation R) {
1224 // Do not enter a scope for the brace, as the arguments are in the same scope
1225 // (the function body) as the body itself. Instead, just read the statement
1226 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001227 OwningStmtResult FnBody(ParseCompoundStatementBody());
1228
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001229 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001230 if (FnBody.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +00001231 FnBody = Owned(Actions.ActOnCompoundStmt(L, R, 0, 0, false));
1232
Sebastian Redleffa8d12008-12-10 00:02:53 +00001233 return Actions.ActOnFinishFunctionBody(Decl, FnBody.release());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001234}