blob: 38a3aaf13c487b21745675153cd5bb4233087cfb [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 Redl76ad2e82009-02-05 15:02:23 +0000121 return Actions.ActOnExprStmt(move(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(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000220 ColonLoc, move(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///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000228Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000229 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattner24e1e702009-03-04 04:23:07 +0000230
231 // It is very very common for code to contain many case statements recursively
232 // nested, as in (but usually without indentation):
233 // case 1:
234 // case 2:
235 // case 3:
236 // case 4:
237 // case 5: etc.
238 //
239 // Parsing this naively works, but is both inefficient and can cause us to run
240 // out of stack space in our recursive descent parser. As a special case,
241 // flatten this recursion into an interative loop. This is complex and gross,
242 // but all the grossness is constrained to ParseCaseStatement (and some
243 // wierdness in the actions), so this is just local grossness :).
244
245 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
246 // example above.
247 OwningStmtResult TopLevelCase(Actions, true);
248
249 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
250 // gets updated each time a new case is parsed, and whose body is unset so
251 // far. When parsing 'case 4', this is the 'case 3' node.
252 StmtTy *DeepestParsedCaseStmt = 0;
253
254 // While we have case statements, eat and stack them.
255 do {
256 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
257
258 OwningExprResult LHS(ParseConstantExpression());
259 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000261 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000263
Chris Lattner24e1e702009-03-04 04:23:07 +0000264 // GNU case range extension.
265 SourceLocation DotDotDotLoc;
266 OwningExprResult RHS(Actions);
267 if (Tok.is(tok::ellipsis)) {
268 Diag(Tok, diag::ext_gnu_case_range);
269 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000270
Chris Lattner24e1e702009-03-04 04:23:07 +0000271 RHS = ParseConstantExpression();
272 if (RHS.isInvalid()) {
273 SkipUntil(tok::colon);
274 return StmtError();
275 }
276 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000277
Chris Lattner24e1e702009-03-04 04:23:07 +0000278 if (Tok.isNot(tok::colon)) {
279 Diag(Tok, diag::err_expected_colon_after) << "'case'";
280 SkipUntil(tok::colon);
281 return StmtError();
282 }
283
284 SourceLocation ColonLoc = ConsumeToken();
285
286 OwningStmtResult Case =
287 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
288 move(RHS), ColonLoc);
289
290 // If we had a sema error parsing this case, then just ignore it and
291 // continue parsing the sub-stmt.
292 if (Case.isInvalid()) {
293 if (TopLevelCase.isInvalid()) // No parsed case stmts.
294 return ParseStatement();
295 // Otherwise, just don't add it as a nested case.
296 } else {
297 // If this is the first case statement we parsed, it becomes TopLevelCase.
298 // Otherwise we link it into the current chain.
299 StmtTy *NextDeepest = Case.get();
300 if (TopLevelCase.isInvalid())
301 TopLevelCase = move(Case);
302 else
303 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
304 DeepestParsedCaseStmt = NextDeepest;
305 }
306
307 // Handle all case statements.
308 } while (Tok.is(tok::kw_case));
309
310 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
311
312 // If we found a non-case statement, start by parsing it.
313 OwningStmtResult SubStmt(Actions);
314
315 if (Tok.isNot(tok::r_brace)) {
316 SubStmt = ParseStatement();
317 } else {
318 // Nicely diagnose the common error "switch (X) { case 4: }", which is
319 // not valid.
320 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000322 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 }
Chris Lattner24e1e702009-03-04 04:23:07 +0000324
325 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000326 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000327 SubStmt = Actions.ActOnNullStmt(SourceLocation());
328
329 // Install the body into the most deeply-nested case.
330 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000331
Chris Lattner24e1e702009-03-04 04:23:07 +0000332 // Return the top level parsed statement tree.
333 return OwningStmtResult(Actions, TopLevelCase.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000334}
335
336/// ParseDefaultStatement
337/// labeled-statement:
338/// 'default' ':' statement
339/// Note that this does not parse the 'statement' at the end.
340///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000341Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000342 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
344
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000345 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000346 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000348 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000350
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000354 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000356 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 }
358
Sebastian Redl61364dd2008-12-11 19:30:53 +0000359 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000360 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000361 return StmtError();
362
Sebastian Redl117054a2008-12-28 16:13:43 +0000363 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000364 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365}
366
367
368/// ParseCompoundStatement - Parse a "{}" block.
369///
370/// compound-statement: [C99 6.8.2]
371/// { block-item-list[opt] }
372/// [GNU] { label-declarations block-item-list } [TODO]
373///
374/// block-item-list:
375/// block-item
376/// block-item-list block-item
377///
378/// block-item:
379/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000380/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000381/// statement
382/// [OMP] openmp-directive [TODO]
383///
384/// [GNU] label-declarations:
385/// [GNU] label-declaration
386/// [GNU] label-declarations label-declaration
387///
388/// [GNU] label-declaration:
389/// [GNU] '__label__' identifier-list ';'
390///
391/// [OMP] openmp-directive: [TODO]
392/// [OMP] barrier-directive
393/// [OMP] flush-directive
394///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000395Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000396 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000397
Chris Lattner31e05722007-08-26 06:24:45 +0000398 // Enter a scope to hold everything within the compound stmt. Compound
399 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000400 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000401
402 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000403 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000404}
405
406
407/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000408/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000409/// consume the '}' at the end of the block. It does not manipulate the scope
410/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000411Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
413
414 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000415 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000416
417 typedef StmtVector StmtsTy;
418 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000419 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000420 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000421 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000422 R = ParseStatementOrDeclaration(false);
423 } else {
424 // __extension__ can start declarations and it can also be a unary
425 // operator for expressions. Consume multiple __extension__ markers here
426 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000427 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000428 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000429 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000430 ConsumeToken();
431
Chris Lattner043a0b52008-03-13 06:32:11 +0000432 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000433 ExtensionRAIIObject O(Diags); // Use RAII to do this.
434
Chris Lattner45a566c2007-08-27 01:01:57 +0000435 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000436 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000437 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000438 SourceLocation DeclStart = Tok.getLocation();
439 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
440 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000441 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000442 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000443 // Otherwise this was a unary __extension__ marker.
444 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000445
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000446 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000447 SkipUntil(tok::semi);
448 continue;
449 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000450
Chris Lattner39146d62008-10-20 06:51:33 +0000451 // Eat the semicolon at the end of stmt and convert the expr into a
452 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000453 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000454 R = Actions.ActOnExprStmt(move(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000455 }
456 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000457
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000458 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000459 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000461
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000463 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000465 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000469 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000470 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000471}
472
Chris Lattner15ff1112008-12-12 06:31:07 +0000473/// ParseParenExprOrCondition:
474/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000475/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000476///
477/// This function parses and performs error recovery on the specified condition
478/// or expression (depending on whether we're in C++ or C mode). This function
479/// goes out of its way to recover well. It returns true if there was a parser
480/// error (the right paren couldn't be found), which indicates that the caller
481/// should try to recover harder. It returns false if the condition is
482/// successfully parsed. Note that a successful parse can still have semantic
483/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000484bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
485 bool OnlyAllowCondition) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000486 SourceLocation LParenLoc = ConsumeParen();
487
488 if (getLang().CPlusPlus)
489 CondExp = ParseCXXCondition();
490 else
491 CondExp = ParseExpression();
492
493 // If the parser was confused by the condition and we don't have a ')', try to
494 // recover by skipping ahead to a semi and bailing out. If condexp is
495 // semantically invalid but we have well formed code, keep going.
496 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
497 SkipUntil(tok::semi);
498 // Skipping may have stopped if it found the containing ')'. If so, we can
499 // continue parsing the if statement.
500 if (Tok.isNot(tok::r_paren))
501 return true;
502 }
503
504 // Otherwise the condition is valid or the rparen is present.
505 MatchRHSPunctuation(tok::r_paren, LParenLoc);
506 return false;
507}
508
509
Reid Spencer5f016e22007-07-11 17:01:13 +0000510/// ParseIfStatement
511/// if-statement: [C99 6.8.4.1]
512/// 'if' '(' expression ')' statement
513/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000514/// [C++] 'if' '(' condition ')' statement
515/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000516///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000517Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000518 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
520
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000521 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000522 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000524 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000526
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000527 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
528
Chris Lattner22153252007-08-26 23:08:06 +0000529 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
530 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000531 //
532 // C++ 6.4p3:
533 // A name introduced by a declaration in a condition is in scope from its
534 // point of declaration until the end of the substatements controlled by the
535 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000536 // C++ 3.3.2p4:
537 // Names declared in the for-init-statement, and in the condition of if,
538 // while, for, and switch statements are local to the if, while, for, or
539 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000540 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000541 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000544 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000545 if (ParseParenExprOrCondition(CondExp))
546 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000547
Chris Lattner0ecea032007-08-22 05:28:50 +0000548 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000549 // there is no compound stmt. C90 does not have this clause. We only do this
550 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000551 //
552 // C++ 6.4p1:
553 // The substatement in a selection-statement (each substatement, in the else
554 // form of the if statement) implicitly defines a local scope.
555 //
556 // For C++ we create a scope for the condition and a new scope for
557 // substatements because:
558 // -When the 'then' scope exits, we want the condition declaration to still be
559 // active for the 'else' scope too.
560 // -Sema will detect name clashes by considering declarations of a
561 // 'ControlScope' as part of its direct subscope.
562 // -If we wanted the condition and substatement to be in the same scope, we
563 // would have to notify ParseStatement not to create a new scope. It's
564 // simpler to let it create a new scope.
565 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000566 ParseScope InnerScope(this, Scope::DeclScope,
567 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000568
Chris Lattnerb96728d2007-10-29 05:08:52 +0000569 // Read the 'then' stmt.
570 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000571 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000572
Chris Lattnera36ce712007-08-22 05:16:28 +0000573 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000574 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000575
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 // If it has an else, parse it.
577 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000578 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000579 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000580
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000581 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000583
Chris Lattner0ecea032007-08-22 05:28:50 +0000584 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000585 // there is no compound stmt. C90 does not have this clause. We only do
586 // this if the body isn't a compound statement to avoid push/pop in common
587 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000588 //
589 // C++ 6.4p1:
590 // The substatement in a selection-statement (each substatement, in the else
591 // form of the if statement) implicitly defines a local scope.
592 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000593 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000594 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000595
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000596 bool WithinElse = CurScope->isWithinElse();
597 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000598 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000600 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000601
602 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000603 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000605
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000606 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000607
608 // If the condition was invalid, discard the if statement. We could recover
609 // better by replacing it with a valid expr, but don't do that yet.
610 if (CondExp.isInvalid())
611 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000612
Chris Lattnerb96728d2007-10-29 05:08:52 +0000613 // If the then or else stmt is invalid and the other is valid (and present),
614 // make turn the invalid one into a null stmt to avoid dropping the other
615 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000616 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
617 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
618 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000619 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000620 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000621 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000622
Chris Lattnerb96728d2007-10-29 05:08:52 +0000623 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000624 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000625 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000626 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000627 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000628
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000629 return Actions.ActOnIfStmt(IfLoc, move(CondExp), move(ThenStmt),
630 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000631}
632
633/// ParseSwitchStatement
634/// switch-statement:
635/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000636/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000637Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000638 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
640
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000641 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000642 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000644 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 }
Chris Lattner22153252007-08-26 23:08:06 +0000646
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000647 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
648
Chris Lattner22153252007-08-26 23:08:06 +0000649 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
650 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000651 //
652 // C++ 6.4p3:
653 // A name introduced by a declaration in a condition is in scope from its
654 // point of declaration until the end of the substatements controlled by the
655 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000656 // C++ 3.3.2p4:
657 // Names declared in the for-init-statement, and in the condition of if,
658 // while, for, and switch statements are local to the if, while, for, or
659 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000660 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000661 unsigned ScopeFlags = Scope::BreakScope;
662 if (C99orCXX)
663 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000664 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000665
666 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000667 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000668 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000669 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000670
671 OwningStmtResult Switch(Actions);
672 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000673 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000674
Chris Lattner0ecea032007-08-22 05:28:50 +0000675 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000676 // there is no compound stmt. C90 does not have this clause. We only do this
677 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000678 //
679 // C++ 6.4p1:
680 // The substatement in a selection-statement (each substatement, in the else
681 // form of the if statement) implicitly defines a local scope.
682 //
683 // See comments in ParseIfStatement for why we create a scope for the
684 // condition and a new scope for substatement in C++.
685 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000686 ParseScope InnerScope(this, Scope::DeclScope,
687 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000690 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000691
Chris Lattner0ecea032007-08-22 05:28:50 +0000692 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000693 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000694
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000695 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000696 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000697 // FIXME: Remove the case statement list from the Switch statement.
698 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000699
700 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000701
Chris Lattner15ff1112008-12-12 06:31:07 +0000702 if (Cond.isInvalid())
703 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000704
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000705 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000706}
707
708/// ParseWhileStatement
709/// while-statement: [C99 6.8.5.1]
710/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000711/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000712Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000713 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 SourceLocation WhileLoc = Tok.getLocation();
715 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000716
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000717 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000718 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000720 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000722
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000723 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
724
Chris Lattner22153252007-08-26 23:08:06 +0000725 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
726 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000727 //
728 // C++ 6.4p3:
729 // A name introduced by a declaration in a condition is in scope from its
730 // point of declaration until the end of the substatements controlled by the
731 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000732 // C++ 3.3.2p4:
733 // Names declared in the for-init-statement, and in the condition of if,
734 // while, for, and switch statements are local to the if, while, for, or
735 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000736 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000737 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000738 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000739 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
740 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000741 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000742 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
743 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000744
745 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000746 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000747 if (ParseParenExprOrCondition(Cond))
748 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000749
Chris Lattner0ecea032007-08-22 05:28:50 +0000750 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000751 // there is no compound stmt. C90 does not have this clause. We only do this
752 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000753 //
754 // C++ 6.5p2:
755 // The substatement in an iteration-statement implicitly defines a local scope
756 // which is entered and exited each time through the loop.
757 //
758 // See comments in ParseIfStatement for why we create a scope for the
759 // condition and a new scope for substatement in C++.
760 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000761 ParseScope InnerScope(this, Scope::DeclScope,
762 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000763
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000765 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000766
Chris Lattner0ecea032007-08-22 05:28:50 +0000767 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000768 InnerScope.Exit();
769 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000770
771 if (Cond.isInvalid() || Body.isInvalid())
772 return StmtError();
773
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000774 return Actions.ActOnWhileStmt(WhileLoc, move(Cond), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000775}
776
777/// ParseDoStatement
778/// do-statement: [C99 6.8.5.2]
779/// 'do' statement 'while' '(' expression ')' ';'
780/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000781Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000782 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000784
Chris Lattner22153252007-08-26 23:08:06 +0000785 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
786 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000787 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000788 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000789 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000790 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000791 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000792
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000793 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000794
Chris Lattner0ecea032007-08-22 05:28:50 +0000795 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000796 // there is no compound stmt. C90 does not have this clause. We only do this
797 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000798 //
799 // C++ 6.5p2:
800 // The substatement in an iteration-statement implicitly defines a local scope
801 // which is entered and exited each time through the loop.
802 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000803 ParseScope InnerScope(this, Scope::DeclScope,
804 (getLang().C99 || getLang().CPlusPlus) &&
805 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000808 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000809
Chris Lattner0ecea032007-08-22 05:28:50 +0000810 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000811 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000812
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000813 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000814 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000815 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000816 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000817 SkipUntil(tok::semi, false, true);
818 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000819 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 }
821 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000822
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000823 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000824 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000825 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000826 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000828
Chris Lattnerff871fb2008-12-12 06:35:28 +0000829 // Parse the parenthesized condition.
830 OwningExprResult Cond(Actions);
831 ParseParenExprOrCondition(Cond, true);
832
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000833 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000834
Sebastian Redl9a920342008-12-11 19:48:14 +0000835 if (Cond.isInvalid() || Body.isInvalid())
836 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000837
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000838 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000839}
840
841/// ParseForStatement
842/// for-statement: [C99 6.8.5.3]
843/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
844/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000845/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
846/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000847/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
848/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000849///
850/// [C++] for-init-statement:
851/// [C++] expression-statement
852/// [C++] simple-declaration
853///
Sebastian Redl9a920342008-12-11 19:48:14 +0000854Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000855 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000857
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000858 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000859 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000861 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000863
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000864 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
865
Chris Lattner22153252007-08-26 23:08:06 +0000866 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
867 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000868 //
869 // C++ 6.4p3:
870 // A name introduced by a declaration in a condition is in scope from its
871 // point of declaration until the end of the substatements controlled by the
872 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000873 // C++ 3.3.2p4:
874 // Names declared in the for-init-statement, and in the condition of if,
875 // while, for, and switch statements are local to the if, while, for, or
876 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000877 // C++ 6.5.3p1:
878 // Names declared in the for-init-statement are in the same declarative-region
879 // as those declared in the condition.
880 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000881 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000882 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000883 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
884 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000885 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000886 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
887
888 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000889
890 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000891 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000892
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000893 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000894 OwningStmtResult FirstPart(Actions);
895 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000898 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 // no first part, eat the ';'.
900 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000901 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000903 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000905
Chris Lattner81c018d2008-03-13 06:29:04 +0000906 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000907 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000908 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000909 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
910 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000911 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000912 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000913 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000914 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 } else {
916 Value = ParseExpression();
917
918 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000919 if (!Value.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000920 FirstPart = Actions.ActOnExprStmt(move(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000921
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000922 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000924 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000925 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000926 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000927 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000928 }
929 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000930 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 SkipUntil(tok::semi);
932 }
933 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000934 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000935 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000936 // Parse the second part of the for specifier.
937 if (Tok.is(tok::semi)) { // for (...;;
938 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000939 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000940 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000941 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000942
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000943 if (Tok.is(tok::semi)) {
944 ConsumeToken();
945 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000946 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000947 SkipUntil(tok::semi);
948 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000949
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000950 // Parse the third part of the for specifier.
951 if (Tok.is(tok::r_paren)) { // for (...;...;)
952 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000953 } else {
Sebastian Redlf05b1522009-01-16 23:28:06 +0000954 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 }
956 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 // Match the ')'.
958 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000959
Chris Lattner0ecea032007-08-22 05:28:50 +0000960 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000961 // there is no compound stmt. C90 does not have this clause. We only do this
962 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000963 //
964 // C++ 6.5p2:
965 // The substatement in an iteration-statement implicitly defines a local scope
966 // which is entered and exited each time through the loop.
967 //
968 // See comments in ParseIfStatement for why we create a scope for
969 // for-init-statement/condition and a new scope for substatement in C++.
970 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000971 ParseScope InnerScope(this, Scope::DeclScope,
972 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000973
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000975 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000976
Chris Lattner0ecea032007-08-22 05:28:50 +0000977 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000978 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000979
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000981 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000982
983 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000984 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000985
986 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000987 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
988 move(SecondPart), move(ThirdPart),
989 RParenLoc, move(Body));
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000990 else
Sebastian Redlf05b1522009-01-16 23:28:06 +0000991 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000992 move(FirstPart),
993 move(SecondPart),
994 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000995}
996
997/// ParseGotoStatement
998/// jump-statement:
999/// 'goto' identifier ';'
1000/// [GNU] 'goto' '*' expression ';'
1001///
1002/// Note: this lets the caller parse the end ';'.
1003///
Sebastian Redl9a920342008-12-11 19:48:14 +00001004Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001005 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001007
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001008 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001009 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001010 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 Tok.getIdentifierInfo());
1012 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001013 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 // GNU indirect goto extension.
1015 Diag(Tok, diag::ext_gnu_indirect_goto);
1016 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001017 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001018 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001020 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001022 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001023 } else {
1024 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001025 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001027
Sebastian Redl9a920342008-12-11 19:48:14 +00001028 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001029}
1030
1031/// ParseContinueStatement
1032/// jump-statement:
1033/// 'continue' ';'
1034///
1035/// Note: this lets the caller parse the end ';'.
1036///
Sebastian Redl9a920342008-12-11 19:48:14 +00001037Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001039 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001040}
1041
1042/// ParseBreakStatement
1043/// jump-statement:
1044/// 'break' ';'
1045///
1046/// Note: this lets the caller parse the end ';'.
1047///
Sebastian Redl9a920342008-12-11 19:48:14 +00001048Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001050 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051}
1052
1053/// ParseReturnStatement
1054/// jump-statement:
1055/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001056Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001057 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001059
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001060 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001061 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001063 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001065 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 }
1067 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001068 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001069}
1070
Steve Naroff5f8aa692008-02-11 23:15:56 +00001071/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1072/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001073Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001074 if (Tok.is(tok::l_brace)) {
1075 unsigned short savedBraceCount = BraceCount;
1076 do {
1077 ConsumeAnyToken();
1078 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1079 } else {
1080 // From the MS website: If used without braces, the __asm keyword means
1081 // that the rest of the line is an assembly-language statement.
1082 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001083 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001084 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001085 do {
1086 ConsumeAnyToken();
1087 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001088 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001089 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1090 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001091 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001092 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001093}
1094
Reid Spencer5f016e22007-07-11 17:01:13 +00001095/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001096/// asm-statement:
1097/// gnu-asm-statement
1098/// ms-asm-statement
1099///
1100/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001101/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1102///
1103/// [GNU] asm-argument:
1104/// asm-string-literal
1105/// asm-string-literal ':' asm-operands[opt]
1106/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1107/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1108/// ':' asm-clobbers
1109///
1110/// [GNU] asm-clobbers:
1111/// asm-string-literal
1112/// asm-clobbers ',' asm-string-literal
1113///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001114/// [MS] ms-asm-statement:
1115/// '__asm' assembly-instruction ';'[opt]
1116/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1117///
1118/// [MS] assembly-instruction-list:
1119/// assembly-instruction ';'[opt]
1120/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1121///
Sebastian Redl9a920342008-12-11 19:48:14 +00001122Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001123 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001124 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001125
Steve Naroff5f8aa692008-02-11 23:15:56 +00001126 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001127 msAsm = true;
1128 return FuzzyParseMicrosoftAsmStatement();
1129 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 DeclSpec DS;
1131 SourceLocation Loc = Tok.getLocation();
1132 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001133
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1135 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001136 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001138 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001139
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001141 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001142 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001143 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001144 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001146 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 }
1148 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001149
Sebastian Redleffa8d12008-12-10 00:02:53 +00001150 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001151 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001152 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001153
Anders Carlssonb235fc22007-11-22 01:36:19 +00001154 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001155 ExprVector Constraints(Actions);
1156 ExprVector Exprs(Actions);
1157 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001158
Anders Carlssondfab34a2008-02-05 23:03:50 +00001159 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001160
Anders Carlssondfab34a2008-02-05 23:03:50 +00001161 SourceLocation RParenLoc;
1162 if (Tok.is(tok::r_paren)) {
1163 // We have a simple asm expression
1164 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001165
Anders Carlssondfab34a2008-02-05 23:03:50 +00001166 RParenLoc = ConsumeParen();
1167 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001168 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001169 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001170 return StmtError();
1171
Anders Carlssondfab34a2008-02-05 23:03:50 +00001172 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001173
Anders Carlssondfab34a2008-02-05 23:03:50 +00001174 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001175 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001176 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001177
Anders Carlssondfab34a2008-02-05 23:03:50 +00001178 assert(Names.size() == Constraints.size() &&
1179 Constraints.size() == Exprs.size()
1180 && "Input operand size mismatch!");
1181
1182 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001183
Anders Carlssondfab34a2008-02-05 23:03:50 +00001184 // Parse the clobbers, if present.
1185 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001186 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001187
Anders Carlssondfab34a2008-02-05 23:03:50 +00001188 // Parse the asm-string list for clobbers.
1189 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001190 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001191
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001192 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001193 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001194
1195 Clobbers.push_back(Clobber.release());
1196
Anders Carlssondfab34a2008-02-05 23:03:50 +00001197 if (Tok.isNot(tok::comma)) break;
1198 ConsumeToken();
1199 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001201
Anders Carlssondfab34a2008-02-05 23:03:50 +00001202 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001204
Sebastian Redl3037ed02009-01-18 16:53:17 +00001205 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1206 NumOutputs, NumInputs, &Names[0],
Sebastian Redlf512e822009-01-18 18:03:53 +00001207 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001208 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001209 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001210}
1211
1212/// ParseAsmOperands - Parse the asm-operands production as used by
1213/// asm-statement. We also parse a leading ':' token. If the leading colon is
1214/// not present, we do not parse anything.
1215///
1216/// [GNU] asm-operands:
1217/// asm-operand
1218/// asm-operands ',' asm-operand
1219///
1220/// [GNU] asm-operand:
1221/// asm-string-literal '(' expression ')'
1222/// '[' identifier ']' asm-string-literal '(' expression ')'
1223///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001224bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001225 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1226 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001228 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 ConsumeToken();
1230
1231 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001232 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001233 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234
Anders Carlssonb235fc22007-11-22 01:36:19 +00001235 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001237 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 SourceLocation Loc = ConsumeBracket();
1239
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001240 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 Diag(Tok, diag::err_expected_ident);
1242 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001243 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 }
Chris Lattner69efba72007-10-29 04:06:22 +00001245
Anders Carlssonb235fc22007-11-22 01:36:19 +00001246 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001247 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001248
1249 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001251 } else
1252 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001253
Sebastian Redleffa8d12008-12-10 00:02:53 +00001254 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001255 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001256 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001257 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001258 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001259 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001260
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001261 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001262 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001264 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001266
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001268 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001269 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001271 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001273 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001275 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 ConsumeToken();
1277 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001278
1279 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001281
1282Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1283 SourceLocation L, SourceLocation R) {
1284 // Do not enter a scope for the brace, as the arguments are in the same scope
1285 // (the function body) as the body itself. Instead, just read the statement
1286 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001287 OwningStmtResult FnBody(ParseCompoundStatementBody());
1288
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001289 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001290 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001291 FnBody = Actions.ActOnCompoundStmt(L, R, MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001292
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001293 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001294}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001295
1296/// ParseCXXTryBlock - Parse a C++ try-block.
1297///
1298/// try-block:
1299/// 'try' compound-statement handler-seq
1300///
1301/// handler-seq:
1302/// handler handler-seq[opt]
1303///
1304Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1305 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1306
1307 SourceLocation TryLoc = ConsumeToken();
1308 if (Tok.isNot(tok::l_brace))
1309 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1310 OwningStmtResult TryBlock(ParseCompoundStatement());
1311 if (TryBlock.isInvalid())
1312 return move(TryBlock);
1313
1314 StmtVector Handlers(Actions);
1315 if (Tok.isNot(tok::kw_catch))
1316 return StmtError(Diag(Tok, diag::err_expected_catch));
1317 while (Tok.is(tok::kw_catch)) {
1318 OwningStmtResult Handler(ParseCXXCatchBlock());
1319 if (!Handler.isInvalid())
1320 Handlers.push_back(Handler.release());
1321 }
1322 // Don't bother creating the full statement if we don't have any usable
1323 // handlers.
1324 if (Handlers.empty())
1325 return StmtError();
1326
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001327 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001328}
1329
1330/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1331///
1332/// handler:
1333/// 'catch' '(' exception-declaration ')' compound-statement
1334///
1335/// exception-declaration:
1336/// type-specifier-seq declarator
1337/// type-specifier-seq abstract-declarator
1338/// type-specifier-seq
1339/// '...'
1340///
1341Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1342 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1343
1344 SourceLocation CatchLoc = ConsumeToken();
1345
1346 SourceLocation LParenLoc = Tok.getLocation();
1347 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1348 return StmtError();
1349
1350 // C++ 3.3.2p3:
1351 // The name in a catch exception-declaration is local to the handler and
1352 // shall not be redeclared in the outermost block of the handler.
1353 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1354
1355 // exception-declaration is equivalent to '...' or a parameter-declaration
1356 // without default arguments.
1357 DeclTy *ExceptionDecl = 0;
1358 if (Tok.isNot(tok::ellipsis)) {
1359 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001360 if (ParseCXXTypeSpecifierSeq(DS))
1361 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001362 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1363 ParseDeclarator(ExDecl);
1364 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1365 } else
1366 ConsumeToken();
1367
1368 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1369 return StmtError();
1370
1371 if (Tok.isNot(tok::l_brace))
1372 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1373
1374 OwningStmtResult Block(ParseCompoundStatement());
1375 if (Block.isInvalid())
1376 return move(Block);
1377
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001378 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001379}