blob: b11ffbe82e492af4edaf339788d5f2e7ddc72a30 [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.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000962 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000963 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 // Match the ')'.
966 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000967
Chris Lattner0ecea032007-08-22 05:28:50 +0000968 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000969 // there is no compound stmt. C90 does not have this clause. We only do this
970 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000971 //
972 // C++ 6.5p2:
973 // The substatement in an iteration-statement implicitly defines a local scope
974 // which is entered and exited each time through the loop.
975 //
976 // See comments in ParseIfStatement for why we create a scope for
977 // for-init-statement/condition and a new scope for substatement in C++.
978 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000979 ParseScope InnerScope(this, Scope::DeclScope,
980 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000981
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000983 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000984
Chris Lattner0ecea032007-08-22 05:28:50 +0000985 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000986 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000987
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000989 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000990
991 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000992 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000993
994 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000995 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
996 move(SecondPart), move(ThirdPart),
997 RParenLoc, move(Body));
Chris Lattner682bf922009-03-29 16:50:03 +0000998
999 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1000 move(FirstPart),
1001 move(SecondPart),
1002 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001003}
1004
1005/// ParseGotoStatement
1006/// jump-statement:
1007/// 'goto' identifier ';'
1008/// [GNU] 'goto' '*' expression ';'
1009///
1010/// Note: this lets the caller parse the end ';'.
1011///
Sebastian Redl9a920342008-12-11 19:48:14 +00001012Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001013 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001015
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001016 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001017 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001018 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 Tok.getIdentifierInfo());
1020 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001021 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 // GNU indirect goto extension.
1023 Diag(Tok, diag::ext_gnu_indirect_goto);
1024 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001025 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001026 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001028 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001030 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001031 } else {
1032 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001033 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001035
Sebastian Redl9a920342008-12-11 19:48:14 +00001036 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001037}
1038
1039/// ParseContinueStatement
1040/// jump-statement:
1041/// 'continue' ';'
1042///
1043/// Note: this lets the caller parse the end ';'.
1044///
Sebastian Redl9a920342008-12-11 19:48:14 +00001045Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001047 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001048}
1049
1050/// ParseBreakStatement
1051/// jump-statement:
1052/// 'break' ';'
1053///
1054/// Note: this lets the caller parse the end ';'.
1055///
Sebastian Redl9a920342008-12-11 19:48:14 +00001056Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001058 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001059}
1060
1061/// ParseReturnStatement
1062/// jump-statement:
1063/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001064Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001065 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001067
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001068 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001069 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001071 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001073 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 }
1075 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001076 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001077}
1078
Steve Naroff5f8aa692008-02-11 23:15:56 +00001079/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1080/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001081Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001082 if (Tok.is(tok::l_brace)) {
1083 unsigned short savedBraceCount = BraceCount;
1084 do {
1085 ConsumeAnyToken();
1086 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1087 } else {
1088 // From the MS website: If used without braces, the __asm keyword means
1089 // that the rest of the line is an assembly-language statement.
1090 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001091 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001092 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001093 do {
1094 ConsumeAnyToken();
1095 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001096 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001097 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1098 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001099 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001100 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001101}
1102
Reid Spencer5f016e22007-07-11 17:01:13 +00001103/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001104/// asm-statement:
1105/// gnu-asm-statement
1106/// ms-asm-statement
1107///
1108/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001109/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1110///
1111/// [GNU] asm-argument:
1112/// asm-string-literal
1113/// asm-string-literal ':' asm-operands[opt]
1114/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1115/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1116/// ':' asm-clobbers
1117///
1118/// [GNU] asm-clobbers:
1119/// asm-string-literal
1120/// asm-clobbers ',' asm-string-literal
1121///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001122/// [MS] ms-asm-statement:
1123/// '__asm' assembly-instruction ';'[opt]
1124/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1125///
1126/// [MS] assembly-instruction-list:
1127/// assembly-instruction ';'[opt]
1128/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1129///
Sebastian Redl9a920342008-12-11 19:48:14 +00001130Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001131 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001132 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001133
Steve Naroff5f8aa692008-02-11 23:15:56 +00001134 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001135 msAsm = true;
1136 return FuzzyParseMicrosoftAsmStatement();
1137 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 DeclSpec DS;
1139 SourceLocation Loc = Tok.getLocation();
1140 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001141
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1143 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001144 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001146 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001147
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001149 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001150 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001151 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001152 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001154 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 }
1156 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001157
Sebastian Redleffa8d12008-12-10 00:02:53 +00001158 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001159 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001160 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001161
Anders Carlssonb235fc22007-11-22 01:36:19 +00001162 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001163 ExprVector Constraints(Actions);
1164 ExprVector Exprs(Actions);
1165 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001166
Anders Carlssondfab34a2008-02-05 23:03:50 +00001167 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001168
Anders Carlssondfab34a2008-02-05 23:03:50 +00001169 SourceLocation RParenLoc;
1170 if (Tok.is(tok::r_paren)) {
1171 // We have a simple asm expression
1172 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001173
Anders Carlssondfab34a2008-02-05 23:03:50 +00001174 RParenLoc = ConsumeParen();
1175 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001176 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001177 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001178 return StmtError();
1179
Anders Carlssondfab34a2008-02-05 23:03:50 +00001180 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001181
Anders Carlssondfab34a2008-02-05 23:03:50 +00001182 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001183 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001184 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001185
Anders Carlssondfab34a2008-02-05 23:03:50 +00001186 assert(Names.size() == Constraints.size() &&
1187 Constraints.size() == Exprs.size()
1188 && "Input operand size mismatch!");
1189
1190 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001191
Anders Carlssondfab34a2008-02-05 23:03:50 +00001192 // Parse the clobbers, if present.
1193 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001194 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001195
Anders Carlssondfab34a2008-02-05 23:03:50 +00001196 // Parse the asm-string list for clobbers.
1197 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001198 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001199
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001200 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001201 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001202
1203 Clobbers.push_back(Clobber.release());
1204
Anders Carlssondfab34a2008-02-05 23:03:50 +00001205 if (Tok.isNot(tok::comma)) break;
1206 ConsumeToken();
1207 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001209
Anders Carlssondfab34a2008-02-05 23:03:50 +00001210 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001212
Sebastian Redl3037ed02009-01-18 16:53:17 +00001213 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1214 NumOutputs, NumInputs, &Names[0],
Sebastian Redlf512e822009-01-18 18:03:53 +00001215 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001216 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001217 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001218}
1219
1220/// ParseAsmOperands - Parse the asm-operands production as used by
1221/// asm-statement. We also parse a leading ':' token. If the leading colon is
1222/// not present, we do not parse anything.
1223///
1224/// [GNU] asm-operands:
1225/// asm-operand
1226/// asm-operands ',' asm-operand
1227///
1228/// [GNU] asm-operand:
1229/// asm-string-literal '(' expression ')'
1230/// '[' identifier ']' asm-string-literal '(' expression ')'
1231///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001232bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001233 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1234 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001236 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 ConsumeToken();
1238
1239 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001240 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001241 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001242
Anders Carlssonb235fc22007-11-22 01:36:19 +00001243 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001245 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 SourceLocation Loc = ConsumeBracket();
1247
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001248 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 Diag(Tok, diag::err_expected_ident);
1250 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001251 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 }
Chris Lattner69efba72007-10-29 04:06:22 +00001253
Anders Carlssonb235fc22007-11-22 01:36:19 +00001254 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001255 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001256
1257 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001259 } else
1260 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001261
Sebastian Redleffa8d12008-12-10 00:02:53 +00001262 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001263 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001264 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001265 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001266 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001267 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001268
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001269 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001270 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001272 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001274
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001276 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001277 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001279 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001281 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001283 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 ConsumeToken();
1285 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001286
1287 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001288}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001289
Chris Lattnerb28317a2009-03-28 19:18:32 +00001290Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001291 assert(Tok.is(tok::l_brace));
1292 SourceLocation LBraceLoc = Tok.getLocation();
1293
Chris Lattner49f28ca2009-03-05 08:00:35 +00001294 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1295 PP.getSourceManager(),
1296 "parsing function body");
Chris Lattner21ff9c92009-03-05 01:25:28 +00001297
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001298 // Do not enter a scope for the brace, as the arguments are in the same scope
1299 // (the function body) as the body itself. Instead, just read the statement
1300 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001301 OwningStmtResult FnBody(ParseCompoundStatementBody());
1302
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001303 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001304 if (FnBody.isInvalid())
Chris Lattner40e9bc82009-03-05 00:49:17 +00001305 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1306 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001307
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001308 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001309}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001310
1311/// ParseCXXTryBlock - Parse a C++ try-block.
1312///
1313/// try-block:
1314/// 'try' compound-statement handler-seq
1315///
1316/// handler-seq:
1317/// handler handler-seq[opt]
1318///
1319Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1320 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1321
1322 SourceLocation TryLoc = ConsumeToken();
1323 if (Tok.isNot(tok::l_brace))
1324 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1325 OwningStmtResult TryBlock(ParseCompoundStatement());
1326 if (TryBlock.isInvalid())
1327 return move(TryBlock);
1328
1329 StmtVector Handlers(Actions);
1330 if (Tok.isNot(tok::kw_catch))
1331 return StmtError(Diag(Tok, diag::err_expected_catch));
1332 while (Tok.is(tok::kw_catch)) {
1333 OwningStmtResult Handler(ParseCXXCatchBlock());
1334 if (!Handler.isInvalid())
1335 Handlers.push_back(Handler.release());
1336 }
1337 // Don't bother creating the full statement if we don't have any usable
1338 // handlers.
1339 if (Handlers.empty())
1340 return StmtError();
1341
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001342 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001343}
1344
1345/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1346///
1347/// handler:
1348/// 'catch' '(' exception-declaration ')' compound-statement
1349///
1350/// exception-declaration:
1351/// type-specifier-seq declarator
1352/// type-specifier-seq abstract-declarator
1353/// type-specifier-seq
1354/// '...'
1355///
1356Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1357 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1358
1359 SourceLocation CatchLoc = ConsumeToken();
1360
1361 SourceLocation LParenLoc = Tok.getLocation();
1362 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1363 return StmtError();
1364
1365 // C++ 3.3.2p3:
1366 // The name in a catch exception-declaration is local to the handler and
1367 // shall not be redeclared in the outermost block of the handler.
1368 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1369
1370 // exception-declaration is equivalent to '...' or a parameter-declaration
1371 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001372 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001373 if (Tok.isNot(tok::ellipsis)) {
1374 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001375 if (ParseCXXTypeSpecifierSeq(DS))
1376 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001377 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1378 ParseDeclarator(ExDecl);
1379 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1380 } else
1381 ConsumeToken();
1382
1383 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1384 return StmtError();
1385
1386 if (Tok.isNot(tok::l_brace))
1387 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1388
1389 OwningStmtResult Block(ParseCompoundStatement());
1390 if (Block.isInvalid())
1391 return move(Block);
1392
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001393 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001394}