blob: 3ca2f02b52bff2ea4532c25e3315413bbb92eea6 [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
Chris Lattnerf919bfe2009-03-24 17:04:48 +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();
Chris Lattner682bf922009-03-29 16:50:03 +0000104 DeclGroupPtrTy 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 Lattnerf919bfe2009-03-24 17:04:48 +0000107 }
108
109 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000111 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
Chris Lattnerf919bfe2009-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 Redl61364dd2008-12-11 19:30:53 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl61364dd2008-12-11 19:30:53 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redla60528c2008-12-21 12:04:03 +0000136 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000137
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl9a920342008-12-11 19:48:14 +0000141 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000142
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000144 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl9a920342008-12-11 19:48:14 +0000150 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl61364dd2008-12-11 19:30:53 +0000168
Sebastian Redla0fd8652008-12-21 16:41:36 +0000169 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000170 bool msAsm = false;
171 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000172 if (msAsm) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 SemiError = "asm statement";
174 break;
175 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000176
Sebastian Redla0fd8652008-12-21 16:41:36 +0000177 case tok::kw_try: // C++ 15: try-block
178 return ParseCXXTryBlock();
179 }
180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000182 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000184 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000185 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000186 // Skip until we see a } or ;, but don't eat it.
187 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000189 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000190}
191
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000192/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000193///
194/// labeled-statement:
195/// identifier ':' statement
196/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000197///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000198Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-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 Redl61364dd2008-12-11 19:30:53 +0000206
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000207 // identifier ':' statement
208 SourceLocation ColonLoc = ConsumeToken();
209
210 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000211 Action::AttrTy *AttrList = 0;
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000212 if (Tok.is(tok::kw___attribute))
213 // TODO: save these somewhere.
214 AttrList = ParseAttributes();
215
Sebastian Redl61364dd2008-12-11 19:30:53 +0000216 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000217
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000218 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000219 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000220 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000221
Sebastian Redlde307472009-01-11 00:38:46 +0000222 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
223 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000224 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000225}
Reid Spencer5f016e22007-07-11 17:01:13 +0000226
227/// ParseCaseStatement
228/// labeled-statement:
229/// 'case' constant-expression ':' statement
230/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
231///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000232Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000233 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattner24e1e702009-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 Lattner26140c62009-03-04 18:24:58 +0000245 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-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()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000265 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000267
Chris Lattner24e1e702009-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 Redl61364dd2008-12-11 19:30:53 +0000274
Chris Lattner24e1e702009-03-04 04:23:07 +0000275 RHS = ParseConstantExpression();
276 if (RHS.isInvalid()) {
277 SkipUntil(tok::colon);
278 return StmtError();
279 }
280 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000281
Chris Lattner24e1e702009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000326 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 }
Chris Lattner24e1e702009-03-04 04:23:07 +0000328
329 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000330 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-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 Redl61364dd2008-12-11 19:30:53 +0000335
Chris Lattner24e1e702009-03-04 04:23:07 +0000336 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000337 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338}
339
340/// ParseDefaultStatement
341/// labeled-statement:
342/// 'default' ':' statement
343/// Note that this does not parse the 'statement' at the end.
344///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000345Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000346 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
348
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000349 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000350 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000352 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000358 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000360 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 }
362
Sebastian Redl61364dd2008-12-11 19:30:53 +0000363 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000364 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000365 return StmtError();
366
Sebastian Redl117054a2008-12-28 16:13:43 +0000367 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000368 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner45a566c2007-08-27 01:01:57 +0000384/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl61364dd2008-12-11 19:30:53 +0000399Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000400 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000401
Chris Lattner31e05722007-08-26 06:24:45 +0000402 // Enter a scope to hold everything within the compound stmt. Compound
403 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000404 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000405
406 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000407 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000408}
409
410
411/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000412/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000413/// consume the '}' at the end of the block. It does not manipulate the scope
414/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000415Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerae50fa02009-03-05 00:00:31 +0000416 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
417 Tok.getLocation(),
418 "in compound statement ('{}')");
419
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
421
422 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000423 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000424
425 typedef StmtVector StmtsTy;
426 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000427 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000428 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000429 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-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 Friedmanadf077f2009-01-27 08:43:38 +0000435 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000436 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000437 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000438 ConsumeToken();
439
Chris Lattner043a0b52008-03-13 06:32:11 +0000440 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000441 ExtensionRAIIObject O(Diags); // Use RAII to do this.
442
Chris Lattner45a566c2007-08-27 01:01:57 +0000443 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000444 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000445 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000446 SourceLocation DeclStart = Tok.getLocation();
Chris Lattner682bf922009-03-29 16:50:03 +0000447 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000448 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000449 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000450 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000451 // Otherwise this was a unary __extension__ marker.
452 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000453
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000454 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000455 SkipUntil(tok::semi);
456 continue;
457 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000458
Chris Lattner39146d62008-10-20 06:51:33 +0000459 // Eat the semicolon at the end of stmt and convert the expr into a
460 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000461 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000462 R = Actions.ActOnExprStmt(move(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000463 }
464 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000465
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000466 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000467 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000471 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000473 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000475
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000477 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000478 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000479}
480
Chris Lattner15ff1112008-12-12 06:31:07 +0000481/// ParseParenExprOrCondition:
482/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000483/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-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 Lattnerff871fb2008-12-12 06:35:28 +0000492bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
493 bool OnlyAllowCondition) {
Chris Lattner15ff1112008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000518/// ParseIfStatement
519/// if-statement: [C99 6.8.4.1]
520/// 'if' '(' expression ')' statement
521/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000522/// [C++] 'if' '(' condition ')' statement
523/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000524///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000525Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000526 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
528
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000529 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000530 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000532 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000533 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000534
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000535 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
536
Chris Lattner22153252007-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.
Argyrios Kyrtzidis488d37e2008-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.
Argyrios Kyrtzidis14d08c02008-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).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000548 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000549 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000550
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000552 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000553 if (ParseParenExprOrCondition(CondExp))
554 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000555
Chris Lattner0ecea032007-08-22 05:28:50 +0000556 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-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.
Argyrios Kyrtzidis488d37e2008-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 Gregor8935b8b2008-12-10 06:34:36 +0000574 ParseScope InnerScope(this, Scope::DeclScope,
575 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000576
Chris Lattnerb96728d2007-10-29 05:08:52 +0000577 // Read the 'then' stmt.
578 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000579 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000580
Chris Lattnera36ce712007-08-22 05:16:28 +0000581 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000582 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 // If it has an else, parse it.
585 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000586 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000587 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000588
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000589 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000591
Chris Lattner0ecea032007-08-22 05:28:50 +0000592 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-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.
Argyrios Kyrtzidis488d37e2008-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 Redl61364dd2008-12-11 19:30:53 +0000601 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000602 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000603
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000604 bool WithinElse = CurScope->isWithinElse();
605 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000606 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000608 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000609
610 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000611 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000613
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000614 IfScope.Exit();
Chris Lattner18914bc2008-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 Lattner22153252007-08-26 23:08:06 +0000620
Chris Lattnerb96728d2007-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 Redl0e9eabc2008-12-09 13:15:23 +0000624 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
625 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
626 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000627 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000628 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000629 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000630
Chris Lattnerb96728d2007-10-29 05:08:52 +0000631 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000632 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000633 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000634 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000635 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000636
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000637 return Actions.ActOnIfStmt(IfLoc, move(CondExp), move(ThenStmt),
638 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000639}
640
641/// ParseSwitchStatement
642/// switch-statement:
643/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000644/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000645Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000646 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
648
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000649 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000650 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000652 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 }
Chris Lattner22153252007-08-26 23:08:06 +0000654
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000655 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
656
Chris Lattner22153252007-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.
Argyrios Kyrtzidis488d37e2008-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.
Argyrios Kyrtzidis14d08c02008-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).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000668 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000669 unsigned ScopeFlags = Scope::BreakScope;
670 if (C99orCXX)
671 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000672 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000673
674 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000675 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000676 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000677 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000678
679 OwningStmtResult Switch(Actions);
680 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000681 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000682
Chris Lattner0ecea032007-08-22 05:28:50 +0000683 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-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.
Argyrios Kyrtzidis488d37e2008-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 Gregor8935b8b2008-12-10 06:34:36 +0000694 ParseScope InnerScope(this, Scope::DeclScope,
695 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000698 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000699
Chris Lattner0ecea032007-08-22 05:28:50 +0000700 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000701 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000702
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000703 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000704 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000705 // FIXME: Remove the case statement list from the Switch statement.
706 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000707
708 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000709
Chris Lattner15ff1112008-12-12 06:31:07 +0000710 if (Cond.isInvalid())
711 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000712
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000713 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000714}
715
716/// ParseWhileStatement
717/// while-statement: [C99 6.8.5.1]
718/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000719/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000720Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000721 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 SourceLocation WhileLoc = Tok.getLocation();
723 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000724
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000725 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000726 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000728 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000730
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000731 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
732
Chris Lattner22153252007-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.
Argyrios Kyrtzidis488d37e2008-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.
Argyrios Kyrtzidis14d08c02008-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).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000744 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000745 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000746 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000747 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
748 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000749 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000750 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
751 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000752
753 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000754 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000755 if (ParseParenExprOrCondition(Cond))
756 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000757
Chris Lattner0ecea032007-08-22 05:28:50 +0000758 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-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.
Argyrios Kyrtzidis488d37e2008-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 Gregor8935b8b2008-12-10 06:34:36 +0000769 ParseScope InnerScope(this, Scope::DeclScope,
770 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000771
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000773 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000774
Chris Lattner0ecea032007-08-22 05:28:50 +0000775 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000776 InnerScope.Exit();
777 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000778
779 if (Cond.isInvalid() || Body.isInvalid())
780 return StmtError();
781
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000782 return Actions.ActOnWhileStmt(WhileLoc, move(Cond), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl9a920342008-12-11 19:48:14 +0000789Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000790 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000792
Chris Lattner22153252007-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 Gregor8935b8b2008-12-10 06:34:36 +0000795 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000796 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000797 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000798 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000799 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000800
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000801 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000802
Chris Lattner0ecea032007-08-22 05:28:50 +0000803 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-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.
Argyrios Kyrtzidis143db712008-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 Gregor8935b8b2008-12-10 06:34:36 +0000811 ParseScope InnerScope(this, Scope::DeclScope,
812 (getLang().C99 || getLang().CPlusPlus) &&
813 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000814
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000816 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000817
Chris Lattner0ecea032007-08-22 05:28:50 +0000818 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000819 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000820
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000821 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000822 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000823 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000824 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000825 SkipUntil(tok::semi, false, true);
826 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000827 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 }
829 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000830
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000831 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000832 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000833 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000834 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000836
Chris Lattnerff871fb2008-12-12 06:35:28 +0000837 // Parse the parenthesized condition.
838 OwningExprResult Cond(Actions);
839 ParseParenExprOrCondition(Cond, true);
840
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000841 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000842
Sebastian Redl9a920342008-12-11 19:48:14 +0000843 if (Cond.isInvalid() || Body.isInvalid())
844 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000845
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000846 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond));
Reid Spencer5f016e22007-07-11 17:01:13 +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
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000853/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
854/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000855/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
856/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000857///
858/// [C++] for-init-statement:
859/// [C++] expression-statement
860/// [C++] simple-declaration
861///
Sebastian Redl9a920342008-12-11 19:48:14 +0000862Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000863 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000865
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000866 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000867 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000869 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000871
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000872 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
873
Chris Lattner22153252007-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.
Argyrios Kyrtzidis488d37e2008-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.
Argyrios Kyrtzidis14d08c02008-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).
Argyrios Kyrtzidis488d37e2008-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 Gregor8935b8b2008-12-10 06:34:36 +0000889 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000890 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000891 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
892 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000893 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000894 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
895
896 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000897
898 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000899 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000900
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000901 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000902 OwningStmtResult FirstPart(Actions);
903 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000904
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000906 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 // no first part, eat the ';'.
908 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000909 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000911 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000913
Chris Lattner81c018d2008-03-13 06:29:04 +0000914 SourceLocation DeclStart = Tok.getLocation();
Chris Lattnercd147752009-03-29 17:27:48 +0000915 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, false);
916 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
917
918 if (Tok.is(tok::semi)) { // for (int x = 4;
919 ConsumeToken();
920 } else if ((ForEach = isTokIdentifier_in())) {
921 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000922 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000923 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000924 } else {
925 Diag(Tok, diag::err_expected_semi_for);
926 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000927 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 } else {
929 Value = ParseExpression();
930
931 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000932 if (!Value.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000933 FirstPart = Actions.ActOnExprStmt(move(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000934
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000935 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000937 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000938 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000939 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000940 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000941 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 SkipUntil(tok::semi);
943 }
944 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000945 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000946 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000947 // Parse the second part of the for specifier.
948 if (Tok.is(tok::semi)) { // for (...;;
949 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000950 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000951 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000952 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000953
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000954 if (Tok.is(tok::semi)) {
955 ConsumeToken();
956 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000957 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000958 SkipUntil(tok::semi);
959 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000960
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000961 // Parse the third part of the for specifier.
962 if (Tok.is(tok::r_paren)) { // for (...;...;)
963 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000964 } else {
Sebastian Redlf05b1522009-01-16 23:28:06 +0000965 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 }
967 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 // Match the ')'.
969 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000970
Chris Lattner0ecea032007-08-22 05:28:50 +0000971 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000972 // there is no compound stmt. C90 does not have this clause. We only do this
973 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000974 //
975 // C++ 6.5p2:
976 // The substatement in an iteration-statement implicitly defines a local scope
977 // which is entered and exited each time through the loop.
978 //
979 // See comments in ParseIfStatement for why we create a scope for
980 // for-init-statement/condition and a new scope for substatement in C++.
981 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000982 ParseScope InnerScope(this, Scope::DeclScope,
983 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000984
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000986 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000987
Chris Lattner0ecea032007-08-22 05:28:50 +0000988 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000989 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000990
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000992 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000993
994 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000995 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000996
997 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000998 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
999 move(SecondPart), move(ThirdPart),
1000 RParenLoc, move(Body));
Chris Lattner682bf922009-03-29 16:50:03 +00001001
1002 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1003 move(FirstPart),
1004 move(SecondPart),
1005 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001006}
1007
1008/// ParseGotoStatement
1009/// jump-statement:
1010/// 'goto' identifier ';'
1011/// [GNU] 'goto' '*' expression ';'
1012///
1013/// Note: this lets the caller parse the end ';'.
1014///
Sebastian Redl9a920342008-12-11 19:48:14 +00001015Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001016 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001018
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001019 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001020 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001021 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 Tok.getIdentifierInfo());
1023 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001024 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 // GNU indirect goto extension.
1026 Diag(Tok, diag::ext_gnu_indirect_goto);
1027 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001028 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001029 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001031 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001033 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001034 } else {
1035 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001036 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001038
Sebastian Redl9a920342008-12-11 19:48:14 +00001039 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001040}
1041
1042/// ParseContinueStatement
1043/// jump-statement:
1044/// 'continue' ';'
1045///
1046/// Note: this lets the caller parse the end ';'.
1047///
Sebastian Redl9a920342008-12-11 19:48:14 +00001048Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001050 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051}
1052
1053/// ParseBreakStatement
1054/// jump-statement:
1055/// 'break' ';'
1056///
1057/// Note: this lets the caller parse the end ';'.
1058///
Sebastian Redl9a920342008-12-11 19:48:14 +00001059Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001061 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001062}
1063
1064/// ParseReturnStatement
1065/// jump-statement:
1066/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001067Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001068 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001070
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001071 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001072 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001074 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001076 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 }
1078 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001079 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001080}
1081
Steve Naroff5f8aa692008-02-11 23:15:56 +00001082/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1083/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001084Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001085 if (Tok.is(tok::l_brace)) {
1086 unsigned short savedBraceCount = BraceCount;
1087 do {
1088 ConsumeAnyToken();
1089 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1090 } else {
1091 // From the MS website: If used without braces, the __asm keyword means
1092 // that the rest of the line is an assembly-language statement.
1093 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001094 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001095 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001096 do {
1097 ConsumeAnyToken();
1098 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001099 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001100 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1101 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001102 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001103 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001104}
1105
Reid Spencer5f016e22007-07-11 17:01:13 +00001106/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001107/// asm-statement:
1108/// gnu-asm-statement
1109/// ms-asm-statement
1110///
1111/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001112/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1113///
1114/// [GNU] asm-argument:
1115/// asm-string-literal
1116/// asm-string-literal ':' asm-operands[opt]
1117/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1118/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1119/// ':' asm-clobbers
1120///
1121/// [GNU] asm-clobbers:
1122/// asm-string-literal
1123/// asm-clobbers ',' asm-string-literal
1124///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001125/// [MS] ms-asm-statement:
1126/// '__asm' assembly-instruction ';'[opt]
1127/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1128///
1129/// [MS] assembly-instruction-list:
1130/// assembly-instruction ';'[opt]
1131/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1132///
Sebastian Redl9a920342008-12-11 19:48:14 +00001133Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001134 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001135 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001136
Steve Naroff5f8aa692008-02-11 23:15:56 +00001137 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001138 msAsm = true;
1139 return FuzzyParseMicrosoftAsmStatement();
1140 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 DeclSpec DS;
1142 SourceLocation Loc = Tok.getLocation();
1143 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1146 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001147 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001149 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001150
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001152 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001153 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001154 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001155 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001157 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 }
1159 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001160
Sebastian Redleffa8d12008-12-10 00:02:53 +00001161 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001162 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001163 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001164
Anders Carlssonb235fc22007-11-22 01:36:19 +00001165 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001166 ExprVector Constraints(Actions);
1167 ExprVector Exprs(Actions);
1168 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001169
Anders Carlssondfab34a2008-02-05 23:03:50 +00001170 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001171
Anders Carlssondfab34a2008-02-05 23:03:50 +00001172 SourceLocation RParenLoc;
1173 if (Tok.is(tok::r_paren)) {
1174 // We have a simple asm expression
1175 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001176
Anders Carlssondfab34a2008-02-05 23:03:50 +00001177 RParenLoc = ConsumeParen();
1178 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001179 // Parse Outputs, 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();
1182
Anders Carlssondfab34a2008-02-05 23:03:50 +00001183 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001184
Anders Carlssondfab34a2008-02-05 23:03:50 +00001185 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001186 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001187 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001188
Anders Carlssondfab34a2008-02-05 23:03:50 +00001189 assert(Names.size() == Constraints.size() &&
1190 Constraints.size() == Exprs.size()
1191 && "Input operand size mismatch!");
1192
1193 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001194
Anders Carlssondfab34a2008-02-05 23:03:50 +00001195 // Parse the clobbers, if present.
1196 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001197 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001198
Anders Carlssondfab34a2008-02-05 23:03:50 +00001199 // Parse the asm-string list for clobbers.
1200 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001201 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001202
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001203 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001204 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001205
1206 Clobbers.push_back(Clobber.release());
1207
Anders Carlssondfab34a2008-02-05 23:03:50 +00001208 if (Tok.isNot(tok::comma)) break;
1209 ConsumeToken();
1210 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001212
Anders Carlssondfab34a2008-02-05 23:03:50 +00001213 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001215
Sebastian Redl3037ed02009-01-18 16:53:17 +00001216 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1217 NumOutputs, NumInputs, &Names[0],
Sebastian Redlf512e822009-01-18 18:03:53 +00001218 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001219 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001220 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001221}
1222
1223/// ParseAsmOperands - Parse the asm-operands production as used by
1224/// asm-statement. We also parse a leading ':' token. If the leading colon is
1225/// not present, we do not parse anything.
1226///
1227/// [GNU] asm-operands:
1228/// asm-operand
1229/// asm-operands ',' asm-operand
1230///
1231/// [GNU] asm-operand:
1232/// asm-string-literal '(' expression ')'
1233/// '[' identifier ']' asm-string-literal '(' expression ')'
1234///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001235bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001236 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1237 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001239 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 ConsumeToken();
1241
1242 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001243 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001244 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001245
Anders Carlssonb235fc22007-11-22 01:36:19 +00001246 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001248 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 SourceLocation Loc = ConsumeBracket();
1250
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001251 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 Diag(Tok, diag::err_expected_ident);
1253 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001254 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 }
Chris Lattner69efba72007-10-29 04:06:22 +00001256
Anders Carlssonb235fc22007-11-22 01:36:19 +00001257 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001258 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001259
1260 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001262 } else
1263 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001264
Sebastian Redleffa8d12008-12-10 00:02:53 +00001265 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001266 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001267 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001268 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001269 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001270 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001271
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001272 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001273 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001275 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001277
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001279 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001280 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001282 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001284 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001286 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 ConsumeToken();
1288 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001289
1290 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001291}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001292
Chris Lattnerb28317a2009-03-28 19:18:32 +00001293Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001294 assert(Tok.is(tok::l_brace));
1295 SourceLocation LBraceLoc = Tok.getLocation();
1296
Chris Lattner49f28ca2009-03-05 08:00:35 +00001297 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1298 PP.getSourceManager(),
1299 "parsing function body");
Chris Lattner21ff9c92009-03-05 01:25:28 +00001300
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001301 // Do not enter a scope for the brace, as the arguments are in the same scope
1302 // (the function body) as the body itself. Instead, just read the statement
1303 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001304 OwningStmtResult FnBody(ParseCompoundStatementBody());
1305
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001306 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001307 if (FnBody.isInvalid())
Chris Lattner40e9bc82009-03-05 00:49:17 +00001308 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1309 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001310
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001311 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001312}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001313
1314/// ParseCXXTryBlock - Parse a C++ try-block.
1315///
1316/// try-block:
1317/// 'try' compound-statement handler-seq
1318///
1319/// handler-seq:
1320/// handler handler-seq[opt]
1321///
1322Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1323 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1324
1325 SourceLocation TryLoc = ConsumeToken();
1326 if (Tok.isNot(tok::l_brace))
1327 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1328 OwningStmtResult TryBlock(ParseCompoundStatement());
1329 if (TryBlock.isInvalid())
1330 return move(TryBlock);
1331
1332 StmtVector Handlers(Actions);
1333 if (Tok.isNot(tok::kw_catch))
1334 return StmtError(Diag(Tok, diag::err_expected_catch));
1335 while (Tok.is(tok::kw_catch)) {
1336 OwningStmtResult Handler(ParseCXXCatchBlock());
1337 if (!Handler.isInvalid())
1338 Handlers.push_back(Handler.release());
1339 }
1340 // Don't bother creating the full statement if we don't have any usable
1341 // handlers.
1342 if (Handlers.empty())
1343 return StmtError();
1344
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001345 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001346}
1347
1348/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1349///
1350/// handler:
1351/// 'catch' '(' exception-declaration ')' compound-statement
1352///
1353/// exception-declaration:
1354/// type-specifier-seq declarator
1355/// type-specifier-seq abstract-declarator
1356/// type-specifier-seq
1357/// '...'
1358///
1359Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1360 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1361
1362 SourceLocation CatchLoc = ConsumeToken();
1363
1364 SourceLocation LParenLoc = Tok.getLocation();
1365 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1366 return StmtError();
1367
1368 // C++ 3.3.2p3:
1369 // The name in a catch exception-declaration is local to the handler and
1370 // shall not be redeclared in the outermost block of the handler.
1371 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1372
1373 // exception-declaration is equivalent to '...' or a parameter-declaration
1374 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001375 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001376 if (Tok.isNot(tok::ellipsis)) {
1377 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001378 if (ParseCXXTypeSpecifierSeq(DS))
1379 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001380 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1381 ParseDeclarator(ExDecl);
1382 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1383 } else
1384 ConsumeToken();
1385
1386 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1387 return StmtError();
1388
1389 if (Tok.isNot(tok::l_brace))
1390 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1391
1392 OwningStmtResult Block(ParseCompoundStatement());
1393 if (Block.isInvalid())
1394 return move(Block);
1395
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001396 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001397}