blob: b87ede9388dbeb5d37d2fdef98d71d889a24e309 [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 Lattner97144fc2009-04-02 04:16:50 +0000103 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
104 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd);
105 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000106 }
107
108 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000110 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 }
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000112
113 // expression[opt] ';'
114 OwningExprResult Expr(ParseExpression());
115 if (Expr.isInvalid()) {
116 // If the expression is invalid, skip ahead to the next semicolon. Not
117 // doing this opens us up to the possibility of infinite loops if
118 // ParseExpression does not consume any tokens.
119 SkipUntil(tok::semi);
120 return StmtError();
121 }
122 // Otherwise, eat the semicolon.
123 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
124 return Actions.ActOnExprStmt(move(Expr));
125 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 case tok::kw_case: // C99 6.8.1: labeled-statement
128 return ParseCaseStatement();
129 case tok::kw_default: // C99 6.8.1: labeled-statement
130 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 case tok::l_brace: // C99 6.8.2: compound-statement
133 return ParseCompoundStatement();
134 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000135 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 case tok::kw_if: // C99 6.8.4.1: if-statement
138 return ParseIfStatement();
139 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000140 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000143 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 case tok::kw_do: // C99 6.8.5.2: do-statement
145 Res = ParseDoStatement();
146 SemiError = "do/while loop";
147 break;
148 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000149 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000150
151 case tok::kw_goto: // C99 6.8.6.1: goto-statement
152 Res = ParseGotoStatement();
153 SemiError = "goto statement";
154 break;
155 case tok::kw_continue: // C99 6.8.6.2: continue-statement
156 Res = ParseContinueStatement();
157 SemiError = "continue statement";
158 break;
159 case tok::kw_break: // C99 6.8.6.3: break-statement
160 Res = ParseBreakStatement();
161 SemiError = "break statement";
162 break;
163 case tok::kw_return: // C99 6.8.6.4: return-statement
164 Res = ParseReturnStatement();
165 SemiError = "return statement";
166 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000167
Sebastian Redla0fd8652008-12-21 16:41:36 +0000168 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000169 bool msAsm = false;
170 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000171 if (msAsm) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 SemiError = "asm statement";
173 break;
174 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000175
Sebastian Redla0fd8652008-12-21 16:41:36 +0000176 case tok::kw_try: // C++ 15: try-block
177 return ParseCXXTryBlock();
178 }
179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000181 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000183 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000184 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000185 // Skip until we see a } or ;, but don't eat it.
186 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000188 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000189}
190
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000191/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000192///
193/// labeled-statement:
194/// identifier ':' statement
195/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000196///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000197Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000198 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
199 "Not an identifier!");
200
201 Token IdentTok = Tok; // Save the whole token.
202 ConsumeToken(); // eat the identifier.
203
204 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000205
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000206 // identifier ':' statement
207 SourceLocation ColonLoc = ConsumeToken();
208
209 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210 Action::AttrTy *AttrList = 0;
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000211 if (Tok.is(tok::kw___attribute))
212 // TODO: save these somewhere.
213 AttrList = ParseAttributes();
214
Sebastian Redl61364dd2008-12-11 19:30:53 +0000215 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000216
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000217 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000218 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000219 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000220
Sebastian Redlde307472009-01-11 00:38:46 +0000221 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
222 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000223 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000224}
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
226/// ParseCaseStatement
227/// labeled-statement:
228/// 'case' constant-expression ':' statement
229/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
230///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000231Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000232 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattner24e1e702009-03-04 04:23:07 +0000233
234 // It is very very common for code to contain many case statements recursively
235 // nested, as in (but usually without indentation):
236 // case 1:
237 // case 2:
238 // case 3:
239 // case 4:
240 // case 5: etc.
241 //
242 // Parsing this naively works, but is both inefficient and can cause us to run
243 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000244 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000245 // but all the grossness is constrained to ParseCaseStatement (and some
246 // wierdness in the actions), so this is just local grossness :).
247
248 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
249 // example above.
250 OwningStmtResult TopLevelCase(Actions, true);
251
252 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
253 // gets updated each time a new case is parsed, and whose body is unset so
254 // far. When parsing 'case 4', this is the 'case 3' node.
255 StmtTy *DeepestParsedCaseStmt = 0;
256
257 // While we have case statements, eat and stack them.
258 do {
259 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
260
261 OwningExprResult LHS(ParseConstantExpression());
262 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000264 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000266
Chris Lattner24e1e702009-03-04 04:23:07 +0000267 // GNU case range extension.
268 SourceLocation DotDotDotLoc;
269 OwningExprResult RHS(Actions);
270 if (Tok.is(tok::ellipsis)) {
271 Diag(Tok, diag::ext_gnu_case_range);
272 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000273
Chris Lattner24e1e702009-03-04 04:23:07 +0000274 RHS = ParseConstantExpression();
275 if (RHS.isInvalid()) {
276 SkipUntil(tok::colon);
277 return StmtError();
278 }
279 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000280
Chris Lattner24e1e702009-03-04 04:23:07 +0000281 if (Tok.isNot(tok::colon)) {
282 Diag(Tok, diag::err_expected_colon_after) << "'case'";
283 SkipUntil(tok::colon);
284 return StmtError();
285 }
286
287 SourceLocation ColonLoc = ConsumeToken();
288
289 OwningStmtResult Case =
290 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
291 move(RHS), ColonLoc);
292
293 // If we had a sema error parsing this case, then just ignore it and
294 // continue parsing the sub-stmt.
295 if (Case.isInvalid()) {
296 if (TopLevelCase.isInvalid()) // No parsed case stmts.
297 return ParseStatement();
298 // Otherwise, just don't add it as a nested case.
299 } else {
300 // If this is the first case statement we parsed, it becomes TopLevelCase.
301 // Otherwise we link it into the current chain.
302 StmtTy *NextDeepest = Case.get();
303 if (TopLevelCase.isInvalid())
304 TopLevelCase = move(Case);
305 else
306 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
307 DeepestParsedCaseStmt = NextDeepest;
308 }
309
310 // Handle all case statements.
311 } while (Tok.is(tok::kw_case));
312
313 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
314
315 // If we found a non-case statement, start by parsing it.
316 OwningStmtResult SubStmt(Actions);
317
318 if (Tok.isNot(tok::r_brace)) {
319 SubStmt = ParseStatement();
320 } else {
321 // Nicely diagnose the common error "switch (X) { case 4: }", which is
322 // not valid.
323 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000325 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 }
Chris Lattner24e1e702009-03-04 04:23:07 +0000327
328 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000329 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000330 SubStmt = Actions.ActOnNullStmt(SourceLocation());
331
332 // Install the body into the most deeply-nested case.
333 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000334
Chris Lattner24e1e702009-03-04 04:23:07 +0000335 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000336 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000337}
338
339/// ParseDefaultStatement
340/// labeled-statement:
341/// 'default' ':' statement
342/// Note that this does not parse the 'statement' at the end.
343///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000344Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000345 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
347
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000348 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000349 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000351 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000357 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000359 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 }
361
Sebastian Redl61364dd2008-12-11 19:30:53 +0000362 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000363 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000364 return StmtError();
365
Sebastian Redl117054a2008-12-28 16:13:43 +0000366 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000367 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000368}
369
370
371/// ParseCompoundStatement - Parse a "{}" block.
372///
373/// compound-statement: [C99 6.8.2]
374/// { block-item-list[opt] }
375/// [GNU] { label-declarations block-item-list } [TODO]
376///
377/// block-item-list:
378/// block-item
379/// block-item-list block-item
380///
381/// block-item:
382/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000383/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000384/// statement
385/// [OMP] openmp-directive [TODO]
386///
387/// [GNU] label-declarations:
388/// [GNU] label-declaration
389/// [GNU] label-declarations label-declaration
390///
391/// [GNU] label-declaration:
392/// [GNU] '__label__' identifier-list ';'
393///
394/// [OMP] openmp-directive: [TODO]
395/// [OMP] barrier-directive
396/// [OMP] flush-directive
397///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000398Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000399 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000400
Chris Lattner31e05722007-08-26 06:24:45 +0000401 // Enter a scope to hold everything within the compound stmt. Compound
402 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000403 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000404
405 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000406 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000407}
408
409
410/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000411/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000412/// consume the '}' at the end of the block. It does not manipulate the scope
413/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000414Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerae50fa02009-03-05 00:00:31 +0000415 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
416 Tok.getLocation(),
417 "in compound statement ('{}')");
418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
420
421 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000422 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000423
424 typedef StmtVector StmtsTy;
425 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000426 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000427 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000428 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000429 R = ParseStatementOrDeclaration(false);
430 } else {
431 // __extension__ can start declarations and it can also be a unary
432 // operator for expressions. Consume multiple __extension__ markers here
433 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000434 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000435 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000436 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000437 ConsumeToken();
438
Chris Lattner043a0b52008-03-13 06:32:11 +0000439 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000440 ExtensionRAIIObject O(Diags); // Use RAII to do this.
441
Chris Lattner45a566c2007-08-27 01:01:57 +0000442 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000443 if (isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000444 // FIXME: Save the __extension__ on the decl as a node somehow?
445 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
446 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext,DeclEnd);
447 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000448 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000449 // Otherwise this was a unary __extension__ marker.
450 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000451
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000452 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000453 SkipUntil(tok::semi);
454 continue;
455 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000456
Chris Lattner39146d62008-10-20 06:51:33 +0000457 // Eat the semicolon at the end of stmt and convert the expr into a
458 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000459 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000460 R = Actions.ActOnExprStmt(move(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000461 }
462 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000463
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000464 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000465 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000469 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000471 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000473
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000475 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000476 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000477}
478
Chris Lattner15ff1112008-12-12 06:31:07 +0000479/// ParseParenExprOrCondition:
480/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000481/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000482///
483/// This function parses and performs error recovery on the specified condition
484/// or expression (depending on whether we're in C++ or C mode). This function
485/// goes out of its way to recover well. It returns true if there was a parser
486/// error (the right paren couldn't be found), which indicates that the caller
487/// should try to recover harder. It returns false if the condition is
488/// successfully parsed. Note that a successful parse can still have semantic
489/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000490bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
491 bool OnlyAllowCondition) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000492 SourceLocation LParenLoc = ConsumeParen();
493
494 if (getLang().CPlusPlus)
495 CondExp = ParseCXXCondition();
496 else
497 CondExp = ParseExpression();
498
499 // If the parser was confused by the condition and we don't have a ')', try to
500 // recover by skipping ahead to a semi and bailing out. If condexp is
501 // semantically invalid but we have well formed code, keep going.
502 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
503 SkipUntil(tok::semi);
504 // Skipping may have stopped if it found the containing ')'. If so, we can
505 // continue parsing the if statement.
506 if (Tok.isNot(tok::r_paren))
507 return true;
508 }
509
510 // Otherwise the condition is valid or the rparen is present.
511 MatchRHSPunctuation(tok::r_paren, LParenLoc);
512 return false;
513}
514
515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516/// ParseIfStatement
517/// if-statement: [C99 6.8.4.1]
518/// 'if' '(' expression ')' statement
519/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000520/// [C++] 'if' '(' condition ')' statement
521/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000522///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000523Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000524 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
526
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000527 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000528 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000530 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000532
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000533 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
534
Chris Lattner22153252007-08-26 23:08:06 +0000535 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
536 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000537 //
538 // C++ 6.4p3:
539 // A name introduced by a declaration in a condition is in scope from its
540 // point of declaration until the end of the substatements controlled by the
541 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000542 // C++ 3.3.2p4:
543 // Names declared in the for-init-statement, and in the condition of if,
544 // while, for, and switch statements are local to the if, while, for, or
545 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000546 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000547 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000550 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000551 if (ParseParenExprOrCondition(CondExp))
552 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000553
Chris Lattner0ecea032007-08-22 05:28:50 +0000554 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000555 // there is no compound stmt. C90 does not have this clause. We only do this
556 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000557 //
558 // C++ 6.4p1:
559 // The substatement in a selection-statement (each substatement, in the else
560 // form of the if statement) implicitly defines a local scope.
561 //
562 // For C++ we create a scope for the condition and a new scope for
563 // substatements because:
564 // -When the 'then' scope exits, we want the condition declaration to still be
565 // active for the 'else' scope too.
566 // -Sema will detect name clashes by considering declarations of a
567 // 'ControlScope' as part of its direct subscope.
568 // -If we wanted the condition and substatement to be in the same scope, we
569 // would have to notify ParseStatement not to create a new scope. It's
570 // simpler to let it create a new scope.
571 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000572 ParseScope InnerScope(this, Scope::DeclScope,
573 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000574
Chris Lattnerb96728d2007-10-29 05:08:52 +0000575 // Read the 'then' stmt.
576 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000577 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
Chris Lattnera36ce712007-08-22 05:16:28 +0000579 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000580 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000581
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 // If it has an else, parse it.
583 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000584 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000585 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000586
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000587 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000589
Chris Lattner0ecea032007-08-22 05:28:50 +0000590 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000591 // there is no compound stmt. C90 does not have this clause. We only do
592 // this if the body isn't a compound statement to avoid push/pop in common
593 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000594 //
595 // C++ 6.4p1:
596 // The substatement in a selection-statement (each substatement, in the else
597 // form of the if statement) implicitly defines a local scope.
598 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000599 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000600 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000601
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000602 bool WithinElse = CurScope->isWithinElse();
603 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000604 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000606 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000607
608 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000609 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000611
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000612 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000613
614 // If the condition was invalid, discard the if statement. We could recover
615 // better by replacing it with a valid expr, but don't do that yet.
616 if (CondExp.isInvalid())
617 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000618
Chris Lattnerb96728d2007-10-29 05:08:52 +0000619 // If the then or else stmt is invalid and the other is valid (and present),
620 // make turn the invalid one into a null stmt to avoid dropping the other
621 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000622 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
623 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
624 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000625 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000626 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000627 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000628
Chris Lattnerb96728d2007-10-29 05:08:52 +0000629 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000630 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000631 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000632 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000633 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000634
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000635 return Actions.ActOnIfStmt(IfLoc, move(CondExp), move(ThenStmt),
636 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000637}
638
639/// ParseSwitchStatement
640/// switch-statement:
641/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000642/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000643Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000644 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
646
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000647 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000648 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000650 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 }
Chris Lattner22153252007-08-26 23:08:06 +0000652
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000653 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
654
Chris Lattner22153252007-08-26 23:08:06 +0000655 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
656 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000657 //
658 // C++ 6.4p3:
659 // A name introduced by a declaration in a condition is in scope from its
660 // point of declaration until the end of the substatements controlled by the
661 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000662 // C++ 3.3.2p4:
663 // Names declared in the for-init-statement, and in the condition of if,
664 // while, for, and switch statements are local to the if, while, for, or
665 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000666 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000667 unsigned ScopeFlags = Scope::BreakScope;
668 if (C99orCXX)
669 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000670 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000671
672 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000673 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000674 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000675 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000676
677 OwningStmtResult Switch(Actions);
678 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000679 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000680
Chris Lattner0ecea032007-08-22 05:28:50 +0000681 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000682 // there is no compound stmt. C90 does not have this clause. We only do this
683 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000684 //
685 // C++ 6.4p1:
686 // The substatement in a selection-statement (each substatement, in the else
687 // form of the if statement) implicitly defines a local scope.
688 //
689 // See comments in ParseIfStatement for why we create a scope for the
690 // condition and a new scope for substatement in C++.
691 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000692 ParseScope InnerScope(this, Scope::DeclScope,
693 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000696 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000697
Chris Lattner0ecea032007-08-22 05:28:50 +0000698 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000699 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000700
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000701 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000702 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000703 // FIXME: Remove the case statement list from the Switch statement.
704 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000705
706 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000707
Chris Lattner15ff1112008-12-12 06:31:07 +0000708 if (Cond.isInvalid())
709 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000710
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000711 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713
714/// ParseWhileStatement
715/// while-statement: [C99 6.8.5.1]
716/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000717/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000718Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000719 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 SourceLocation WhileLoc = Tok.getLocation();
721 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000722
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000723 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000724 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000726 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000728
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000729 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
730
Chris Lattner22153252007-08-26 23:08:06 +0000731 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
732 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000733 //
734 // C++ 6.4p3:
735 // A name introduced by a declaration in a condition is in scope from its
736 // point of declaration until the end of the substatements controlled by the
737 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000738 // C++ 3.3.2p4:
739 // Names declared in the for-init-statement, and in the condition of if,
740 // while, for, and switch statements are local to the if, while, for, or
741 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000742 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000743 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000744 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000745 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
746 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000747 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000748 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
749 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000750
751 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000752 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000753 if (ParseParenExprOrCondition(Cond))
754 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000755
Chris Lattner0ecea032007-08-22 05:28:50 +0000756 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000757 // there is no compound stmt. C90 does not have this clause. We only do this
758 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000759 //
760 // C++ 6.5p2:
761 // The substatement in an iteration-statement implicitly defines a local scope
762 // which is entered and exited each time through the loop.
763 //
764 // See comments in ParseIfStatement for why we create a scope for the
765 // condition and a new scope for substatement in C++.
766 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000767 ParseScope InnerScope(this, Scope::DeclScope,
768 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000771 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000772
Chris Lattner0ecea032007-08-22 05:28:50 +0000773 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000774 InnerScope.Exit();
775 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000776
777 if (Cond.isInvalid() || Body.isInvalid())
778 return StmtError();
779
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000780 return Actions.ActOnWhileStmt(WhileLoc, move(Cond), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000781}
782
783/// ParseDoStatement
784/// do-statement: [C99 6.8.5.2]
785/// 'do' statement 'while' '(' expression ')' ';'
786/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000787Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000788 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000790
Chris Lattner22153252007-08-26 23:08:06 +0000791 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
792 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000793 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000794 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000795 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000796 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000797 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000798
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000799 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000800
Chris Lattner0ecea032007-08-22 05:28:50 +0000801 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000802 // there is no compound stmt. C90 does not have this clause. We only do this
803 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000804 //
805 // C++ 6.5p2:
806 // The substatement in an iteration-statement implicitly defines a local scope
807 // which is entered and exited each time through the loop.
808 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000809 ParseScope InnerScope(this, Scope::DeclScope,
810 (getLang().C99 || getLang().CPlusPlus) &&
811 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000812
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000814 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000815
Chris Lattner0ecea032007-08-22 05:28:50 +0000816 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000817 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000818
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000819 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000820 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000821 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000822 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000823 SkipUntil(tok::semi, false, true);
824 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000825 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 }
827 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000828
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000829 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000830 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000831 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000832 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000834
Chris Lattnerff871fb2008-12-12 06:35:28 +0000835 // Parse the parenthesized condition.
836 OwningExprResult Cond(Actions);
837 ParseParenExprOrCondition(Cond, true);
838
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000839 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000840
Sebastian Redl9a920342008-12-11 19:48:14 +0000841 if (Cond.isInvalid() || Body.isInvalid())
842 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000843
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000844 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000845}
846
847/// ParseForStatement
848/// for-statement: [C99 6.8.5.3]
849/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
850/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000851/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
852/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000853/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
854/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000855///
856/// [C++] for-init-statement:
857/// [C++] expression-statement
858/// [C++] simple-declaration
859///
Sebastian Redl9a920342008-12-11 19:48:14 +0000860Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000861 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000863
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000864 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000865 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000867 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000869
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000870 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
871
Chris Lattner22153252007-08-26 23:08:06 +0000872 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
873 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000874 //
875 // C++ 6.4p3:
876 // A name introduced by a declaration in a condition is in scope from its
877 // point of declaration until the end of the substatements controlled by the
878 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000879 // C++ 3.3.2p4:
880 // Names declared in the for-init-statement, and in the condition of if,
881 // while, for, and switch statements are local to the if, while, for, or
882 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000883 // C++ 6.5.3p1:
884 // Names declared in the for-init-statement are in the same declarative-region
885 // as those declared in the condition.
886 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000887 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000888 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000889 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
890 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000891 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000892 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
893
894 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000895
896 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000897 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000898
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000899 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000900 OwningStmtResult FirstPart(Actions);
901 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000904 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 // no first part, eat the ';'.
906 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000907 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000909 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000911
Chris Lattner97144fc2009-04-02 04:16:50 +0000912 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
913 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
914 false);
Chris Lattnercd147752009-03-29 17:27:48 +0000915 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
916
917 if (Tok.is(tok::semi)) { // for (int x = 4;
918 ConsumeToken();
919 } else if ((ForEach = isTokIdentifier_in())) {
920 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000921 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000922 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000923 } else {
924 Diag(Tok, diag::err_expected_semi_for);
925 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000926 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 } else {
928 Value = ParseExpression();
929
930 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000931 if (!Value.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000932 FirstPart = Actions.ActOnExprStmt(move(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000933
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000934 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000936 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000937 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000938 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000939 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000940 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 SkipUntil(tok::semi);
942 }
943 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000944 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000945 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000946 // Parse the second part of the for specifier.
947 if (Tok.is(tok::semi)) { // for (...;;
948 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000949 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000950 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000951 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000952
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000953 if (Tok.is(tok::semi)) {
954 ConsumeToken();
955 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000956 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000957 SkipUntil(tok::semi);
958 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000959
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000960 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000961 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000962 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 // Match the ')'.
965 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000966
Chris Lattner0ecea032007-08-22 05:28:50 +0000967 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000968 // there is no compound stmt. C90 does not have this clause. We only do this
969 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000970 //
971 // C++ 6.5p2:
972 // The substatement in an iteration-statement implicitly defines a local scope
973 // which is entered and exited each time through the loop.
974 //
975 // See comments in ParseIfStatement for why we create a scope for
976 // for-init-statement/condition and a new scope for substatement in C++.
977 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000978 ParseScope InnerScope(this, Scope::DeclScope,
979 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000980
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000982 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000983
Chris Lattner0ecea032007-08-22 05:28:50 +0000984 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000985 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000986
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000988 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000989
990 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000991 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000992
993 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000994 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
995 move(SecondPart), move(ThirdPart),
996 RParenLoc, move(Body));
Chris Lattner682bf922009-03-29 16:50:03 +0000997
998 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
999 move(FirstPart),
1000 move(SecondPart),
1001 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001002}
1003
1004/// ParseGotoStatement
1005/// jump-statement:
1006/// 'goto' identifier ';'
1007/// [GNU] 'goto' '*' expression ';'
1008///
1009/// Note: this lets the caller parse the end ';'.
1010///
Sebastian Redl9a920342008-12-11 19:48:14 +00001011Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001012 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001014
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001015 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001016 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001017 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 Tok.getIdentifierInfo());
1019 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001020 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 // GNU indirect goto extension.
1022 Diag(Tok, diag::ext_gnu_indirect_goto);
1023 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001024 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001025 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001027 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001029 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001030 } else {
1031 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001032 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001034
Sebastian Redl9a920342008-12-11 19:48:14 +00001035 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001036}
1037
1038/// ParseContinueStatement
1039/// jump-statement:
1040/// 'continue' ';'
1041///
1042/// Note: this lets the caller parse the end ';'.
1043///
Sebastian Redl9a920342008-12-11 19:48:14 +00001044Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001046 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001047}
1048
1049/// ParseBreakStatement
1050/// jump-statement:
1051/// 'break' ';'
1052///
1053/// Note: this lets the caller parse the end ';'.
1054///
Sebastian Redl9a920342008-12-11 19:48:14 +00001055Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001057 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058}
1059
1060/// ParseReturnStatement
1061/// jump-statement:
1062/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001063Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001064 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001066
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001067 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001068 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001070 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001072 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 }
1074 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001075 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001076}
1077
Steve Naroff5f8aa692008-02-11 23:15:56 +00001078/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1079/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001080Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001081 if (Tok.is(tok::l_brace)) {
1082 unsigned short savedBraceCount = BraceCount;
1083 do {
1084 ConsumeAnyToken();
1085 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1086 } else {
1087 // From the MS website: If used without braces, the __asm keyword means
1088 // that the rest of the line is an assembly-language statement.
1089 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001090 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001091 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001092 do {
1093 ConsumeAnyToken();
1094 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001095 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001096 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1097 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001098 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001099 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001100}
1101
Reid Spencer5f016e22007-07-11 17:01:13 +00001102/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001103/// asm-statement:
1104/// gnu-asm-statement
1105/// ms-asm-statement
1106///
1107/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001108/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1109///
1110/// [GNU] asm-argument:
1111/// asm-string-literal
1112/// asm-string-literal ':' asm-operands[opt]
1113/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1114/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1115/// ':' asm-clobbers
1116///
1117/// [GNU] asm-clobbers:
1118/// asm-string-literal
1119/// asm-clobbers ',' asm-string-literal
1120///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001121/// [MS] ms-asm-statement:
1122/// '__asm' assembly-instruction ';'[opt]
1123/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1124///
1125/// [MS] assembly-instruction-list:
1126/// assembly-instruction ';'[opt]
1127/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1128///
Sebastian Redl9a920342008-12-11 19:48:14 +00001129Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001130 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001131 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001132
Steve Naroff5f8aa692008-02-11 23:15:56 +00001133 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001134 msAsm = true;
1135 return FuzzyParseMicrosoftAsmStatement();
1136 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 DeclSpec DS;
1138 SourceLocation Loc = Tok.getLocation();
1139 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1142 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001143 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001145 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001148 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001149 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001150 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001151 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001153 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 }
1155 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001156
Sebastian Redleffa8d12008-12-10 00:02:53 +00001157 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001158 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001159 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001160
Anders Carlssonb235fc22007-11-22 01:36:19 +00001161 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001162 ExprVector Constraints(Actions);
1163 ExprVector Exprs(Actions);
1164 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165
Anders Carlssondfab34a2008-02-05 23:03:50 +00001166 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001167
Anders Carlssondfab34a2008-02-05 23:03:50 +00001168 SourceLocation RParenLoc;
1169 if (Tok.is(tok::r_paren)) {
1170 // We have a simple asm expression
1171 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001172
Anders Carlssondfab34a2008-02-05 23:03:50 +00001173 RParenLoc = ConsumeParen();
1174 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001175 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001176 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001177 return StmtError();
1178
Anders Carlssondfab34a2008-02-05 23:03:50 +00001179 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001180
Anders Carlssondfab34a2008-02-05 23:03:50 +00001181 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001182 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001183 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001184
Anders Carlssondfab34a2008-02-05 23:03:50 +00001185 assert(Names.size() == Constraints.size() &&
1186 Constraints.size() == Exprs.size()
1187 && "Input operand size mismatch!");
1188
1189 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001190
Anders Carlssondfab34a2008-02-05 23:03:50 +00001191 // Parse the clobbers, if present.
1192 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001193 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001194
Anders Carlssondfab34a2008-02-05 23:03:50 +00001195 // Parse the asm-string list for clobbers.
1196 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001197 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001198
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001199 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001200 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001201
1202 Clobbers.push_back(Clobber.release());
1203
Anders Carlssondfab34a2008-02-05 23:03:50 +00001204 if (Tok.isNot(tok::comma)) break;
1205 ConsumeToken();
1206 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001208
Anders Carlssondfab34a2008-02-05 23:03:50 +00001209 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001211
Sebastian Redl3037ed02009-01-18 16:53:17 +00001212 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1213 NumOutputs, NumInputs, &Names[0],
Sebastian Redlf512e822009-01-18 18:03:53 +00001214 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001215 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001216 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217}
1218
1219/// ParseAsmOperands - Parse the asm-operands production as used by
1220/// asm-statement. We also parse a leading ':' token. If the leading colon is
1221/// not present, we do not parse anything.
1222///
1223/// [GNU] asm-operands:
1224/// asm-operand
1225/// asm-operands ',' asm-operand
1226///
1227/// [GNU] asm-operand:
1228/// asm-string-literal '(' expression ')'
1229/// '[' identifier ']' asm-string-literal '(' expression ')'
1230///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001231bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001232 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1233 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001235 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 ConsumeToken();
1237
1238 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001239 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001240 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001241
Anders Carlssonb235fc22007-11-22 01:36:19 +00001242 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001244 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 SourceLocation Loc = ConsumeBracket();
1246
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001247 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 Diag(Tok, diag::err_expected_ident);
1249 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001250 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 }
Chris Lattner69efba72007-10-29 04:06:22 +00001252
Anders Carlssonb235fc22007-11-22 01:36:19 +00001253 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001254 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001255
1256 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001258 } else
1259 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001260
Sebastian Redleffa8d12008-12-10 00:02:53 +00001261 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001262 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001263 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001264 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001265 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001266 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001267
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001268 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001269 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001271 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001273
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001275 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001276 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001278 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001280 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001282 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 ConsumeToken();
1284 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001285
1286 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001287}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001288
Chris Lattnerb28317a2009-03-28 19:18:32 +00001289Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001290 assert(Tok.is(tok::l_brace));
1291 SourceLocation LBraceLoc = Tok.getLocation();
1292
Chris Lattner49f28ca2009-03-05 08:00:35 +00001293 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1294 PP.getSourceManager(),
1295 "parsing function body");
Chris Lattner21ff9c92009-03-05 01:25:28 +00001296
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001297 // Do not enter a scope for the brace, as the arguments are in the same scope
1298 // (the function body) as the body itself. Instead, just read the statement
1299 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001300 OwningStmtResult FnBody(ParseCompoundStatementBody());
1301
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001302 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001303 if (FnBody.isInvalid())
Chris Lattner40e9bc82009-03-05 00:49:17 +00001304 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1305 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001306
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001307 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001308}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001309
1310/// ParseCXXTryBlock - Parse a C++ try-block.
1311///
1312/// try-block:
1313/// 'try' compound-statement handler-seq
1314///
1315/// handler-seq:
1316/// handler handler-seq[opt]
1317///
1318Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1319 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1320
1321 SourceLocation TryLoc = ConsumeToken();
1322 if (Tok.isNot(tok::l_brace))
1323 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1324 OwningStmtResult TryBlock(ParseCompoundStatement());
1325 if (TryBlock.isInvalid())
1326 return move(TryBlock);
1327
1328 StmtVector Handlers(Actions);
1329 if (Tok.isNot(tok::kw_catch))
1330 return StmtError(Diag(Tok, diag::err_expected_catch));
1331 while (Tok.is(tok::kw_catch)) {
1332 OwningStmtResult Handler(ParseCXXCatchBlock());
1333 if (!Handler.isInvalid())
1334 Handlers.push_back(Handler.release());
1335 }
1336 // Don't bother creating the full statement if we don't have any usable
1337 // handlers.
1338 if (Handlers.empty())
1339 return StmtError();
1340
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001341 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001342}
1343
1344/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1345///
1346/// handler:
1347/// 'catch' '(' exception-declaration ')' compound-statement
1348///
1349/// exception-declaration:
1350/// type-specifier-seq declarator
1351/// type-specifier-seq abstract-declarator
1352/// type-specifier-seq
1353/// '...'
1354///
1355Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1356 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1357
1358 SourceLocation CatchLoc = ConsumeToken();
1359
1360 SourceLocation LParenLoc = Tok.getLocation();
1361 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1362 return StmtError();
1363
1364 // C++ 3.3.2p3:
1365 // The name in a catch exception-declaration is local to the handler and
1366 // shall not be redeclared in the outermost block of the handler.
1367 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1368
1369 // exception-declaration is equivalent to '...' or a parameter-declaration
1370 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001371 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001372 if (Tok.isNot(tok::ellipsis)) {
1373 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001374 if (ParseCXXTypeSpecifierSeq(DS))
1375 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001376 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1377 ParseDeclarator(ExDecl);
1378 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1379 } else
1380 ConsumeToken();
1381
1382 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1383 return StmtError();
1384
1385 if (Tok.isNot(tok::l_brace))
1386 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1387
1388 OwningStmtResult Block(ParseCompoundStatement());
1389 if (Block.isInvalid())
1390 return move(Block);
1391
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001392 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001393}