blob: f041d7dfd7907adf96e1fb66119375587a94d0e7 [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
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000553 FullExprArg FullCondExp(Actions.FullExpr(CondExp));
554
Chris Lattner0ecea032007-08-22 05:28:50 +0000555 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000556 // there is no compound stmt. C90 does not have this clause. We only do this
557 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000558 //
559 // C++ 6.4p1:
560 // The substatement in a selection-statement (each substatement, in the else
561 // form of the if statement) implicitly defines a local scope.
562 //
563 // For C++ we create a scope for the condition and a new scope for
564 // substatements because:
565 // -When the 'then' scope exits, we want the condition declaration to still be
566 // active for the 'else' scope too.
567 // -Sema will detect name clashes by considering declarations of a
568 // 'ControlScope' as part of its direct subscope.
569 // -If we wanted the condition and substatement to be in the same scope, we
570 // would have to notify ParseStatement not to create a new scope. It's
571 // simpler to let it create a new scope.
572 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000573 ParseScope InnerScope(this, Scope::DeclScope,
574 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000575
Chris Lattnerb96728d2007-10-29 05:08:52 +0000576 // Read the 'then' stmt.
577 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000578 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000579
Chris Lattnera36ce712007-08-22 05:16:28 +0000580 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000581 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 // If it has an else, parse it.
584 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000585 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000586 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000587
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000588 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000590
Chris Lattner0ecea032007-08-22 05:28:50 +0000591 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000592 // there is no compound stmt. C90 does not have this clause. We only do
593 // this if the body isn't a compound statement to avoid push/pop in common
594 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000595 //
596 // C++ 6.4p1:
597 // The substatement in a selection-statement (each substatement, in the else
598 // form of the if statement) implicitly defines a local scope.
599 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000600 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000601 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000602
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000603 bool WithinElse = CurScope->isWithinElse();
604 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000605 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000607 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000608
609 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000610 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000612
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000613 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000614
615 // If the condition was invalid, discard the if statement. We could recover
616 // better by replacing it with a valid expr, but don't do that yet.
617 if (CondExp.isInvalid())
618 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000619
Chris Lattnerb96728d2007-10-29 05:08:52 +0000620 // If the then or else stmt is invalid and the other is valid (and present),
621 // make turn the invalid one into a null stmt to avoid dropping the other
622 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000623 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
624 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
625 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000626 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000627 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000628 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000629
Chris Lattnerb96728d2007-10-29 05:08:52 +0000630 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000631 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000632 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000633 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000634 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000635
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000636 return Actions.ActOnIfStmt(IfLoc, FullCondExp, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000637 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000638}
639
640/// ParseSwitchStatement
641/// switch-statement:
642/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000643/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000644Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000645 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
647
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000648 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000649 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000651 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 }
Chris Lattner22153252007-08-26 23:08:06 +0000653
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000654 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
655
Chris Lattner22153252007-08-26 23:08:06 +0000656 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
657 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000658 //
659 // C++ 6.4p3:
660 // A name introduced by a declaration in a condition is in scope from its
661 // point of declaration until the end of the substatements controlled by the
662 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000663 // C++ 3.3.2p4:
664 // Names declared in the for-init-statement, and in the condition of if,
665 // while, for, and switch statements are local to the if, while, for, or
666 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000667 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000668 unsigned ScopeFlags = Scope::BreakScope;
669 if (C99orCXX)
670 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000671 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000672
673 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000674 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000675 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000676 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000677
678 OwningStmtResult Switch(Actions);
679 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000680 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000681
Chris Lattner0ecea032007-08-22 05:28:50 +0000682 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000683 // there is no compound stmt. C90 does not have this clause. We only do this
684 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000685 //
686 // C++ 6.4p1:
687 // The substatement in a selection-statement (each substatement, in the else
688 // form of the if statement) implicitly defines a local scope.
689 //
690 // See comments in ParseIfStatement for why we create a scope for the
691 // condition and a new scope for substatement in C++.
692 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000693 ParseScope InnerScope(this, Scope::DeclScope,
694 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000697 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000698
Chris Lattner0ecea032007-08-22 05:28:50 +0000699 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000700 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000701
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000702 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000703 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000704 // FIXME: Remove the case statement list from the Switch statement.
705 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000706
707 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000708
Chris Lattner15ff1112008-12-12 06:31:07 +0000709 if (Cond.isInvalid())
710 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000711
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000712 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000713}
714
715/// ParseWhileStatement
716/// while-statement: [C99 6.8.5.1]
717/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000718/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000719Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000720 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 SourceLocation WhileLoc = Tok.getLocation();
722 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000723
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000724 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000725 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000727 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000729
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000730 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
731
Chris Lattner22153252007-08-26 23:08:06 +0000732 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
733 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000734 //
735 // C++ 6.4p3:
736 // A name introduced by a declaration in a condition is in scope from its
737 // point of declaration until the end of the substatements controlled by the
738 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000739 // C++ 3.3.2p4:
740 // Names declared in the for-init-statement, and in the condition of if,
741 // while, for, and switch statements are local to the if, while, for, or
742 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000743 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000744 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000745 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000746 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
747 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000748 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000749 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
750 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000751
752 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000753 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000754 if (ParseParenExprOrCondition(Cond))
755 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000756
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000757 FullExprArg FullCond(Actions.FullExpr(Cond));
758
Chris Lattner0ecea032007-08-22 05:28:50 +0000759 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000760 // there is no compound stmt. C90 does not have this clause. We only do this
761 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000762 //
763 // C++ 6.5p2:
764 // The substatement in an iteration-statement implicitly defines a local scope
765 // which is entered and exited each time through the loop.
766 //
767 // See comments in ParseIfStatement for why we create a scope for the
768 // condition and a new scope for substatement in C++.
769 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000770 ParseScope InnerScope(this, Scope::DeclScope,
771 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000772
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000774 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000775
Chris Lattner0ecea032007-08-22 05:28:50 +0000776 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000777 InnerScope.Exit();
778 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000779
780 if (Cond.isInvalid() || Body.isInvalid())
781 return StmtError();
782
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000783 return Actions.ActOnWhileStmt(WhileLoc, FullCond, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000784}
785
786/// ParseDoStatement
787/// do-statement: [C99 6.8.5.2]
788/// 'do' statement 'while' '(' expression ')' ';'
789/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000790Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000791 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000793
Chris Lattner22153252007-08-26 23:08:06 +0000794 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
795 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000796 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000797 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000798 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000799 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000800 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000801
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000802 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000803
Chris Lattner0ecea032007-08-22 05:28:50 +0000804 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000805 // there is no compound stmt. C90 does not have this clause. We only do this
806 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000807 //
808 // C++ 6.5p2:
809 // The substatement in an iteration-statement implicitly defines a local scope
810 // which is entered and exited each time through the loop.
811 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000812 ParseScope InnerScope(this, Scope::DeclScope,
813 (getLang().C99 || getLang().CPlusPlus) &&
814 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000815
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000817 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000818
Chris Lattner0ecea032007-08-22 05:28:50 +0000819 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000820 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000821
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000822 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000823 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000824 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000825 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000826 SkipUntil(tok::semi, false, true);
827 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000828 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 }
830 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000831
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000832 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000833 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000834 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000835 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000837
Chris Lattnerff871fb2008-12-12 06:35:28 +0000838 // Parse the parenthesized condition.
839 OwningExprResult Cond(Actions);
840 ParseParenExprOrCondition(Cond, true);
841
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000842 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000843
Sebastian Redl9a920342008-12-11 19:48:14 +0000844 if (Cond.isInvalid() || Body.isInvalid())
845 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000846
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000847 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000848}
849
850/// ParseForStatement
851/// for-statement: [C99 6.8.5.3]
852/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
853/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000854/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
855/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000856/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
857/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000858///
859/// [C++] for-init-statement:
860/// [C++] expression-statement
861/// [C++] simple-declaration
862///
Sebastian Redl9a920342008-12-11 19:48:14 +0000863Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000864 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000866
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000867 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000868 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000870 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000872
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000873 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000874
Chris Lattner22153252007-08-26 23:08:06 +0000875 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
876 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000877 //
878 // C++ 6.4p3:
879 // A name introduced by a declaration in a condition is in scope from its
880 // point of declaration until the end of the substatements controlled by the
881 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000882 // C++ 3.3.2p4:
883 // Names declared in the for-init-statement, and in the condition of if,
884 // while, for, and switch statements are local to the if, while, for, or
885 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000886 // C++ 6.5.3p1:
887 // Names declared in the for-init-statement are in the same declarative-region
888 // as those declared in the condition.
889 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000890 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000891 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000892 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
893 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000894 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000895 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
896
897 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000898
899 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000900 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000901
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000902 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000903 OwningStmtResult FirstPart(Actions);
904 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000905
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000907 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 // no first part, eat the ';'.
909 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000910 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000912 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000914
Chris Lattner97144fc2009-04-02 04:16:50 +0000915 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
916 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
917 false);
Chris Lattnercd147752009-03-29 17:27:48 +0000918 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
919
920 if (Tok.is(tok::semi)) { // for (int x = 4;
921 ConsumeToken();
922 } else if ((ForEach = isTokIdentifier_in())) {
923 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000924 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000925 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000926 } else {
927 Diag(Tok, diag::err_expected_semi_for);
928 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000929 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 } else {
931 Value = ParseExpression();
932
933 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000934 if (!Value.isInvalid())
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000935 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000936
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000937 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000939 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000940 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000941 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000942 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000943 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 SkipUntil(tok::semi);
945 }
946 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000947 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000948 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000949 // Parse the second part of the for specifier.
950 if (Tok.is(tok::semi)) { // for (...;;
951 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000952 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000953 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000954 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000955
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000956 if (Tok.is(tok::semi)) {
957 ConsumeToken();
958 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000959 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000960 SkipUntil(tok::semi);
961 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000962
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000963 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000964 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000965 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 // Match the ')'.
968 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000969
Chris Lattner0ecea032007-08-22 05:28:50 +0000970 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000971 // there is no compound stmt. C90 does not have this clause. We only do this
972 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000973 //
974 // C++ 6.5p2:
975 // The substatement in an iteration-statement implicitly defines a local scope
976 // which is entered and exited each time through the loop.
977 //
978 // See comments in ParseIfStatement for why we create a scope for
979 // for-init-statement/condition and a new scope for substatement in C++.
980 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000981 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000982 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000983
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000985 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000986
Chris Lattner0ecea032007-08-22 05:28:50 +0000987 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000988 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000989
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000991 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000992
993 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000994 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000995
996 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000997 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
998 move(SecondPart), move(ThirdPart),
999 RParenLoc, move(Body));
Chris Lattner682bf922009-03-29 16:50:03 +00001000
1001 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1002 move(FirstPart),
1003 move(SecondPart),
1004 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001005}
1006
1007/// ParseGotoStatement
1008/// jump-statement:
1009/// 'goto' identifier ';'
1010/// [GNU] 'goto' '*' expression ';'
1011///
1012/// Note: this lets the caller parse the end ';'.
1013///
Sebastian Redl9a920342008-12-11 19:48:14 +00001014Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001015 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001017
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001018 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001019 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001020 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 Tok.getIdentifierInfo());
1022 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001023 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 // GNU indirect goto extension.
1025 Diag(Tok, diag::ext_gnu_indirect_goto);
1026 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001027 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001028 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001030 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001032 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001033 } else {
1034 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001035 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001037
Sebastian Redl9a920342008-12-11 19:48:14 +00001038 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001039}
1040
1041/// ParseContinueStatement
1042/// jump-statement:
1043/// 'continue' ';'
1044///
1045/// Note: this lets the caller parse the end ';'.
1046///
Sebastian Redl9a920342008-12-11 19:48:14 +00001047Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001049 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001050}
1051
1052/// ParseBreakStatement
1053/// jump-statement:
1054/// 'break' ';'
1055///
1056/// Note: this lets the caller parse the end ';'.
1057///
Sebastian Redl9a920342008-12-11 19:48:14 +00001058Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001060 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001061}
1062
1063/// ParseReturnStatement
1064/// jump-statement:
1065/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001066Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001067 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001069
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001070 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001071 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001073 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001075 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 }
1077 }
Anders Carlssona0ab25d2009-05-30 21:42:34 +00001078 return Actions.ActOnReturnStmt(ReturnLoc, Actions.FullExpr(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001079}
1080
Steve Naroff5f8aa692008-02-11 23:15:56 +00001081/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1082/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001083Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001084 if (Tok.is(tok::l_brace)) {
1085 unsigned short savedBraceCount = BraceCount;
1086 do {
1087 ConsumeAnyToken();
1088 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1089 } else {
1090 // From the MS website: If used without braces, the __asm keyword means
1091 // that the rest of the line is an assembly-language statement.
1092 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001093 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001094 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001095 do {
1096 ConsumeAnyToken();
1097 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001098 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001099 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1100 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001101 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001102 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001103}
1104
Reid Spencer5f016e22007-07-11 17:01:13 +00001105/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001106/// asm-statement:
1107/// gnu-asm-statement
1108/// ms-asm-statement
1109///
1110/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001111/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1112///
1113/// [GNU] asm-argument:
1114/// asm-string-literal
1115/// asm-string-literal ':' asm-operands[opt]
1116/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1117/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1118/// ':' asm-clobbers
1119///
1120/// [GNU] asm-clobbers:
1121/// asm-string-literal
1122/// asm-clobbers ',' asm-string-literal
1123///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001124/// [MS] ms-asm-statement:
1125/// '__asm' assembly-instruction ';'[opt]
1126/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1127///
1128/// [MS] assembly-instruction-list:
1129/// assembly-instruction ';'[opt]
1130/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1131///
Sebastian Redl9a920342008-12-11 19:48:14 +00001132Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001133 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001134 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001135
Steve Naroff5f8aa692008-02-11 23:15:56 +00001136 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001137 msAsm = true;
1138 return FuzzyParseMicrosoftAsmStatement();
1139 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 DeclSpec DS;
1141 SourceLocation Loc = Tok.getLocation();
1142 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001143
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1145 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001146 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001148 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001149
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001151 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001152 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001153 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001154 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001156 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 }
1158 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001159
Sebastian Redleffa8d12008-12-10 00:02:53 +00001160 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001161 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001162 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001163
Anders Carlssonb235fc22007-11-22 01:36:19 +00001164 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001165 ExprVector Constraints(Actions);
1166 ExprVector Exprs(Actions);
1167 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001168
Anders Carlssondfab34a2008-02-05 23:03:50 +00001169 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001170
Anders Carlssondfab34a2008-02-05 23:03:50 +00001171 SourceLocation RParenLoc;
1172 if (Tok.is(tok::r_paren)) {
1173 // We have a simple asm expression
1174 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001175
Anders Carlssondfab34a2008-02-05 23:03:50 +00001176 RParenLoc = ConsumeParen();
1177 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001178 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001179 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001180 return StmtError();
1181
Anders Carlssondfab34a2008-02-05 23:03:50 +00001182 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001183
Anders Carlssondfab34a2008-02-05 23:03:50 +00001184 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001185 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001186 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001187
Anders Carlssondfab34a2008-02-05 23:03:50 +00001188 assert(Names.size() == Constraints.size() &&
1189 Constraints.size() == Exprs.size()
1190 && "Input operand size mismatch!");
1191
1192 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001193
Anders Carlssondfab34a2008-02-05 23:03:50 +00001194 // Parse the clobbers, if present.
1195 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001196 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001197
Anders Carlssondfab34a2008-02-05 23:03:50 +00001198 // Parse the asm-string list for clobbers.
1199 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001200 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001201
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001202 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001203 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001204
1205 Clobbers.push_back(Clobber.release());
1206
Anders Carlssondfab34a2008-02-05 23:03:50 +00001207 if (Tok.isNot(tok::comma)) break;
1208 ConsumeToken();
1209 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001211
Anders Carlssondfab34a2008-02-05 23:03:50 +00001212 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001214
Sebastian Redl3037ed02009-01-18 16:53:17 +00001215 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001216 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001217 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001218 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001219 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001220}
1221
1222/// ParseAsmOperands - Parse the asm-operands production as used by
1223/// asm-statement. We also parse a leading ':' token. If the leading colon is
1224/// not present, we do not parse anything.
1225///
1226/// [GNU] asm-operands:
1227/// asm-operand
1228/// asm-operands ',' asm-operand
1229///
1230/// [GNU] asm-operand:
1231/// asm-string-literal '(' expression ')'
1232/// '[' identifier ']' asm-string-literal '(' expression ')'
1233///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001234bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001235 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1236 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001238 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 ConsumeToken();
1240
1241 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001242 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001243 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001244
Anders Carlssonb235fc22007-11-22 01:36:19 +00001245 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001247 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 SourceLocation Loc = ConsumeBracket();
1249
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001250 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 Diag(Tok, diag::err_expected_ident);
1252 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001253 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 }
Chris Lattner69efba72007-10-29 04:06:22 +00001255
Anders Carlssonb235fc22007-11-22 01:36:19 +00001256 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001257 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001258
1259 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001261 } else
1262 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001263
Sebastian Redleffa8d12008-12-10 00:02:53 +00001264 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001265 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001266 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001267 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001268 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001269 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001270
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001271 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001272 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001274 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001276
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001278 SourceLocation OpenLoc = ConsumeParen();
1279 OwningExprResult Res(ParseExpression());
1280 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001281 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001283 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001285 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001287 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 ConsumeToken();
1289 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001290
1291 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001292}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001293
Chris Lattnerb28317a2009-03-28 19:18:32 +00001294Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001295 assert(Tok.is(tok::l_brace));
1296 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001297
Chris Lattner49f28ca2009-03-05 08:00:35 +00001298 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1299 PP.getSourceManager(),
1300 "parsing function body");
Chris Lattner21ff9c92009-03-05 01:25:28 +00001301
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001302 // Do not enter a scope for the brace, as the arguments are in the same scope
1303 // (the function body) as the body itself. Instead, just read the statement
1304 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001305 OwningStmtResult FnBody(ParseCompoundStatementBody());
1306
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001307 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001308 if (FnBody.isInvalid())
Chris Lattner40e9bc82009-03-05 00:49:17 +00001309 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1310 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001311
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001312 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001313}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001314
Sebastian Redld3a413d2009-04-26 20:35:05 +00001315/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1316///
1317/// function-try-block:
1318/// 'try' ctor-initializer[opt] compound-statement handler-seq
1319///
1320Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1321 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1322 SourceLocation TryLoc = ConsumeToken();
1323
1324 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1325 PP.getSourceManager(),
1326 "parsing function try block");
1327
1328 // Constructor initializer list?
1329 if (Tok.is(tok::colon))
1330 ParseConstructorInitializer(Decl);
1331
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001332 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001333 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1334 // If we failed to parse the try-catch, we just give the function an empty
1335 // compound statement as the body.
1336 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001337 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001338 MultiStmtArg(Actions), false);
1339
1340 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1341}
1342
Sebastian Redla0fd8652008-12-21 16:41:36 +00001343/// ParseCXXTryBlock - Parse a C++ try-block.
1344///
1345/// try-block:
1346/// 'try' compound-statement handler-seq
1347///
Sebastian Redla0fd8652008-12-21 16:41:36 +00001348Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1349 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1350
1351 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001352 return ParseCXXTryBlockCommon(TryLoc);
1353}
1354
1355/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1356/// function-try-block.
1357///
1358/// try-block:
1359/// 'try' compound-statement handler-seq
1360///
1361/// function-try-block:
1362/// 'try' ctor-initializer[opt] compound-statement handler-seq
1363///
1364/// handler-seq:
1365/// handler handler-seq[opt]
1366///
1367Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001368 if (Tok.isNot(tok::l_brace))
1369 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1370 OwningStmtResult TryBlock(ParseCompoundStatement());
1371 if (TryBlock.isInvalid())
1372 return move(TryBlock);
1373
1374 StmtVector Handlers(Actions);
1375 if (Tok.isNot(tok::kw_catch))
1376 return StmtError(Diag(Tok, diag::err_expected_catch));
1377 while (Tok.is(tok::kw_catch)) {
1378 OwningStmtResult Handler(ParseCXXCatchBlock());
1379 if (!Handler.isInvalid())
1380 Handlers.push_back(Handler.release());
1381 }
1382 // Don't bother creating the full statement if we don't have any usable
1383 // handlers.
1384 if (Handlers.empty())
1385 return StmtError();
1386
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001387 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001388}
1389
1390/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1391///
1392/// handler:
1393/// 'catch' '(' exception-declaration ')' compound-statement
1394///
1395/// exception-declaration:
1396/// type-specifier-seq declarator
1397/// type-specifier-seq abstract-declarator
1398/// type-specifier-seq
1399/// '...'
1400///
1401Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1402 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1403
1404 SourceLocation CatchLoc = ConsumeToken();
1405
1406 SourceLocation LParenLoc = Tok.getLocation();
1407 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1408 return StmtError();
1409
1410 // C++ 3.3.2p3:
1411 // The name in a catch exception-declaration is local to the handler and
1412 // shall not be redeclared in the outermost block of the handler.
1413 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1414
1415 // exception-declaration is equivalent to '...' or a parameter-declaration
1416 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001417 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001418 if (Tok.isNot(tok::ellipsis)) {
1419 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001420 if (ParseCXXTypeSpecifierSeq(DS))
1421 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001422 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1423 ParseDeclarator(ExDecl);
1424 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1425 } else
1426 ConsumeToken();
1427
1428 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1429 return StmtError();
1430
1431 if (Tok.isNot(tok::l_brace))
1432 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1433
1434 OwningStmtResult Block(ParseCompoundStatement());
1435 if (Block.isInvalid())
1436 return move(Block);
1437
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001438 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001439}