blob: 153aeb824eb6cbba54c20d29b6a69c8cc5485412 [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
Sebastian Redla0fd8652008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// [GNU] asm-statement
46/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
53/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
62/// expression-statement:
63/// expression[opt] ';'
64///
65/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
70/// [GNU] 'goto' '*' expression ';'
71///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
74/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000075///
Sebastian Redl61364dd2008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000078 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000080
Reid Spencer5f016e22007-07-11 17:01:13 +000081 // Cases in this switch statement should fall through if the parser expects
82 // the token to end in a semicolon (in which case SemiError should be set),
83 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000084 tok::TokenKind Kind = Tok.getKind();
85 SourceLocation AtLoc;
86 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000090 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +000093 case tok::identifier:
94 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
95 // identifier ':' statement
96 return ParseLabeledStatement();
97 }
98 // PASS THROUGH.
99
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000101 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000102 SourceLocation DeclStart = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000103 DeclTy *Decl = ParseDeclaration(Declarator::BlockContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000104 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redla60528c2008-12-21 12:04:03 +0000105 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000106 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000108 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 } else {
110 // expression[opt] ';'
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000111 OwningExprResult Expr(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000112 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 // If the expression is invalid, skip ahead to the next semicolon. Not
114 // doing this opens us up to the possibility of infinite loops if
115 // ParseExpression does not consume any tokens.
116 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000117 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 // Otherwise, eat the semicolon.
120 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redla60528c2008-12-21 12:04:03 +0000121 return Actions.ActOnExprStmt(move_convert(Expr));
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 case tok::kw_case: // C99 6.8.1: labeled-statement
125 return ParseCaseStatement();
126 case tok::kw_default: // C99 6.8.1: labeled-statement
127 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 case tok::l_brace: // C99 6.8.2: compound-statement
130 return ParseCompoundStatement();
131 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000132 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 case tok::kw_if: // C99 6.8.4.1: if-statement
135 return ParseIfStatement();
136 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000137 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000138
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000140 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_do: // C99 6.8.5.2: do-statement
142 Res = ParseDoStatement();
143 SemiError = "do/while loop";
144 break;
145 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000146 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000147
148 case tok::kw_goto: // C99 6.8.6.1: goto-statement
149 Res = ParseGotoStatement();
150 SemiError = "goto statement";
151 break;
152 case tok::kw_continue: // C99 6.8.6.2: continue-statement
153 Res = ParseContinueStatement();
154 SemiError = "continue statement";
155 break;
156 case tok::kw_break: // C99 6.8.6.3: break-statement
157 Res = ParseBreakStatement();
158 SemiError = "break statement";
159 break;
160 case tok::kw_return: // C99 6.8.6.4: return-statement
161 Res = ParseReturnStatement();
162 SemiError = "return statement";
163 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000164
Sebastian Redla0fd8652008-12-21 16:41:36 +0000165 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000166 bool msAsm = false;
167 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000168 if (msAsm) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 SemiError = "asm statement";
170 break;
171 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000172
Sebastian Redla0fd8652008-12-21 16:41:36 +0000173 case tok::kw_try: // C++ 15: try-block
174 return ParseCXXTryBlock();
175 }
176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000178 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000180 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000181 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000182 // Skip until we see a } or ;, but don't eat it.
183 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000185 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186}
187
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000188/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000189///
190/// labeled-statement:
191/// identifier ':' statement
192/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000193///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000194Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000195 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
196 "Not an identifier!");
197
198 Token IdentTok = Tok; // Save the whole token.
199 ConsumeToken(); // eat the identifier.
200
201 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000202
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000203 // identifier ':' statement
204 SourceLocation ColonLoc = ConsumeToken();
205
206 // Read label attributes, if present.
207 DeclTy *AttrList = 0;
208 if (Tok.is(tok::kw___attribute))
209 // TODO: save these somewhere.
210 AttrList = ParseAttributes();
211
Sebastian Redl61364dd2008-12-11 19:30:53 +0000212 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000213
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000214 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000215 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000216 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000217
Sebastian Redlde307472009-01-11 00:38:46 +0000218 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
219 IdentTok.getIdentifierInfo(),
220 ColonLoc, move_convert(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000221}
Reid Spencer5f016e22007-07-11 17:01:13 +0000222
223/// ParseCaseStatement
224/// labeled-statement:
225/// 'case' constant-expression ':' statement
226/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
227///
228/// Note that this does not parse the 'statement' at the end.
229///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000230Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000231 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
233
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000234 OwningExprResult LHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000235 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000237 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // GNU case range extension.
241 SourceLocation DotDotDotLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000242 OwningExprResult RHS(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000243 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 Diag(Tok, diag::ext_gnu_case_range);
245 DotDotDotLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000246
247 RHS = ParseConstantExpression();
248 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000250 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000253
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000254 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000255 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000257 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000263 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000265 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000267
268 OwningStmtResult SubStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000269
270 // Broken substmt shouldn't prevent the case from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000271 if (SubStmt.isInvalid())
Steve Naroff1b273c42007-09-16 14:56:35 +0000272 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000273
Sebastian Redl117054a2008-12-28 16:13:43 +0000274 return Actions.ActOnCaseStmt(CaseLoc, move_convert(LHS), DotDotDotLoc,
275 move_convert(RHS), ColonLoc,
276 move_convert(SubStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000277}
278
279/// ParseDefaultStatement
280/// labeled-statement:
281/// 'default' ':' statement
282/// Note that this does not parse the 'statement' at the end.
283///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000284Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000285 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
287
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000288 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000289 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000291 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000297 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000299 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 }
301
Sebastian Redl61364dd2008-12-11 19:30:53 +0000302 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000303 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000304 return StmtError();
305
Sebastian Redl117054a2008-12-28 16:13:43 +0000306 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
307 move_convert(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000308}
309
310
311/// ParseCompoundStatement - Parse a "{}" block.
312///
313/// compound-statement: [C99 6.8.2]
314/// { block-item-list[opt] }
315/// [GNU] { label-declarations block-item-list } [TODO]
316///
317/// block-item-list:
318/// block-item
319/// block-item-list block-item
320///
321/// block-item:
322/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000323/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000324/// statement
325/// [OMP] openmp-directive [TODO]
326///
327/// [GNU] label-declarations:
328/// [GNU] label-declaration
329/// [GNU] label-declarations label-declaration
330///
331/// [GNU] label-declaration:
332/// [GNU] '__label__' identifier-list ';'
333///
334/// [OMP] openmp-directive: [TODO]
335/// [OMP] barrier-directive
336/// [OMP] flush-directive
337///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000338Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000339 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000340
Chris Lattner31e05722007-08-26 06:24:45 +0000341 // Enter a scope to hold everything within the compound stmt. Compound
342 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000343 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000344
345 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000346 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000347}
348
349
350/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000351/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000352/// consume the '}' at the end of the block. It does not manipulate the scope
353/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000354Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
356
357 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000358 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000359
360 typedef StmtVector StmtsTy;
361 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000362 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000363 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000364 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000365 R = ParseStatementOrDeclaration(false);
366 } else {
367 // __extension__ can start declarations and it can also be a unary
368 // operator for expressions. Consume multiple __extension__ markers here
369 // until we can determine which is which.
370 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000371 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000372 ConsumeToken();
373
Chris Lattner043a0b52008-03-13 06:32:11 +0000374 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000375 ExtensionRAIIObject O(Diags); // Use RAII to do this.
376
Chris Lattner45a566c2007-08-27 01:01:57 +0000377 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000378 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000379 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000380 SourceLocation DeclStart = Tok.getLocation();
381 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
382 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000383 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000384 } else {
385 // Otherwise this was a unary __extension__ marker. Parse the
386 // subexpression and add the __extension__ unary op.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000387 OwningExprResult Res(ParseCastExpression(false));
Chris Lattner043a0b52008-03-13 06:32:11 +0000388
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000389 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000390 SkipUntil(tok::semi);
391 continue;
392 }
393
394 // Add the __extension__ node to the AST.
Douglas Gregor74253732008-11-19 15:42:04 +0000395 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000396 Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000397 if (Res.isInvalid())
Chris Lattner45a566c2007-08-27 01:01:57 +0000398 continue;
399
Chris Lattner39146d62008-10-20 06:51:33 +0000400 // Eat the semicolon at the end of stmt and convert the expr into a
401 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000402 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redla60528c2008-12-21 12:04:03 +0000403 R = Actions.ActOnExprStmt(move_convert(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000404 }
405 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000406
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000407 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000408 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000410
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000412 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000414 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redla60528c2008-12-21 12:04:03 +0000418 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_convert(Stmts),
419 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000420}
421
Chris Lattner15ff1112008-12-12 06:31:07 +0000422/// ParseParenExprOrCondition:
423/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000424/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000425///
426/// This function parses and performs error recovery on the specified condition
427/// or expression (depending on whether we're in C++ or C mode). This function
428/// goes out of its way to recover well. It returns true if there was a parser
429/// error (the right paren couldn't be found), which indicates that the caller
430/// should try to recover harder. It returns false if the condition is
431/// successfully parsed. Note that a successful parse can still have semantic
432/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000433bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
434 bool OnlyAllowCondition) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000435 SourceLocation LParenLoc = ConsumeParen();
436
437 if (getLang().CPlusPlus)
438 CondExp = ParseCXXCondition();
439 else
440 CondExp = ParseExpression();
441
442 // If the parser was confused by the condition and we don't have a ')', try to
443 // recover by skipping ahead to a semi and bailing out. If condexp is
444 // semantically invalid but we have well formed code, keep going.
445 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
446 SkipUntil(tok::semi);
447 // Skipping may have stopped if it found the containing ')'. If so, we can
448 // continue parsing the if statement.
449 if (Tok.isNot(tok::r_paren))
450 return true;
451 }
452
453 // Otherwise the condition is valid or the rparen is present.
454 MatchRHSPunctuation(tok::r_paren, LParenLoc);
455 return false;
456}
457
458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459/// ParseIfStatement
460/// if-statement: [C99 6.8.4.1]
461/// 'if' '(' expression ')' statement
462/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000463/// [C++] 'if' '(' condition ')' statement
464/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000465///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000466Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000467 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
469
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000470 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000471 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000473 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000475
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000476 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
477
Chris Lattner22153252007-08-26 23:08:06 +0000478 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
479 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000480 //
481 // C++ 6.4p3:
482 // A name introduced by a declaration in a condition is in scope from its
483 // point of declaration until the end of the substatements controlled by the
484 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000485 // C++ 3.3.2p4:
486 // Names declared in the for-init-statement, and in the condition of if,
487 // while, for, and switch statements are local to the if, while, for, or
488 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000489 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000490 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000491
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000493 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000494 if (ParseParenExprOrCondition(CondExp))
495 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000496
Chris Lattner0ecea032007-08-22 05:28:50 +0000497 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000498 // there is no compound stmt. C90 does not have this clause. We only do this
499 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000500 //
501 // C++ 6.4p1:
502 // The substatement in a selection-statement (each substatement, in the else
503 // form of the if statement) implicitly defines a local scope.
504 //
505 // For C++ we create a scope for the condition and a new scope for
506 // substatements because:
507 // -When the 'then' scope exits, we want the condition declaration to still be
508 // active for the 'else' scope too.
509 // -Sema will detect name clashes by considering declarations of a
510 // 'ControlScope' as part of its direct subscope.
511 // -If we wanted the condition and substatement to be in the same scope, we
512 // would have to notify ParseStatement not to create a new scope. It's
513 // simpler to let it create a new scope.
514 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000515 ParseScope InnerScope(this, Scope::DeclScope,
516 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000517
Chris Lattnerb96728d2007-10-29 05:08:52 +0000518 // Read the 'then' stmt.
519 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000520 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000521
Chris Lattnera36ce712007-08-22 05:16:28 +0000522 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000523 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000524
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 // If it has an else, parse it.
526 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000527 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000528 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000529
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000530 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000532
Chris Lattner0ecea032007-08-22 05:28:50 +0000533 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000534 // there is no compound stmt. C90 does not have this clause. We only do
535 // this if the body isn't a compound statement to avoid push/pop in common
536 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000537 //
538 // C++ 6.4p1:
539 // The substatement in a selection-statement (each substatement, in the else
540 // form of the if statement) implicitly defines a local scope.
541 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000542 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000543 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000544
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000545 bool WithinElse = CurScope->isWithinElse();
546 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000547 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000549 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000550
551 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000552 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000554
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000555 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000556
557 // If the condition was invalid, discard the if statement. We could recover
558 // better by replacing it with a valid expr, but don't do that yet.
559 if (CondExp.isInvalid())
560 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000561
Chris Lattnerb96728d2007-10-29 05:08:52 +0000562 // If the then or else stmt is invalid and the other is valid (and present),
563 // make turn the invalid one into a null stmt to avoid dropping the other
564 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000565 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
566 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
567 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000568 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000569 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000570 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000571
Chris Lattnerb96728d2007-10-29 05:08:52 +0000572 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000573 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000574 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000575 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000576 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000577
Sebastian Redlde307472009-01-11 00:38:46 +0000578 return Actions.ActOnIfStmt(IfLoc, move_convert(CondExp),
579 move_convert(ThenStmt), ElseLoc,
580 move_convert(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000581}
582
583/// ParseSwitchStatement
584/// switch-statement:
585/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000586/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000587Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000588 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
590
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000591 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000592 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000594 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 }
Chris Lattner22153252007-08-26 23:08:06 +0000596
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000597 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
598
Chris Lattner22153252007-08-26 23:08:06 +0000599 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
600 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000601 //
602 // C++ 6.4p3:
603 // A name introduced by a declaration in a condition is in scope from its
604 // point of declaration until the end of the substatements controlled by the
605 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000606 // C++ 3.3.2p4:
607 // Names declared in the for-init-statement, and in the condition of if,
608 // while, for, and switch statements are local to the if, while, for, or
609 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000610 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000611 unsigned ScopeFlags = Scope::BreakScope;
612 if (C99orCXX)
613 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000614 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000615
616 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000617 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000618 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000619 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000620
621 OwningStmtResult Switch(Actions);
622 if (!Cond.isInvalid())
Sebastian Redlde307472009-01-11 00:38:46 +0000623 Switch = Actions.ActOnStartOfSwitchStmt(move_convert(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000624
Chris Lattner0ecea032007-08-22 05:28:50 +0000625 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000626 // there is no compound stmt. C90 does not have this clause. We only do this
627 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000628 //
629 // C++ 6.4p1:
630 // The substatement in a selection-statement (each substatement, in the else
631 // form of the if statement) implicitly defines a local scope.
632 //
633 // See comments in ParseIfStatement for why we create a scope for the
634 // condition and a new scope for substatement in C++.
635 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000636 ParseScope InnerScope(this, Scope::DeclScope,
637 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000640 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000641
Chris Lattner0ecea032007-08-22 05:28:50 +0000642 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000643 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000644
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000645 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000646 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000647 // FIXME: Remove the case statement list from the Switch statement.
648 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000649
650 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000651
Chris Lattner15ff1112008-12-12 06:31:07 +0000652 if (Cond.isInvalid())
653 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000654
655 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move_convert(Switch),
656 move_convert(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000657}
658
659/// ParseWhileStatement
660/// while-statement: [C99 6.8.5.1]
661/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000662/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000663Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000664 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 SourceLocation WhileLoc = Tok.getLocation();
666 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000667
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000668 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000669 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000671 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000673
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000674 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
675
Chris Lattner22153252007-08-26 23:08:06 +0000676 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
677 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000678 //
679 // C++ 6.4p3:
680 // A name introduced by a declaration in a condition is in scope from its
681 // point of declaration until the end of the substatements controlled by the
682 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000683 // C++ 3.3.2p4:
684 // Names declared in the for-init-statement, and in the condition of if,
685 // while, for, and switch statements are local to the if, while, for, or
686 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000687 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000688 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000689 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000690 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
691 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000692 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000693 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
694 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000695
696 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000697 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000698 if (ParseParenExprOrCondition(Cond))
699 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000700
Chris Lattner0ecea032007-08-22 05:28:50 +0000701 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000702 // there is no compound stmt. C90 does not have this clause. We only do this
703 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000704 //
705 // C++ 6.5p2:
706 // The substatement in an iteration-statement implicitly defines a local scope
707 // which is entered and exited each time through the loop.
708 //
709 // See comments in ParseIfStatement for why we create a scope for the
710 // condition and a new scope for substatement in C++.
711 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000712 ParseScope InnerScope(this, Scope::DeclScope,
713 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000714
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000716 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000717
Chris Lattner0ecea032007-08-22 05:28:50 +0000718 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000719 InnerScope.Exit();
720 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000721
722 if (Cond.isInvalid() || Body.isInvalid())
723 return StmtError();
724
725 return Owned(Actions.ActOnWhileStmt(WhileLoc, Cond.release(),Body.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000726}
727
728/// ParseDoStatement
729/// do-statement: [C99 6.8.5.2]
730/// 'do' statement 'while' '(' expression ')' ';'
731/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000732Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000733 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000735
Chris Lattner22153252007-08-26 23:08:06 +0000736 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
737 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000738 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000739 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000740 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000741 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000742 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000743
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000744 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000745
Chris Lattner0ecea032007-08-22 05:28:50 +0000746 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000747 // there is no compound stmt. C90 does not have this clause. We only do this
748 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000749 //
750 // C++ 6.5p2:
751 // The substatement in an iteration-statement implicitly defines a local scope
752 // which is entered and exited each time through the loop.
753 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000754 ParseScope InnerScope(this, Scope::DeclScope,
755 (getLang().C99 || getLang().CPlusPlus) &&
756 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000757
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000759 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000760
Chris Lattner0ecea032007-08-22 05:28:50 +0000761 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000762 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000763
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000764 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000765 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000766 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000767 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000768 SkipUntil(tok::semi, false, true);
769 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000770 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 }
772 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000773
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000774 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000775 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000776 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000777 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000779
Chris Lattnerff871fb2008-12-12 06:35:28 +0000780 // Parse the parenthesized condition.
781 OwningExprResult Cond(Actions);
782 ParseParenExprOrCondition(Cond, true);
783
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000784 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000785
Sebastian Redl9a920342008-12-11 19:48:14 +0000786 if (Cond.isInvalid() || Body.isInvalid())
787 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000788
Sebastian Redl9a920342008-12-11 19:48:14 +0000789 return Owned(Actions.ActOnDoStmt(DoLoc, Body.release(), WhileLoc,
790 Cond.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000791}
792
793/// ParseForStatement
794/// for-statement: [C99 6.8.5.3]
795/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
796/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000797/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
798/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000799/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
800/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000801///
802/// [C++] for-init-statement:
803/// [C++] expression-statement
804/// [C++] simple-declaration
805///
Sebastian Redl9a920342008-12-11 19:48:14 +0000806Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000807 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000809
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000810 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000811 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000813 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000815
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000816 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
817
Chris Lattner22153252007-08-26 23:08:06 +0000818 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
819 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000820 //
821 // C++ 6.4p3:
822 // A name introduced by a declaration in a condition is in scope from its
823 // point of declaration until the end of the substatements controlled by the
824 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000825 // C++ 3.3.2p4:
826 // Names declared in the for-init-statement, and in the condition of if,
827 // while, for, and switch statements are local to the if, while, for, or
828 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000829 // C++ 6.5.3p1:
830 // Names declared in the for-init-statement are in the same declarative-region
831 // as those declared in the condition.
832 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000833 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000834 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000835 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
836 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000837 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000838 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
839
840 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841
842 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000843 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000844
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000845 bool ForEach = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000846 OwningStmtResult FirstPart(Actions), ThirdPart(Actions);
847 OwningExprResult SecondPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000850 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 // no first part, eat the ';'.
852 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000853 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000855 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000857
Chris Lattner81c018d2008-03-13 06:29:04 +0000858 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000859 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000860 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000861 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
862 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000863 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000864 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000865 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000866 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 } else {
868 Value = ParseExpression();
869
870 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000871 if (!Value.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +0000872 FirstPart = Actions.ActOnExprStmt(move_convert(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000873
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000874 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000876 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000877 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000878 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000879 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000880 }
881 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000882 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 SkipUntil(tok::semi);
884 }
885 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000886 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000887 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000888 // Parse the second part of the for specifier.
889 if (Tok.is(tok::semi)) { // for (...;;
890 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000891 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000892 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000893 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000894
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000895 if (Tok.is(tok::semi)) {
896 ConsumeToken();
897 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000898 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000899 SkipUntil(tok::semi);
900 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000901
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000902 // Parse the third part of the for specifier.
903 if (Tok.is(tok::r_paren)) { // for (...;...;)
904 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000905 } else {
906 Value = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000907 if (!Value.isInvalid()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000908 // Turn the expression into a stmt.
Sebastian Redla60528c2008-12-21 12:04:03 +0000909 ThirdPart = Actions.ActOnExprStmt(move_convert(Value));
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000910 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 }
912 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 // Match the ')'.
914 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000915
Chris Lattner0ecea032007-08-22 05:28:50 +0000916 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000917 // there is no compound stmt. C90 does not have this clause. We only do this
918 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000919 //
920 // C++ 6.5p2:
921 // The substatement in an iteration-statement implicitly defines a local scope
922 // which is entered and exited each time through the loop.
923 //
924 // See comments in ParseIfStatement for why we create a scope for
925 // for-init-statement/condition and a new scope for substatement in C++.
926 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000927 ParseScope InnerScope(this, Scope::DeclScope,
928 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000929
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000931 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000932
Chris Lattner0ecea032007-08-22 05:28:50 +0000933 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000934 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000937 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000938
939 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000940 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000941
942 if (!ForEach)
Sebastian Redl9a920342008-12-11 19:48:14 +0000943 return Owned(Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.release(),
944 SecondPart.release(), ThirdPart.release(),
945 RParenLoc, Body.release()));
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000946 else
Sebastian Redl9a920342008-12-11 19:48:14 +0000947 return Owned(Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
948 FirstPart.release(),
949 SecondPart.release(),
950 RParenLoc, Body.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
952
953/// ParseGotoStatement
954/// jump-statement:
955/// 'goto' identifier ';'
956/// [GNU] 'goto' '*' expression ';'
957///
958/// Note: this lets the caller parse the end ';'.
959///
Sebastian Redl9a920342008-12-11 19:48:14 +0000960Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000961 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000963
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000964 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000965 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000966 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 Tok.getIdentifierInfo());
968 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000969 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 // GNU indirect goto extension.
971 Diag(Tok, diag::ext_gnu_indirect_goto);
972 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000973 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000974 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000976 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000978 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.release());
Chris Lattner95cfb852007-07-22 04:13:33 +0000979 } else {
980 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +0000981 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000983
Sebastian Redl9a920342008-12-11 19:48:14 +0000984 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000985}
986
987/// ParseContinueStatement
988/// jump-statement:
989/// 'continue' ';'
990///
991/// Note: this lets the caller parse the end ';'.
992///
Sebastian Redl9a920342008-12-11 19:48:14 +0000993Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000995 return Owned(Actions.ActOnContinueStmt(ContinueLoc, CurScope));
Reid Spencer5f016e22007-07-11 17:01:13 +0000996}
997
998/// ParseBreakStatement
999/// jump-statement:
1000/// 'break' ';'
1001///
1002/// Note: this lets the caller parse the end ';'.
1003///
Sebastian Redl9a920342008-12-11 19:48:14 +00001004Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001006 return Owned(Actions.ActOnBreakStmt(BreakLoc, CurScope));
Reid Spencer5f016e22007-07-11 17:01:13 +00001007}
1008
1009/// ParseReturnStatement
1010/// jump-statement:
1011/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001012Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001013 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001015
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001016 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001017 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001019 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001020 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001021 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 }
1023 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001024 return Owned(Actions.ActOnReturnStmt(ReturnLoc, R.release()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001025}
1026
Steve Naroff5f8aa692008-02-11 23:15:56 +00001027/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1028/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001029Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001030 if (Tok.is(tok::l_brace)) {
1031 unsigned short savedBraceCount = BraceCount;
1032 do {
1033 ConsumeAnyToken();
1034 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1035 } else {
1036 // From the MS website: If used without braces, the __asm keyword means
1037 // that the rest of the line is an assembly-language statement.
1038 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001039 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001040 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001041 do {
1042 ConsumeAnyToken();
1043 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001044 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001045 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1046 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001047 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001048 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001049}
1050
Reid Spencer5f016e22007-07-11 17:01:13 +00001051/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001052/// asm-statement:
1053/// gnu-asm-statement
1054/// ms-asm-statement
1055///
1056/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001057/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1058///
1059/// [GNU] asm-argument:
1060/// asm-string-literal
1061/// asm-string-literal ':' asm-operands[opt]
1062/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1063/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1064/// ':' asm-clobbers
1065///
1066/// [GNU] asm-clobbers:
1067/// asm-string-literal
1068/// asm-clobbers ',' asm-string-literal
1069///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001070/// [MS] ms-asm-statement:
1071/// '__asm' assembly-instruction ';'[opt]
1072/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1073///
1074/// [MS] assembly-instruction-list:
1075/// assembly-instruction ';'[opt]
1076/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1077///
Sebastian Redl9a920342008-12-11 19:48:14 +00001078Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001079 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001080 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001081
Steve Naroff5f8aa692008-02-11 23:15:56 +00001082 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001083 msAsm = true;
1084 return FuzzyParseMicrosoftAsmStatement();
1085 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 DeclSpec DS;
1087 SourceLocation Loc = Tok.getLocation();
1088 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001089
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1091 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001092 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001094 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001095
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001097 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001098 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001099 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001100 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001102 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 }
1104 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001105
Sebastian Redleffa8d12008-12-10 00:02:53 +00001106 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001107 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001108 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001109
Anders Carlssonb235fc22007-11-22 01:36:19 +00001110 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001111 ExprVector Constraints(Actions);
1112 ExprVector Exprs(Actions);
1113 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001114
Anders Carlssondfab34a2008-02-05 23:03:50 +00001115 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001116
Anders Carlssondfab34a2008-02-05 23:03:50 +00001117 SourceLocation RParenLoc;
1118 if (Tok.is(tok::r_paren)) {
1119 // We have a simple asm expression
1120 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001121
Anders Carlssondfab34a2008-02-05 23:03:50 +00001122 RParenLoc = ConsumeParen();
1123 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001124 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001125 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001126 return StmtError();
1127
Anders Carlssondfab34a2008-02-05 23:03:50 +00001128 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001129
Anders Carlssondfab34a2008-02-05 23:03:50 +00001130 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001131 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001132 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001133
Anders Carlssondfab34a2008-02-05 23:03:50 +00001134 assert(Names.size() == Constraints.size() &&
1135 Constraints.size() == Exprs.size()
1136 && "Input operand size mismatch!");
1137
1138 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001139
Anders Carlssondfab34a2008-02-05 23:03:50 +00001140 // Parse the clobbers, if present.
1141 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001142 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001143
Anders Carlssondfab34a2008-02-05 23:03:50 +00001144 // Parse the asm-string list for clobbers.
1145 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001146 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001147
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001148 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001149 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001150
1151 Clobbers.push_back(Clobber.release());
1152
Anders Carlssondfab34a2008-02-05 23:03:50 +00001153 if (Tok.isNot(tok::comma)) break;
1154 ConsumeToken();
1155 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001157
Anders Carlssondfab34a2008-02-05 23:03:50 +00001158 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001160
Sebastian Redl9a920342008-12-11 19:48:14 +00001161 return Owned(Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1162 NumOutputs, NumInputs,
1163 &Names[0], Constraints.take(),
1164 Exprs.take(), AsmString.release(),
1165 Clobbers.size(), Clobbers.take(),
1166 RParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001167}
1168
1169/// ParseAsmOperands - Parse the asm-operands production as used by
1170/// asm-statement. We also parse a leading ':' token. If the leading colon is
1171/// not present, we do not parse anything.
1172///
1173/// [GNU] asm-operands:
1174/// asm-operand
1175/// asm-operands ',' asm-operand
1176///
1177/// [GNU] asm-operand:
1178/// asm-string-literal '(' expression ')'
1179/// '[' identifier ']' asm-string-literal '(' expression ')'
1180///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001181bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001182 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1183 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001185 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 ConsumeToken();
1187
1188 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001189 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001190 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001191
Anders Carlssonb235fc22007-11-22 01:36:19 +00001192 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001194 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 SourceLocation Loc = ConsumeBracket();
1196
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001197 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 Diag(Tok, diag::err_expected_ident);
1199 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001200 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 }
Chris Lattner69efba72007-10-29 04:06:22 +00001202
Anders Carlssonb235fc22007-11-22 01:36:19 +00001203 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001204 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001205
1206 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001208 } else
1209 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001210
Sebastian Redleffa8d12008-12-10 00:02:53 +00001211 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001212 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001213 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001214 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001215 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001216 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001217
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001218 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001219 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001221 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001223
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001225 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001226 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001228 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001230 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001232 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 ConsumeToken();
1234 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001235
1236 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001237}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001238
1239Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1240 SourceLocation L, SourceLocation R) {
1241 // Do not enter a scope for the brace, as the arguments are in the same scope
1242 // (the function body) as the body itself. Instead, just read the statement
1243 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001244 OwningStmtResult FnBody(ParseCompoundStatementBody());
1245
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001246 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001247 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001248 FnBody = Actions.ActOnCompoundStmt(L, R, MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001249
Sebastian Redl798d1192008-12-13 16:23:55 +00001250 return Actions.ActOnFinishFunctionBody(Decl, move_convert(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001251}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001252
1253/// ParseCXXTryBlock - Parse a C++ try-block.
1254///
1255/// try-block:
1256/// 'try' compound-statement handler-seq
1257///
1258/// handler-seq:
1259/// handler handler-seq[opt]
1260///
1261Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1262 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1263
1264 SourceLocation TryLoc = ConsumeToken();
1265 if (Tok.isNot(tok::l_brace))
1266 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1267 OwningStmtResult TryBlock(ParseCompoundStatement());
1268 if (TryBlock.isInvalid())
1269 return move(TryBlock);
1270
1271 StmtVector Handlers(Actions);
1272 if (Tok.isNot(tok::kw_catch))
1273 return StmtError(Diag(Tok, diag::err_expected_catch));
1274 while (Tok.is(tok::kw_catch)) {
1275 OwningStmtResult Handler(ParseCXXCatchBlock());
1276 if (!Handler.isInvalid())
1277 Handlers.push_back(Handler.release());
1278 }
1279 // Don't bother creating the full statement if we don't have any usable
1280 // handlers.
1281 if (Handlers.empty())
1282 return StmtError();
1283
1284 return Actions.ActOnCXXTryBlock(TryLoc, move_convert(TryBlock),
1285 move_convert(Handlers));
1286}
1287
1288/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1289///
1290/// handler:
1291/// 'catch' '(' exception-declaration ')' compound-statement
1292///
1293/// exception-declaration:
1294/// type-specifier-seq declarator
1295/// type-specifier-seq abstract-declarator
1296/// type-specifier-seq
1297/// '...'
1298///
1299Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1300 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1301
1302 SourceLocation CatchLoc = ConsumeToken();
1303
1304 SourceLocation LParenLoc = Tok.getLocation();
1305 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1306 return StmtError();
1307
1308 // C++ 3.3.2p3:
1309 // The name in a catch exception-declaration is local to the handler and
1310 // shall not be redeclared in the outermost block of the handler.
1311 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1312
1313 // exception-declaration is equivalent to '...' or a parameter-declaration
1314 // without default arguments.
1315 DeclTy *ExceptionDecl = 0;
1316 if (Tok.isNot(tok::ellipsis)) {
1317 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001318 if (ParseCXXTypeSpecifierSeq(DS))
1319 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001320 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1321 ParseDeclarator(ExDecl);
1322 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1323 } else
1324 ConsumeToken();
1325
1326 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1327 return StmtError();
1328
1329 if (Tok.isNot(tok::l_brace))
1330 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1331
1332 OwningStmtResult Block(ParseCompoundStatement());
1333 if (Block.isInvalid())
1334 return move(Block);
1335
1336 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl,
1337 move_convert(Block));
1338}