blob: b1a32decffcf6e0d056fb8f5302ce2f8331d040c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner658e6872008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000017#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Parse/DeclSpec.h"
19#include "clang/Parse/Scope.h"
Chris Lattnerc24b8892009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argiris Kirtzidis7fce94d2008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redlb12ac332008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanian37c9c612007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Chris Lattner4b009652007-07-25 00:24:17 +000046/// [GNU] asm-statement
47/// [OMP] openmp-construct [TODO]
48///
49/// labeled-statement:
50/// identifier ':' statement
51/// 'case' constant-expression ':' statement
52/// 'default' ':' statement
53///
54/// selection-statement:
55/// if-statement
56/// switch-statement
57///
58/// iteration-statement:
59/// while-statement
60/// do-statement
61/// for-statement
62///
63/// expression-statement:
64/// expression[opt] ';'
65///
66/// jump-statement:
67/// 'goto' identifier ';'
68/// 'continue' ';'
69/// 'break' ';'
70/// 'return' expression[opt] ';'
71/// [GNU] 'goto' '*' expression ';'
72///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
75/// [OBC] '@' 'throw' ';'
Chris Lattner4b009652007-07-25 00:24:17 +000076///
Sebastian Redl10c32952008-12-11 19:30:53 +000077Parser::OwningStmtResult
78Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner4b009652007-07-25 00:24:17 +000079 const char *SemiError = 0;
Sebastian Redl62261042008-12-09 20:22:58 +000080 OwningStmtResult Res(Actions);
Sebastian Redlbb4dae72008-12-09 13:15:23 +000081
Chris Lattner4b009652007-07-25 00:24:17 +000082 // Cases in this switch statement should fall through if the parser expects
83 // the token to end in a semicolon (in which case SemiError should be set),
84 // or they directly 'return;' if not.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000085 tok::TokenKind Kind = Tok.getKind();
86 SourceLocation AtLoc;
87 switch (Kind) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000088 case tok::at: // May be a @try or @throw statement
89 {
90 AtLoc = ConsumeToken(); // consume @
Sebastian Redl15bf1452008-12-11 20:12:42 +000091 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000092 }
Fariborz Jahanian64b864e2007-09-19 19:14:32 +000093
Argiris Kirtzidis824a3712008-07-12 21:04:42 +000094 case tok::identifier:
95 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
96 // identifier ':' statement
97 return ParseLabeledStatement();
98 }
99 // PASS THROUGH.
100
Chris Lattner053dd2d2009-03-24 17:04:48 +0000101 default: {
Argiris Kirtzidis74b477c2008-10-05 00:06:24 +0000102 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000103 SourceLocation DeclStart = Tok.getLocation();
Chris Lattnera17991f2009-03-29 16:50:03 +0000104 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext);
Chris Lattnera4ff4272008-03-13 06:29:04 +0000105 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl76b9ddb2008-12-21 12:04:03 +0000106 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart);
Chris Lattner053dd2d2009-03-24 17:04:48 +0000107 }
108
109 if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000110 Diag(Tok, diag::err_expected_statement);
Sebastian Redl10c32952008-12-11 19:30:53 +0000111 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000112 }
Chris Lattner053dd2d2009-03-24 17:04:48 +0000113
114 // expression[opt] ';'
115 OwningExprResult Expr(ParseExpression());
116 if (Expr.isInvalid()) {
117 // If the expression is invalid, skip ahead to the next semicolon. Not
118 // doing this opens us up to the possibility of infinite loops if
119 // ParseExpression does not consume any tokens.
120 SkipUntil(tok::semi);
121 return StmtError();
122 }
123 // Otherwise, eat the semicolon.
124 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
125 return Actions.ActOnExprStmt(move(Expr));
126 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000127
Chris Lattner4b009652007-07-25 00:24:17 +0000128 case tok::kw_case: // C99 6.8.1: labeled-statement
129 return ParseCaseStatement();
130 case tok::kw_default: // C99 6.8.1: labeled-statement
131 return ParseDefaultStatement();
Sebastian Redl10c32952008-12-11 19:30:53 +0000132
Chris Lattner4b009652007-07-25 00:24:17 +0000133 case tok::l_brace: // C99 6.8.2: compound-statement
134 return ParseCompoundStatement();
135 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl76b9ddb2008-12-21 12:04:03 +0000136 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl10c32952008-12-11 19:30:53 +0000137
Chris Lattner4b009652007-07-25 00:24:17 +0000138 case tok::kw_if: // C99 6.8.4.1: if-statement
139 return ParseIfStatement();
140 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redla88da592008-12-11 19:48:14 +0000141 return ParseSwitchStatement();
Sebastian Redl10c32952008-12-11 19:30:53 +0000142
Chris Lattner4b009652007-07-25 00:24:17 +0000143 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redla88da592008-12-11 19:48:14 +0000144 return ParseWhileStatement();
Chris Lattner4b009652007-07-25 00:24:17 +0000145 case tok::kw_do: // C99 6.8.5.2: do-statement
146 Res = ParseDoStatement();
147 SemiError = "do/while loop";
148 break;
149 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redla88da592008-12-11 19:48:14 +0000150 return ParseForStatement();
Chris Lattner4b009652007-07-25 00:24:17 +0000151
152 case tok::kw_goto: // C99 6.8.6.1: goto-statement
153 Res = ParseGotoStatement();
154 SemiError = "goto statement";
155 break;
156 case tok::kw_continue: // C99 6.8.6.2: continue-statement
157 Res = ParseContinueStatement();
158 SemiError = "continue statement";
159 break;
160 case tok::kw_break: // C99 6.8.6.3: break-statement
161 Res = ParseBreakStatement();
162 SemiError = "break statement";
163 break;
164 case tok::kw_return: // C99 6.8.6.4: return-statement
165 Res = ParseReturnStatement();
166 SemiError = "return statement";
167 break;
Sebastian Redl10c32952008-12-11 19:30:53 +0000168
Sebastian Redlb12ac332008-12-21 16:41:36 +0000169 case tok::kw_asm: {
Steve Naroff73a07032008-02-07 03:50:06 +0000170 bool msAsm = false;
171 Res = ParseAsmStatement(msAsm);
Sebastian Redl10c32952008-12-11 19:30:53 +0000172 if (msAsm) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000173 SemiError = "asm statement";
174 break;
175 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000176
Sebastian Redlb12ac332008-12-21 16:41:36 +0000177 case tok::kw_try: // C++ 15: try-block
178 return ParseCXXTryBlock();
179 }
180
Chris Lattner4b009652007-07-25 00:24:17 +0000181 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000182 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000183 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000184 } else if (!Res.isInvalid()) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000185 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattnerae6a2c52008-11-13 18:52:53 +0000186 // Skip until we see a } or ;, but don't eat it.
187 SkipUntil(tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000188 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000189 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000190}
191
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000192/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner4b009652007-07-25 00:24:17 +0000193///
194/// labeled-statement:
195/// identifier ':' statement
196/// [GNU] identifier ':' attributes[opt] statement
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000197///
Sebastian Redl10c32952008-12-11 19:30:53 +0000198Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000199 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
200 "Not an identifier!");
201
202 Token IdentTok = Tok; // Save the whole token.
203 ConsumeToken(); // eat the identifier.
204
205 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl10c32952008-12-11 19:30:53 +0000206
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000207 // identifier ':' statement
208 SourceLocation ColonLoc = ConsumeToken();
209
210 // Read label attributes, if present.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000211 Action::AttrTy *AttrList = 0;
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000212 if (Tok.is(tok::kw___attribute))
213 // TODO: save these somewhere.
214 AttrList = ParseAttributes();
215
Sebastian Redl10c32952008-12-11 19:30:53 +0000216 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000217
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000218 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000219 if (SubStmt.isInvalid())
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000220 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000221
Sebastian Redl2437ec62009-01-11 00:38:46 +0000222 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
223 IdentTok.getIdentifierInfo(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000224 ColonLoc, move(SubStmt));
Argiris Kirtzidis680e1d92008-07-09 22:53:07 +0000225}
Chris Lattner4b009652007-07-25 00:24:17 +0000226
227/// ParseCaseStatement
228/// labeled-statement:
229/// 'case' constant-expression ':' statement
230/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
231///
Sebastian Redl10c32952008-12-11 19:30:53 +0000232Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000233 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattnerdaac6942009-03-04 04:23:07 +0000234
235 // It is very very common for code to contain many case statements recursively
236 // nested, as in (but usually without indentation):
237 // case 1:
238 // case 2:
239 // case 3:
240 // case 4:
241 // case 5: etc.
242 //
243 // Parsing this naively works, but is both inefficient and can cause us to run
244 // out of stack space in our recursive descent parser. As a special case,
Chris Lattnerff7ff6c2009-03-04 18:24:58 +0000245 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattnerdaac6942009-03-04 04:23:07 +0000246 // but all the grossness is constrained to ParseCaseStatement (and some
247 // wierdness in the actions), so this is just local grossness :).
248
249 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
250 // example above.
251 OwningStmtResult TopLevelCase(Actions, true);
252
253 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
254 // gets updated each time a new case is parsed, and whose body is unset so
255 // far. When parsing 'case 4', this is the 'case 3' node.
256 StmtTy *DeepestParsedCaseStmt = 0;
257
258 // While we have case statements, eat and stack them.
259 do {
260 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
261
262 OwningExprResult LHS(ParseConstantExpression());
263 if (LHS.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000264 SkipUntil(tok::colon);
Sebastian Redl10c32952008-12-11 19:30:53 +0000265 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000266 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000267
Chris Lattnerdaac6942009-03-04 04:23:07 +0000268 // GNU case range extension.
269 SourceLocation DotDotDotLoc;
270 OwningExprResult RHS(Actions);
271 if (Tok.is(tok::ellipsis)) {
272 Diag(Tok, diag::ext_gnu_case_range);
273 DotDotDotLoc = ConsumeToken();
Sebastian Redl10c32952008-12-11 19:30:53 +0000274
Chris Lattnerdaac6942009-03-04 04:23:07 +0000275 RHS = ParseConstantExpression();
276 if (RHS.isInvalid()) {
277 SkipUntil(tok::colon);
278 return StmtError();
279 }
280 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000281
Chris Lattnerdaac6942009-03-04 04:23:07 +0000282 if (Tok.isNot(tok::colon)) {
283 Diag(Tok, diag::err_expected_colon_after) << "'case'";
284 SkipUntil(tok::colon);
285 return StmtError();
286 }
287
288 SourceLocation ColonLoc = ConsumeToken();
289
290 OwningStmtResult Case =
291 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
292 move(RHS), ColonLoc);
293
294 // If we had a sema error parsing this case, then just ignore it and
295 // continue parsing the sub-stmt.
296 if (Case.isInvalid()) {
297 if (TopLevelCase.isInvalid()) // No parsed case stmts.
298 return ParseStatement();
299 // Otherwise, just don't add it as a nested case.
300 } else {
301 // If this is the first case statement we parsed, it becomes TopLevelCase.
302 // Otherwise we link it into the current chain.
303 StmtTy *NextDeepest = Case.get();
304 if (TopLevelCase.isInvalid())
305 TopLevelCase = move(Case);
306 else
307 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
308 DeepestParsedCaseStmt = NextDeepest;
309 }
310
311 // Handle all case statements.
312 } while (Tok.is(tok::kw_case));
313
314 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
315
316 // If we found a non-case statement, start by parsing it.
317 OwningStmtResult SubStmt(Actions);
318
319 if (Tok.isNot(tok::r_brace)) {
320 SubStmt = ParseStatement();
321 } else {
322 // Nicely diagnose the common error "switch (X) { case 4: }", which is
323 // not valid.
324 // FIXME: add insertion hint.
Chris Lattner4b009652007-07-25 00:24:17 +0000325 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattnerdaac6942009-03-04 04:23:07 +0000326 SubStmt = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000327 }
Chris Lattnerdaac6942009-03-04 04:23:07 +0000328
329 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000330 if (SubStmt.isInvalid())
Chris Lattnerdaac6942009-03-04 04:23:07 +0000331 SubStmt = Actions.ActOnNullStmt(SourceLocation());
332
333 // Install the body into the most deeply-nested case.
334 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl10c32952008-12-11 19:30:53 +0000335
Chris Lattnerdaac6942009-03-04 04:23:07 +0000336 // Return the top level parsed statement tree.
Chris Lattnerff7ff6c2009-03-04 18:24:58 +0000337 return move(TopLevelCase);
Chris Lattner4b009652007-07-25 00:24:17 +0000338}
339
340/// ParseDefaultStatement
341/// labeled-statement:
342/// 'default' ':' statement
343/// Note that this does not parse the 'statement' at the end.
344///
Sebastian Redl10c32952008-12-11 19:30:53 +0000345Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000346 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000347 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
348
Chris Lattner4d7d2342007-10-09 17:41:39 +0000349 if (Tok.isNot(tok::colon)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000350 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattner4b009652007-07-25 00:24:17 +0000351 SkipUntil(tok::colon);
Sebastian Redl10c32952008-12-11 19:30:53 +0000352 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000353 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000354
Chris Lattner4b009652007-07-25 00:24:17 +0000355 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl10c32952008-12-11 19:30:53 +0000356
Chris Lattner4b009652007-07-25 00:24:17 +0000357 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000358 if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000359 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl10c32952008-12-11 19:30:53 +0000360 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000361 }
362
Sebastian Redl10c32952008-12-11 19:30:53 +0000363 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000364 if (SubStmt.isInvalid())
Sebastian Redl10c32952008-12-11 19:30:53 +0000365 return StmtError();
366
Sebastian Redl0a23e8f2008-12-28 16:13:43 +0000367 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +0000368 move(SubStmt), CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000369}
370
371
372/// ParseCompoundStatement - Parse a "{}" block.
373///
374/// compound-statement: [C99 6.8.2]
375/// { block-item-list[opt] }
376/// [GNU] { label-declarations block-item-list } [TODO]
377///
378/// block-item-list:
379/// block-item
380/// block-item-list block-item
381///
382/// block-item:
383/// declaration
Chris Lattner81417722007-08-27 01:01:57 +0000384/// [GNU] '__extension__' declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000385/// statement
386/// [OMP] openmp-directive [TODO]
387///
388/// [GNU] label-declarations:
389/// [GNU] label-declaration
390/// [GNU] label-declarations label-declaration
391///
392/// [GNU] label-declaration:
393/// [GNU] '__label__' identifier-list ';'
394///
395/// [OMP] openmp-directive: [TODO]
396/// [OMP] barrier-directive
397/// [OMP] flush-directive
398///
Sebastian Redl10c32952008-12-11 19:30:53 +0000399Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000400 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl10c32952008-12-11 19:30:53 +0000401
Chris Lattnera7549902007-08-26 06:24:45 +0000402 // Enter a scope to hold everything within the compound stmt. Compound
403 // statements can always hold declarations.
Douglas Gregor95d40792008-12-10 06:34:36 +0000404 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000405
406 // Parse the statements in the body.
Sebastian Redl10c32952008-12-11 19:30:53 +0000407 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000408}
409
410
411/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000412/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattner4b009652007-07-25 00:24:17 +0000413/// consume the '}' at the end of the block. It does not manipulate the scope
414/// stack.
Sebastian Redl10c32952008-12-11 19:30:53 +0000415Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerc24b8892009-03-05 00:00:31 +0000416 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
417 Tok.getLocation(),
418 "in compound statement ('{}')");
419
Chris Lattner4b009652007-07-25 00:24:17 +0000420 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
421
422 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner81417722007-08-27 01:01:57 +0000423 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000424
425 typedef StmtVector StmtsTy;
426 StmtsTy Stmts(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000427 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl62261042008-12-09 20:22:58 +0000428 OwningStmtResult R(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000429 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner81417722007-08-27 01:01:57 +0000430 R = ParseStatementOrDeclaration(false);
431 } else {
432 // __extension__ can start declarations and it can also be a unary
433 // operator for expressions. Consume multiple __extension__ markers here
434 // until we can determine which is which.
Eli Friedmanc4772072009-01-27 08:43:38 +0000435 // FIXME: This loses extension expressions in the AST!
Chris Lattner81417722007-08-27 01:01:57 +0000436 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000437 while (Tok.is(tok::kw___extension__))
Chris Lattner81417722007-08-27 01:01:57 +0000438 ConsumeToken();
439
Chris Lattner1ede3302008-03-13 06:32:11 +0000440 // __extension__ silences extension warnings in the subexpression.
Chris Lattner658e6872008-10-20 06:51:33 +0000441 ExtensionRAIIObject O(Diags); // Use RAII to do this.
442
Chris Lattner81417722007-08-27 01:01:57 +0000443 // If this is the start of a declaration, parse it as such.
Argiris Kirtzidis74b477c2008-10-05 00:06:24 +0000444 if (isDeclarationStatement()) {
Chris Lattner81417722007-08-27 01:01:57 +0000445 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattnera4ff4272008-03-13 06:29:04 +0000446 SourceLocation DeclStart = Tok.getLocation();
Chris Lattnera17991f2009-03-29 16:50:03 +0000447 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext);
Chris Lattnera4ff4272008-03-13 06:29:04 +0000448 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerdaf1c312008-03-13 06:29:54 +0000449 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner81417722007-08-27 01:01:57 +0000450 } else {
Eli Friedmanc4772072009-01-27 08:43:38 +0000451 // Otherwise this was a unary __extension__ marker.
452 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner1ede3302008-03-13 06:32:11 +0000453
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000454 if (Res.isInvalid()) {
Chris Lattner81417722007-08-27 01:01:57 +0000455 SkipUntil(tok::semi);
456 continue;
457 }
Sebastian Redl6619aa52009-01-18 18:03:53 +0000458
Chris Lattner658e6872008-10-20 06:51:33 +0000459 // Eat the semicolon at the end of stmt and convert the expr into a
460 // statement.
Chris Lattner81417722007-08-27 01:01:57 +0000461 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl81db6682009-02-05 15:02:23 +0000462 R = Actions.ActOnExprStmt(move(Res));
Chris Lattner81417722007-08-27 01:01:57 +0000463 }
464 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000465
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000466 if (R.isUsable())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000467 Stmts.push_back(R.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000468 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000469
Chris Lattner4b009652007-07-25 00:24:17 +0000470 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000471 if (Tok.isNot(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000472 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl10c32952008-12-11 19:30:53 +0000473 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000474 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000475
Chris Lattner4b009652007-07-25 00:24:17 +0000476 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redl6619aa52009-01-18 18:03:53 +0000477 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl76b9ddb2008-12-21 12:04:03 +0000478 isStmtExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000479}
480
Chris Lattnerf9593972008-12-12 06:31:07 +0000481/// ParseParenExprOrCondition:
482/// [C ] '(' expression ')'
Chris Lattnera82444d2008-12-12 06:35:28 +0000483/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerf9593972008-12-12 06:31:07 +0000484///
485/// This function parses and performs error recovery on the specified condition
486/// or expression (depending on whether we're in C++ or C mode). This function
487/// goes out of its way to recover well. It returns true if there was a parser
488/// error (the right paren couldn't be found), which indicates that the caller
489/// should try to recover harder. It returns false if the condition is
490/// successfully parsed. Note that a successful parse can still have semantic
491/// errors in the condition.
Chris Lattnera82444d2008-12-12 06:35:28 +0000492bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
493 bool OnlyAllowCondition) {
Chris Lattnerf9593972008-12-12 06:31:07 +0000494 SourceLocation LParenLoc = ConsumeParen();
495
496 if (getLang().CPlusPlus)
497 CondExp = ParseCXXCondition();
498 else
499 CondExp = ParseExpression();
500
501 // If the parser was confused by the condition and we don't have a ')', try to
502 // recover by skipping ahead to a semi and bailing out. If condexp is
503 // semantically invalid but we have well formed code, keep going.
504 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
505 SkipUntil(tok::semi);
506 // Skipping may have stopped if it found the containing ')'. If so, we can
507 // continue parsing the if statement.
508 if (Tok.isNot(tok::r_paren))
509 return true;
510 }
511
512 // Otherwise the condition is valid or the rparen is present.
513 MatchRHSPunctuation(tok::r_paren, LParenLoc);
514 return false;
515}
516
517
Chris Lattner4b009652007-07-25 00:24:17 +0000518/// ParseIfStatement
519/// if-statement: [C99 6.8.4.1]
520/// 'if' '(' expression ')' statement
521/// 'if' '(' expression ')' statement 'else' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000522/// [C++] 'if' '(' condition ')' statement
523/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner4b009652007-07-25 00:24:17 +0000524///
Sebastian Redl10c32952008-12-11 19:30:53 +0000525Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000526 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000527 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
528
Chris Lattner4d7d2342007-10-09 17:41:39 +0000529 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000530 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattner4b009652007-07-25 00:24:17 +0000531 SkipUntil(tok::semi);
Sebastian Redl10c32952008-12-11 19:30:53 +0000532 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000533 }
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000534
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000535 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
536
Chris Lattnere0cc5082007-08-26 23:08:06 +0000537 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
538 // the case for C90.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000539 //
540 // C++ 6.4p3:
541 // A name introduced by a declaration in a condition is in scope from its
542 // point of declaration until the end of the substatements controlled by the
543 // condition.
Argiris Kirtzidis16fe6e42008-09-11 23:08:39 +0000544 // C++ 3.3.2p4:
545 // Names declared in the for-init-statement, and in the condition of if,
546 // while, for, and switch statements are local to the if, while, for, or
547 // switch statement (including the controlled statement).
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000548 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000549 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattnere0cc5082007-08-26 23:08:06 +0000550
Chris Lattner4b009652007-07-25 00:24:17 +0000551 // Parse the condition.
Sebastian Redl62261042008-12-09 20:22:58 +0000552 OwningExprResult CondExp(Actions);
Chris Lattnerf9593972008-12-12 06:31:07 +0000553 if (ParseParenExprOrCondition(CondExp))
554 return StmtError();
Chris Lattner3651bf62008-12-12 06:19:11 +0000555
Chris Lattnerf446f722007-08-22 05:28:50 +0000556 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000557 // there is no compound stmt. C90 does not have this clause. We only do this
558 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000559 //
560 // C++ 6.4p1:
561 // The substatement in a selection-statement (each substatement, in the else
562 // form of the if statement) implicitly defines a local scope.
563 //
564 // For C++ we create a scope for the condition and a new scope for
565 // substatements because:
566 // -When the 'then' scope exits, we want the condition declaration to still be
567 // active for the 'else' scope too.
568 // -Sema will detect name clashes by considering declarations of a
569 // 'ControlScope' as part of its direct subscope.
570 // -If we wanted the condition and substatement to be in the same scope, we
571 // would have to notify ParseStatement not to create a new scope. It's
572 // simpler to let it create a new scope.
573 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000574 ParseScope InnerScope(this, Scope::DeclScope,
575 C99orCXX && Tok.isNot(tok::l_brace));
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000576
Chris Lattner84b20712007-10-29 05:08:52 +0000577 // Read the 'then' stmt.
578 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl10c32952008-12-11 19:30:53 +0000579 OwningStmtResult ThenStmt(ParseStatement());
Chris Lattner4b009652007-07-25 00:24:17 +0000580
Chris Lattnerd190ac22007-08-22 05:16:28 +0000581 // Pop the 'if' scope if needed.
Douglas Gregor95d40792008-12-10 06:34:36 +0000582 InnerScope.Exit();
Sebastian Redl10c32952008-12-11 19:30:53 +0000583
Chris Lattner4b009652007-07-25 00:24:17 +0000584 // If it has an else, parse it.
585 SourceLocation ElseLoc;
Chris Lattner84b20712007-10-29 05:08:52 +0000586 SourceLocation ElseStmtLoc;
Sebastian Redl62261042008-12-09 20:22:58 +0000587 OwningStmtResult ElseStmt(Actions);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000588
Chris Lattner4d7d2342007-10-09 17:41:39 +0000589 if (Tok.is(tok::kw_else)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000590 ElseLoc = ConsumeToken();
Sebastian Redl10c32952008-12-11 19:30:53 +0000591
Chris Lattnerf446f722007-08-22 05:28:50 +0000592 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000593 // there is no compound stmt. C90 does not have this clause. We only do
594 // this if the body isn't a compound statement to avoid push/pop in common
595 // cases.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000596 //
597 // C++ 6.4p1:
598 // The substatement in a selection-statement (each substatement, in the else
599 // form of the if statement) implicitly defines a local scope.
600 //
Sebastian Redl10c32952008-12-11 19:30:53 +0000601 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor95d40792008-12-10 06:34:36 +0000602 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000603
Douglas Gregor48840c72008-12-10 23:01:14 +0000604 bool WithinElse = CurScope->isWithinElse();
605 CurScope->setWithinElse(true);
Chris Lattner84b20712007-10-29 05:08:52 +0000606 ElseStmtLoc = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000607 ElseStmt = ParseStatement();
Douglas Gregor48840c72008-12-10 23:01:14 +0000608 CurScope->setWithinElse(WithinElse);
Chris Lattnerd190ac22007-08-22 05:16:28 +0000609
610 // Pop the 'else' scope if needed.
Douglas Gregor95d40792008-12-10 06:34:36 +0000611 InnerScope.Exit();
Chris Lattner4b009652007-07-25 00:24:17 +0000612 }
Sebastian Redl10c32952008-12-11 19:30:53 +0000613
Douglas Gregor95d40792008-12-10 06:34:36 +0000614 IfScope.Exit();
Chris Lattner3651bf62008-12-12 06:19:11 +0000615
616 // If the condition was invalid, discard the if statement. We could recover
617 // better by replacing it with a valid expr, but don't do that yet.
618 if (CondExp.isInvalid())
619 return StmtError();
Chris Lattnere0cc5082007-08-26 23:08:06 +0000620
Chris Lattner84b20712007-10-29 05:08:52 +0000621 // If the then or else stmt is invalid and the other is valid (and present),
622 // make turn the invalid one into a null stmt to avoid dropping the other
623 // part. If both are invalid, return error.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000624 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
625 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
626 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl6008ac32008-11-25 22:21:31 +0000627 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl10c32952008-12-11 19:30:53 +0000628 return StmtError();
Chris Lattner84b20712007-10-29 05:08:52 +0000629 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000630
Chris Lattner84b20712007-10-29 05:08:52 +0000631 // Now if either are invalid, replace with a ';'.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000632 if (ThenStmt.isInvalid())
Chris Lattner84b20712007-10-29 05:08:52 +0000633 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000634 if (ElseStmt.isInvalid())
Chris Lattner84b20712007-10-29 05:08:52 +0000635 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000636
Sebastian Redl81db6682009-02-05 15:02:23 +0000637 return Actions.ActOnIfStmt(IfLoc, move(CondExp), move(ThenStmt),
638 ElseLoc, move(ElseStmt));
Chris Lattner4b009652007-07-25 00:24:17 +0000639}
640
641/// ParseSwitchStatement
642/// switch-statement:
643/// 'switch' '(' expression ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000644/// [C++] 'switch' '(' condition ')' statement
Sebastian Redla88da592008-12-11 19:48:14 +0000645Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000646 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000647 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
648
Chris Lattner4d7d2342007-10-09 17:41:39 +0000649 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000650 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner4b009652007-07-25 00:24:17 +0000651 SkipUntil(tok::semi);
Sebastian Redla88da592008-12-11 19:48:14 +0000652 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000653 }
Chris Lattnere0cc5082007-08-26 23:08:06 +0000654
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000655 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
656
Chris Lattnere0cc5082007-08-26 23:08:06 +0000657 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
658 // not the case for C90. Start the switch scope.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000659 //
660 // C++ 6.4p3:
661 // A name introduced by a declaration in a condition is in scope from its
662 // point of declaration until the end of the substatements controlled by the
663 // condition.
Argiris Kirtzidis16fe6e42008-09-11 23:08:39 +0000664 // C++ 3.3.2p4:
665 // Names declared in the for-init-statement, and in the condition of if,
666 // while, for, and switch statements are local to the if, while, for, or
667 // switch statement (including the controlled statement).
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000668 //
Chris Lattnerf9593972008-12-12 06:31:07 +0000669 unsigned ScopeFlags = Scope::BreakScope;
670 if (C99orCXX)
671 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor95d40792008-12-10 06:34:36 +0000672 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner4b009652007-07-25 00:24:17 +0000673
674 // Parse the condition.
Sebastian Redl62261042008-12-09 20:22:58 +0000675 OwningExprResult Cond(Actions);
Chris Lattnerf9593972008-12-12 06:31:07 +0000676 if (ParseParenExprOrCondition(Cond))
Sebastian Redla88da592008-12-11 19:48:14 +0000677 return StmtError();
Eli Friedmanba932e62008-12-17 22:19:57 +0000678
679 OwningStmtResult Switch(Actions);
680 if (!Cond.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000681 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000682
Chris Lattnerf446f722007-08-22 05:28:50 +0000683 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000684 // there is no compound stmt. C90 does not have this clause. We only do this
685 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000686 //
687 // C++ 6.4p1:
688 // The substatement in a selection-statement (each substatement, in the else
689 // form of the if statement) implicitly defines a local scope.
690 //
691 // See comments in ParseIfStatement for why we create a scope for the
692 // condition and a new scope for substatement in C++.
693 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000694 ParseScope InnerScope(this, Scope::DeclScope,
695 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl10c32952008-12-11 19:30:53 +0000696
Chris Lattner4b009652007-07-25 00:24:17 +0000697 // Read the body statement.
Sebastian Redl10c32952008-12-11 19:30:53 +0000698 OwningStmtResult Body(ParseStatement());
Chris Lattner4b009652007-07-25 00:24:17 +0000699
Chris Lattnerf446f722007-08-22 05:28:50 +0000700 // Pop the body scope if needed.
Douglas Gregor95d40792008-12-10 06:34:36 +0000701 InnerScope.Exit();
Sebastian Redla88da592008-12-11 19:48:14 +0000702
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000703 if (Body.isInvalid()) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000704 Body = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000705 // FIXME: Remove the case statement list from the Switch statement.
706 }
Douglas Gregor95d40792008-12-10 06:34:36 +0000707
708 SwitchScope.Exit();
Sebastian Redl10c32952008-12-11 19:30:53 +0000709
Chris Lattnerf9593972008-12-12 06:31:07 +0000710 if (Cond.isInvalid())
711 return StmtError();
Sebastian Redl2437ec62009-01-11 00:38:46 +0000712
Sebastian Redl81db6682009-02-05 15:02:23 +0000713 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Chris Lattner4b009652007-07-25 00:24:17 +0000714}
715
716/// ParseWhileStatement
717/// while-statement: [C99 6.8.5.1]
718/// 'while' '(' expression ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000719/// [C++] 'while' '(' condition ')' statement
Sebastian Redla88da592008-12-11 19:48:14 +0000720Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000721 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000722 SourceLocation WhileLoc = Tok.getLocation();
723 ConsumeToken(); // eat the 'while'.
Sebastian Redla88da592008-12-11 19:48:14 +0000724
Chris Lattner4d7d2342007-10-09 17:41:39 +0000725 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000726 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner4b009652007-07-25 00:24:17 +0000727 SkipUntil(tok::semi);
Sebastian Redla88da592008-12-11 19:48:14 +0000728 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000729 }
Sebastian Redla88da592008-12-11 19:48:14 +0000730
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000731 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
732
Chris Lattnere0cc5082007-08-26 23:08:06 +0000733 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
734 // the case for C90. Start the loop scope.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000735 //
736 // C++ 6.4p3:
737 // A name introduced by a declaration in a condition is in scope from its
738 // point of declaration until the end of the substatements controlled by the
739 // condition.
Argiris Kirtzidis16fe6e42008-09-11 23:08:39 +0000740 // C++ 3.3.2p4:
741 // Names declared in the for-init-statement, and in the condition of if,
742 // while, for, and switch statements are local to the if, while, for, or
743 // switch statement (including the controlled statement).
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000744 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000745 unsigned ScopeFlags;
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000746 if (C99orCXX)
Douglas Gregor95d40792008-12-10 06:34:36 +0000747 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
748 Scope::DeclScope | Scope::ControlScope;
Chris Lattnere0cc5082007-08-26 23:08:06 +0000749 else
Douglas Gregor95d40792008-12-10 06:34:36 +0000750 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
751 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner4b009652007-07-25 00:24:17 +0000752
753 // Parse the condition.
Sebastian Redl62261042008-12-09 20:22:58 +0000754 OwningExprResult Cond(Actions);
Chris Lattnerf9593972008-12-12 06:31:07 +0000755 if (ParseParenExprOrCondition(Cond))
756 return StmtError();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000757
Chris Lattnerf446f722007-08-22 05:28:50 +0000758 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000759 // there is no compound stmt. C90 does not have this clause. We only do this
760 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000761 //
762 // C++ 6.5p2:
763 // The substatement in an iteration-statement implicitly defines a local scope
764 // which is entered and exited each time through the loop.
765 //
766 // See comments in ParseIfStatement for why we create a scope for the
767 // condition and a new scope for substatement in C++.
768 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000769 ParseScope InnerScope(this, Scope::DeclScope,
770 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redla88da592008-12-11 19:48:14 +0000771
Chris Lattner4b009652007-07-25 00:24:17 +0000772 // Read the body statement.
Sebastian Redl10c32952008-12-11 19:30:53 +0000773 OwningStmtResult Body(ParseStatement());
Chris Lattner4b009652007-07-25 00:24:17 +0000774
Chris Lattnerf446f722007-08-22 05:28:50 +0000775 // Pop the body scope if needed.
Douglas Gregor95d40792008-12-10 06:34:36 +0000776 InnerScope.Exit();
777 WhileScope.Exit();
Sebastian Redla88da592008-12-11 19:48:14 +0000778
779 if (Cond.isInvalid() || Body.isInvalid())
780 return StmtError();
781
Sebastian Redl81db6682009-02-05 15:02:23 +0000782 return Actions.ActOnWhileStmt(WhileLoc, move(Cond), move(Body));
Chris Lattner4b009652007-07-25 00:24:17 +0000783}
784
785/// ParseDoStatement
786/// do-statement: [C99 6.8.5.2]
787/// 'do' statement 'while' '(' expression ')' ';'
788/// Note: this lets the caller parse the end ';'.
Sebastian Redla88da592008-12-11 19:48:14 +0000789Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000790 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000791 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redla88da592008-12-11 19:48:14 +0000792
Chris Lattnere0cc5082007-08-26 23:08:06 +0000793 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
794 // the case for C90. Start the loop scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000795 unsigned ScopeFlags;
Chris Lattnere0cc5082007-08-26 23:08:06 +0000796 if (getLang().C99)
Douglas Gregor95d40792008-12-10 06:34:36 +0000797 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattnere0cc5082007-08-26 23:08:06 +0000798 else
Douglas Gregor95d40792008-12-10 06:34:36 +0000799 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redla88da592008-12-11 19:48:14 +0000800
Douglas Gregor95d40792008-12-10 06:34:36 +0000801 ParseScope DoScope(this, ScopeFlags);
Chris Lattner4b009652007-07-25 00:24:17 +0000802
Chris Lattnerf446f722007-08-22 05:28:50 +0000803 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000804 // there is no compound stmt. C90 does not have this clause. We only do this
805 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidis8d36abb2008-09-11 04:46:46 +0000806 //
807 // C++ 6.5p2:
808 // The substatement in an iteration-statement implicitly defines a local scope
809 // which is entered and exited each time through the loop.
810 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000811 ParseScope InnerScope(this, Scope::DeclScope,
812 (getLang().C99 || getLang().CPlusPlus) &&
813 Tok.isNot(tok::l_brace));
Sebastian Redla88da592008-12-11 19:48:14 +0000814
Chris Lattner4b009652007-07-25 00:24:17 +0000815 // Read the body statement.
Sebastian Redl10c32952008-12-11 19:30:53 +0000816 OwningStmtResult Body(ParseStatement());
Chris Lattner4b009652007-07-25 00:24:17 +0000817
Chris Lattnerf446f722007-08-22 05:28:50 +0000818 // Pop the body scope if needed.
Douglas Gregor95d40792008-12-10 06:34:36 +0000819 InnerScope.Exit();
Chris Lattnerf446f722007-08-22 05:28:50 +0000820
Chris Lattner4d7d2342007-10-09 17:41:39 +0000821 if (Tok.isNot(tok::kw_while)) {
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000822 if (!Body.isInvalid()) {
Chris Lattnerae6a2c52008-11-13 18:52:53 +0000823 Diag(Tok, diag::err_expected_while);
Chris Lattner921342c2008-11-23 23:17:07 +0000824 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattnerae6a2c52008-11-13 18:52:53 +0000825 SkipUntil(tok::semi, false, true);
826 }
Sebastian Redla88da592008-12-11 19:48:14 +0000827 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000828 }
829 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redla88da592008-12-11 19:48:14 +0000830
Chris Lattner4d7d2342007-10-09 17:41:39 +0000831 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000832 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattnerae6a2c52008-11-13 18:52:53 +0000833 SkipUntil(tok::semi, false, true);
Sebastian Redla88da592008-12-11 19:48:14 +0000834 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000835 }
Sebastian Redla88da592008-12-11 19:48:14 +0000836
Chris Lattnera82444d2008-12-12 06:35:28 +0000837 // Parse the parenthesized condition.
838 OwningExprResult Cond(Actions);
839 ParseParenExprOrCondition(Cond, true);
840
Douglas Gregor95d40792008-12-10 06:34:36 +0000841 DoScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000842
Sebastian Redla88da592008-12-11 19:48:14 +0000843 if (Cond.isInvalid() || Body.isInvalid())
844 return StmtError();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000845
Sebastian Redl81db6682009-02-05 15:02:23 +0000846 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond));
Chris Lattner4b009652007-07-25 00:24:17 +0000847}
848
849/// ParseForStatement
850/// for-statement: [C99 6.8.5.3]
851/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
852/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000853/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
854/// [C++] statement
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000855/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
856/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argiris Kirtzidis873f2782008-09-09 20:38:47 +0000857///
858/// [C++] for-init-statement:
859/// [C++] expression-statement
860/// [C++] simple-declaration
861///
Sebastian Redla88da592008-12-11 19:48:14 +0000862Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000863 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +0000864 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redla88da592008-12-11 19:48:14 +0000865
Chris Lattner4d7d2342007-10-09 17:41:39 +0000866 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000867 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner4b009652007-07-25 00:24:17 +0000868 SkipUntil(tok::semi);
Sebastian Redla88da592008-12-11 19:48:14 +0000869 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +0000870 }
Sebastian Redla88da592008-12-11 19:48:14 +0000871
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000872 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
873
Chris Lattnere0cc5082007-08-26 23:08:06 +0000874 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
875 // the case for C90. Start the loop scope.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000876 //
877 // C++ 6.4p3:
878 // A name introduced by a declaration in a condition is in scope from its
879 // point of declaration until the end of the substatements controlled by the
880 // condition.
Argiris Kirtzidis16fe6e42008-09-11 23:08:39 +0000881 // C++ 3.3.2p4:
882 // Names declared in the for-init-statement, and in the condition of if,
883 // while, for, and switch statements are local to the if, while, for, or
884 // switch statement (including the controlled statement).
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000885 // C++ 6.5.3p1:
886 // Names declared in the for-init-statement are in the same declarative-region
887 // as those declared in the condition.
888 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000889 unsigned ScopeFlags;
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000890 if (C99orCXX)
Douglas Gregor95d40792008-12-10 06:34:36 +0000891 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
892 Scope::DeclScope | Scope::ControlScope;
Chris Lattnere0cc5082007-08-26 23:08:06 +0000893 else
Douglas Gregor95d40792008-12-10 06:34:36 +0000894 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
895
896 ParseScope ForScope(this, ScopeFlags);
Chris Lattner4b009652007-07-25 00:24:17 +0000897
898 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +0000899 OwningExprResult Value(Actions);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000900
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000901 bool ForEach = false;
Sebastian Redl19c74d32009-01-16 23:28:06 +0000902 OwningStmtResult FirstPart(Actions);
903 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000904
Chris Lattner4b009652007-07-25 00:24:17 +0000905 // Parse the first part of the for specifier.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000906 if (Tok.is(tok::semi)) { // for (;
Chris Lattner4b009652007-07-25 00:24:17 +0000907 // no first part, eat the ';'.
908 ConsumeToken();
Argiris Kirtzidis58f362d2008-10-05 15:50:46 +0000909 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner4b009652007-07-25 00:24:17 +0000910 // Parse declaration, which eats the ';'.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000911 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Chris Lattner4b009652007-07-25 00:24:17 +0000912 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redla88da592008-12-11 19:48:14 +0000913
Chris Lattnera4ff4272008-03-13 06:29:04 +0000914 SourceLocation DeclStart = Tok.getLocation();
Chris Lattnera17991f2009-03-29 16:50:03 +0000915 DeclGroupPtrTy VarDecls = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattnera4ff4272008-03-13 06:29:04 +0000916 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnera17991f2009-03-29 16:50:03 +0000917 FirstPart = Actions.ActOnDeclStmt(VarDecls, DeclStart, DeclStart);
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000918 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000919 ConsumeToken(); // consume 'in'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000920 SecondPart = ParseExpression();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000921 }
Chris Lattner4b009652007-07-25 00:24:17 +0000922 } else {
923 Value = ParseExpression();
924
925 // Turn the expression into a stmt.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000926 if (!Value.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000927 FirstPart = Actions.ActOnExprStmt(move(Value));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000928
Chris Lattner4d7d2342007-10-09 17:41:39 +0000929 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000930 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000931 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000932 ConsumeToken(); // consume 'in'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000933 SecondPart = ParseExpression();
Chris Lattnera17991f2009-03-29 16:50:03 +0000934 } else {
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000935 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner4b009652007-07-25 00:24:17 +0000936 SkipUntil(tok::semi);
937 }
938 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000939 if (!ForEach) {
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000940 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000941 // Parse the second part of the for specifier.
942 if (Tok.is(tok::semi)) { // for (...;;
943 // no second part.
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000944 } else {
Chris Lattnerf9593972008-12-12 06:31:07 +0000945 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000946 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000947
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000948 if (Tok.is(tok::semi)) {
949 ConsumeToken();
950 } else {
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000951 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000952 SkipUntil(tok::semi);
953 }
Sebastian Redla88da592008-12-11 19:48:14 +0000954
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000955 // Parse the third part of the for specifier.
956 if (Tok.is(tok::r_paren)) { // for (...;...;)
957 // no third part.
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000958 } else {
Sebastian Redl19c74d32009-01-16 23:28:06 +0000959 ThirdPart = ParseExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000960 }
961 }
Chris Lattner4b009652007-07-25 00:24:17 +0000962 // Match the ')'.
963 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000964
Chris Lattnerf446f722007-08-22 05:28:50 +0000965 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner59ed6e22007-08-22 05:33:11 +0000966 // there is no compound stmt. C90 does not have this clause. We only do this
967 // if the body isn't a compound statement to avoid push/pop in common cases.
Argiris Kirtzidisfd8d65d2008-09-11 03:06:46 +0000968 //
969 // C++ 6.5p2:
970 // The substatement in an iteration-statement implicitly defines a local scope
971 // which is entered and exited each time through the loop.
972 //
973 // See comments in ParseIfStatement for why we create a scope for
974 // for-init-statement/condition and a new scope for substatement in C++.
975 //
Douglas Gregor95d40792008-12-10 06:34:36 +0000976 ParseScope InnerScope(this, Scope::DeclScope,
977 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000978
Chris Lattner4b009652007-07-25 00:24:17 +0000979 // Read the body statement.
Sebastian Redl10c32952008-12-11 19:30:53 +0000980 OwningStmtResult Body(ParseStatement());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000981
Chris Lattnerf446f722007-08-22 05:28:50 +0000982 // Pop the body scope if needed.
Douglas Gregor95d40792008-12-10 06:34:36 +0000983 InnerScope.Exit();
Chris Lattnerf446f722007-08-22 05:28:50 +0000984
Chris Lattner4b009652007-07-25 00:24:17 +0000985 // Leave the for-scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000986 ForScope.Exit();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000987
988 if (Body.isInvalid())
Sebastian Redla88da592008-12-11 19:48:14 +0000989 return StmtError();
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000990
991 if (!ForEach)
Sebastian Redl81db6682009-02-05 15:02:23 +0000992 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
993 move(SecondPart), move(ThirdPart),
994 RParenLoc, move(Body));
Chris Lattnera17991f2009-03-29 16:50:03 +0000995
996 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
997 move(FirstPart),
998 move(SecondPart),
999 RParenLoc, move(Body));
Chris Lattner4b009652007-07-25 00:24:17 +00001000}
1001
1002/// ParseGotoStatement
1003/// jump-statement:
1004/// 'goto' identifier ';'
1005/// [GNU] 'goto' '*' expression ';'
1006///
1007/// Note: this lets the caller parse the end ';'.
1008///
Sebastian Redla88da592008-12-11 19:48:14 +00001009Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001010 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +00001011 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redla88da592008-12-11 19:48:14 +00001012
Sebastian Redl62261042008-12-09 20:22:58 +00001013 OwningStmtResult Res(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +00001014 if (Tok.is(tok::identifier)) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001015 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +00001016 Tok.getIdentifierInfo());
1017 ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +00001018 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner4b009652007-07-25 00:24:17 +00001019 // GNU indirect goto extension.
1020 Diag(Tok, diag::ext_gnu_indirect_goto);
1021 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001022 OwningExprResult R(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001023 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattner4b009652007-07-25 00:24:17 +00001024 SkipUntil(tok::semi, false, true);
Sebastian Redla88da592008-12-11 19:48:14 +00001025 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +00001026 }
Sebastian Redl81db6682009-02-05 15:02:23 +00001027 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner4b009652007-07-25 00:24:17 +00001028 } else {
1029 Diag(Tok, diag::err_expected_ident);
Sebastian Redla88da592008-12-11 19:48:14 +00001030 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +00001031 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001032
Sebastian Redla88da592008-12-11 19:48:14 +00001033 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +00001034}
1035
1036/// ParseContinueStatement
1037/// jump-statement:
1038/// 'continue' ';'
1039///
1040/// Note: this lets the caller parse the end ';'.
1041///
Sebastian Redla88da592008-12-11 19:48:14 +00001042Parser::OwningStmtResult Parser::ParseContinueStatement() {
Chris Lattner4b009652007-07-25 00:24:17 +00001043 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl539eb572009-01-18 13:19:59 +00001044 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001045}
1046
1047/// ParseBreakStatement
1048/// jump-statement:
1049/// 'break' ';'
1050///
1051/// Note: this lets the caller parse the end ';'.
1052///
Sebastian Redla88da592008-12-11 19:48:14 +00001053Parser::OwningStmtResult Parser::ParseBreakStatement() {
Chris Lattner4b009652007-07-25 00:24:17 +00001054 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl539eb572009-01-18 13:19:59 +00001055 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001056}
1057
1058/// ParseReturnStatement
1059/// jump-statement:
1060/// 'return' expression[opt] ';'
Sebastian Redla88da592008-12-11 19:48:14 +00001061Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001062 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattner4b009652007-07-25 00:24:17 +00001063 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redla88da592008-12-11 19:48:14 +00001064
Sebastian Redl62261042008-12-09 20:22:58 +00001065 OwningExprResult R(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +00001066 if (Tok.isNot(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001067 R = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001068 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattner4b009652007-07-25 00:24:17 +00001069 SkipUntil(tok::semi, false, true);
Sebastian Redla88da592008-12-11 19:48:14 +00001070 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +00001071 }
1072 }
Sebastian Redl81db6682009-02-05 15:02:23 +00001073 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Chris Lattner4b009652007-07-25 00:24:17 +00001074}
1075
Steve Naroff6f9f9552008-02-11 23:15:56 +00001076/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1077/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redla88da592008-12-11 19:48:14 +00001078Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffbe880ec2008-02-07 23:24:32 +00001079 if (Tok.is(tok::l_brace)) {
1080 unsigned short savedBraceCount = BraceCount;
1081 do {
1082 ConsumeAnyToken();
1083 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1084 } else {
1085 // From the MS website: If used without braces, the __asm keyword means
1086 // that the rest of the line is an assembly-language statement.
1087 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffab3dfe02008-02-08 03:36:19 +00001088 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner18c8dc02009-01-16 07:36:28 +00001089 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8ce442a2008-02-08 18:01:27 +00001090 do {
1091 ConsumeAnyToken();
1092 TokLoc = Tok.getLocation();
Chris Lattner18c8dc02009-01-16 07:36:28 +00001093 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff8ce442a2008-02-08 18:01:27 +00001094 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1095 Tok.isNot(tok::eof));
Steve Naroffbe880ec2008-02-07 23:24:32 +00001096 }
Sebastian Redl76b9ddb2008-12-21 12:04:03 +00001097 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroff73a07032008-02-07 03:50:06 +00001098}
1099
Chris Lattner4b009652007-07-25 00:24:17 +00001100/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff6f9f9552008-02-11 23:15:56 +00001101/// asm-statement:
1102/// gnu-asm-statement
1103/// ms-asm-statement
1104///
1105/// [GNU] gnu-asm-statement:
Chris Lattner4b009652007-07-25 00:24:17 +00001106/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1107///
1108/// [GNU] asm-argument:
1109/// asm-string-literal
1110/// asm-string-literal ':' asm-operands[opt]
1111/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1112/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1113/// ':' asm-clobbers
1114///
1115/// [GNU] asm-clobbers:
1116/// asm-string-literal
1117/// asm-clobbers ',' asm-string-literal
1118///
Steve Naroff6f9f9552008-02-11 23:15:56 +00001119/// [MS] ms-asm-statement:
1120/// '__asm' assembly-instruction ';'[opt]
1121/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1122///
1123/// [MS] assembly-instruction-list:
1124/// assembly-instruction ';'[opt]
1125/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1126///
Sebastian Redla88da592008-12-11 19:48:14 +00001127Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001128 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner8a40a832007-10-29 04:04:16 +00001129 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redla88da592008-12-11 19:48:14 +00001130
Steve Naroff6f9f9552008-02-11 23:15:56 +00001131 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroff73a07032008-02-07 03:50:06 +00001132 msAsm = true;
1133 return FuzzyParseMicrosoftAsmStatement();
1134 }
Chris Lattner4b009652007-07-25 00:24:17 +00001135 DeclSpec DS;
1136 SourceLocation Loc = Tok.getLocation();
1137 ParseTypeQualifierListOpt(DS);
Sebastian Redla88da592008-12-11 19:48:14 +00001138
Chris Lattner4b009652007-07-25 00:24:17 +00001139 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1140 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattnerf006a222008-11-18 07:48:38 +00001141 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001142 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattnerf006a222008-11-18 07:48:38 +00001143 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redla88da592008-12-11 19:48:14 +00001144
Chris Lattner4b009652007-07-25 00:24:17 +00001145 // Remember if this was a volatile asm.
Anders Carlsson759f45d2007-11-23 23:12:25 +00001146 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001147 bool isSimple = false;
Chris Lattner4d7d2342007-10-09 17:41:39 +00001148 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001149 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner4b009652007-07-25 00:24:17 +00001150 SkipUntil(tok::r_paren);
Sebastian Redla88da592008-12-11 19:48:14 +00001151 return StmtError();
Chris Lattner4b009652007-07-25 00:24:17 +00001152 }
1153 Loc = ConsumeParen();
Sebastian Redla88da592008-12-11 19:48:14 +00001154
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001155 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001156 if (AsmString.isInvalid())
Sebastian Redla88da592008-12-11 19:48:14 +00001157 return StmtError();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001158
Anders Carlsson965d5202007-11-22 01:36:19 +00001159 llvm::SmallVector<std::string, 4> Names;
Sebastian Redl6008ac32008-11-25 22:21:31 +00001160 ExprVector Constraints(Actions);
1161 ExprVector Exprs(Actions);
1162 ExprVector Clobbers(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +00001163
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001164 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redla88da592008-12-11 19:48:14 +00001165
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001166 SourceLocation RParenLoc;
1167 if (Tok.is(tok::r_paren)) {
1168 // We have a simple asm expression
1169 isSimple = true;
Sebastian Redla88da592008-12-11 19:48:14 +00001170
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001171 RParenLoc = ConsumeParen();
1172 } else {
Sebastian Redl6008ac32008-11-25 22:21:31 +00001173 // Parse Outputs, if present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001174 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redla88da592008-12-11 19:48:14 +00001175 return StmtError();
1176
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001177 NumOutputs = Names.size();
Sebastian Redla88da592008-12-11 19:48:14 +00001178
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001179 // Parse Inputs, if present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001180 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redla88da592008-12-11 19:48:14 +00001181 return StmtError();
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001182
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001183 assert(Names.size() == Constraints.size() &&
1184 Constraints.size() == Exprs.size()
1185 && "Input operand size mismatch!");
1186
1187 NumInputs = Names.size() - NumOutputs;
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001188
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001189 // Parse the clobbers, if present.
1190 if (Tok.is(tok::colon)) {
Anders Carlsson861a2852007-11-21 23:27:34 +00001191 ConsumeToken();
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001192
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001193 // Parse the asm-string list for clobbers.
1194 while (1) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001195 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001196
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001197 if (Clobber.isInvalid())
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001198 break;
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001199
1200 Clobbers.push_back(Clobber.release());
1201
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001202 if (Tok.isNot(tok::comma)) break;
1203 ConsumeToken();
1204 }
Chris Lattner4b009652007-07-25 00:24:17 +00001205 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001206
Anders Carlssonde6a9c42008-02-05 23:03:50 +00001207 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001208 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001209
Sebastian Redlc6b86332009-01-18 16:53:17 +00001210 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1211 NumOutputs, NumInputs, &Names[0],
Sebastian Redl6619aa52009-01-18 18:03:53 +00001212 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl81db6682009-02-05 15:02:23 +00001213 move(AsmString), move_arg(Clobbers),
Sebastian Redlc6b86332009-01-18 16:53:17 +00001214 RParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001215}
1216
1217/// ParseAsmOperands - Parse the asm-operands production as used by
1218/// asm-statement. We also parse a leading ':' token. If the leading colon is
1219/// not present, we do not parse anything.
1220///
1221/// [GNU] asm-operands:
1222/// asm-operand
1223/// asm-operands ',' asm-operand
1224///
1225/// [GNU] asm-operand:
1226/// asm-string-literal '(' expression ')'
1227/// '[' identifier ']' asm-string-literal '(' expression ')'
1228///
Anders Carlsson749d7b02008-02-09 19:57:29 +00001229bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson965d5202007-11-22 01:36:19 +00001230 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1231 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner4b009652007-07-25 00:24:17 +00001232 // Only do anything if this operand is present.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001233 if (Tok.isNot(tok::colon)) return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001234 ConsumeToken();
1235
1236 // 'asm-operands' isn't present?
Chris Lattner4d7d2342007-10-09 17:41:39 +00001237 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson749d7b02008-02-09 19:57:29 +00001238 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001239
Anders Carlsson965d5202007-11-22 01:36:19 +00001240 while (1) {
Chris Lattner4b009652007-07-25 00:24:17 +00001241 // Read the [id] if present.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001242 if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001243 SourceLocation Loc = ConsumeBracket();
1244
Chris Lattner4d7d2342007-10-09 17:41:39 +00001245 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001246 Diag(Tok, diag::err_expected_ident);
1247 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001248 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001249 }
Chris Lattner4e21a9b2007-10-29 04:06:22 +00001250
Anders Carlsson965d5202007-11-22 01:36:19 +00001251 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner4e21a9b2007-10-29 04:06:22 +00001252 ConsumeToken();
Anders Carlsson965d5202007-11-22 01:36:19 +00001253
1254 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner4b009652007-07-25 00:24:17 +00001255 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson965d5202007-11-22 01:36:19 +00001256 } else
1257 Names.push_back(std::string());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001258
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001259 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001260 if (Constraint.isInvalid()) {
Anders Carlsson965d5202007-11-22 01:36:19 +00001261 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001262 return true;
Anders Carlsson965d5202007-11-22 01:36:19 +00001263 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001264 Constraints.push_back(Constraint.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001265
Chris Lattner4d7d2342007-10-09 17:41:39 +00001266 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001267 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner4b009652007-07-25 00:24:17 +00001268 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001269 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001270 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001271
Chris Lattner4b009652007-07-25 00:24:17 +00001272 // Read the parenthesized expression.
Sebastian Redla6817a02008-12-11 22:33:27 +00001273 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001274 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001275 SkipUntil(tok::r_paren);
Anders Carlsson749d7b02008-02-09 19:57:29 +00001276 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001277 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001278 Exprs.push_back(Res.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001279 // Eat the comma and continue parsing if it exists.
Anders Carlsson749d7b02008-02-09 19:57:29 +00001280 if (Tok.isNot(tok::comma)) return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001281 ConsumeToken();
1282 }
Anders Carlsson749d7b02008-02-09 19:57:29 +00001283
1284 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001285}
Fariborz Jahanian829dfe52007-11-08 19:01:26 +00001286
Chris Lattner5261d0c2009-03-28 19:18:32 +00001287Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner0818a7a2009-03-05 00:49:17 +00001288 assert(Tok.is(tok::l_brace));
1289 SourceLocation LBraceLoc = Tok.getLocation();
1290
Chris Lattnerc309ade2009-03-05 08:00:35 +00001291 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1292 PP.getSourceManager(),
1293 "parsing function body");
Chris Lattnerd042d0f2009-03-05 01:25:28 +00001294
Fariborz Jahanian829dfe52007-11-08 19:01:26 +00001295 // Do not enter a scope for the brace, as the arguments are in the same scope
1296 // (the function body) as the body itself. Instead, just read the statement
1297 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl10c32952008-12-11 19:30:53 +00001298 OwningStmtResult FnBody(ParseCompoundStatementBody());
1299
Fariborz Jahanian829dfe52007-11-08 19:01:26 +00001300 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001301 if (FnBody.isInvalid())
Chris Lattner0818a7a2009-03-05 00:49:17 +00001302 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1303 MultiStmtArg(Actions), false);
Sebastian Redl10c32952008-12-11 19:30:53 +00001304
Sebastian Redl81db6682009-02-05 15:02:23 +00001305 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeone128c1d2007-12-01 08:06:07 +00001306}
Sebastian Redlb12ac332008-12-21 16:41:36 +00001307
1308/// ParseCXXTryBlock - Parse a C++ try-block.
1309///
1310/// try-block:
1311/// 'try' compound-statement handler-seq
1312///
1313/// handler-seq:
1314/// handler handler-seq[opt]
1315///
1316Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1317 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1318
1319 SourceLocation TryLoc = ConsumeToken();
1320 if (Tok.isNot(tok::l_brace))
1321 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1322 OwningStmtResult TryBlock(ParseCompoundStatement());
1323 if (TryBlock.isInvalid())
1324 return move(TryBlock);
1325
1326 StmtVector Handlers(Actions);
1327 if (Tok.isNot(tok::kw_catch))
1328 return StmtError(Diag(Tok, diag::err_expected_catch));
1329 while (Tok.is(tok::kw_catch)) {
1330 OwningStmtResult Handler(ParseCXXCatchBlock());
1331 if (!Handler.isInvalid())
1332 Handlers.push_back(Handler.release());
1333 }
1334 // Don't bother creating the full statement if we don't have any usable
1335 // handlers.
1336 if (Handlers.empty())
1337 return StmtError();
1338
Sebastian Redl81db6682009-02-05 15:02:23 +00001339 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redlb12ac332008-12-21 16:41:36 +00001340}
1341
1342/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1343///
1344/// handler:
1345/// 'catch' '(' exception-declaration ')' compound-statement
1346///
1347/// exception-declaration:
1348/// type-specifier-seq declarator
1349/// type-specifier-seq abstract-declarator
1350/// type-specifier-seq
1351/// '...'
1352///
1353Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1354 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1355
1356 SourceLocation CatchLoc = ConsumeToken();
1357
1358 SourceLocation LParenLoc = Tok.getLocation();
1359 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1360 return StmtError();
1361
1362 // C++ 3.3.2p3:
1363 // The name in a catch exception-declaration is local to the handler and
1364 // shall not be redeclared in the outermost block of the handler.
1365 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1366
1367 // exception-declaration is equivalent to '...' or a parameter-declaration
1368 // without default arguments.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001369 DeclPtrTy ExceptionDecl;
Sebastian Redlb12ac332008-12-21 16:41:36 +00001370 if (Tok.isNot(tok::ellipsis)) {
1371 DeclSpec DS;
Sebastian Redl743c8162008-12-22 19:15:10 +00001372 if (ParseCXXTypeSpecifierSeq(DS))
1373 return StmtError();
Sebastian Redlb12ac332008-12-21 16:41:36 +00001374 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1375 ParseDeclarator(ExDecl);
1376 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1377 } else
1378 ConsumeToken();
1379
1380 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1381 return StmtError();
1382
1383 if (Tok.isNot(tok::l_brace))
1384 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1385
1386 OwningStmtResult Block(ParseCompoundStatement());
1387 if (Block.isInvalid())
1388 return move(Block);
1389
Sebastian Redl81db6682009-02-05 15:02:23 +00001390 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redlb12ac332008-12-21 16:41:36 +00001391}