blob: 7766bfada76644cc0cc393731859781002150c84 [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,
Chris Lattner98913592009-06-12 23:04:47 +0000490 bool OnlyAllowCondition,
491 SourceLocation *LParenLocPtr,
492 SourceLocation *RParenLocPtr) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000493 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner98913592009-06-12 23:04:47 +0000494 if (LParenLocPtr) *LParenLocPtr = LParenLoc;
Chris Lattner15ff1112008-12-12 06:31:07 +0000495
496 if (getLang().CPlusPlus)
497 CondExp = ParseCXXCondition();
498 else
499 CondExp = ParseExpression();
500
501 // If the parser was confused by the condition and we don't have a ')', try to
502 // recover by skipping ahead to a semi and bailing out. If condexp is
503 // semantically invalid but we have well formed code, keep going.
504 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
505 SkipUntil(tok::semi);
506 // Skipping may have stopped if it found the containing ')'. If so, we can
507 // continue parsing the if statement.
508 if (Tok.isNot(tok::r_paren))
509 return true;
510 }
511
512 // Otherwise the condition is valid or the rparen is present.
Chris Lattner98913592009-06-12 23:04:47 +0000513 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
514 if (RParenLocPtr) *RParenLocPtr = RPLoc;
Chris Lattner15ff1112008-12-12 06:31:07 +0000515 return false;
516}
517
518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519/// ParseIfStatement
520/// if-statement: [C99 6.8.4.1]
521/// 'if' '(' expression ')' statement
522/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000523/// [C++] 'if' '(' condition ')' statement
524/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000525///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000526Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000527 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
529
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000530 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000531 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000533 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000535
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000536 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
537
Chris Lattner22153252007-08-26 23:08:06 +0000538 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
539 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000540 //
541 // C++ 6.4p3:
542 // A name introduced by a declaration in a condition is in scope from its
543 // point of declaration until the end of the substatements controlled by the
544 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000545 // C++ 3.3.2p4:
546 // Names declared in the for-init-statement, and in the condition of if,
547 // while, for, and switch statements are local to the if, while, for, or
548 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000549 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000550 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000551
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000553 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000554 if (ParseParenExprOrCondition(CondExp))
555 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000556
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000557 FullExprArg FullCondExp(Actions.FullExpr(CondExp));
558
Chris Lattner0ecea032007-08-22 05:28:50 +0000559 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000560 // there is no compound stmt. C90 does not have this clause. We only do this
561 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000562 //
563 // C++ 6.4p1:
564 // The substatement in a selection-statement (each substatement, in the else
565 // form of the if statement) implicitly defines a local scope.
566 //
567 // For C++ we create a scope for the condition and a new scope for
568 // substatements because:
569 // -When the 'then' scope exits, we want the condition declaration to still be
570 // active for the 'else' scope too.
571 // -Sema will detect name clashes by considering declarations of a
572 // 'ControlScope' as part of its direct subscope.
573 // -If we wanted the condition and substatement to be in the same scope, we
574 // would have to notify ParseStatement not to create a new scope. It's
575 // simpler to let it create a new scope.
576 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000577 ParseScope InnerScope(this, Scope::DeclScope,
578 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000579
Chris Lattnerb96728d2007-10-29 05:08:52 +0000580 // Read the 'then' stmt.
581 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000582 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000583
Chris Lattnera36ce712007-08-22 05:16:28 +0000584 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000585 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000586
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 // If it has an else, parse it.
588 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000589 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000590 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000591
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000592 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000594
Chris Lattner0ecea032007-08-22 05:28:50 +0000595 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000596 // there is no compound stmt. C90 does not have this clause. We only do
597 // this if the body isn't a compound statement to avoid push/pop in common
598 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000599 //
600 // C++ 6.4p1:
601 // The substatement in a selection-statement (each substatement, in the else
602 // form of the if statement) implicitly defines a local scope.
603 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000604 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000605 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000606
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000607 bool WithinElse = CurScope->isWithinElse();
608 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000609 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000611 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000612
613 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000614 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000616
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000617 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000618
619 // If the condition was invalid, discard the if statement. We could recover
620 // better by replacing it with a valid expr, but don't do that yet.
621 if (CondExp.isInvalid())
622 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000623
Chris Lattnerb96728d2007-10-29 05:08:52 +0000624 // If the then or else stmt is invalid and the other is valid (and present),
625 // make turn the invalid one into a null stmt to avoid dropping the other
626 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000627 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
628 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
629 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000630 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000631 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000632 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000633
Chris Lattnerb96728d2007-10-29 05:08:52 +0000634 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000635 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000636 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000637 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000638 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000639
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000640 return Actions.ActOnIfStmt(IfLoc, FullCondExp, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000641 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000642}
643
644/// ParseSwitchStatement
645/// switch-statement:
646/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000647/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000648Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000649 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
651
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000652 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000653 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000655 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 }
Chris Lattner22153252007-08-26 23:08:06 +0000657
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000658 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
659
Chris Lattner22153252007-08-26 23:08:06 +0000660 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
661 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000662 //
663 // C++ 6.4p3:
664 // A name introduced by a declaration in a condition is in scope from its
665 // point of declaration until the end of the substatements controlled by the
666 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000667 // C++ 3.3.2p4:
668 // Names declared in the for-init-statement, and in the condition of if,
669 // while, for, and switch statements are local to the if, while, for, or
670 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000671 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000672 unsigned ScopeFlags = Scope::BreakScope;
673 if (C99orCXX)
674 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000675 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000676
677 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000678 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000679 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000680 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000681
682 OwningStmtResult Switch(Actions);
683 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000684 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000685
Chris Lattner0ecea032007-08-22 05:28:50 +0000686 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000687 // there is no compound stmt. C90 does not have this clause. We only do this
688 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000689 //
690 // C++ 6.4p1:
691 // The substatement in a selection-statement (each substatement, in the else
692 // form of the if statement) implicitly defines a local scope.
693 //
694 // See comments in ParseIfStatement for why we create a scope for the
695 // condition and a new scope for substatement in C++.
696 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000697 ParseScope InnerScope(this, Scope::DeclScope,
698 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000699
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000701 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000702
Chris Lattner0ecea032007-08-22 05:28:50 +0000703 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000704 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000705
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000706 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000707 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000708 // FIXME: Remove the case statement list from the Switch statement.
709 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000710
711 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000712
Chris Lattner15ff1112008-12-12 06:31:07 +0000713 if (Cond.isInvalid())
714 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000715
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000716 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718
719/// ParseWhileStatement
720/// while-statement: [C99 6.8.5.1]
721/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000722/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000723Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000724 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 SourceLocation WhileLoc = Tok.getLocation();
726 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000727
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000728 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000729 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000731 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000733
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000734 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
735
Chris Lattner22153252007-08-26 23:08:06 +0000736 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
737 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000738 //
739 // C++ 6.4p3:
740 // A name introduced by a declaration in a condition is in scope from its
741 // point of declaration until the end of the substatements controlled by the
742 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000743 // C++ 3.3.2p4:
744 // Names declared in the for-init-statement, and in the condition of if,
745 // while, for, and switch statements are local to the if, while, for, or
746 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000747 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000748 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000749 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000750 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
751 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000752 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000753 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
754 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000755
756 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000757 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000758 if (ParseParenExprOrCondition(Cond))
759 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000760
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000761 FullExprArg FullCond(Actions.FullExpr(Cond));
762
Chris Lattner0ecea032007-08-22 05:28:50 +0000763 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000764 // there is no compound stmt. C90 does not have this clause. We only do this
765 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000766 //
767 // C++ 6.5p2:
768 // The substatement in an iteration-statement implicitly defines a local scope
769 // which is entered and exited each time through the loop.
770 //
771 // See comments in ParseIfStatement for why we create a scope for the
772 // condition and a new scope for substatement in C++.
773 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000774 ParseScope InnerScope(this, Scope::DeclScope,
775 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000776
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000778 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000779
Chris Lattner0ecea032007-08-22 05:28:50 +0000780 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000781 InnerScope.Exit();
782 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000783
784 if (Cond.isInvalid() || Body.isInvalid())
785 return StmtError();
786
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000787 return Actions.ActOnWhileStmt(WhileLoc, FullCond, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000788}
789
790/// ParseDoStatement
791/// do-statement: [C99 6.8.5.2]
792/// 'do' statement 'while' '(' expression ')' ';'
793/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000794Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000795 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000797
Chris Lattner22153252007-08-26 23:08:06 +0000798 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
799 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000800 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000801 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000802 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000803 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000804 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000805
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000806 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000807
Chris Lattner0ecea032007-08-22 05:28:50 +0000808 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000809 // there is no compound stmt. C90 does not have this clause. We only do this
810 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000811 //
812 // C++ 6.5p2:
813 // The substatement in an iteration-statement implicitly defines a local scope
814 // which is entered and exited each time through the loop.
815 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000816 ParseScope InnerScope(this, Scope::DeclScope,
817 (getLang().C99 || getLang().CPlusPlus) &&
818 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000821 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000822
Chris Lattner0ecea032007-08-22 05:28:50 +0000823 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000824 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000825
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000826 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000827 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000828 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000829 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000830 SkipUntil(tok::semi, false, true);
831 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000832 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 }
834 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000835
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000836 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000837 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000838 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000839 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000841
Chris Lattnerff871fb2008-12-12 06:35:28 +0000842 // Parse the parenthesized condition.
843 OwningExprResult Cond(Actions);
Chris Lattner98913592009-06-12 23:04:47 +0000844 SourceLocation LPLoc, RPLoc;
845 ParseParenExprOrCondition(Cond, true, &LPLoc, &RPLoc);
Chris Lattnerff871fb2008-12-12 06:35:28 +0000846
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000847 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000848
Sebastian Redl9a920342008-12-11 19:48:14 +0000849 if (Cond.isInvalid() || Body.isInvalid())
850 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000851
Chris Lattner98913592009-06-12 23:04:47 +0000852 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
853 move(Cond), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000854}
855
856/// ParseForStatement
857/// for-statement: [C99 6.8.5.3]
858/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
859/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000860/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
861/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000862/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
863/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000864///
865/// [C++] for-init-statement:
866/// [C++] expression-statement
867/// [C++] simple-declaration
868///
Sebastian Redl9a920342008-12-11 19:48:14 +0000869Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000870 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000872
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000873 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000874 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000876 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000878
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000879 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000880
Chris Lattner22153252007-08-26 23:08:06 +0000881 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
882 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000883 //
884 // C++ 6.4p3:
885 // A name introduced by a declaration in a condition is in scope from its
886 // point of declaration until the end of the substatements controlled by the
887 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000888 // C++ 3.3.2p4:
889 // Names declared in the for-init-statement, and in the condition of if,
890 // while, for, and switch statements are local to the if, while, for, or
891 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000892 // C++ 6.5.3p1:
893 // Names declared in the for-init-statement are in the same declarative-region
894 // as those declared in the condition.
895 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000896 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000897 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000898 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
899 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000900 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000901 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
902
903 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000904
905 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000906 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000907
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000908 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000909 OwningStmtResult FirstPart(Actions);
910 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000913 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 // no first part, eat the ';'.
915 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000916 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000918 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000920
Chris Lattner97144fc2009-04-02 04:16:50 +0000921 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
922 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
923 false);
Chris Lattnercd147752009-03-29 17:27:48 +0000924 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
925
926 if (Tok.is(tok::semi)) { // for (int x = 4;
927 ConsumeToken();
928 } else if ((ForEach = isTokIdentifier_in())) {
929 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000930 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000931 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000932 } else {
933 Diag(Tok, diag::err_expected_semi_for);
934 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000935 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 } else {
937 Value = ParseExpression();
938
939 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000940 if (!Value.isInvalid())
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000941 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000942
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000943 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000945 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000946 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000947 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000948 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000949 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 SkipUntil(tok::semi);
951 }
952 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000953 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000954 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000955 // Parse the second part of the for specifier.
956 if (Tok.is(tok::semi)) { // for (...;;
957 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000958 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000959 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000960 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000961
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000962 if (Tok.is(tok::semi)) {
963 ConsumeToken();
964 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000965 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000966 SkipUntil(tok::semi);
967 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000968
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000969 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000970 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000971 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 // Match the ')'.
974 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000975
Chris Lattner0ecea032007-08-22 05:28:50 +0000976 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000977 // there is no compound stmt. C90 does not have this clause. We only do this
978 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000979 //
980 // C++ 6.5p2:
981 // The substatement in an iteration-statement implicitly defines a local scope
982 // which is entered and exited each time through the loop.
983 //
984 // See comments in ParseIfStatement for why we create a scope for
985 // for-init-statement/condition and a new scope for substatement in C++.
986 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000987 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000988 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000989
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000991 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000992
Chris Lattner0ecea032007-08-22 05:28:50 +0000993 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000994 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000995
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000997 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000998
999 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001000 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001001
1002 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001003 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
1004 move(SecondPart), move(ThirdPart),
1005 RParenLoc, move(Body));
Chris Lattner682bf922009-03-29 16:50:03 +00001006
1007 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1008 move(FirstPart),
1009 move(SecondPart),
1010 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001011}
1012
1013/// ParseGotoStatement
1014/// jump-statement:
1015/// 'goto' identifier ';'
1016/// [GNU] 'goto' '*' expression ';'
1017///
1018/// Note: this lets the caller parse the end ';'.
1019///
Sebastian Redl9a920342008-12-11 19:48:14 +00001020Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001021 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001023
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001024 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001025 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001026 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 Tok.getIdentifierInfo());
1028 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001029 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 // GNU indirect goto extension.
1031 Diag(Tok, diag::ext_gnu_indirect_goto);
1032 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001033 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001034 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001036 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001038 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001039 } else {
1040 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001041 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001043
Sebastian Redl9a920342008-12-11 19:48:14 +00001044 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001045}
1046
1047/// ParseContinueStatement
1048/// jump-statement:
1049/// 'continue' ';'
1050///
1051/// Note: this lets the caller parse the end ';'.
1052///
Sebastian Redl9a920342008-12-11 19:48:14 +00001053Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001055 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056}
1057
1058/// ParseBreakStatement
1059/// jump-statement:
1060/// 'break' ';'
1061///
1062/// Note: this lets the caller parse the end ';'.
1063///
Sebastian Redl9a920342008-12-11 19:48:14 +00001064Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001066 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001067}
1068
1069/// ParseReturnStatement
1070/// jump-statement:
1071/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001072Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001073 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001075
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001076 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001077 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001079 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001081 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 }
1083 }
Anders Carlssona0ab25d2009-05-30 21:42:34 +00001084 return Actions.ActOnReturnStmt(ReturnLoc, Actions.FullExpr(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001085}
1086
Steve Naroff5f8aa692008-02-11 23:15:56 +00001087/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1088/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001089Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001090 if (Tok.is(tok::l_brace)) {
1091 unsigned short savedBraceCount = BraceCount;
1092 do {
1093 ConsumeAnyToken();
1094 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1095 } else {
1096 // From the MS website: If used without braces, the __asm keyword means
1097 // that the rest of the line is an assembly-language statement.
1098 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001099 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001100 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001101 do {
1102 ConsumeAnyToken();
1103 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001104 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001105 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1106 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001107 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001108 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001109}
1110
Reid Spencer5f016e22007-07-11 17:01:13 +00001111/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001112/// asm-statement:
1113/// gnu-asm-statement
1114/// ms-asm-statement
1115///
1116/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001117/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1118///
1119/// [GNU] asm-argument:
1120/// asm-string-literal
1121/// asm-string-literal ':' asm-operands[opt]
1122/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1123/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1124/// ':' asm-clobbers
1125///
1126/// [GNU] asm-clobbers:
1127/// asm-string-literal
1128/// asm-clobbers ',' asm-string-literal
1129///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001130/// [MS] ms-asm-statement:
1131/// '__asm' assembly-instruction ';'[opt]
1132/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1133///
1134/// [MS] assembly-instruction-list:
1135/// assembly-instruction ';'[opt]
1136/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1137///
Sebastian Redl9a920342008-12-11 19:48:14 +00001138Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001139 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001140 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001141
Steve Naroff5f8aa692008-02-11 23:15:56 +00001142 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001143 msAsm = true;
1144 return FuzzyParseMicrosoftAsmStatement();
1145 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 DeclSpec DS;
1147 SourceLocation Loc = Tok.getLocation();
1148 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001149
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1151 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001152 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001154 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001155
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001157 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001158 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001159 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001160 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001162 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 }
1164 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001165
Sebastian Redleffa8d12008-12-10 00:02:53 +00001166 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001167 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001168 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001169
Anders Carlssonb235fc22007-11-22 01:36:19 +00001170 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001171 ExprVector Constraints(Actions);
1172 ExprVector Exprs(Actions);
1173 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001174
Anders Carlssondfab34a2008-02-05 23:03:50 +00001175 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001176
Anders Carlssondfab34a2008-02-05 23:03:50 +00001177 SourceLocation RParenLoc;
1178 if (Tok.is(tok::r_paren)) {
1179 // We have a simple asm expression
1180 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001181
Anders Carlssondfab34a2008-02-05 23:03:50 +00001182 RParenLoc = ConsumeParen();
1183 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001184 // Parse Outputs, 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();
1187
Anders Carlssondfab34a2008-02-05 23:03:50 +00001188 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001189
Anders Carlssondfab34a2008-02-05 23:03:50 +00001190 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001191 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001192 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001193
Anders Carlssondfab34a2008-02-05 23:03:50 +00001194 assert(Names.size() == Constraints.size() &&
1195 Constraints.size() == Exprs.size()
1196 && "Input operand size mismatch!");
1197
1198 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001199
Anders Carlssondfab34a2008-02-05 23:03:50 +00001200 // Parse the clobbers, if present.
1201 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001202 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001203
Anders Carlssondfab34a2008-02-05 23:03:50 +00001204 // Parse the asm-string list for clobbers.
1205 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001206 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001207
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001208 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001209 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001210
1211 Clobbers.push_back(Clobber.release());
1212
Anders Carlssondfab34a2008-02-05 23:03:50 +00001213 if (Tok.isNot(tok::comma)) break;
1214 ConsumeToken();
1215 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001217
Anders Carlssondfab34a2008-02-05 23:03:50 +00001218 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001220
Sebastian Redl3037ed02009-01-18 16:53:17 +00001221 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001222 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001223 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001224 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001225 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001226}
1227
1228/// ParseAsmOperands - Parse the asm-operands production as used by
1229/// asm-statement. We also parse a leading ':' token. If the leading colon is
1230/// not present, we do not parse anything.
1231///
1232/// [GNU] asm-operands:
1233/// asm-operand
1234/// asm-operands ',' asm-operand
1235///
1236/// [GNU] asm-operand:
1237/// asm-string-literal '(' expression ')'
1238/// '[' identifier ']' asm-string-literal '(' expression ')'
1239///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001240bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001241 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1242 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001244 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 ConsumeToken();
1246
1247 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001248 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001249 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001250
Anders Carlssonb235fc22007-11-22 01:36:19 +00001251 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001253 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 SourceLocation Loc = ConsumeBracket();
1255
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001256 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 Diag(Tok, diag::err_expected_ident);
1258 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001259 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 }
Chris Lattner69efba72007-10-29 04:06:22 +00001261
Anders Carlssonb235fc22007-11-22 01:36:19 +00001262 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001263 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001264
1265 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001267 } else
1268 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001269
Sebastian Redleffa8d12008-12-10 00:02:53 +00001270 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001271 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001272 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001273 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001274 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001275 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001276
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001277 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001278 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001280 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001282
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001284 SourceLocation OpenLoc = ConsumeParen();
1285 OwningExprResult Res(ParseExpression());
1286 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001287 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001289 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001291 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001293 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 ConsumeToken();
1295 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001296
1297 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001298}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001299
Chris Lattnerb28317a2009-03-28 19:18:32 +00001300Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001301 assert(Tok.is(tok::l_brace));
1302 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001303
Chris Lattner49f28ca2009-03-05 08:00:35 +00001304 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1305 PP.getSourceManager(),
1306 "parsing function body");
Chris Lattner21ff9c92009-03-05 01:25:28 +00001307
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001308 // Do not enter a scope for the brace, as the arguments are in the same scope
1309 // (the function body) as the body itself. Instead, just read the statement
1310 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001311 OwningStmtResult FnBody(ParseCompoundStatementBody());
1312
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001313 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001314 if (FnBody.isInvalid())
Chris Lattner40e9bc82009-03-05 00:49:17 +00001315 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1316 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001317
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001318 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001319}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001320
Sebastian Redld3a413d2009-04-26 20:35:05 +00001321/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1322///
1323/// function-try-block:
1324/// 'try' ctor-initializer[opt] compound-statement handler-seq
1325///
1326Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1327 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1328 SourceLocation TryLoc = ConsumeToken();
1329
1330 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1331 PP.getSourceManager(),
1332 "parsing function try block");
1333
1334 // Constructor initializer list?
1335 if (Tok.is(tok::colon))
1336 ParseConstructorInitializer(Decl);
1337
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001338 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001339 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1340 // If we failed to parse the try-catch, we just give the function an empty
1341 // compound statement as the body.
1342 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001343 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001344 MultiStmtArg(Actions), false);
1345
1346 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1347}
1348
Sebastian Redla0fd8652008-12-21 16:41:36 +00001349/// ParseCXXTryBlock - Parse a C++ try-block.
1350///
1351/// try-block:
1352/// 'try' compound-statement handler-seq
1353///
Sebastian Redla0fd8652008-12-21 16:41:36 +00001354Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1355 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1356
1357 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001358 return ParseCXXTryBlockCommon(TryLoc);
1359}
1360
1361/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1362/// function-try-block.
1363///
1364/// try-block:
1365/// 'try' compound-statement handler-seq
1366///
1367/// function-try-block:
1368/// 'try' ctor-initializer[opt] compound-statement handler-seq
1369///
1370/// handler-seq:
1371/// handler handler-seq[opt]
1372///
1373Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001374 if (Tok.isNot(tok::l_brace))
1375 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1376 OwningStmtResult TryBlock(ParseCompoundStatement());
1377 if (TryBlock.isInvalid())
1378 return move(TryBlock);
1379
1380 StmtVector Handlers(Actions);
1381 if (Tok.isNot(tok::kw_catch))
1382 return StmtError(Diag(Tok, diag::err_expected_catch));
1383 while (Tok.is(tok::kw_catch)) {
1384 OwningStmtResult Handler(ParseCXXCatchBlock());
1385 if (!Handler.isInvalid())
1386 Handlers.push_back(Handler.release());
1387 }
1388 // Don't bother creating the full statement if we don't have any usable
1389 // handlers.
1390 if (Handlers.empty())
1391 return StmtError();
1392
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001393 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001394}
1395
1396/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1397///
1398/// handler:
1399/// 'catch' '(' exception-declaration ')' compound-statement
1400///
1401/// exception-declaration:
1402/// type-specifier-seq declarator
1403/// type-specifier-seq abstract-declarator
1404/// type-specifier-seq
1405/// '...'
1406///
1407Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1408 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1409
1410 SourceLocation CatchLoc = ConsumeToken();
1411
1412 SourceLocation LParenLoc = Tok.getLocation();
1413 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1414 return StmtError();
1415
1416 // C++ 3.3.2p3:
1417 // The name in a catch exception-declaration is local to the handler and
1418 // shall not be redeclared in the outermost block of the handler.
1419 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1420
1421 // exception-declaration is equivalent to '...' or a parameter-declaration
1422 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001423 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001424 if (Tok.isNot(tok::ellipsis)) {
1425 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001426 if (ParseCXXTypeSpecifierSeq(DS))
1427 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001428 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1429 ParseDeclarator(ExDecl);
1430 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1431 } else
1432 ConsumeToken();
1433
1434 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1435 return StmtError();
1436
1437 if (Tok.isNot(tok::l_brace))
1438 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1439
1440 OwningStmtResult Block(ParseCompoundStatement());
1441 if (Block.isInvalid())
1442 return move(Block);
1443
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001444 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001445}