blob: d340d574d4d29f4f3f6ebb187c9acc20c3bc9cff [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 Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000046/// [GNU] asm-statement
47/// [OMP] openmp-construct [TODO]
48///
49/// labeled-statement:
50/// identifier ':' statement
51/// 'case' constant-expression ':' statement
52/// 'default' ':' statement
53///
54/// selection-statement:
55/// if-statement
56/// switch-statement
57///
58/// iteration-statement:
59/// while-statement
60/// do-statement
61/// for-statement
62///
63/// expression-statement:
64/// expression[opt] ';'
65///
66/// jump-statement:
67/// 'goto' identifier ';'
68/// 'continue' ';'
69/// 'break' ';'
70/// 'return' expression[opt] ';'
71/// [GNU] 'goto' '*' expression ';'
72///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000075/// [OBC] '@' 'throw' ';'
76///
John McCall60d7b3a2010-08-24 06:29:42 +000077StmtResult
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +000078Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000079 const char *SemiError = 0;
John McCall60d7b3a2010-08-24 06:29:42 +000080 StmtResult Res;
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000081
82 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000083
Sean Huntbbd37c62009-11-21 08:43:09 +000084 CXX0XAttributeList Attr;
85 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
86 Attr = ParseCXX0XAttributes();
Ted Kremenek1e377652010-02-11 02:19:13 +000087 llvm::OwningPtr<AttributeList> AttrList(Attr.AttrList);
Sean Huntbbd37c62009-11-21 08:43:09 +000088
Reid Spencer5f016e22007-07-11 17:01:13 +000089 // Cases in this switch statement should fall through if the parser expects
90 // the token to end in a semicolon (in which case SemiError should be set),
91 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092 tok::TokenKind Kind = Tok.getKind();
93 SourceLocation AtLoc;
94 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000095 case tok::at: // May be a @try or @throw statement
96 {
97 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000098 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000099 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000100
Douglas Gregor791215b2009-09-21 20:51:25 +0000101 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000102 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorc8bddde2010-05-28 00:22:41 +0000103 ConsumeCodeCompletionToken();
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000104 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor791215b2009-09-21 20:51:25 +0000105
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000106 case tok::identifier:
107 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
108 // identifier ':' statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000109 return ParseLabeledStatement(AttrList.take());
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000110 }
111 // PASS THROUGH.
112
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000113 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000114 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000115 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek1e377652010-02-11 02:19:13 +0000116 AttrList.take(); //Passing 'Attr' to ParseDeclaration transfers ownership.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000117 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext, DeclEnd,
Sean Huntbbd37c62009-11-21 08:43:09 +0000118 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000119 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000120 }
121
122 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000124 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Sean Huntbbd37c62009-11-21 08:43:09 +0000127 // FIXME: Use the attributes
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000128 // expression[opt] ';'
John McCall60d7b3a2010-08-24 06:29:42 +0000129 ExprResult Expr(ParseExpression());
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000130 if (Expr.isInvalid()) {
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000131 // If the expression is invalid, skip ahead to the next semicolon or '}'.
132 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000133 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000134 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
135 if (Tok.is(tok::semi))
136 ConsumeToken();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000137 return StmtError();
138 }
139 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000140 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000141 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000142 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 case tok::kw_case: // C99 6.8.1: labeled-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000145 return ParseCaseStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 case tok::kw_default: // C99 6.8.1: labeled-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000147 return ParseDefaultStatement(AttrList.take());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 case tok::l_brace: // C99 6.8.2: compound-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000150 return ParseCompoundStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000152 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 case tok::kw_if: // C99 6.8.4.1: if-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000155 return ParseIfStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000157 return ParseSwitchStatement(AttrList.take());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 case tok::kw_while: // C99 6.8.5.1: while-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000160 return ParseWhileStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000162 Res = ParseDoStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000163 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 break;
165 case tok::kw_for: // C99 6.8.5.3: for-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000166 return ParseForStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000167
168 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000169 Res = ParseGotoStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000170 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 break;
172 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000173 Res = ParseContinueStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000174 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 break;
176 case tok::kw_break: // C99 6.8.6.3: break-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000177 Res = ParseBreakStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000178 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 break;
180 case tok::kw_return: // C99 6.8.6.4: return-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000181 Res = ParseReturnStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000182 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000184
Sebastian Redla0fd8652008-12-21 16:41:36 +0000185 case tok::kw_asm: {
Sean Huntbbd37c62009-11-21 08:43:09 +0000186 if (Attr.HasAttr)
187 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
188 << Attr.Range;
Steve Naroffd62701b2008-02-07 03:50:06 +0000189 bool msAsm = false;
190 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000191 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000192 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 break;
194 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000195
Sebastian Redla0fd8652008-12-21 16:41:36 +0000196 case tok::kw_try: // C++ 15: try-block
Ted Kremenek1e377652010-02-11 02:19:13 +0000197 return ParseCXXTryBlock(AttrList.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +0000198 }
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000201 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000203 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000204 // If the result was valid, then we do want to diagnose this. Use
205 // ExpectAndConsume to emit the diagnostic, even though we know it won't
206 // succeed.
207 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000208 // Skip until we see a } or ;, but don't eat it.
209 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Sebastian Redl61364dd2008-12-11 19:30:53 +0000212 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000213}
214
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000215/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000216///
217/// labeled-statement:
218/// identifier ':' statement
219/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000220///
John McCall60d7b3a2010-08-24 06:29:42 +0000221StmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000222 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
223 "Not an identifier!");
224
Ted Kremenek1e377652010-02-11 02:19:13 +0000225 llvm::OwningPtr<AttributeList> AttrList(Attr);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000226 Token IdentTok = Tok; // Save the whole token.
227 ConsumeToken(); // eat the identifier.
228
229 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000230
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000231 // identifier ':' statement
232 SourceLocation ColonLoc = ConsumeToken();
233
234 // Read label attributes, if present.
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000235 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000236 AttrList.reset(addAttributeLists(AttrList.take(), ParseGNUAttributes()));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000237
John McCall60d7b3a2010-08-24 06:29:42 +0000238 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000239
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000240 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000241 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000242 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000243
Sebastian Redlde307472009-01-11 00:38:46 +0000244 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
245 IdentTok.getIdentifierInfo(),
Argyrios Kyrtzidis1a186002010-09-28 14:54:07 +0000246 ColonLoc, SubStmt.get(), AttrList.take());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000247}
Reid Spencer5f016e22007-07-11 17:01:13 +0000248
249/// ParseCaseStatement
250/// labeled-statement:
251/// 'case' constant-expression ':' statement
252/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
253///
John McCall60d7b3a2010-08-24 06:29:42 +0000254StmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000255 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000256 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000257 delete Attr;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner24e1e702009-03-04 04:23:07 +0000259 // It is very very common for code to contain many case statements recursively
260 // nested, as in (but usually without indentation):
261 // case 1:
262 // case 2:
263 // case 3:
264 // case 4:
265 // case 5: etc.
266 //
267 // Parsing this naively works, but is both inefficient and can cause us to run
268 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000269 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000270 // but all the grossness is constrained to ParseCaseStatement (and some
271 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner24e1e702009-03-04 04:23:07 +0000273 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
274 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000275 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner24e1e702009-03-04 04:23:07 +0000277 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
278 // gets updated each time a new case is parsed, and whose body is unset so
279 // far. When parsing 'case 4', this is the 'case 3' node.
280 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner24e1e702009-03-04 04:23:07 +0000282 // While we have case statements, eat and stack them.
283 do {
284 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000286 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000287 Actions.CodeCompleteCase(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000288 ConsumeCodeCompletionToken();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000289 }
290
Chris Lattner6fb09c82009-12-10 00:38:54 +0000291 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
292 /// Disable this form of error recovery while we're parsing the case
293 /// expression.
294 ColonProtectionRAIIObject ColonProtection(*this);
295
John McCall60d7b3a2010-08-24 06:29:42 +0000296 ExprResult LHS(ParseConstantExpression());
Chris Lattner24e1e702009-03-04 04:23:07 +0000297 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000299 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000301
Chris Lattner24e1e702009-03-04 04:23:07 +0000302 // GNU case range extension.
303 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000304 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000305 if (Tok.is(tok::ellipsis)) {
306 Diag(Tok, diag::ext_gnu_case_range);
307 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000308
Chris Lattner24e1e702009-03-04 04:23:07 +0000309 RHS = ParseConstantExpression();
310 if (RHS.isInvalid()) {
311 SkipUntil(tok::colon);
312 return StmtError();
313 }
314 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000315
316 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000317
Chris Lattner24e1e702009-03-04 04:23:07 +0000318 if (Tok.isNot(tok::colon)) {
319 Diag(Tok, diag::err_expected_colon_after) << "'case'";
320 SkipUntil(tok::colon);
321 return StmtError();
322 }
323
324 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
John McCall60d7b3a2010-08-24 06:29:42 +0000326 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000327 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
328 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner24e1e702009-03-04 04:23:07 +0000330 // If we had a sema error parsing this case, then just ignore it and
331 // continue parsing the sub-stmt.
332 if (Case.isInvalid()) {
333 if (TopLevelCase.isInvalid()) // No parsed case stmts.
334 return ParseStatement();
335 // Otherwise, just don't add it as a nested case.
336 } else {
337 // If this is the first case statement we parsed, it becomes TopLevelCase.
338 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000339 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000340 if (TopLevelCase.isInvalid())
341 TopLevelCase = move(Case);
342 else
John McCall9ae2f072010-08-23 23:25:46 +0000343 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000344 DeepestParsedCaseStmt = NextDeepest;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner24e1e702009-03-04 04:23:07 +0000347 // Handle all case statements.
348 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner24e1e702009-03-04 04:23:07 +0000350 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner24e1e702009-03-04 04:23:07 +0000352 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000353 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner24e1e702009-03-04 04:23:07 +0000355 if (Tok.isNot(tok::r_brace)) {
356 SubStmt = ParseStatement();
357 } else {
358 // Nicely diagnose the common error "switch (X) { case 4: }", which is
359 // not valid.
360 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000362 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner24e1e702009-03-04 04:23:07 +0000365 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000366 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000367 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattner24e1e702009-03-04 04:23:07 +0000369 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000370 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000371
Chris Lattner24e1e702009-03-04 04:23:07 +0000372 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000373 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000374}
375
376/// ParseDefaultStatement
377/// labeled-statement:
378/// 'default' ':' statement
379/// Note that this does not parse the 'statement' at the end.
380///
John McCall60d7b3a2010-08-24 06:29:42 +0000381StmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000382 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000383 delete Attr;
384
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000385 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
387
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000388 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000389 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000391 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000395
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000397 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000399 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
401
John McCall60d7b3a2010-08-24 06:29:42 +0000402 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000403 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000404 return StmtError();
405
Sebastian Redl117054a2008-12-28 16:13:43 +0000406 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000407 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000408}
409
410
411/// ParseCompoundStatement - Parse a "{}" block.
412///
413/// compound-statement: [C99 6.8.2]
414/// { block-item-list[opt] }
415/// [GNU] { label-declarations block-item-list } [TODO]
416///
417/// block-item-list:
418/// block-item
419/// block-item-list block-item
420///
421/// block-item:
422/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000423/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000424/// statement
425/// [OMP] openmp-directive [TODO]
426///
427/// [GNU] label-declarations:
428/// [GNU] label-declaration
429/// [GNU] label-declarations label-declaration
430///
431/// [GNU] label-declaration:
432/// [GNU] '__label__' identifier-list ';'
433///
434/// [OMP] openmp-directive: [TODO]
435/// [OMP] barrier-directive
436/// [OMP] flush-directive
437///
John McCall60d7b3a2010-08-24 06:29:42 +0000438StmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
Sean Huntbbd37c62009-11-21 08:43:09 +0000439 bool isStmtExpr) {
440 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000441 delete Attr;
442
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000443 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000444
Chris Lattner31e05722007-08-26 06:24:45 +0000445 // Enter a scope to hold everything within the compound stmt. Compound
446 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000447 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000448
449 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000450 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000451}
452
453
454/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000455/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000456/// consume the '}' at the end of the block. It does not manipulate the scope
457/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000458StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000459 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000460 Tok.getLocation(),
461 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000462 InMessageExpressionRAIIObject InMessage(*this, false);
463
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
465
466 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000467 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000468
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000469 StmtVector Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000470 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall60d7b3a2010-08-24 06:29:42 +0000471 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000472 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000473 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000474 } else {
475 // __extension__ can start declarations and it can also be a unary
476 // operator for expressions. Consume multiple __extension__ markers here
477 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000478 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000479 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000480 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000481 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000482
Sean Huntbbd37c62009-11-21 08:43:09 +0000483 CXX0XAttributeList Attr;
484 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
485 Attr = ParseCXX0XAttributes();
486
Chris Lattner45a566c2007-08-27 01:01:57 +0000487 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000488 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000489 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000490 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000491 ExtensionRAIIObject O(Diags);
492
Chris Lattner97144fc2009-04-02 04:16:50 +0000493 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000494 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
495 Declarator::BlockContext, DeclEnd,
Sean Huntbbd37c62009-11-21 08:43:09 +0000496 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000497 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000498 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000499 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000500 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000501
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000502 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000503 SkipUntil(tok::semi);
504 continue;
505 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000506
Sean Huntbbd37c62009-11-21 08:43:09 +0000507 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000508 // Eat the semicolon at the end of stmt and convert the expr into a
509 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000510 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000511 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000512 }
513 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000514
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000515 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000516 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000520 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 Diag(Tok, diag::err_expected_rbrace);
Chris Lattnerf65086b2010-09-01 15:49:26 +0000522 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000523 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000525
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000527 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000528 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000529}
530
Chris Lattner15ff1112008-12-12 06:31:07 +0000531/// ParseParenExprOrCondition:
532/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000533/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000534///
535/// This function parses and performs error recovery on the specified condition
536/// or expression (depending on whether we're in C++ or C mode). This function
537/// goes out of its way to recover well. It returns true if there was a parser
538/// error (the right paren couldn't be found), which indicates that the caller
539/// should try to recover harder. It returns false if the condition is
540/// successfully parsed. Note that a successful parse can still have semantic
541/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000542bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000543 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000544 SourceLocation Loc,
545 bool ConvertToBoolean) {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000546 bool ParseError = false;
547
Chris Lattner15ff1112008-12-12 06:31:07 +0000548 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000549 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +0000550 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
551 ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000552 else {
553 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000554 DeclResult = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000555
556 // If required, convert to a boolean value.
557 if (!ExprResult.isInvalid() && ConvertToBoolean)
558 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000559 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000560 }
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattner15ff1112008-12-12 06:31:07 +0000562 // If the parser was confused by the condition and we don't have a ')', try to
563 // recover by skipping ahead to a semi and bailing out. If condexp is
564 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000565 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000566 SkipUntil(tok::semi);
567 // Skipping may have stopped if it found the containing ')'. If so, we can
568 // continue parsing the if statement.
569 if (Tok.isNot(tok::r_paren))
570 return true;
571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattner15ff1112008-12-12 06:31:07 +0000573 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000574 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000575 return false;
576}
577
578
Reid Spencer5f016e22007-07-11 17:01:13 +0000579/// ParseIfStatement
580/// if-statement: [C99 6.8.4.1]
581/// 'if' '(' expression ')' statement
582/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000583/// [C++] 'if' '(' condition ')' statement
584/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000585///
John McCall60d7b3a2010-08-24 06:29:42 +0000586StmtResult Parser::ParseIfStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000587 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000588 delete Attr;
589
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000590 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
592
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000593 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000594 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000596 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000598
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000599 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
600
Chris Lattner22153252007-08-26 23:08:06 +0000601 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
602 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000603 //
604 // C++ 6.4p3:
605 // A name introduced by a declaration in a condition is in scope from its
606 // point of declaration until the end of the substatements controlled by the
607 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000608 // C++ 3.3.2p4:
609 // Names declared in the for-init-statement, and in the condition of if,
610 // while, for, and switch statements are local to the if, while, for, or
611 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000612 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000613 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000614
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000616 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000617 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000618 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000619 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000620
John McCall9ae2f072010-08-23 23:25:46 +0000621 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Chris Lattner0ecea032007-08-22 05:28:50 +0000623 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000624 // there is no compound stmt. C90 does not have this clause. We only do this
625 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000626 //
627 // C++ 6.4p1:
628 // The substatement in a selection-statement (each substatement, in the else
629 // form of the if statement) implicitly defines a local scope.
630 //
631 // For C++ we create a scope for the condition and a new scope for
632 // substatements because:
633 // -When the 'then' scope exits, we want the condition declaration to still be
634 // active for the 'else' scope too.
635 // -Sema will detect name clashes by considering declarations of a
636 // 'ControlScope' as part of its direct subscope.
637 // -If we wanted the condition and substatement to be in the same scope, we
638 // would have to notify ParseStatement not to create a new scope. It's
639 // simpler to let it create a new scope.
640 //
Mike Stump1eb44332009-09-09 15:08:12 +0000641 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000642 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000643
Chris Lattnerb96728d2007-10-29 05:08:52 +0000644 // Read the 'then' stmt.
645 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000646 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000647
Chris Lattnera36ce712007-08-22 05:16:28 +0000648 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000649 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000650
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 // If it has an else, parse it.
652 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000653 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000654 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000655
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000656 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000658 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000659
Chris Lattner0ecea032007-08-22 05:28:50 +0000660 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000661 // there is no compound stmt. C90 does not have this clause. We only do
662 // this if the body isn't a compound statement to avoid push/pop in common
663 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000664 //
665 // C++ 6.4p1:
666 // The substatement in a selection-statement (each substatement, in the else
667 // form of the if statement) implicitly defines a local scope.
668 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000669 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000670 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000673
Chris Lattnera36ce712007-08-22 05:16:28 +0000674 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000675 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000677
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000678 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Chris Lattner18914bc2008-12-12 06:19:11 +0000680 // If the condition was invalid, discard the if statement. We could recover
681 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000682 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000683 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000684
Chris Lattnerb96728d2007-10-29 05:08:52 +0000685 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000686 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000687 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000688 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
689 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
690 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000691 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000692 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000693 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000694
Chris Lattnerb96728d2007-10-29 05:08:52 +0000695 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000696 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000697 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000698 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000699 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000700
John McCall9ae2f072010-08-23 23:25:46 +0000701 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
702 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000703}
704
705/// ParseSwitchStatement
706/// switch-statement:
707/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000708/// [C++] 'switch' '(' condition ')' statement
John McCall60d7b3a2010-08-24 06:29:42 +0000709StmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000710 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000711 delete Attr;
712
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000713 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
715
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000716 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000717 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000719 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 }
Chris Lattner22153252007-08-26 23:08:06 +0000721
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000722 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
723
Chris Lattner22153252007-08-26 23:08:06 +0000724 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
725 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000726 //
727 // C++ 6.4p3:
728 // A name introduced by a declaration in a condition is in scope from its
729 // point of declaration until the end of the substatements controlled by the
730 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000731 // C++ 3.3.2p4:
732 // Names declared in the for-init-statement, and in the condition of if,
733 // while, for, and switch statements are local to the if, while, for, or
734 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000735 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000736 unsigned ScopeFlags = Scope::BreakScope;
737 if (C99orCXX)
738 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000739 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000740
741 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000742 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000743 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000744 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +0000745 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000746
John McCall60d7b3a2010-08-24 06:29:42 +0000747 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +0000748 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000749
Douglas Gregor586596f2010-05-06 17:25:47 +0000750 if (Switch.isInvalid()) {
751 // Skip the switch body.
752 // FIXME: This is not optimal recovery, but parsing the body is more
753 // dangerous due to the presence of case and default statements, which
754 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +0000755 if (Tok.is(tok::l_brace)) {
756 ConsumeBrace();
757 SkipUntil(tok::r_brace, false, false);
758 } else
Douglas Gregor586596f2010-05-06 17:25:47 +0000759 SkipUntil(tok::semi);
760 return move(Switch);
761 }
762
Chris Lattner0ecea032007-08-22 05:28:50 +0000763 // C99 6.8.4p3 - In C99, the body of the switch 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.4p1:
768 // The substatement in a selection-statement (each substatement, in the else
769 // form of the if statement) implicitly defines a local scope.
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 //
Mike Stump1eb44332009-09-09 15:08:12 +0000774 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000775 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000776
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000778 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000779
Chris Lattner7e52de42010-01-24 01:50:29 +0000780 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000781 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000782 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000783
Chris Lattner7e52de42010-01-24 01:50:29 +0000784 if (Body.isInvalid())
785 // FIXME: Remove the case statement list from the Switch statement.
786 Body = Actions.ActOnNullStmt(Tok.getLocation());
787
John McCall9ae2f072010-08-23 23:25:46 +0000788 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000789}
790
791/// ParseWhileStatement
792/// while-statement: [C99 6.8.5.1]
793/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000794/// [C++] 'while' '(' condition ')' statement
John McCall60d7b3a2010-08-24 06:29:42 +0000795StmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000796 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000797 delete Attr;
798
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000799 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 SourceLocation WhileLoc = Tok.getLocation();
801 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000802
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000803 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000804 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000806 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000808
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000809 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
810
Chris Lattner22153252007-08-26 23:08:06 +0000811 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
812 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000813 //
814 // C++ 6.4p3:
815 // A name introduced by a declaration in a condition is in scope from its
816 // point of declaration until the end of the substatements controlled by the
817 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000818 // C++ 3.3.2p4:
819 // Names declared in the for-init-statement, and in the condition of if,
820 // while, for, and switch statements are local to the if, while, for, or
821 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000822 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000823 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000824 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000825 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
826 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000827 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000828 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
829 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830
831 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000832 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000833 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000834 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000835 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000836
John McCall9ae2f072010-08-23 23:25:46 +0000837 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Chris Lattner0ecea032007-08-22 05:28:50 +0000839 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000840 // there is no compound stmt. C90 does not have this clause. We only do this
841 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000842 //
843 // C++ 6.5p2:
844 // The substatement in an iteration-statement implicitly defines a local scope
845 // which is entered and exited each time through the loop.
846 //
847 // See comments in ParseIfStatement for why we create a scope for the
848 // condition and a new scope for substatement in C++.
849 //
Mike Stump1eb44332009-09-09 15:08:12 +0000850 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000851 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000854 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000855
Chris Lattner0ecea032007-08-22 05:28:50 +0000856 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000857 InnerScope.Exit();
858 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000859
John McCalld226f652010-08-21 09:40:31 +0000860 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000861 return StmtError();
862
John McCall9ae2f072010-08-23 23:25:46 +0000863 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000864}
865
866/// ParseDoStatement
867/// do-statement: [C99 6.8.5.2]
868/// 'do' statement 'while' '(' expression ')' ';'
869/// Note: this lets the caller parse the end ';'.
John McCall60d7b3a2010-08-24 06:29:42 +0000870StmtResult Parser::ParseDoStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000871 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000872 delete Attr;
873
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000874 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000876
Chris Lattner22153252007-08-26 23:08:06 +0000877 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
878 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000879 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000880 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000881 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000882 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000883 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000884
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000885 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000886
Chris Lattner0ecea032007-08-22 05:28:50 +0000887 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000888 // there is no compound stmt. C90 does not have this clause. We only do this
889 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000890 //
891 // C++ 6.5p2:
892 // The substatement in an iteration-statement implicitly defines a local scope
893 // which is entered and exited each time through the loop.
894 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000895 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000896 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000897 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000900 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000901
Chris Lattner0ecea032007-08-22 05:28:50 +0000902 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000903 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000904
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000905 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000906 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000907 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000908 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000909 SkipUntil(tok::semi, false, true);
910 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000911 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 }
913 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000914
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000915 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000916 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000917 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000918 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000920
Chris Lattnerff871fb2008-12-12 06:35:28 +0000921 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000922 SourceLocation LPLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +0000923 ExprResult Cond = ParseExpression();
Douglas Gregor04895d32009-11-24 21:34:32 +0000924 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000925 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000926
Sebastian Redl9a920342008-12-11 19:48:14 +0000927 if (Cond.isInvalid() || Body.isInvalid())
928 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000929
John McCall9ae2f072010-08-23 23:25:46 +0000930 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
931 Cond.get(), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000932}
933
934/// ParseForStatement
935/// for-statement: [C99 6.8.5.3]
936/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
937/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000938/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
939/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000940/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
941/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000942///
943/// [C++] for-init-statement:
944/// [C++] expression-statement
945/// [C++] simple-declaration
946///
John McCall60d7b3a2010-08-24 06:29:42 +0000947StmtResult Parser::ParseForStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000948 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000949 delete Attr;
950
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000951 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000953
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000954 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000955 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000957 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000959
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000960 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000961
Chris Lattner22153252007-08-26 23:08:06 +0000962 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
963 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000964 //
965 // C++ 6.4p3:
966 // A name introduced by a declaration in a condition is in scope from its
967 // point of declaration until the end of the substatements controlled by the
968 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000969 // C++ 3.3.2p4:
970 // Names declared in the for-init-statement, and in the condition of if,
971 // while, for, and switch statements are local to the if, while, for, or
972 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000973 // C++ 6.5.3p1:
974 // Names declared in the for-init-statement are in the same declarative-region
975 // as those declared in the condition.
976 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000977 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000978 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000979 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
980 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000981 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000982 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
983
984 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000985
986 SourceLocation LParenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +0000987 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000988
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000989 bool ForEach = false;
John McCall60d7b3a2010-08-24 06:29:42 +0000990 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +0000991 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +0000992 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +0000993 ExprResult Collection;
Douglas Gregor586596f2010-05-06 17:25:47 +0000994 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +0000995 Decl *SecondVar = 0;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000996
Douglas Gregor791215b2009-09-21 20:51:25 +0000997 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000998 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000999 C99orCXXorObjC? Sema::PCC_ForInit
1000 : Sema::PCC_Expression);
Douglas Gregordc845342010-05-25 05:58:43 +00001001 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +00001002 }
1003
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001005 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 // no first part, eat the ';'.
1007 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001008 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001010 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001012
Sean Huntbbd37c62009-11-21 08:43:09 +00001013 AttributeList *AttrList = 0;
1014 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
1015 AttrList = ParseCXX0XAttributes().AttrList;
1016
Chris Lattner97144fc2009-04-02 04:16:50 +00001017 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001018 StmtVector Stmts(Actions);
1019 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
1020 DeclEnd, AttrList, false);
Chris Lattnercd147752009-03-29 17:27:48 +00001021 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Chris Lattnercd147752009-03-29 17:27:48 +00001023 if (Tok.is(tok::semi)) { // for (int x = 4;
1024 ConsumeToken();
1025 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001026 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001027 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001028 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001029
1030 if (Tok.is(tok::code_completion)) {
1031 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1032 ConsumeCodeCompletionToken();
1033 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001034 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001035 } else {
1036 Diag(Tok, diag::err_expected_semi_for);
1037 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001038 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 } else {
1040 Value = ParseExpression();
1041
1042 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001043 if (!Value.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001044 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
Sebastian Redleffa8d12008-12-10 00:02:53 +00001045
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001046 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001048 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001049 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001050
1051 if (Tok.is(tok::code_completion)) {
1052 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1053 ConsumeCodeCompletionToken();
1054 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001055 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001056 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001057 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 SkipUntil(tok::semi);
1059 }
1060 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001061 if (!ForEach) {
John McCall9ae2f072010-08-23 23:25:46 +00001062 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001063 // Parse the second part of the for specifier.
1064 if (Tok.is(tok::semi)) { // for (...;;
1065 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001066 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001067 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001068 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001069 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1070 else {
1071 Second = ParseExpression();
1072 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001073 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001074 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001075 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001076 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001077 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001078 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001079
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001080 if (Tok.is(tok::semi)) {
1081 ConsumeToken();
1082 } else {
John McCalld226f652010-08-21 09:40:31 +00001083 if (!SecondPartIsInvalid || SecondVar)
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001084 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001085 SkipUntil(tok::semi);
1086 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001087
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001088 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001089 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001090 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001091 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001092 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 // Match the ')'.
1095 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001096
Chris Lattner0ecea032007-08-22 05:28:50 +00001097 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001098 // there is no compound stmt. C90 does not have this clause. We only do this
1099 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001100 //
1101 // C++ 6.5p2:
1102 // The substatement in an iteration-statement implicitly defines a local scope
1103 // which is entered and exited each time through the loop.
1104 //
1105 // See comments in ParseIfStatement for why we create a scope for
1106 // for-init-statement/condition and a new scope for substatement in C++.
1107 //
Mike Stump1eb44332009-09-09 15:08:12 +00001108 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001109 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001110
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001112 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001113
Chris Lattner0ecea032007-08-22 05:28:50 +00001114 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001115 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001116
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001118 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001119
1120 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001121 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001122
1123 if (!ForEach)
John McCall9ae2f072010-08-23 23:25:46 +00001124 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1125 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Douglas Gregor586596f2010-05-06 17:25:47 +00001127 // FIXME: It isn't clear how to communicate the late destruction of
1128 // C++ temporaries used to create the collection.
John McCall9ae2f072010-08-23 23:25:46 +00001129 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1130 Collection.take(), RParenLoc,
1131 Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001132}
1133
1134/// ParseGotoStatement
1135/// jump-statement:
1136/// 'goto' identifier ';'
1137/// [GNU] 'goto' '*' expression ';'
1138///
1139/// Note: this lets the caller parse the end ';'.
1140///
John McCall60d7b3a2010-08-24 06:29:42 +00001141StmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001142 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001143 delete Attr;
1144
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001145 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001147
John McCall60d7b3a2010-08-24 06:29:42 +00001148 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001149 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001150 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 Tok.getIdentifierInfo());
1152 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001153 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 // GNU indirect goto extension.
1155 Diag(Tok, diag::ext_gnu_indirect_goto);
1156 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001157 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001158 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001160 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 }
John McCall9ae2f072010-08-23 23:25:46 +00001162 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001163 } else {
1164 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001165 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001167
Sebastian Redl9a920342008-12-11 19:48:14 +00001168 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001169}
1170
1171/// ParseContinueStatement
1172/// jump-statement:
1173/// 'continue' ';'
1174///
1175/// Note: this lets the caller parse the end ';'.
1176///
John McCall60d7b3a2010-08-24 06:29:42 +00001177StmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001178 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001179 delete Attr;
1180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001182 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001183}
1184
1185/// ParseBreakStatement
1186/// jump-statement:
1187/// 'break' ';'
1188///
1189/// Note: this lets the caller parse the end ';'.
1190///
John McCall60d7b3a2010-08-24 06:29:42 +00001191StmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001192 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001193 delete Attr;
1194
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001196 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001197}
1198
1199/// ParseReturnStatement
1200/// jump-statement:
1201/// 'return' expression[opt] ';'
John McCall60d7b3a2010-08-24 06:29:42 +00001202StmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001203 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001204 delete Attr;
1205
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001206 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001208
John McCall60d7b3a2010-08-24 06:29:42 +00001209 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001210 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001211 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001212 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001213 ConsumeCodeCompletionToken();
1214 SkipUntil(tok::semi, false, true);
1215 return StmtError();
1216 }
1217
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001219 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001221 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 }
1223 }
John McCall9ae2f072010-08-23 23:25:46 +00001224 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001225}
1226
Steve Naroff5f8aa692008-02-11 23:15:56 +00001227/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1228/// routine is called to skip/ignore tokens that comprise the MS asm statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001229StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001230 if (Tok.is(tok::l_brace)) {
1231 unsigned short savedBraceCount = BraceCount;
1232 do {
1233 ConsumeAnyToken();
1234 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001235 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001236 // From the MS website: If used without braces, the __asm keyword means
1237 // that the rest of the line is an assembly-language statement.
1238 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001239 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001240 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001241 do {
1242 ConsumeAnyToken();
1243 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001244 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1245 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001246 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001247 }
Mike Stump95059b52009-12-11 00:04:56 +00001248 Token t;
1249 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001250 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001251 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001252 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001253 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001254 ExprVector Constraints(Actions);
1255 ExprVector Exprs(Actions);
1256 ExprVector Clobbers(Actions);
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001257 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001258 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001259 AsmString.take(), move_arg(Clobbers),
Mike Stump3b11fd32010-01-04 22:37:17 +00001260 Tok.getLocation(), true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001261}
1262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001264/// asm-statement:
1265/// gnu-asm-statement
1266/// ms-asm-statement
1267///
1268/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001269/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1270///
1271/// [GNU] asm-argument:
1272/// asm-string-literal
1273/// asm-string-literal ':' asm-operands[opt]
1274/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1275/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1276/// ':' asm-clobbers
1277///
1278/// [GNU] asm-clobbers:
1279/// asm-string-literal
1280/// asm-clobbers ',' asm-string-literal
1281///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001282/// [MS] ms-asm-statement:
1283/// '__asm' assembly-instruction ';'[opt]
1284/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1285///
1286/// [MS] assembly-instruction-list:
1287/// assembly-instruction ';'[opt]
1288/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1289///
John McCall60d7b3a2010-08-24 06:29:42 +00001290StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001291 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001292 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001293
Steve Naroff5f8aa692008-02-11 23:15:56 +00001294 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001295 msAsm = true;
1296 return FuzzyParseMicrosoftAsmStatement();
1297 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 DeclSpec DS;
1299 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001300 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001301
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1303 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001304 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001306 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001309 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001310 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001311 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001313 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 }
1315 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001316
John McCall60d7b3a2010-08-24 06:29:42 +00001317 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001318 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001319 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001320
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001321 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001322 ExprVector Constraints(Actions);
1323 ExprVector Exprs(Actions);
1324 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001325
Anders Carlssondfab34a2008-02-05 23:03:50 +00001326 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001327 // We have a simple asm expression like 'asm("foo")'.
1328 SourceLocation RParenLoc = ConsumeParen();
1329 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1330 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1331 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001332 AsmString.take(), move_arg(Clobbers),
Chris Lattner64cb4752009-12-20 23:00:41 +00001333 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001335
Chris Lattner64cb4752009-12-20 23:00:41 +00001336 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001337 bool AteExtraColon = false;
1338 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1339 // In C++ mode, parse "::" like ": :".
1340 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001341 ConsumeToken();
1342
Chris Lattner64056462009-12-20 23:08:04 +00001343 if (!AteExtraColon &&
1344 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001345 return StmtError();
1346 }
Chris Lattner64056462009-12-20 23:08:04 +00001347
Chris Lattner64cb4752009-12-20 23:00:41 +00001348 unsigned NumOutputs = Names.size();
1349
1350 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001351 if (AteExtraColon ||
1352 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1353 // In C++ mode, parse "::" like ": :".
1354 if (AteExtraColon)
1355 AteExtraColon = false;
1356 else {
1357 AteExtraColon = Tok.is(tok::coloncolon);
1358 ConsumeToken();
1359 }
1360
1361 if (!AteExtraColon &&
1362 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001363 return StmtError();
1364 }
1365
1366 assert(Names.size() == Constraints.size() &&
1367 Constraints.size() == Exprs.size() &&
1368 "Input operand size mismatch!");
1369
1370 unsigned NumInputs = Names.size() - NumOutputs;
1371
1372 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001373 if (AteExtraColon || Tok.is(tok::colon)) {
1374 if (!AteExtraColon)
1375 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001376
Chandler Carruth102e1b62010-07-22 07:11:21 +00001377 // Parse the asm-string list for clobbers if present.
1378 if (Tok.isNot(tok::r_paren)) {
1379 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001380 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001381
Chandler Carruth102e1b62010-07-22 07:11:21 +00001382 if (Clobber.isInvalid())
1383 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001384
Chandler Carruth102e1b62010-07-22 07:11:21 +00001385 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001386
Chandler Carruth102e1b62010-07-22 07:11:21 +00001387 if (Tok.isNot(tok::comma)) break;
1388 ConsumeToken();
1389 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001390 }
1391 }
1392
1393 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1394 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001395 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001396 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001397 AsmString.take(), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001398 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001399}
1400
1401/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001402/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001403///
1404/// [GNU] asm-operands:
1405/// asm-operand
1406/// asm-operands ',' asm-operand
1407///
1408/// [GNU] asm-operand:
1409/// asm-string-literal '(' expression ')'
1410/// '[' identifier ']' asm-string-literal '(' expression ')'
1411///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001412//
1413// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001414bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1415 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1416 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001418 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001419 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
1421 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001422 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001423 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001426 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 Diag(Tok, diag::err_expected_ident);
1428 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001429 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Anders Carlssonb235fc22007-11-22 01:36:19 +00001432 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001433 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001434
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001435 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001437 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001438 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001439
John McCall60d7b3a2010-08-24 06:29:42 +00001440 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001441 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001442 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001443 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001444 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001445 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001446
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001447 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001448 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001450 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001452
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001454 SourceLocation OpenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001455 ExprResult Res(ParseExpression());
Eli Friedman72056a22009-05-03 07:49:42 +00001456 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001457 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001459 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001460 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001461 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001463 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 ConsumeToken();
1465 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001466
1467 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001468}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001469
John McCalld226f652010-08-21 09:40:31 +00001470Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001471 assert(Tok.is(tok::l_brace));
1472 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001473
John McCallf312b1e2010-08-26 23:41:50 +00001474 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1475 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001477 // Do not enter a scope for the brace, as the arguments are in the same scope
1478 // (the function body) as the body itself. Instead, just read the statement
1479 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001480 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001481
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001482 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001483 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001484 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001485 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001486
John McCall9ae2f072010-08-23 23:25:46 +00001487 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001488}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001489
Sebastian Redld3a413d2009-04-26 20:35:05 +00001490/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1491///
1492/// function-try-block:
1493/// 'try' ctor-initializer[opt] compound-statement handler-seq
1494///
John McCalld226f652010-08-21 09:40:31 +00001495Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001496 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1497 SourceLocation TryLoc = ConsumeToken();
1498
John McCallf312b1e2010-08-26 23:41:50 +00001499 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1500 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001501
1502 // Constructor initializer list?
1503 if (Tok.is(tok::colon))
1504 ParseConstructorInitializer(Decl);
1505
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001506 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001507 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001508 // If we failed to parse the try-catch, we just give the function an empty
1509 // compound statement as the body.
1510 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001511 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001512 MultiStmtArg(Actions), false);
1513
John McCall9ae2f072010-08-23 23:25:46 +00001514 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001515}
1516
Sebastian Redla0fd8652008-12-21 16:41:36 +00001517/// ParseCXXTryBlock - Parse a C++ try-block.
1518///
1519/// try-block:
1520/// 'try' compound-statement handler-seq
1521///
John McCall60d7b3a2010-08-24 06:29:42 +00001522StmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001523 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001524 delete Attr;
1525
Sebastian Redla0fd8652008-12-21 16:41:36 +00001526 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1527
1528 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001529 return ParseCXXTryBlockCommon(TryLoc);
1530}
1531
1532/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1533/// function-try-block.
1534///
1535/// try-block:
1536/// 'try' compound-statement handler-seq
1537///
1538/// function-try-block:
1539/// 'try' ctor-initializer[opt] compound-statement handler-seq
1540///
1541/// handler-seq:
1542/// handler handler-seq[opt]
1543///
John McCall60d7b3a2010-08-24 06:29:42 +00001544StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001545 if (Tok.isNot(tok::l_brace))
1546 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001547 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall60d7b3a2010-08-24 06:29:42 +00001548 StmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001549 if (TryBlock.isInvalid())
1550 return move(TryBlock);
1551
1552 StmtVector Handlers(Actions);
Sean Huntbbd37c62009-11-21 08:43:09 +00001553 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1554 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1555 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1556 << Attr.Range;
1557 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00001558 if (Tok.isNot(tok::kw_catch))
1559 return StmtError(Diag(Tok, diag::err_expected_catch));
1560 while (Tok.is(tok::kw_catch)) {
John McCall60d7b3a2010-08-24 06:29:42 +00001561 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001562 if (!Handler.isInvalid())
1563 Handlers.push_back(Handler.release());
1564 }
1565 // Don't bother creating the full statement if we don't have any usable
1566 // handlers.
1567 if (Handlers.empty())
1568 return StmtError();
1569
John McCall9ae2f072010-08-23 23:25:46 +00001570 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001571}
1572
1573/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1574///
1575/// handler:
1576/// 'catch' '(' exception-declaration ')' compound-statement
1577///
1578/// exception-declaration:
1579/// type-specifier-seq declarator
1580/// type-specifier-seq abstract-declarator
1581/// type-specifier-seq
1582/// '...'
1583///
John McCall60d7b3a2010-08-24 06:29:42 +00001584StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001585 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1586
1587 SourceLocation CatchLoc = ConsumeToken();
1588
1589 SourceLocation LParenLoc = Tok.getLocation();
1590 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1591 return StmtError();
1592
1593 // C++ 3.3.2p3:
1594 // The name in a catch exception-declaration is local to the handler and
1595 // shall not be redeclared in the outermost block of the handler.
1596 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1597
1598 // exception-declaration is equivalent to '...' or a parameter-declaration
1599 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00001600 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001601 if (Tok.isNot(tok::ellipsis)) {
1602 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001603 if (ParseCXXTypeSpecifierSeq(DS))
1604 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001605 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1606 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001607 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001608 } else
1609 ConsumeToken();
1610
1611 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1612 return StmtError();
1613
1614 if (Tok.isNot(tok::l_brace))
1615 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1616
Sean Huntbbd37c62009-11-21 08:43:09 +00001617 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall60d7b3a2010-08-24 06:29:42 +00001618 StmtResult Block(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001619 if (Block.isInvalid())
1620 return move(Block);
1621
John McCall9ae2f072010-08-23 23:25:46 +00001622 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001623}