blob: 952c4c15f3bb3c3e0bb4747a2e28cbff8892ab88 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// [GNU] asm-statement
46/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
53/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
62/// expression-statement:
63/// expression[opt] ';'
64///
65/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
70/// [GNU] 'goto' '*' expression ';'
71///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
74/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000075///
Sebastian Redl61364dd2008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000078 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000080
Reid Spencer5f016e22007-07-11 17:01:13 +000081 // Cases in this switch statement should fall through if the parser expects
82 // the token to end in a semicolon (in which case SemiError should be set),
83 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000084 tok::TokenKind Kind = Tok.getKind();
85 SourceLocation AtLoc;
86 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000090 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +000093 case tok::identifier:
94 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
95 // identifier ':' statement
96 return ParseLabeledStatement();
97 }
98 // PASS THROUGH.
99
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000100 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000101 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000102 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
103 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd);
104 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000105 }
106
107 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000109 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 }
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000111
112 // expression[opt] ';'
113 OwningExprResult Expr(ParseExpression());
114 if (Expr.isInvalid()) {
115 // If the expression is invalid, skip ahead to the next semicolon. Not
116 // doing this opens us up to the possibility of infinite loops if
117 // ParseExpression does not consume any tokens.
118 SkipUntil(tok::semi);
119 return StmtError();
120 }
121 // Otherwise, eat the semicolon.
122 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000123 return Actions.ActOnExprStmt(Actions.FullExpr(Expr));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000124 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000125
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 case tok::kw_case: // C99 6.8.1: labeled-statement
127 return ParseCaseStatement();
128 case tok::kw_default: // C99 6.8.1: labeled-statement
129 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 case tok::l_brace: // C99 6.8.2: compound-statement
132 return ParseCompoundStatement();
133 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000134 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000135
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 case tok::kw_if: // C99 6.8.4.1: if-statement
137 return ParseIfStatement();
138 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000139 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000142 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case tok::kw_do: // C99 6.8.5.2: do-statement
144 Res = ParseDoStatement();
145 SemiError = "do/while loop";
146 break;
147 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000148 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000149
150 case tok::kw_goto: // C99 6.8.6.1: goto-statement
151 Res = ParseGotoStatement();
152 SemiError = "goto statement";
153 break;
154 case tok::kw_continue: // C99 6.8.6.2: continue-statement
155 Res = ParseContinueStatement();
156 SemiError = "continue statement";
157 break;
158 case tok::kw_break: // C99 6.8.6.3: break-statement
159 Res = ParseBreakStatement();
160 SemiError = "break statement";
161 break;
162 case tok::kw_return: // C99 6.8.6.4: return-statement
163 Res = ParseReturnStatement();
164 SemiError = "return statement";
165 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000166
Sebastian Redla0fd8652008-12-21 16:41:36 +0000167 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000168 bool msAsm = false;
169 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000170 if (msAsm) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 SemiError = "asm statement";
172 break;
173 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000174
Sebastian Redla0fd8652008-12-21 16:41:36 +0000175 case tok::kw_try: // C++ 15: try-block
176 return ParseCXXTryBlock();
177 }
178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000180 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000182 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000183 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000184 // Skip until we see a } or ;, but don't eat it.
185 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000187 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188}
189
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000190/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000191///
192/// labeled-statement:
193/// identifier ':' statement
194/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000195///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000196Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000197 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
198 "Not an identifier!");
199
200 Token IdentTok = Tok; // Save the whole token.
201 ConsumeToken(); // eat the identifier.
202
203 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000204
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000205 // identifier ':' statement
206 SourceLocation ColonLoc = ConsumeToken();
207
208 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000209 Action::AttrTy *AttrList = 0;
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000210 if (Tok.is(tok::kw___attribute))
211 // TODO: save these somewhere.
212 AttrList = ParseAttributes();
213
Sebastian Redl61364dd2008-12-11 19:30:53 +0000214 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000215
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000216 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000217 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000218 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000219
Sebastian Redlde307472009-01-11 00:38:46 +0000220 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
221 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000222 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000223}
Reid Spencer5f016e22007-07-11 17:01:13 +0000224
225/// ParseCaseStatement
226/// labeled-statement:
227/// 'case' constant-expression ':' statement
228/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
229///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000230Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000231 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattner24e1e702009-03-04 04:23:07 +0000232
233 // It is very very common for code to contain many case statements recursively
234 // nested, as in (but usually without indentation):
235 // case 1:
236 // case 2:
237 // case 3:
238 // case 4:
239 // case 5: etc.
240 //
241 // Parsing this naively works, but is both inefficient and can cause us to run
242 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000243 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000244 // but all the grossness is constrained to ParseCaseStatement (and some
245 // wierdness in the actions), so this is just local grossness :).
246
247 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
248 // example above.
249 OwningStmtResult TopLevelCase(Actions, true);
250
251 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
252 // gets updated each time a new case is parsed, and whose body is unset so
253 // far. When parsing 'case 4', this is the 'case 3' node.
254 StmtTy *DeepestParsedCaseStmt = 0;
255
256 // While we have case statements, eat and stack them.
257 do {
258 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
259
260 OwningExprResult LHS(ParseConstantExpression());
261 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000263 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000265
Chris Lattner24e1e702009-03-04 04:23:07 +0000266 // GNU case range extension.
267 SourceLocation DotDotDotLoc;
268 OwningExprResult RHS(Actions);
269 if (Tok.is(tok::ellipsis)) {
270 Diag(Tok, diag::ext_gnu_case_range);
271 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000272
Chris Lattner24e1e702009-03-04 04:23:07 +0000273 RHS = ParseConstantExpression();
274 if (RHS.isInvalid()) {
275 SkipUntil(tok::colon);
276 return StmtError();
277 }
278 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000279
Chris Lattner24e1e702009-03-04 04:23:07 +0000280 if (Tok.isNot(tok::colon)) {
281 Diag(Tok, diag::err_expected_colon_after) << "'case'";
282 SkipUntil(tok::colon);
283 return StmtError();
284 }
285
286 SourceLocation ColonLoc = ConsumeToken();
287
288 OwningStmtResult Case =
289 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
290 move(RHS), ColonLoc);
291
292 // If we had a sema error parsing this case, then just ignore it and
293 // continue parsing the sub-stmt.
294 if (Case.isInvalid()) {
295 if (TopLevelCase.isInvalid()) // No parsed case stmts.
296 return ParseStatement();
297 // Otherwise, just don't add it as a nested case.
298 } else {
299 // If this is the first case statement we parsed, it becomes TopLevelCase.
300 // Otherwise we link it into the current chain.
301 StmtTy *NextDeepest = Case.get();
302 if (TopLevelCase.isInvalid())
303 TopLevelCase = move(Case);
304 else
305 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
306 DeepestParsedCaseStmt = NextDeepest;
307 }
308
309 // Handle all case statements.
310 } while (Tok.is(tok::kw_case));
311
312 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
313
314 // If we found a non-case statement, start by parsing it.
315 OwningStmtResult SubStmt(Actions);
316
317 if (Tok.isNot(tok::r_brace)) {
318 SubStmt = ParseStatement();
319 } else {
320 // Nicely diagnose the common error "switch (X) { case 4: }", which is
321 // not valid.
322 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000324 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
Chris Lattner24e1e702009-03-04 04:23:07 +0000326
327 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000328 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000329 SubStmt = Actions.ActOnNullStmt(SourceLocation());
330
331 // Install the body into the most deeply-nested case.
332 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000333
Chris Lattner24e1e702009-03-04 04:23:07 +0000334 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000335 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000336}
337
338/// ParseDefaultStatement
339/// labeled-statement:
340/// 'default' ':' statement
341/// Note that this does not parse the 'statement' at the end.
342///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000343Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000344 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
346
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000347 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000348 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000350 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000356 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000358 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 }
360
Sebastian Redl61364dd2008-12-11 19:30:53 +0000361 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000362 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000363 return StmtError();
364
Sebastian Redl117054a2008-12-28 16:13:43 +0000365 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000366 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367}
368
369
370/// ParseCompoundStatement - Parse a "{}" block.
371///
372/// compound-statement: [C99 6.8.2]
373/// { block-item-list[opt] }
374/// [GNU] { label-declarations block-item-list } [TODO]
375///
376/// block-item-list:
377/// block-item
378/// block-item-list block-item
379///
380/// block-item:
381/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000382/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000383/// statement
384/// [OMP] openmp-directive [TODO]
385///
386/// [GNU] label-declarations:
387/// [GNU] label-declaration
388/// [GNU] label-declarations label-declaration
389///
390/// [GNU] label-declaration:
391/// [GNU] '__label__' identifier-list ';'
392///
393/// [OMP] openmp-directive: [TODO]
394/// [OMP] barrier-directive
395/// [OMP] flush-directive
396///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000397Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000398 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000399
Chris Lattner31e05722007-08-26 06:24:45 +0000400 // Enter a scope to hold everything within the compound stmt. Compound
401 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000402 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000403
404 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000405 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000406}
407
408
409/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000410/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000411/// consume the '}' at the end of the block. It does not manipulate the scope
412/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000413Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerae50fa02009-03-05 00:00:31 +0000414 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
415 Tok.getLocation(),
416 "in compound statement ('{}')");
417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
419
420 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000421 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000422
423 typedef StmtVector StmtsTy;
424 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000425 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000426 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000427 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000428 R = ParseStatementOrDeclaration(false);
429 } else {
430 // __extension__ can start declarations and it can also be a unary
431 // operator for expressions. Consume multiple __extension__ markers here
432 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000433 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000434 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000435 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000436 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000437
Chris Lattner45a566c2007-08-27 01:01:57 +0000438 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000439 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000440 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000441 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000442 ExtensionRAIIObject O(Diags);
443
Chris Lattner97144fc2009-04-02 04:16:50 +0000444 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
445 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext,DeclEnd);
446 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000447 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000448 // Otherwise this was a unary __extension__ marker.
449 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000450
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000451 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000452 SkipUntil(tok::semi);
453 continue;
454 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000455
Chris Lattner39146d62008-10-20 06:51:33 +0000456 // Eat the semicolon at the end of stmt and convert the expr into a
457 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000458 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000459 R = Actions.ActOnExprStmt(Actions.FullExpr(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000460 }
461 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000462
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000463 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000464 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000468 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000470 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000472
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000474 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000475 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000476}
477
Chris Lattner15ff1112008-12-12 06:31:07 +0000478/// ParseParenExprOrCondition:
479/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000480/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000481///
482/// This function parses and performs error recovery on the specified condition
483/// or expression (depending on whether we're in C++ or C mode). This function
484/// goes out of its way to recover well. It returns true if there was a parser
485/// error (the right paren couldn't be found), which indicates that the caller
486/// should try to recover harder. It returns false if the condition is
487/// successfully parsed. Note that a successful parse can still have semantic
488/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000489bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
490 bool OnlyAllowCondition) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000491 SourceLocation LParenLoc = ConsumeParen();
492
493 if (getLang().CPlusPlus)
494 CondExp = ParseCXXCondition();
495 else
496 CondExp = ParseExpression();
497
498 // If the parser was confused by the condition and we don't have a ')', try to
499 // recover by skipping ahead to a semi and bailing out. If condexp is
500 // semantically invalid but we have well formed code, keep going.
501 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
502 SkipUntil(tok::semi);
503 // Skipping may have stopped if it found the containing ')'. If so, we can
504 // continue parsing the if statement.
505 if (Tok.isNot(tok::r_paren))
506 return true;
507 }
508
509 // Otherwise the condition is valid or the rparen is present.
510 MatchRHSPunctuation(tok::r_paren, LParenLoc);
511 return false;
512}
513
514
Reid Spencer5f016e22007-07-11 17:01:13 +0000515/// ParseIfStatement
516/// if-statement: [C99 6.8.4.1]
517/// 'if' '(' expression ')' statement
518/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000519/// [C++] 'if' '(' condition ')' statement
520/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000521///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000522Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000523 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
525
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000526 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000527 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000529 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000531
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000532 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
533
Chris Lattner22153252007-08-26 23:08:06 +0000534 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
535 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000536 //
537 // C++ 6.4p3:
538 // A name introduced by a declaration in a condition is in scope from its
539 // point of declaration until the end of the substatements controlled by the
540 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000541 // C++ 3.3.2p4:
542 // Names declared in the for-init-statement, and in the condition of if,
543 // while, for, and switch statements are local to the if, while, for, or
544 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000545 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000546 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000547
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000549 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000550 if (ParseParenExprOrCondition(CondExp))
551 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000552
Chris Lattner0ecea032007-08-22 05:28:50 +0000553 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000554 // there is no compound stmt. C90 does not have this clause. We only do this
555 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000556 //
557 // C++ 6.4p1:
558 // The substatement in a selection-statement (each substatement, in the else
559 // form of the if statement) implicitly defines a local scope.
560 //
561 // For C++ we create a scope for the condition and a new scope for
562 // substatements because:
563 // -When the 'then' scope exits, we want the condition declaration to still be
564 // active for the 'else' scope too.
565 // -Sema will detect name clashes by considering declarations of a
566 // 'ControlScope' as part of its direct subscope.
567 // -If we wanted the condition and substatement to be in the same scope, we
568 // would have to notify ParseStatement not to create a new scope. It's
569 // simpler to let it create a new scope.
570 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000571 ParseScope InnerScope(this, Scope::DeclScope,
572 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000573
Chris Lattnerb96728d2007-10-29 05:08:52 +0000574 // Read the 'then' stmt.
575 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000576 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000577
Chris Lattnera36ce712007-08-22 05:16:28 +0000578 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000579 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000580
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 // If it has an else, parse it.
582 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000583 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000584 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000585
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000586 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000588
Chris Lattner0ecea032007-08-22 05:28:50 +0000589 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000590 // there is no compound stmt. C90 does not have this clause. We only do
591 // this if the body isn't a compound statement to avoid push/pop in common
592 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000593 //
594 // C++ 6.4p1:
595 // The substatement in a selection-statement (each substatement, in the else
596 // form of the if statement) implicitly defines a local scope.
597 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000598 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000599 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000600
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000601 bool WithinElse = CurScope->isWithinElse();
602 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000603 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000605 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000606
607 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000608 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000610
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000611 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000612
613 // If the condition was invalid, discard the if statement. We could recover
614 // better by replacing it with a valid expr, but don't do that yet.
615 if (CondExp.isInvalid())
616 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000617
Chris Lattnerb96728d2007-10-29 05:08:52 +0000618 // If the then or else stmt is invalid and the other is valid (and present),
619 // make turn the invalid one into a null stmt to avoid dropping the other
620 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000621 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
622 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
623 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000624 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000625 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000626 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000627
Chris Lattnerb96728d2007-10-29 05:08:52 +0000628 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000629 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000630 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000631 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000632 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000633
Anders Carlssona99fad82009-05-17 18:26:53 +0000634 return Actions.ActOnIfStmt(IfLoc, Actions.FullExpr(CondExp), move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000635 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000636}
637
638/// ParseSwitchStatement
639/// switch-statement:
640/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000641/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000642Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000643 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
645
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000646 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000647 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000649 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 }
Chris Lattner22153252007-08-26 23:08:06 +0000651
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000652 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
653
Chris Lattner22153252007-08-26 23:08:06 +0000654 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
655 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000656 //
657 // C++ 6.4p3:
658 // A name introduced by a declaration in a condition is in scope from its
659 // point of declaration until the end of the substatements controlled by the
660 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000661 // C++ 3.3.2p4:
662 // Names declared in the for-init-statement, and in the condition of if,
663 // while, for, and switch statements are local to the if, while, for, or
664 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000665 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000666 unsigned ScopeFlags = Scope::BreakScope;
667 if (C99orCXX)
668 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000669 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000670
671 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000672 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000673 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000674 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000675
676 OwningStmtResult Switch(Actions);
677 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000678 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000679
Chris Lattner0ecea032007-08-22 05:28:50 +0000680 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000681 // there is no compound stmt. C90 does not have this clause. We only do this
682 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000683 //
684 // C++ 6.4p1:
685 // The substatement in a selection-statement (each substatement, in the else
686 // form of the if statement) implicitly defines a local scope.
687 //
688 // See comments in ParseIfStatement for why we create a scope for the
689 // condition and a new scope for substatement in C++.
690 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000691 ParseScope InnerScope(this, Scope::DeclScope,
692 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000695 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000696
Chris Lattner0ecea032007-08-22 05:28:50 +0000697 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000698 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000699
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000700 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000701 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000702 // FIXME: Remove the case statement list from the Switch statement.
703 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000704
705 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000706
Chris Lattner15ff1112008-12-12 06:31:07 +0000707 if (Cond.isInvalid())
708 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000709
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000710 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000711}
712
713/// ParseWhileStatement
714/// while-statement: [C99 6.8.5.1]
715/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000716/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000717Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000718 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 SourceLocation WhileLoc = Tok.getLocation();
720 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000721
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000722 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000723 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000725 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000727
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000728 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
729
Chris Lattner22153252007-08-26 23:08:06 +0000730 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
731 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000732 //
733 // C++ 6.4p3:
734 // A name introduced by a declaration in a condition is in scope from its
735 // point of declaration until the end of the substatements controlled by the
736 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000737 // C++ 3.3.2p4:
738 // Names declared in the for-init-statement, and in the condition of if,
739 // while, for, and switch statements are local to the if, while, for, or
740 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000741 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000742 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000743 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000744 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
745 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000746 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000747 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
748 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749
750 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000751 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000752 if (ParseParenExprOrCondition(Cond))
753 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000754
Chris Lattner0ecea032007-08-22 05:28:50 +0000755 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000756 // there is no compound stmt. C90 does not have this clause. We only do this
757 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000758 //
759 // C++ 6.5p2:
760 // The substatement in an iteration-statement implicitly defines a local scope
761 // which is entered and exited each time through the loop.
762 //
763 // See comments in ParseIfStatement for why we create a scope for the
764 // condition and a new scope for substatement in C++.
765 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000766 ParseScope InnerScope(this, Scope::DeclScope,
767 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000768
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000770 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000771
Chris Lattner0ecea032007-08-22 05:28:50 +0000772 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000773 InnerScope.Exit();
774 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000775
776 if (Cond.isInvalid() || Body.isInvalid())
777 return StmtError();
778
Anders Carlsson7f537c12009-05-17 21:22:26 +0000779 return Actions.ActOnWhileStmt(WhileLoc, Actions.FullExpr(Cond), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000780}
781
782/// ParseDoStatement
783/// do-statement: [C99 6.8.5.2]
784/// 'do' statement 'while' '(' expression ')' ';'
785/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000786Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000787 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000789
Chris Lattner22153252007-08-26 23:08:06 +0000790 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
791 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000792 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000793 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000794 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000795 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000796 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000797
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000798 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799
Chris Lattner0ecea032007-08-22 05:28:50 +0000800 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000801 // there is no compound stmt. C90 does not have this clause. We only do this
802 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000803 //
804 // C++ 6.5p2:
805 // The substatement in an iteration-statement implicitly defines a local scope
806 // which is entered and exited each time through the loop.
807 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000808 ParseScope InnerScope(this, Scope::DeclScope,
809 (getLang().C99 || getLang().CPlusPlus) &&
810 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000811
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000813 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000814
Chris Lattner0ecea032007-08-22 05:28:50 +0000815 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000816 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000817
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000818 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000819 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000820 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000821 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000822 SkipUntil(tok::semi, false, true);
823 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000824 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 }
826 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000827
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000828 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000829 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000830 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000831 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000833
Chris Lattnerff871fb2008-12-12 06:35:28 +0000834 // Parse the parenthesized condition.
835 OwningExprResult Cond(Actions);
836 ParseParenExprOrCondition(Cond, true);
837
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000838 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000839
Sebastian Redl9a920342008-12-11 19:48:14 +0000840 if (Cond.isInvalid() || Body.isInvalid())
841 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000842
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000843 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000844}
845
846/// ParseForStatement
847/// for-statement: [C99 6.8.5.3]
848/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
849/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000850/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
851/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000852/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
853/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000854///
855/// [C++] for-init-statement:
856/// [C++] expression-statement
857/// [C++] simple-declaration
858///
Sebastian Redl9a920342008-12-11 19:48:14 +0000859Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000860 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000862
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000863 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000864 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000866 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000868
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000869 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000870
Chris Lattner22153252007-08-26 23:08:06 +0000871 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
872 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000873 //
874 // C++ 6.4p3:
875 // A name introduced by a declaration in a condition is in scope from its
876 // point of declaration until the end of the substatements controlled by the
877 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000878 // C++ 3.3.2p4:
879 // Names declared in the for-init-statement, and in the condition of if,
880 // while, for, and switch statements are local to the if, while, for, or
881 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000882 // C++ 6.5.3p1:
883 // Names declared in the for-init-statement are in the same declarative-region
884 // as those declared in the condition.
885 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000886 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000887 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000888 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
889 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000890 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000891 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
892
893 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000894
895 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000896 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000897
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000898 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000899 OwningStmtResult FirstPart(Actions);
900 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000903 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 // no first part, eat the ';'.
905 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000906 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000908 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000910
Chris Lattner97144fc2009-04-02 04:16:50 +0000911 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
912 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
913 false);
Chris Lattnercd147752009-03-29 17:27:48 +0000914 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
915
916 if (Tok.is(tok::semi)) { // for (int x = 4;
917 ConsumeToken();
918 } else if ((ForEach = isTokIdentifier_in())) {
919 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000920 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000921 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000922 } else {
923 Diag(Tok, diag::err_expected_semi_for);
924 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000925 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 } else {
927 Value = ParseExpression();
928
929 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000930 if (!Value.isInvalid())
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000931 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000932
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000933 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000935 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000936 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000937 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000938 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000939 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 SkipUntil(tok::semi);
941 }
942 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000943 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000944 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000945 // Parse the second part of the for specifier.
946 if (Tok.is(tok::semi)) { // for (...;;
947 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000948 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000949 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000950 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000951
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000952 if (Tok.is(tok::semi)) {
953 ConsumeToken();
954 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000955 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000956 SkipUntil(tok::semi);
957 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000958
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000959 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000960 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000961 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 // Match the ')'.
964 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000965
Chris Lattner0ecea032007-08-22 05:28:50 +0000966 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000967 // there is no compound stmt. C90 does not have this clause. We only do this
968 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000969 //
970 // C++ 6.5p2:
971 // The substatement in an iteration-statement implicitly defines a local scope
972 // which is entered and exited each time through the loop.
973 //
974 // See comments in ParseIfStatement for why we create a scope for
975 // for-init-statement/condition and a new scope for substatement in C++.
976 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000977 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000978 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000979
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000981 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000982
Chris Lattner0ecea032007-08-22 05:28:50 +0000983 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000984 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000985
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000987 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000988
989 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000990 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000991
992 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000993 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
994 move(SecondPart), move(ThirdPart),
995 RParenLoc, move(Body));
Chris Lattner682bf922009-03-29 16:50:03 +0000996
997 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
998 move(FirstPart),
999 move(SecondPart),
1000 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001001}
1002
1003/// ParseGotoStatement
1004/// jump-statement:
1005/// 'goto' identifier ';'
1006/// [GNU] 'goto' '*' expression ';'
1007///
1008/// Note: this lets the caller parse the end ';'.
1009///
Sebastian Redl9a920342008-12-11 19:48:14 +00001010Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001011 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001013
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001014 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001015 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001016 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 Tok.getIdentifierInfo());
1018 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001019 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001020 // GNU indirect goto extension.
1021 Diag(Tok, diag::ext_gnu_indirect_goto);
1022 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001023 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001024 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001026 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001028 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001029 } else {
1030 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001031 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001033
Sebastian Redl9a920342008-12-11 19:48:14 +00001034 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001035}
1036
1037/// ParseContinueStatement
1038/// jump-statement:
1039/// 'continue' ';'
1040///
1041/// Note: this lets the caller parse the end ';'.
1042///
Sebastian Redl9a920342008-12-11 19:48:14 +00001043Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001045 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001046}
1047
1048/// ParseBreakStatement
1049/// jump-statement:
1050/// 'break' ';'
1051///
1052/// Note: this lets the caller parse the end ';'.
1053///
Sebastian Redl9a920342008-12-11 19:48:14 +00001054Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001056 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057}
1058
1059/// ParseReturnStatement
1060/// jump-statement:
1061/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001062Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001063 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001065
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001066 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001067 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001069 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001071 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 }
1073 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001074 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001075}
1076
Steve Naroff5f8aa692008-02-11 23:15:56 +00001077/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1078/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001079Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001080 if (Tok.is(tok::l_brace)) {
1081 unsigned short savedBraceCount = BraceCount;
1082 do {
1083 ConsumeAnyToken();
1084 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1085 } else {
1086 // From the MS website: If used without braces, the __asm keyword means
1087 // that the rest of the line is an assembly-language statement.
1088 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001089 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001090 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001091 do {
1092 ConsumeAnyToken();
1093 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001094 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001095 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1096 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001097 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001098 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001099}
1100
Reid Spencer5f016e22007-07-11 17:01:13 +00001101/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001102/// asm-statement:
1103/// gnu-asm-statement
1104/// ms-asm-statement
1105///
1106/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001107/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1108///
1109/// [GNU] asm-argument:
1110/// asm-string-literal
1111/// asm-string-literal ':' asm-operands[opt]
1112/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1113/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1114/// ':' asm-clobbers
1115///
1116/// [GNU] asm-clobbers:
1117/// asm-string-literal
1118/// asm-clobbers ',' asm-string-literal
1119///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001120/// [MS] ms-asm-statement:
1121/// '__asm' assembly-instruction ';'[opt]
1122/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1123///
1124/// [MS] assembly-instruction-list:
1125/// assembly-instruction ';'[opt]
1126/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1127///
Sebastian Redl9a920342008-12-11 19:48:14 +00001128Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001129 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001130 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001131
Steve Naroff5f8aa692008-02-11 23:15:56 +00001132 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001133 msAsm = true;
1134 return FuzzyParseMicrosoftAsmStatement();
1135 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 DeclSpec DS;
1137 SourceLocation Loc = Tok.getLocation();
1138 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001139
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1141 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001142 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001144 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001145
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001147 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001148 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001149 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001150 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001152 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 }
1154 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001155
Sebastian Redleffa8d12008-12-10 00:02:53 +00001156 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001157 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001158 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001159
Anders Carlssonb235fc22007-11-22 01:36:19 +00001160 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001161 ExprVector Constraints(Actions);
1162 ExprVector Exprs(Actions);
1163 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164
Anders Carlssondfab34a2008-02-05 23:03:50 +00001165 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001166
Anders Carlssondfab34a2008-02-05 23:03:50 +00001167 SourceLocation RParenLoc;
1168 if (Tok.is(tok::r_paren)) {
1169 // We have a simple asm expression
1170 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001171
Anders Carlssondfab34a2008-02-05 23:03:50 +00001172 RParenLoc = ConsumeParen();
1173 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001174 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001175 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001176 return StmtError();
1177
Anders Carlssondfab34a2008-02-05 23:03:50 +00001178 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001179
Anders Carlssondfab34a2008-02-05 23:03:50 +00001180 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001181 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001182 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001183
Anders Carlssondfab34a2008-02-05 23:03:50 +00001184 assert(Names.size() == Constraints.size() &&
1185 Constraints.size() == Exprs.size()
1186 && "Input operand size mismatch!");
1187
1188 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001189
Anders Carlssondfab34a2008-02-05 23:03:50 +00001190 // Parse the clobbers, if present.
1191 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001192 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001193
Anders Carlssondfab34a2008-02-05 23:03:50 +00001194 // Parse the asm-string list for clobbers.
1195 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001196 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001197
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001198 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001199 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001200
1201 Clobbers.push_back(Clobber.release());
1202
Anders Carlssondfab34a2008-02-05 23:03:50 +00001203 if (Tok.isNot(tok::comma)) break;
1204 ConsumeToken();
1205 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001207
Anders Carlssondfab34a2008-02-05 23:03:50 +00001208 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001210
Sebastian Redl3037ed02009-01-18 16:53:17 +00001211 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001212 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001213 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001214 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001215 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001216}
1217
1218/// ParseAsmOperands - Parse the asm-operands production as used by
1219/// asm-statement. We also parse a leading ':' token. If the leading colon is
1220/// not present, we do not parse anything.
1221///
1222/// [GNU] asm-operands:
1223/// asm-operand
1224/// asm-operands ',' asm-operand
1225///
1226/// [GNU] asm-operand:
1227/// asm-string-literal '(' expression ')'
1228/// '[' identifier ']' asm-string-literal '(' expression ')'
1229///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001230bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001231 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1232 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001234 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 ConsumeToken();
1236
1237 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001238 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001239 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001240
Anders Carlssonb235fc22007-11-22 01:36:19 +00001241 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001243 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 SourceLocation Loc = ConsumeBracket();
1245
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001246 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 Diag(Tok, diag::err_expected_ident);
1248 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001249 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 }
Chris Lattner69efba72007-10-29 04:06:22 +00001251
Anders Carlssonb235fc22007-11-22 01:36:19 +00001252 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001253 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001254
1255 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001257 } else
1258 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001259
Sebastian Redleffa8d12008-12-10 00:02:53 +00001260 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001261 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001262 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001263 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001264 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001265 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001266
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001267 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001268 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001270 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001272
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001274 SourceLocation OpenLoc = ConsumeParen();
1275 OwningExprResult Res(ParseExpression());
1276 MatchRHSPunctuation(tok::r_paren, OpenLoc);
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();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001293
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
Sebastian Redld3a413d2009-04-26 20:35:05 +00001311/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1312///
1313/// function-try-block:
1314/// 'try' ctor-initializer[opt] compound-statement handler-seq
1315///
1316Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1317 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1318 SourceLocation TryLoc = ConsumeToken();
1319
1320 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1321 PP.getSourceManager(),
1322 "parsing function try block");
1323
1324 // Constructor initializer list?
1325 if (Tok.is(tok::colon))
1326 ParseConstructorInitializer(Decl);
1327
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001328 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001329 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1330 // If we failed to parse the try-catch, we just give the function an empty
1331 // compound statement as the body.
1332 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001333 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001334 MultiStmtArg(Actions), false);
1335
1336 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1337}
1338
Sebastian Redla0fd8652008-12-21 16:41:36 +00001339/// ParseCXXTryBlock - Parse a C++ try-block.
1340///
1341/// try-block:
1342/// 'try' compound-statement handler-seq
1343///
Sebastian Redla0fd8652008-12-21 16:41:36 +00001344Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1345 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1346
1347 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001348 return ParseCXXTryBlockCommon(TryLoc);
1349}
1350
1351/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1352/// function-try-block.
1353///
1354/// try-block:
1355/// 'try' compound-statement handler-seq
1356///
1357/// function-try-block:
1358/// 'try' ctor-initializer[opt] compound-statement handler-seq
1359///
1360/// handler-seq:
1361/// handler handler-seq[opt]
1362///
1363Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001364 if (Tok.isNot(tok::l_brace))
1365 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1366 OwningStmtResult TryBlock(ParseCompoundStatement());
1367 if (TryBlock.isInvalid())
1368 return move(TryBlock);
1369
1370 StmtVector Handlers(Actions);
1371 if (Tok.isNot(tok::kw_catch))
1372 return StmtError(Diag(Tok, diag::err_expected_catch));
1373 while (Tok.is(tok::kw_catch)) {
1374 OwningStmtResult Handler(ParseCXXCatchBlock());
1375 if (!Handler.isInvalid())
1376 Handlers.push_back(Handler.release());
1377 }
1378 // Don't bother creating the full statement if we don't have any usable
1379 // handlers.
1380 if (Handlers.empty())
1381 return StmtError();
1382
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001383 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001384}
1385
1386/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1387///
1388/// handler:
1389/// 'catch' '(' exception-declaration ')' compound-statement
1390///
1391/// exception-declaration:
1392/// type-specifier-seq declarator
1393/// type-specifier-seq abstract-declarator
1394/// type-specifier-seq
1395/// '...'
1396///
1397Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1398 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1399
1400 SourceLocation CatchLoc = ConsumeToken();
1401
1402 SourceLocation LParenLoc = Tok.getLocation();
1403 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1404 return StmtError();
1405
1406 // C++ 3.3.2p3:
1407 // The name in a catch exception-declaration is local to the handler and
1408 // shall not be redeclared in the outermost block of the handler.
1409 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1410
1411 // exception-declaration is equivalent to '...' or a parameter-declaration
1412 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001413 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001414 if (Tok.isNot(tok::ellipsis)) {
1415 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001416 if (ParseCXXTypeSpecifierSeq(DS))
1417 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001418 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1419 ParseDeclarator(ExDecl);
1420 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1421 } else
1422 ConsumeToken();
1423
1424 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1425 return StmtError();
1426
1427 if (Tok.isNot(tok::l_brace))
1428 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1429
1430 OwningStmtResult Block(ParseCompoundStatement());
1431 if (Block.isInvalid())
1432 return move(Block);
1433
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001434 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001435}