blob: 1ecbe8bd84a647e9a4cdf26709708367c2d55bc1 [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/Parse/DeclSpec.h"
19#include "clang/Parse/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +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 Jahanianb384d322007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
75/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000076///
Sebastian Redl61364dd2008-12-11 19:30:53 +000077Parser::OwningStmtResult
78Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000079 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000080 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000081
Reid Spencer5f016e22007-07-11 17:01:13 +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 Jahanian397fcc12007-09-19 19:14:32 +000085 tok::TokenKind Kind = Tok.getKind();
86 SourceLocation AtLoc;
87 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000088 case tok::at: // May be a @try or @throw statement
89 {
90 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000091 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000093
Argyrios Kyrtzidisb9f930d2008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000102 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000103 SourceLocation DeclStart = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000104 DeclTy *Decl = ParseDeclaration(Declarator::BlockContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000105 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redla60528c2008-12-21 12:04:03 +0000106 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000107 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000109 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 } else {
111 // expression[opt] ';'
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000112 OwningExprResult Expr(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000113 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // If the expression is invalid, skip ahead to the next semicolon. Not
115 // doing this opens us up to the possibility of infinite loops if
116 // ParseExpression does not consume any tokens.
117 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000118 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 // Otherwise, eat the semicolon.
121 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000122 return Actions.ActOnExprStmt(move(Expr));
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 case tok::kw_case: // C99 6.8.1: labeled-statement
126 return ParseCaseStatement();
127 case tok::kw_default: // C99 6.8.1: labeled-statement
128 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 case tok::l_brace: // C99 6.8.2: compound-statement
131 return ParseCompoundStatement();
132 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000133 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000134
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 case tok::kw_if: // C99 6.8.4.1: if-statement
136 return ParseIfStatement();
137 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000138 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000141 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 case tok::kw_do: // C99 6.8.5.2: do-statement
143 Res = ParseDoStatement();
144 SemiError = "do/while loop";
145 break;
146 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000147 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000148
149 case tok::kw_goto: // C99 6.8.6.1: goto-statement
150 Res = ParseGotoStatement();
151 SemiError = "goto statement";
152 break;
153 case tok::kw_continue: // C99 6.8.6.2: continue-statement
154 Res = ParseContinueStatement();
155 SemiError = "continue statement";
156 break;
157 case tok::kw_break: // C99 6.8.6.3: break-statement
158 Res = ParseBreakStatement();
159 SemiError = "break statement";
160 break;
161 case tok::kw_return: // C99 6.8.6.4: return-statement
162 Res = ParseReturnStatement();
163 SemiError = "return statement";
164 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000165
Sebastian Redla0fd8652008-12-21 16:41:36 +0000166 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000167 bool msAsm = false;
168 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000169 if (msAsm) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 SemiError = "asm statement";
171 break;
172 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000173
Sebastian Redla0fd8652008-12-21 16:41:36 +0000174 case tok::kw_try: // C++ 15: try-block
175 return ParseCXXTryBlock();
176 }
177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000179 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000181 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000182 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000183 // Skip until we see a } or ;, but don't eat it.
184 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000186 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000187}
188
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000189/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000190///
191/// labeled-statement:
192/// identifier ':' statement
193/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000194///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000195Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000196 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
197 "Not an identifier!");
198
199 Token IdentTok = Tok; // Save the whole token.
200 ConsumeToken(); // eat the identifier.
201
202 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000203
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000204 // identifier ':' statement
205 SourceLocation ColonLoc = ConsumeToken();
206
207 // Read label attributes, if present.
208 DeclTy *AttrList = 0;
209 if (Tok.is(tok::kw___attribute))
210 // TODO: save these somewhere.
211 AttrList = ParseAttributes();
212
Sebastian Redl61364dd2008-12-11 19:30:53 +0000213 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000214
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000215 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000216 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000217 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000218
Sebastian Redlde307472009-01-11 00:38:46 +0000219 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
220 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000221 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000222}
Reid Spencer5f016e22007-07-11 17:01:13 +0000223
224/// ParseCaseStatement
225/// labeled-statement:
226/// 'case' constant-expression ':' statement
227/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
228///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000229Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000230 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattner24e1e702009-03-04 04:23:07 +0000231
232 // It is very very common for code to contain many case statements recursively
233 // nested, as in (but usually without indentation):
234 // case 1:
235 // case 2:
236 // case 3:
237 // case 4:
238 // case 5: etc.
239 //
240 // Parsing this naively works, but is both inefficient and can cause us to run
241 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000242 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000243 // but all the grossness is constrained to ParseCaseStatement (and some
244 // wierdness in the actions), so this is just local grossness :).
245
246 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
247 // example above.
248 OwningStmtResult TopLevelCase(Actions, true);
249
250 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
251 // gets updated each time a new case is parsed, and whose body is unset so
252 // far. When parsing 'case 4', this is the 'case 3' node.
253 StmtTy *DeepestParsedCaseStmt = 0;
254
255 // While we have case statements, eat and stack them.
256 do {
257 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
258
259 OwningExprResult LHS(ParseConstantExpression());
260 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000262 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000264
Chris Lattner24e1e702009-03-04 04:23:07 +0000265 // GNU case range extension.
266 SourceLocation DotDotDotLoc;
267 OwningExprResult RHS(Actions);
268 if (Tok.is(tok::ellipsis)) {
269 Diag(Tok, diag::ext_gnu_case_range);
270 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000271
Chris Lattner24e1e702009-03-04 04:23:07 +0000272 RHS = ParseConstantExpression();
273 if (RHS.isInvalid()) {
274 SkipUntil(tok::colon);
275 return StmtError();
276 }
277 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000278
Chris Lattner24e1e702009-03-04 04:23:07 +0000279 if (Tok.isNot(tok::colon)) {
280 Diag(Tok, diag::err_expected_colon_after) << "'case'";
281 SkipUntil(tok::colon);
282 return StmtError();
283 }
284
285 SourceLocation ColonLoc = ConsumeToken();
286
287 OwningStmtResult Case =
288 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
289 move(RHS), ColonLoc);
290
291 // If we had a sema error parsing this case, then just ignore it and
292 // continue parsing the sub-stmt.
293 if (Case.isInvalid()) {
294 if (TopLevelCase.isInvalid()) // No parsed case stmts.
295 return ParseStatement();
296 // Otherwise, just don't add it as a nested case.
297 } else {
298 // If this is the first case statement we parsed, it becomes TopLevelCase.
299 // Otherwise we link it into the current chain.
300 StmtTy *NextDeepest = Case.get();
301 if (TopLevelCase.isInvalid())
302 TopLevelCase = move(Case);
303 else
304 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
305 DeepestParsedCaseStmt = NextDeepest;
306 }
307
308 // Handle all case statements.
309 } while (Tok.is(tok::kw_case));
310
311 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
312
313 // If we found a non-case statement, start by parsing it.
314 OwningStmtResult SubStmt(Actions);
315
316 if (Tok.isNot(tok::r_brace)) {
317 SubStmt = ParseStatement();
318 } else {
319 // Nicely diagnose the common error "switch (X) { case 4: }", which is
320 // not valid.
321 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000323 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 }
Chris Lattner24e1e702009-03-04 04:23:07 +0000325
326 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000327 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000328 SubStmt = Actions.ActOnNullStmt(SourceLocation());
329
330 // Install the body into the most deeply-nested case.
331 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000332
Chris Lattner24e1e702009-03-04 04:23:07 +0000333 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000334 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000335}
336
337/// ParseDefaultStatement
338/// labeled-statement:
339/// 'default' ':' statement
340/// Note that this does not parse the 'statement' at the end.
341///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000342Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000343 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
345
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000346 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000347 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000349 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000355 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000357 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 }
359
Sebastian Redl61364dd2008-12-11 19:30:53 +0000360 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000361 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000362 return StmtError();
363
Sebastian Redl117054a2008-12-28 16:13:43 +0000364 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000365 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000366}
367
368
369/// ParseCompoundStatement - Parse a "{}" block.
370///
371/// compound-statement: [C99 6.8.2]
372/// { block-item-list[opt] }
373/// [GNU] { label-declarations block-item-list } [TODO]
374///
375/// block-item-list:
376/// block-item
377/// block-item-list block-item
378///
379/// block-item:
380/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000381/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000382/// statement
383/// [OMP] openmp-directive [TODO]
384///
385/// [GNU] label-declarations:
386/// [GNU] label-declaration
387/// [GNU] label-declarations label-declaration
388///
389/// [GNU] label-declaration:
390/// [GNU] '__label__' identifier-list ';'
391///
392/// [OMP] openmp-directive: [TODO]
393/// [OMP] barrier-directive
394/// [OMP] flush-directive
395///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000396Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000397 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000398
Chris Lattner31e05722007-08-26 06:24:45 +0000399 // Enter a scope to hold everything within the compound stmt. Compound
400 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000401 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000402
403 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000404 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000405}
406
407
408/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000409/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000410/// consume the '}' at the end of the block. It does not manipulate the scope
411/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000412Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerae50fa02009-03-05 00:00:31 +0000413 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
414 Tok.getLocation(),
415 "in compound statement ('{}')");
416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
418
419 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000420 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000421
422 typedef StmtVector StmtsTy;
423 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000424 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000425 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000426 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000427 R = ParseStatementOrDeclaration(false);
428 } else {
429 // __extension__ can start declarations and it can also be a unary
430 // operator for expressions. Consume multiple __extension__ markers here
431 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000432 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000433 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000434 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000435 ConsumeToken();
436
Chris Lattner043a0b52008-03-13 06:32:11 +0000437 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000438 ExtensionRAIIObject O(Diags); // Use RAII to do this.
439
Chris Lattner45a566c2007-08-27 01:01:57 +0000440 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000441 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000442 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000443 SourceLocation DeclStart = Tok.getLocation();
444 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
445 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000446 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000447 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000448 // Otherwise this was a unary __extension__ marker.
449 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000450
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000451 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000452 SkipUntil(tok::semi);
453 continue;
454 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000455
Chris Lattner39146d62008-10-20 06:51:33 +0000456 // Eat the semicolon at the end of stmt and convert the expr into a
457 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000458 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000459 R = Actions.ActOnExprStmt(move(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000460 }
461 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000462
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000463 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000464 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000468 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000470 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000472
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000474 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000475 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000476}
477
Chris Lattner15ff1112008-12-12 06:31:07 +0000478/// ParseParenExprOrCondition:
479/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000480/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000481///
482/// This function parses and performs error recovery on the specified condition
483/// or expression (depending on whether we're in C++ or C mode). This function
484/// goes out of its way to recover well. It returns true if there was a parser
485/// error (the right paren couldn't be found), which indicates that the caller
486/// should try to recover harder. It returns false if the condition is
487/// successfully parsed. Note that a successful parse can still have semantic
488/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000489bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
490 bool OnlyAllowCondition) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000491 SourceLocation LParenLoc = ConsumeParen();
492
493 if (getLang().CPlusPlus)
494 CondExp = ParseCXXCondition();
495 else
496 CondExp = ParseExpression();
497
498 // If the parser was confused by the condition and we don't have a ')', try to
499 // recover by skipping ahead to a semi and bailing out. If condexp is
500 // semantically invalid but we have well formed code, keep going.
501 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
502 SkipUntil(tok::semi);
503 // Skipping may have stopped if it found the containing ')'. If so, we can
504 // continue parsing the if statement.
505 if (Tok.isNot(tok::r_paren))
506 return true;
507 }
508
509 // Otherwise the condition is valid or the rparen is present.
510 MatchRHSPunctuation(tok::r_paren, LParenLoc);
511 return false;
512}
513
514
Reid Spencer5f016e22007-07-11 17:01:13 +0000515/// ParseIfStatement
516/// if-statement: [C99 6.8.4.1]
517/// 'if' '(' expression ')' statement
518/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000519/// [C++] 'if' '(' condition ')' statement
520/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000521///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000522Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000523 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
525
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000526 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000527 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000529 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000531
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000532 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
533
Chris Lattner22153252007-08-26 23:08:06 +0000534 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
535 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000536 //
537 // C++ 6.4p3:
538 // A name introduced by a declaration in a condition is in scope from its
539 // point of declaration until the end of the substatements controlled by the
540 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000541 // C++ 3.3.2p4:
542 // Names declared in the for-init-statement, and in the condition of if,
543 // while, for, and switch statements are local to the if, while, for, or
544 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000545 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000546 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000547
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000549 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000550 if (ParseParenExprOrCondition(CondExp))
551 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000552
Chris Lattner0ecea032007-08-22 05:28:50 +0000553 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000554 // there is no compound stmt. C90 does not have this clause. We only do this
555 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000556 //
557 // C++ 6.4p1:
558 // The substatement in a selection-statement (each substatement, in the else
559 // form of the if statement) implicitly defines a local scope.
560 //
561 // For C++ we create a scope for the condition and a new scope for
562 // substatements because:
563 // -When the 'then' scope exits, we want the condition declaration to still be
564 // active for the 'else' scope too.
565 // -Sema will detect name clashes by considering declarations of a
566 // 'ControlScope' as part of its direct subscope.
567 // -If we wanted the condition and substatement to be in the same scope, we
568 // would have to notify ParseStatement not to create a new scope. It's
569 // simpler to let it create a new scope.
570 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000571 ParseScope InnerScope(this, Scope::DeclScope,
572 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000573
Chris Lattnerb96728d2007-10-29 05:08:52 +0000574 // Read the 'then' stmt.
575 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000576 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000577
Chris Lattnera36ce712007-08-22 05:16:28 +0000578 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000579 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000580
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 // If it has an else, parse it.
582 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000583 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000584 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000585
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000586 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000588
Chris Lattner0ecea032007-08-22 05:28:50 +0000589 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000590 // there is no compound stmt. C90 does not have this clause. We only do
591 // this if the body isn't a compound statement to avoid push/pop in common
592 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000593 //
594 // C++ 6.4p1:
595 // The substatement in a selection-statement (each substatement, in the else
596 // form of the if statement) implicitly defines a local scope.
597 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000598 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000599 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000600
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000601 bool WithinElse = CurScope->isWithinElse();
602 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000603 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000605 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000606
607 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000608 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000610
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000611 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000612
613 // If the condition was invalid, discard the if statement. We could recover
614 // better by replacing it with a valid expr, but don't do that yet.
615 if (CondExp.isInvalid())
616 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000617
Chris Lattnerb96728d2007-10-29 05:08:52 +0000618 // If the then or else stmt is invalid and the other is valid (and present),
619 // make turn the invalid one into a null stmt to avoid dropping the other
620 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000621 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
622 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
623 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000624 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000625 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000626 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000627
Chris Lattnerb96728d2007-10-29 05:08:52 +0000628 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000629 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000630 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000631 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000632 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000633
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000634 return Actions.ActOnIfStmt(IfLoc, move(CondExp), move(ThenStmt),
635 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000636}
637
638/// ParseSwitchStatement
639/// switch-statement:
640/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000641/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000642Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000643 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
645
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000646 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000647 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000649 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 }
Chris Lattner22153252007-08-26 23:08:06 +0000651
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000652 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
653
Chris Lattner22153252007-08-26 23:08:06 +0000654 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
655 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000656 //
657 // C++ 6.4p3:
658 // A name introduced by a declaration in a condition is in scope from its
659 // point of declaration until the end of the substatements controlled by the
660 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000661 // C++ 3.3.2p4:
662 // Names declared in the for-init-statement, and in the condition of if,
663 // while, for, and switch statements are local to the if, while, for, or
664 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000665 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000666 unsigned ScopeFlags = Scope::BreakScope;
667 if (C99orCXX)
668 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000669 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000670
671 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000672 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000673 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000674 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000675
676 OwningStmtResult Switch(Actions);
677 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000678 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000679
Chris Lattner0ecea032007-08-22 05:28:50 +0000680 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000681 // there is no compound stmt. C90 does not have this clause. We only do this
682 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000683 //
684 // C++ 6.4p1:
685 // The substatement in a selection-statement (each substatement, in the else
686 // form of the if statement) implicitly defines a local scope.
687 //
688 // See comments in ParseIfStatement for why we create a scope for the
689 // condition and a new scope for substatement in C++.
690 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000691 ParseScope InnerScope(this, Scope::DeclScope,
692 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000695 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000696
Chris Lattner0ecea032007-08-22 05:28:50 +0000697 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000698 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000699
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000700 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000701 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000702 // FIXME: Remove the case statement list from the Switch statement.
703 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000704
705 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000706
Chris Lattner15ff1112008-12-12 06:31:07 +0000707 if (Cond.isInvalid())
708 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000709
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000710 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000711}
712
713/// ParseWhileStatement
714/// while-statement: [C99 6.8.5.1]
715/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000716/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000717Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000718 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 SourceLocation WhileLoc = Tok.getLocation();
720 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000721
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000722 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000723 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000725 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000727
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000728 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
729
Chris Lattner22153252007-08-26 23:08:06 +0000730 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
731 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000732 //
733 // C++ 6.4p3:
734 // A name introduced by a declaration in a condition is in scope from its
735 // point of declaration until the end of the substatements controlled by the
736 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000737 // C++ 3.3.2p4:
738 // Names declared in the for-init-statement, and in the condition of if,
739 // while, for, and switch statements are local to the if, while, for, or
740 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000741 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000742 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000743 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000744 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
745 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000746 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000747 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
748 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749
750 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000751 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000752 if (ParseParenExprOrCondition(Cond))
753 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000754
Chris Lattner0ecea032007-08-22 05:28:50 +0000755 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000756 // there is no compound stmt. C90 does not have this clause. We only do this
757 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000758 //
759 // C++ 6.5p2:
760 // The substatement in an iteration-statement implicitly defines a local scope
761 // which is entered and exited each time through the loop.
762 //
763 // See comments in ParseIfStatement for why we create a scope for the
764 // condition and a new scope for substatement in C++.
765 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000766 ParseScope InnerScope(this, Scope::DeclScope,
767 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000768
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000770 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000771
Chris Lattner0ecea032007-08-22 05:28:50 +0000772 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000773 InnerScope.Exit();
774 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000775
776 if (Cond.isInvalid() || Body.isInvalid())
777 return StmtError();
778
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000779 return Actions.ActOnWhileStmt(WhileLoc, move(Cond), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000780}
781
782/// ParseDoStatement
783/// do-statement: [C99 6.8.5.2]
784/// 'do' statement 'while' '(' expression ')' ';'
785/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000786Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000787 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000789
Chris Lattner22153252007-08-26 23:08:06 +0000790 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
791 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000792 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000793 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000794 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000795 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000796 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000797
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000798 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799
Chris Lattner0ecea032007-08-22 05:28:50 +0000800 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000801 // there is no compound stmt. C90 does not have this clause. We only do this
802 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000803 //
804 // C++ 6.5p2:
805 // The substatement in an iteration-statement implicitly defines a local scope
806 // which is entered and exited each time through the loop.
807 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000808 ParseScope InnerScope(this, Scope::DeclScope,
809 (getLang().C99 || getLang().CPlusPlus) &&
810 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000811
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000813 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000814
Chris Lattner0ecea032007-08-22 05:28:50 +0000815 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000816 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000817
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000818 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000819 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000820 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000821 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000822 SkipUntil(tok::semi, false, true);
823 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000824 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 }
826 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000827
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000828 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000829 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000830 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000831 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000833
Chris Lattnerff871fb2008-12-12 06:35:28 +0000834 // Parse the parenthesized condition.
835 OwningExprResult Cond(Actions);
836 ParseParenExprOrCondition(Cond, true);
837
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000838 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000839
Sebastian Redl9a920342008-12-11 19:48:14 +0000840 if (Cond.isInvalid() || Body.isInvalid())
841 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000842
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000843 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000844}
845
846/// ParseForStatement
847/// for-statement: [C99 6.8.5.3]
848/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
849/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000850/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
851/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000852/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
853/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000854///
855/// [C++] for-init-statement:
856/// [C++] expression-statement
857/// [C++] simple-declaration
858///
Sebastian Redl9a920342008-12-11 19:48:14 +0000859Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000860 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000862
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000863 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000864 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000866 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000868
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000869 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
870
Chris Lattner22153252007-08-26 23:08:06 +0000871 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
872 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000873 //
874 // C++ 6.4p3:
875 // A name introduced by a declaration in a condition is in scope from its
876 // point of declaration until the end of the substatements controlled by the
877 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000878 // C++ 3.3.2p4:
879 // Names declared in the for-init-statement, and in the condition of if,
880 // while, for, and switch statements are local to the if, while, for, or
881 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000882 // C++ 6.5.3p1:
883 // Names declared in the for-init-statement are in the same declarative-region
884 // as those declared in the condition.
885 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000886 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000887 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000888 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
889 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000890 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000891 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
892
893 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000894
895 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000896 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000897
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000898 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000899 OwningStmtResult FirstPart(Actions);
900 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000903 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 // no first part, eat the ';'.
905 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000906 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000908 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000910
Chris Lattner81c018d2008-03-13 06:29:04 +0000911 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000912 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000913 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000914 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
915 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000916 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000917 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000918 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000919 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 } else {
921 Value = ParseExpression();
922
923 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000924 if (!Value.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000925 FirstPart = Actions.ActOnExprStmt(move(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000926
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000927 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000929 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000930 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000931 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000932 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000933 }
934 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000935 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 SkipUntil(tok::semi);
937 }
938 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000939 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000940 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-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 Jahanian3ba5a0f2008-01-03 17:55:25 +0000944 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000945 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000946 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000947
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000948 if (Tok.is(tok::semi)) {
949 ConsumeToken();
950 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000951 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000952 SkipUntil(tok::semi);
953 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000954
Fariborz Jahanian3ba5a0f2008-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 Jahanian3ba5a0f2008-01-03 17:55:25 +0000958 } else {
Sebastian Redlf05b1522009-01-16 23:28:06 +0000959 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 }
961 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // Match the ')'.
963 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000964
Chris Lattner0ecea032007-08-22 05:28:50 +0000965 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-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.
Argyrios Kyrtzidis488d37e2008-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 Gregor8935b8b2008-12-10 06:34:36 +0000976 ParseScope InnerScope(this, Scope::DeclScope,
977 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000978
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000980 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000981
Chris Lattner0ecea032007-08-22 05:28:50 +0000982 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000983 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000984
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000986 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000987
988 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000989 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000990
991 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000992 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
993 move(SecondPart), move(ThirdPart),
994 RParenLoc, move(Body));
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000995 else
Sebastian Redlf05b1522009-01-16 23:28:06 +0000996 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000997 move(FirstPart),
998 move(SecondPart),
999 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl9a920342008-12-11 19:48:14 +00001009Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001010 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001012
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001013 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001014 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001015 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 Tok.getIdentifierInfo());
1017 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001018 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 // GNU indirect goto extension.
1020 Diag(Tok, diag::ext_gnu_indirect_goto);
1021 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001022 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001023 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001025 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001027 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001028 } else {
1029 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001030 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001032
Sebastian Redl9a920342008-12-11 19:48:14 +00001033 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001034}
1035
1036/// ParseContinueStatement
1037/// jump-statement:
1038/// 'continue' ';'
1039///
1040/// Note: this lets the caller parse the end ';'.
1041///
Sebastian Redl9a920342008-12-11 19:48:14 +00001042Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001044 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001045}
1046
1047/// ParseBreakStatement
1048/// jump-statement:
1049/// 'break' ';'
1050///
1051/// Note: this lets the caller parse the end ';'.
1052///
Sebastian Redl9a920342008-12-11 19:48:14 +00001053Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001055 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056}
1057
1058/// ParseReturnStatement
1059/// jump-statement:
1060/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001061Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001062 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001064
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001065 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001066 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001068 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001070 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 }
1072 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001073 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001074}
1075
Steve Naroff5f8aa692008-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 Redl9a920342008-12-11 19:48:14 +00001078Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-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 Naroff03d6bc62008-02-08 03:36:19 +00001088 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001089 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001090 do {
1091 ConsumeAnyToken();
1092 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001093 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001094 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1095 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001096 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001097 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001098}
1099
Reid Spencer5f016e22007-07-11 17:01:13 +00001100/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001101/// asm-statement:
1102/// gnu-asm-statement
1103/// ms-asm-statement
1104///
1105/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff5f8aa692008-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 Redl9a920342008-12-11 19:48:14 +00001127Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001128 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001129 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001130
Steve Naroff5f8aa692008-02-11 23:15:56 +00001131 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001132 msAsm = true;
1133 return FuzzyParseMicrosoftAsmStatement();
1134 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 DeclSpec DS;
1136 SourceLocation Loc = Tok.getLocation();
1137 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001138
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1140 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001141 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001143 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001146 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001147 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001148 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001149 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001151 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 }
1153 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001154
Sebastian Redleffa8d12008-12-10 00:02:53 +00001155 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001156 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001157 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001158
Anders Carlssonb235fc22007-11-22 01:36:19 +00001159 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001160 ExprVector Constraints(Actions);
1161 ExprVector Exprs(Actions);
1162 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163
Anders Carlssondfab34a2008-02-05 23:03:50 +00001164 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001165
Anders Carlssondfab34a2008-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 Redl9a920342008-12-11 19:48:14 +00001170
Anders Carlssondfab34a2008-02-05 23:03:50 +00001171 RParenLoc = ConsumeParen();
1172 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001173 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001174 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001175 return StmtError();
1176
Anders Carlssondfab34a2008-02-05 23:03:50 +00001177 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001178
Anders Carlssondfab34a2008-02-05 23:03:50 +00001179 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001180 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001181 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001182
Anders Carlssondfab34a2008-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 Redleffa8d12008-12-10 00:02:53 +00001188
Anders Carlssondfab34a2008-02-05 23:03:50 +00001189 // Parse the clobbers, if present.
1190 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001191 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001192
Anders Carlssondfab34a2008-02-05 23:03:50 +00001193 // Parse the asm-string list for clobbers.
1194 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001195 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001196
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001197 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001198 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001199
1200 Clobbers.push_back(Clobber.release());
1201
Anders Carlssondfab34a2008-02-05 23:03:50 +00001202 if (Tok.isNot(tok::comma)) break;
1203 ConsumeToken();
1204 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001206
Anders Carlssondfab34a2008-02-05 23:03:50 +00001207 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001209
Sebastian Redl3037ed02009-01-18 16:53:17 +00001210 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1211 NumOutputs, NumInputs, &Names[0],
Sebastian Redlf512e822009-01-18 18:03:53 +00001212 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001213 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001214 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Carlsson8bd36fc2008-02-09 19:57:29 +00001229bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001230 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1231 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001233 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 ConsumeToken();
1235
1236 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001237 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001238 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001239
Anders Carlssonb235fc22007-11-22 01:36:19 +00001240 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001242 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 SourceLocation Loc = ConsumeBracket();
1244
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001245 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 Diag(Tok, diag::err_expected_ident);
1247 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001248 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 }
Chris Lattner69efba72007-10-29 04:06:22 +00001250
Anders Carlssonb235fc22007-11-22 01:36:19 +00001251 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001252 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001253
1254 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001256 } else
1257 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001258
Sebastian Redleffa8d12008-12-10 00:02:53 +00001259 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001260 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001261 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001262 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001263 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001264 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001265
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001266 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001267 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001269 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001271
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001273 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001274 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001276 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001278 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001280 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 ConsumeToken();
1282 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001283
1284 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001285}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001286
Chris Lattner40e9bc82009-03-05 00:49:17 +00001287Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl) {
1288 assert(Tok.is(tok::l_brace));
1289 SourceLocation LBraceLoc = Tok.getLocation();
1290
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001291 // Do not enter a scope for the brace, as the arguments are in the same scope
1292 // (the function body) as the body itself. Instead, just read the statement
1293 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001294 OwningStmtResult FnBody(ParseCompoundStatementBody());
1295
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001296 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001297 if (FnBody.isInvalid())
Chris Lattner40e9bc82009-03-05 00:49:17 +00001298 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1299 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001300
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001301 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001302}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001303
1304/// ParseCXXTryBlock - Parse a C++ try-block.
1305///
1306/// try-block:
1307/// 'try' compound-statement handler-seq
1308///
1309/// handler-seq:
1310/// handler handler-seq[opt]
1311///
1312Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1313 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1314
1315 SourceLocation TryLoc = ConsumeToken();
1316 if (Tok.isNot(tok::l_brace))
1317 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1318 OwningStmtResult TryBlock(ParseCompoundStatement());
1319 if (TryBlock.isInvalid())
1320 return move(TryBlock);
1321
1322 StmtVector Handlers(Actions);
1323 if (Tok.isNot(tok::kw_catch))
1324 return StmtError(Diag(Tok, diag::err_expected_catch));
1325 while (Tok.is(tok::kw_catch)) {
1326 OwningStmtResult Handler(ParseCXXCatchBlock());
1327 if (!Handler.isInvalid())
1328 Handlers.push_back(Handler.release());
1329 }
1330 // Don't bother creating the full statement if we don't have any usable
1331 // handlers.
1332 if (Handlers.empty())
1333 return StmtError();
1334
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001335 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001336}
1337
1338/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1339///
1340/// handler:
1341/// 'catch' '(' exception-declaration ')' compound-statement
1342///
1343/// exception-declaration:
1344/// type-specifier-seq declarator
1345/// type-specifier-seq abstract-declarator
1346/// type-specifier-seq
1347/// '...'
1348///
1349Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1350 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1351
1352 SourceLocation CatchLoc = ConsumeToken();
1353
1354 SourceLocation LParenLoc = Tok.getLocation();
1355 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1356 return StmtError();
1357
1358 // C++ 3.3.2p3:
1359 // The name in a catch exception-declaration is local to the handler and
1360 // shall not be redeclared in the outermost block of the handler.
1361 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1362
1363 // exception-declaration is equivalent to '...' or a parameter-declaration
1364 // without default arguments.
1365 DeclTy *ExceptionDecl = 0;
1366 if (Tok.isNot(tok::ellipsis)) {
1367 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001368 if (ParseCXXTypeSpecifierSeq(DS))
1369 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001370 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1371 ParseDeclarator(ExDecl);
1372 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1373 } else
1374 ConsumeToken();
1375
1376 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1377 return StmtError();
1378
1379 if (Tok.isNot(tok::l_brace))
1380 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1381
1382 OwningStmtResult Block(ParseCompoundStatement());
1383 if (Block.isInvalid())
1384 return move(Block);
1385
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001386 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001387}