blob: 2c4ab655aec22af931c78dff40b3fcd59813a842 [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
John McCall7f040a92010-12-24 02:08:15 +000084 ParsedAttributesWithRange attrs;
85 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 // Cases in this switch statement should fall through if the parser expects
88 // the token to end in a semicolon (in which case SemiError should be set),
89 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000090 tok::TokenKind Kind = Tok.getKind();
91 SourceLocation AtLoc;
92 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000093 case tok::at: // May be a @try or @throw statement
94 {
95 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000096 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000097 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000098
Douglas Gregor791215b2009-09-21 20:51:25 +000099 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000100 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorc8bddde2010-05-28 00:22:41 +0000101 ConsumeCodeCompletionToken();
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000102 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor791215b2009-09-21 20:51:25 +0000103
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000104 case tok::identifier:
105 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
106 // identifier ':' statement
John McCall7f040a92010-12-24 02:08:15 +0000107 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000108 }
109 // PASS THROUGH.
110
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000111 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000112 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000113 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000114 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall7f040a92010-12-24 02:08:15 +0000115 DeclEnd, attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000116 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000117 }
118
119 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000121 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Sean Huntbbd37c62009-11-21 08:43:09 +0000124 // FIXME: Use the attributes
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000125 // expression[opt] ';'
John McCall60d7b3a2010-08-24 06:29:42 +0000126 ExprResult Expr(ParseExpression());
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000127 if (Expr.isInvalid()) {
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000128 // If the expression is invalid, skip ahead to the next semicolon or '}'.
129 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000130 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000131 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
132 if (Tok.is(tok::semi))
133 ConsumeToken();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000134 return StmtError();
135 }
136 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000137 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000138 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000139 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000142 return ParseCaseStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000144 return ParseDefaultStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall7f040a92010-12-24 02:08:15 +0000147 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000148 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
149 bool LeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
150 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacro);
151 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall7f040a92010-12-24 02:08:15 +0000154 return ParseIfStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall7f040a92010-12-24 02:08:15 +0000156 return ParseSwitchStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall7f040a92010-12-24 02:08:15 +0000159 return ParseWhileStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000161 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000162 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 break;
164 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall7f040a92010-12-24 02:08:15 +0000165 return ParseForStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
167 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000168 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000169 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 break;
171 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000172 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000173 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 break;
175 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000176 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000177 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 break;
179 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000180 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000181 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000183
Sebastian Redla0fd8652008-12-21 16:41:36 +0000184 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000185 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000186 bool msAsm = false;
187 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000188 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000189 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000190 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 break;
192 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000193
Sebastian Redla0fd8652008-12-21 16:41:36 +0000194 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000195 return ParseCXXTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000196 }
197
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000199 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000201 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000202 // If the result was valid, then we do want to diagnose this. Use
203 // ExpectAndConsume to emit the diagnostic, even though we know it won't
204 // succeed.
205 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000206 // Skip until we see a } or ;, but don't eat it.
207 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Sebastian Redl61364dd2008-12-11 19:30:53 +0000210 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000213/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000214///
215/// labeled-statement:
216/// identifier ':' statement
217/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000218///
John McCall7f040a92010-12-24 02:08:15 +0000219StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000220 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
221 "Not an identifier!");
222
223 Token IdentTok = Tok; // Save the whole token.
224 ConsumeToken(); // eat the identifier.
225
226 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000227
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000228 // identifier ':' statement
229 SourceLocation ColonLoc = ConsumeToken();
230
231 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000232 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000233
John McCall60d7b3a2010-08-24 06:29:42 +0000234 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000235
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000236 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000237 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000238 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner337e5502011-02-18 01:27:55 +0000239
240 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
241 IdentTok.getLocation());
242 if (AttributeList *Attrs = attrs.getList())
243 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
244
245 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
246 SubStmt.get());
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 McCall7f040a92010-12-24 02:08:15 +0000254StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs) {
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?
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner24e1e702009-03-04 04:23:07 +0000258 // It is very very common for code to contain many case statements recursively
259 // nested, as in (but usually without indentation):
260 // case 1:
261 // case 2:
262 // case 3:
263 // case 4:
264 // case 5: etc.
265 //
266 // Parsing this naively works, but is both inefficient and can cause us to run
267 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000268 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000269 // but all the grossness is constrained to ParseCaseStatement (and some
270 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner24e1e702009-03-04 04:23:07 +0000272 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
273 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000274 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner24e1e702009-03-04 04:23:07 +0000276 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
277 // gets updated each time a new case is parsed, and whose body is unset so
278 // far. When parsing 'case 4', this is the 'case 3' node.
279 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattner24e1e702009-03-04 04:23:07 +0000281 // While we have case statements, eat and stack them.
282 do {
283 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000285 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000286 Actions.CodeCompleteCase(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000287 ConsumeCodeCompletionToken();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000288 }
289
Chris Lattner6fb09c82009-12-10 00:38:54 +0000290 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
291 /// Disable this form of error recovery while we're parsing the case
292 /// expression.
293 ColonProtectionRAIIObject ColonProtection(*this);
294
John McCall60d7b3a2010-08-24 06:29:42 +0000295 ExprResult LHS(ParseConstantExpression());
Chris Lattner24e1e702009-03-04 04:23:07 +0000296 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000298 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000300
Chris Lattner24e1e702009-03-04 04:23:07 +0000301 // GNU case range extension.
302 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000303 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000304 if (Tok.is(tok::ellipsis)) {
305 Diag(Tok, diag::ext_gnu_case_range);
306 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000307
Chris Lattner24e1e702009-03-04 04:23:07 +0000308 RHS = ParseConstantExpression();
309 if (RHS.isInvalid()) {
310 SkipUntil(tok::colon);
311 return StmtError();
312 }
313 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000314
315 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000316
Douglas Gregor662a4822010-12-23 22:56:40 +0000317 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000318 if (Tok.is(tok::colon)) {
319 ColonLoc = ConsumeToken();
320
321 // Treat "case blah;" as a typo for "case blah:".
322 } else if (Tok.is(tok::semi)) {
323 ColonLoc = ConsumeToken();
324 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
325 << FixItHint::CreateReplacement(ColonLoc, ":");
326 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000327 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
328 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
329 << FixItHint::CreateInsertion(ExpectedLoc, ":");
330 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000331 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000332
John McCall60d7b3a2010-08-24 06:29:42 +0000333 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000334 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
335 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner24e1e702009-03-04 04:23:07 +0000337 // If we had a sema error parsing this case, then just ignore it and
338 // continue parsing the sub-stmt.
339 if (Case.isInvalid()) {
340 if (TopLevelCase.isInvalid()) // No parsed case stmts.
341 return ParseStatement();
342 // Otherwise, just don't add it as a nested case.
343 } else {
344 // If this is the first case statement we parsed, it becomes TopLevelCase.
345 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000346 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000347 if (TopLevelCase.isInvalid())
348 TopLevelCase = move(Case);
349 else
John McCall9ae2f072010-08-23 23:25:46 +0000350 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000351 DeepestParsedCaseStmt = NextDeepest;
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattner24e1e702009-03-04 04:23:07 +0000354 // Handle all case statements.
355 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner24e1e702009-03-04 04:23:07 +0000357 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner24e1e702009-03-04 04:23:07 +0000359 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000360 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattner24e1e702009-03-04 04:23:07 +0000362 if (Tok.isNot(tok::r_brace)) {
363 SubStmt = ParseStatement();
364 } else {
365 // Nicely diagnose the common error "switch (X) { case 4: }", which is
366 // not valid.
367 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000369 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner24e1e702009-03-04 04:23:07 +0000372 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000373 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000374 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattner24e1e702009-03-04 04:23:07 +0000376 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000377 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000378
Chris Lattner24e1e702009-03-04 04:23:07 +0000379 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000380 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381}
382
383/// ParseDefaultStatement
384/// labeled-statement:
385/// 'default' ':' statement
386/// Note that this does not parse the 'statement' at the end.
387///
John McCall7f040a92010-12-24 02:08:15 +0000388StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000389 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000390
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000391 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
393
Douglas Gregor662a4822010-12-23 22:56:40 +0000394 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000395 if (Tok.is(tok::colon)) {
396 ColonLoc = ConsumeToken();
397
398 // Treat "default;" as a typo for "default:".
399 } else if (Tok.is(tok::semi)) {
400 ColonLoc = ConsumeToken();
401 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
402 << FixItHint::CreateReplacement(ColonLoc, ":");
403 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000404 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
405 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
406 << FixItHint::CreateInsertion(ExpectedLoc, ":");
407 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000411 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000413 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 }
415
John McCall60d7b3a2010-08-24 06:29:42 +0000416 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000417 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000418 return StmtError();
419
Sebastian Redl117054a2008-12-28 16:13:43 +0000420 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000421 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000422}
423
424
425/// ParseCompoundStatement - Parse a "{}" block.
426///
427/// compound-statement: [C99 6.8.2]
428/// { block-item-list[opt] }
429/// [GNU] { label-declarations block-item-list } [TODO]
430///
431/// block-item-list:
432/// block-item
433/// block-item-list block-item
434///
435/// block-item:
436/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000437/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000438/// statement
439/// [OMP] openmp-directive [TODO]
440///
441/// [GNU] label-declarations:
442/// [GNU] label-declaration
443/// [GNU] label-declarations label-declaration
444///
445/// [GNU] label-declaration:
446/// [GNU] '__label__' identifier-list ';'
447///
448/// [OMP] openmp-directive: [TODO]
449/// [OMP] barrier-directive
450/// [OMP] flush-directive
451///
John McCall7f040a92010-12-24 02:08:15 +0000452StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Sean Huntbbd37c62009-11-21 08:43:09 +0000453 bool isStmtExpr) {
454 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000455
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000456 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000457
Chris Lattner31e05722007-08-26 06:24:45 +0000458 // Enter a scope to hold everything within the compound stmt. Compound
459 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000460 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000461
462 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000463 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000464}
465
466
467/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000468/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000469/// consume the '}' at the end of the block. It does not manipulate the scope
470/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000471StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000472 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000473 Tok.getLocation(),
474 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000475 InMessageExpressionRAIIObject InMessage(*this, false);
476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
478
479 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000480 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000481
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000482 StmtVector Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000483 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000484
485 if (Tok.is(tok::annot_pragma_unused)) {
486 HandlePragmaUnused();
487 continue;
488 }
489
John McCall60d7b3a2010-08-24 06:29:42 +0000490 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000491 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000492 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000493 } else {
494 // __extension__ can start declarations and it can also be a unary
495 // operator for expressions. Consume multiple __extension__ markers here
496 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000497 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000498 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000499 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000500 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000501
John McCall7f040a92010-12-24 02:08:15 +0000502 ParsedAttributesWithRange attrs;
503 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000504
Chris Lattner45a566c2007-08-27 01:01:57 +0000505 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000506 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000507 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000508 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000509 ExtensionRAIIObject O(Diags);
510
Chris Lattner97144fc2009-04-02 04:16:50 +0000511 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000512 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
513 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000514 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000515 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000516 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000517 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000518 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000519
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000520 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000521 SkipUntil(tok::semi);
522 continue;
523 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000524
Sean Huntbbd37c62009-11-21 08:43:09 +0000525 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000526 // Eat the semicolon at the end of stmt and convert the expr into a
527 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000528 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000529 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000530 }
531 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000532
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000533 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000534 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000536
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000538 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 Diag(Tok, diag::err_expected_rbrace);
Chris Lattnerf65086b2010-09-01 15:49:26 +0000540 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000541 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000545 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000546 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000547}
548
Chris Lattner15ff1112008-12-12 06:31:07 +0000549/// ParseParenExprOrCondition:
550/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000551/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000552///
553/// This function parses and performs error recovery on the specified condition
554/// or expression (depending on whether we're in C++ or C mode). This function
555/// goes out of its way to recover well. It returns true if there was a parser
556/// error (the right paren couldn't be found), which indicates that the caller
557/// should try to recover harder. It returns false if the condition is
558/// successfully parsed. Note that a successful parse can still have semantic
559/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000560bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000561 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000562 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000563 bool ConvertToBoolean) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000564 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000565 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000566 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000567 else {
568 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000569 DeclResult = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000570
571 // If required, convert to a boolean value.
572 if (!ExprResult.isInvalid() && ConvertToBoolean)
573 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000574 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner15ff1112008-12-12 06:31:07 +0000577 // If the parser was confused by the condition and we don't have a ')', try to
578 // recover by skipping ahead to a semi and bailing out. If condexp is
579 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000580 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000581 SkipUntil(tok::semi);
582 // Skipping may have stopped if it found the containing ')'. If so, we can
583 // continue parsing the if statement.
584 if (Tok.isNot(tok::r_paren))
585 return true;
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner15ff1112008-12-12 06:31:07 +0000588 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000589 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000590 return false;
591}
592
593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594/// ParseIfStatement
595/// if-statement: [C99 6.8.4.1]
596/// 'if' '(' expression ')' statement
597/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000598/// [C++] 'if' '(' condition ')' statement
599/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000600///
John McCall7f040a92010-12-24 02:08:15 +0000601StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000602 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000603
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000604 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
606
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000607 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000608 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000610 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000612
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000613 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
614
Chris Lattner22153252007-08-26 23:08:06 +0000615 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
616 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000617 //
618 // C++ 6.4p3:
619 // A name introduced by a declaration in a condition is in scope from its
620 // point of declaration until the end of the substatements controlled by the
621 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000622 // C++ 3.3.2p4:
623 // Names declared in the for-init-statement, and in the condition of if,
624 // while, for, and switch statements are local to the if, while, for, or
625 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000626 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000627 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000628
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000630 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000631 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000632 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000633 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000634
John McCall9ae2f072010-08-23 23:25:46 +0000635 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Chris Lattner0ecea032007-08-22 05:28:50 +0000637 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000638 // there is no compound stmt. C90 does not have this clause. We only do this
639 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000640 //
641 // C++ 6.4p1:
642 // The substatement in a selection-statement (each substatement, in the else
643 // form of the if statement) implicitly defines a local scope.
644 //
645 // For C++ we create a scope for the condition and a new scope for
646 // substatements because:
647 // -When the 'then' scope exits, we want the condition declaration to still be
648 // active for the 'else' scope too.
649 // -Sema will detect name clashes by considering declarations of a
650 // 'ControlScope' as part of its direct subscope.
651 // -If we wanted the condition and substatement to be in the same scope, we
652 // would have to notify ParseStatement not to create a new scope. It's
653 // simpler to let it create a new scope.
654 //
Mike Stump1eb44332009-09-09 15:08:12 +0000655 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000656 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000657
Chris Lattnerb96728d2007-10-29 05:08:52 +0000658 // Read the 'then' stmt.
659 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000660 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000661
Chris Lattnera36ce712007-08-22 05:16:28 +0000662 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000663 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 // If it has an else, parse it.
666 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000667 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000668 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000669
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000670 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000672 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000673
Chris Lattner0ecea032007-08-22 05:28:50 +0000674 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000675 // there is no compound stmt. C90 does not have this clause. We only do
676 // this if the body isn't a compound statement to avoid push/pop in common
677 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000678 //
679 // C++ 6.4p1:
680 // The substatement in a selection-statement (each substatement, in the else
681 // form of the if statement) implicitly defines a local scope.
682 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000683 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000684 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000687
Chris Lattnera36ce712007-08-22 05:16:28 +0000688 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000689 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000691
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000692 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattner18914bc2008-12-12 06:19:11 +0000694 // If the condition was invalid, discard the if statement. We could recover
695 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000696 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000697 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000698
Chris Lattnerb96728d2007-10-29 05:08:52 +0000699 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000700 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000701 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000702 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
703 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
704 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000705 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000706 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000707 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000708
Chris Lattnerb96728d2007-10-29 05:08:52 +0000709 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000710 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000711 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000712 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000713 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000714
John McCall9ae2f072010-08-23 23:25:46 +0000715 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000716 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718
719/// ParseSwitchStatement
720/// switch-statement:
721/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000722/// [C++] 'switch' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000723StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000724 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000725
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000726 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
728
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000729 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000730 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000732 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 }
Chris Lattner22153252007-08-26 23:08:06 +0000734
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000735 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
736
Chris Lattner22153252007-08-26 23:08:06 +0000737 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
738 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000739 //
740 // C++ 6.4p3:
741 // A name introduced by a declaration in a condition is in scope from its
742 // point of declaration until the end of the substatements controlled by the
743 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000744 // C++ 3.3.2p4:
745 // Names declared in the for-init-statement, and in the condition of if,
746 // while, for, and switch statements are local to the if, while, for, or
747 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000748 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000749 unsigned ScopeFlags = Scope::BreakScope;
750 if (C99orCXX)
751 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000752 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753
754 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000755 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000756 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000757 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +0000758 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000759
John McCall60d7b3a2010-08-24 06:29:42 +0000760 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +0000761 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000762
Douglas Gregor586596f2010-05-06 17:25:47 +0000763 if (Switch.isInvalid()) {
764 // Skip the switch body.
765 // FIXME: This is not optimal recovery, but parsing the body is more
766 // dangerous due to the presence of case and default statements, which
767 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +0000768 if (Tok.is(tok::l_brace)) {
769 ConsumeBrace();
770 SkipUntil(tok::r_brace, false, false);
771 } else
Douglas Gregor586596f2010-05-06 17:25:47 +0000772 SkipUntil(tok::semi);
773 return move(Switch);
774 }
775
Chris Lattner0ecea032007-08-22 05:28:50 +0000776 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000777 // there is no compound stmt. C90 does not have this clause. We only do this
778 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000779 //
780 // C++ 6.4p1:
781 // The substatement in a selection-statement (each substatement, in the else
782 // form of the if statement) implicitly defines a local scope.
783 //
784 // See comments in ParseIfStatement for why we create a scope for the
785 // condition and a new scope for substatement in C++.
786 //
Mike Stump1eb44332009-09-09 15:08:12 +0000787 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000788 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000789
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000791 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000792
Chris Lattner7e52de42010-01-24 01:50:29 +0000793 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000794 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000795 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000796
Chris Lattner7e52de42010-01-24 01:50:29 +0000797 if (Body.isInvalid())
798 // FIXME: Remove the case statement list from the Switch statement.
799 Body = Actions.ActOnNullStmt(Tok.getLocation());
800
John McCall9ae2f072010-08-23 23:25:46 +0000801 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000802}
803
804/// ParseWhileStatement
805/// while-statement: [C99 6.8.5.1]
806/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000807/// [C++] 'while' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000808StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000809 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000810
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000811 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 SourceLocation WhileLoc = Tok.getLocation();
813 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000814
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000815 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000816 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000818 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000820
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000821 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
822
Chris Lattner22153252007-08-26 23:08:06 +0000823 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
824 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000825 //
826 // C++ 6.4p3:
827 // A name introduced by a declaration in a condition is in scope from its
828 // point of declaration until the end of the substatements controlled by the
829 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000830 // C++ 3.3.2p4:
831 // Names declared in the for-init-statement, and in the condition of if,
832 // while, for, and switch statements are local to the if, while, for, or
833 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000834 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000835 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000836 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000837 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
838 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000839 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000840 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
841 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000842
843 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000844 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000845 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000846 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000847 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000848
John McCall9ae2f072010-08-23 23:25:46 +0000849 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattner0ecea032007-08-22 05:28:50 +0000851 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000852 // there is no compound stmt. C90 does not have this clause. We only do this
853 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000854 //
855 // C++ 6.5p2:
856 // The substatement in an iteration-statement implicitly defines a local scope
857 // which is entered and exited each time through the loop.
858 //
859 // See comments in ParseIfStatement for why we create a scope for the
860 // condition and a new scope for substatement in C++.
861 //
Mike Stump1eb44332009-09-09 15:08:12 +0000862 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000863 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000864
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000866 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000867
Chris Lattner0ecea032007-08-22 05:28:50 +0000868 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000869 InnerScope.Exit();
870 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000871
John McCalld226f652010-08-21 09:40:31 +0000872 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000873 return StmtError();
874
John McCall9ae2f072010-08-23 23:25:46 +0000875 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000876}
877
878/// ParseDoStatement
879/// do-statement: [C99 6.8.5.2]
880/// 'do' statement 'while' '(' expression ')' ';'
881/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +0000882StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000883 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000884
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000885 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000887
Chris Lattner22153252007-08-26 23:08:06 +0000888 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
889 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000890 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000891 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000892 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000893 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000894 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000895
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000896 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000897
Chris Lattner0ecea032007-08-22 05:28:50 +0000898 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000899 // there is no compound stmt. C90 does not have this clause. We only do this
900 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000901 //
902 // C++ 6.5p2:
903 // The substatement in an iteration-statement implicitly defines a local scope
904 // which is entered and exited each time through the loop.
905 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000906 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000907 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000908 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000909
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000911 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000912
Chris Lattner0ecea032007-08-22 05:28:50 +0000913 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000914 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000915
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000916 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000917 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000918 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000919 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000920 SkipUntil(tok::semi, false, true);
921 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000922 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 }
924 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000925
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000926 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000927 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000928 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000929 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000931
Chris Lattnerff871fb2008-12-12 06:35:28 +0000932 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000933 SourceLocation LPLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +0000934 ExprResult Cond = ParseExpression();
Douglas Gregor04895d32009-11-24 21:34:32 +0000935 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000936 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000937
Sebastian Redl9a920342008-12-11 19:48:14 +0000938 if (Cond.isInvalid() || Body.isInvalid())
939 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000940
John McCall9ae2f072010-08-23 23:25:46 +0000941 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
942 Cond.get(), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000943}
944
945/// ParseForStatement
946/// for-statement: [C99 6.8.5.3]
947/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
948/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000949/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
950/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000951/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
952/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000953///
954/// [C++] for-init-statement:
955/// [C++] expression-statement
956/// [C++] simple-declaration
957///
John McCall7f040a92010-12-24 02:08:15 +0000958StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000959 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000960
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000961 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000963
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000964 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000965 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000967 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000969
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000970 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000971
Chris Lattner22153252007-08-26 23:08:06 +0000972 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
973 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000974 //
975 // C++ 6.4p3:
976 // A name introduced by a declaration in a condition is in scope from its
977 // point of declaration until the end of the substatements controlled by the
978 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000979 // C++ 3.3.2p4:
980 // Names declared in the for-init-statement, and in the condition of if,
981 // while, for, and switch statements are local to the if, while, for, or
982 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000983 // C++ 6.5.3p1:
984 // Names declared in the for-init-statement are in the same declarative-region
985 // as those declared in the condition.
986 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000987 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000988 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000989 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
990 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000991 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000992 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
993
994 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000995
996 SourceLocation LParenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +0000997 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000998
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000999 bool ForEach = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001000 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001001 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001002 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001003 ExprResult Collection;
Douglas Gregor586596f2010-05-06 17:25:47 +00001004 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001005 Decl *SecondVar = 0;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001006
Douglas Gregor791215b2009-09-21 20:51:25 +00001007 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001008 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001009 C99orCXXorObjC? Sema::PCC_ForInit
1010 : Sema::PCC_Expression);
Douglas Gregordc845342010-05-25 05:58:43 +00001011 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +00001012 }
1013
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001015 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 // no first part, eat the ';'.
1017 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001018 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001020 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001022
John McCall7f040a92010-12-24 02:08:15 +00001023 ParsedAttributesWithRange attrs;
1024 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001025
Chris Lattner97144fc2009-04-02 04:16:50 +00001026 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001027 StmtVector Stmts(Actions);
1028 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
John McCall7f040a92010-12-24 02:08:15 +00001029 DeclEnd, attrs, false);
Chris Lattnercd147752009-03-29 17:27:48 +00001030 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattnercd147752009-03-29 17:27:48 +00001032 if (Tok.is(tok::semi)) { // for (int x = 4;
1033 ConsumeToken();
1034 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001035 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001036 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001037 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001038
1039 if (Tok.is(tok::code_completion)) {
1040 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1041 ConsumeCodeCompletionToken();
1042 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001043 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001044 } else {
1045 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001046 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 } else {
1048 Value = ParseExpression();
1049
John McCallf6a16482010-12-04 03:47:34 +00001050 ForEach = isTokIdentifier_in();
1051
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001053 if (!Value.isInvalid()) {
1054 if (ForEach)
1055 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1056 else
1057 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1058 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001059
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001060 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001062 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001063 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001064
1065 if (Tok.is(tok::code_completion)) {
1066 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1067 ConsumeCodeCompletionToken();
1068 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001069 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001070 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001071 if (!Value.isInvalid()) {
1072 Diag(Tok, diag::err_expected_semi_for);
1073 } else {
1074 // Skip until semicolon or rparen, don't consume it.
1075 SkipUntil(tok::r_paren, true, true);
1076 if (Tok.is(tok::semi))
1077 ConsumeToken();
1078 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 }
1080 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001081 if (!ForEach) {
John McCall9ae2f072010-08-23 23:25:46 +00001082 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001083 // Parse the second part of the for specifier.
1084 if (Tok.is(tok::semi)) { // for (...;;
1085 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001086 } else if (Tok.is(tok::r_paren)) {
1087 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001088 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001089 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001090 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001091 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1092 else {
1093 Second = ParseExpression();
1094 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001095 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001096 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001097 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001098 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001099 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001100 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001101
Douglas Gregorb72c7782011-02-17 03:38:46 +00001102 if (Tok.isNot(tok::semi)) {
1103 if (!SecondPartIsInvalid || SecondVar)
1104 Diag(Tok, diag::err_expected_semi_for);
1105 else
1106 // Skip until semicolon or rparen, don't consume it.
1107 SkipUntil(tok::r_paren, true, true);
1108 }
1109
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001110 if (Tok.is(tok::semi)) {
1111 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001112 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001113
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001114 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001115 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001116 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001117 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001118 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 // Match the ')'.
1121 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001122
Chris Lattner0ecea032007-08-22 05:28:50 +00001123 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001124 // there is no compound stmt. C90 does not have this clause. We only do this
1125 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001126 //
1127 // C++ 6.5p2:
1128 // The substatement in an iteration-statement implicitly defines a local scope
1129 // which is entered and exited each time through the loop.
1130 //
1131 // See comments in ParseIfStatement for why we create a scope for
1132 // for-init-statement/condition and a new scope for substatement in C++.
1133 //
Mike Stump1eb44332009-09-09 15:08:12 +00001134 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001135 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001136
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001138 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001139
Chris Lattner0ecea032007-08-22 05:28:50 +00001140 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001141 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001142
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001144 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001145
1146 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001147 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001148
1149 if (!ForEach)
John McCall9ae2f072010-08-23 23:25:46 +00001150 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1151 SecondVar, ThirdPart, RParenLoc, Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor586596f2010-05-06 17:25:47 +00001153 // FIXME: It isn't clear how to communicate the late destruction of
1154 // C++ temporaries used to create the collection.
John McCall9ae2f072010-08-23 23:25:46 +00001155 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart.take(),
1156 Collection.take(), RParenLoc,
1157 Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001158}
1159
1160/// ParseGotoStatement
1161/// jump-statement:
1162/// 'goto' identifier ';'
1163/// [GNU] 'goto' '*' expression ';'
1164///
1165/// Note: this lets the caller parse the end ';'.
1166///
John McCall7f040a92010-12-24 02:08:15 +00001167StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001168 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001169
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001170 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001172
John McCall60d7b3a2010-08-24 06:29:42 +00001173 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001174 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001175 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1176 Tok.getLocation());
1177 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001179 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // GNU indirect goto extension.
1181 Diag(Tok, diag::ext_gnu_indirect_goto);
1182 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001183 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001184 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001186 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 }
John McCall9ae2f072010-08-23 23:25:46 +00001188 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001189 } else {
1190 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001191 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001193
Sebastian Redl9a920342008-12-11 19:48:14 +00001194 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001195}
1196
1197/// ParseContinueStatement
1198/// jump-statement:
1199/// 'continue' ';'
1200///
1201/// Note: this lets the caller parse the end ';'.
1202///
John McCall7f040a92010-12-24 02:08:15 +00001203StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001204 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001205
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001207 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001208}
1209
1210/// ParseBreakStatement
1211/// jump-statement:
1212/// 'break' ';'
1213///
1214/// Note: this lets the caller parse the end ';'.
1215///
John McCall7f040a92010-12-24 02:08:15 +00001216StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001217 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001220 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001221}
1222
1223/// ParseReturnStatement
1224/// jump-statement:
1225/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001226StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001227 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001228
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001229 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001231
John McCall60d7b3a2010-08-24 06:29:42 +00001232 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001233 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001234 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001235 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001236 ConsumeCodeCompletionToken();
1237 SkipUntil(tok::semi, false, true);
1238 return StmtError();
1239 }
1240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001242 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001244 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 }
1246 }
John McCall9ae2f072010-08-23 23:25:46 +00001247 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001248}
1249
Steve Naroff5f8aa692008-02-11 23:15:56 +00001250/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1251/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001252StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1253 SourceLocation EndLoc;
Steve Naroffb746ce82008-02-07 23:24:32 +00001254 if (Tok.is(tok::l_brace)) {
1255 unsigned short savedBraceCount = BraceCount;
1256 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001257 EndLoc = Tok.getLocation();
Steve Naroffb746ce82008-02-07 23:24:32 +00001258 ConsumeAnyToken();
1259 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001260 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001261 // From the MS website: If used without braces, the __asm keyword means
1262 // that the rest of the line is an assembly-language statement.
1263 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001264 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001265 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001266 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001267 EndLoc = TokLoc;
Steve Naroff36280972008-02-08 18:01:27 +00001268 ConsumeAnyToken();
1269 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001270 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1271 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001272 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001273 }
Mike Stump95059b52009-12-11 00:04:56 +00001274 Token t;
1275 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001276 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001277 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001278 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001279 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001280 ExprVector Constraints(Actions);
1281 ExprVector Exprs(Actions);
1282 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001283 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001284 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001285 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001286 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001287}
1288
Reid Spencer5f016e22007-07-11 17:01:13 +00001289/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001290/// asm-statement:
1291/// gnu-asm-statement
1292/// ms-asm-statement
1293///
1294/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001295/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1296///
1297/// [GNU] asm-argument:
1298/// asm-string-literal
1299/// asm-string-literal ':' asm-operands[opt]
1300/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1301/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1302/// ':' asm-clobbers
1303///
1304/// [GNU] asm-clobbers:
1305/// asm-string-literal
1306/// asm-clobbers ',' asm-string-literal
1307///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001308/// [MS] ms-asm-statement:
1309/// '__asm' assembly-instruction ';'[opt]
1310/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1311///
1312/// [MS] assembly-instruction-list:
1313/// assembly-instruction ';'[opt]
1314/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1315///
John McCall60d7b3a2010-08-24 06:29:42 +00001316StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001317 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001318 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001319
Steve Naroff5f8aa692008-02-11 23:15:56 +00001320 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001321 msAsm = true;
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001322 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001323 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 DeclSpec DS;
1325 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001326 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001327
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1329 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001330 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001332 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001333
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001335 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001336 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001337 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001339 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 }
1341 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001342
John McCall60d7b3a2010-08-24 06:29:42 +00001343 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001344 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001345 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001346
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001347 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001348 ExprVector Constraints(Actions);
1349 ExprVector Exprs(Actions);
1350 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001351
Anders Carlssondfab34a2008-02-05 23:03:50 +00001352 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001353 // We have a simple asm expression like 'asm("foo")'.
1354 SourceLocation RParenLoc = ConsumeParen();
1355 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1356 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1357 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001358 AsmString.take(), move_arg(Clobbers),
Chris Lattner64cb4752009-12-20 23:00:41 +00001359 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001361
Chris Lattner64cb4752009-12-20 23:00:41 +00001362 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001363 bool AteExtraColon = false;
1364 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1365 // In C++ mode, parse "::" like ": :".
1366 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001367 ConsumeToken();
1368
Chris Lattner64056462009-12-20 23:08:04 +00001369 if (!AteExtraColon &&
1370 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001371 return StmtError();
1372 }
Chris Lattner64056462009-12-20 23:08:04 +00001373
Chris Lattner64cb4752009-12-20 23:00:41 +00001374 unsigned NumOutputs = Names.size();
1375
1376 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001377 if (AteExtraColon ||
1378 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1379 // In C++ mode, parse "::" like ": :".
1380 if (AteExtraColon)
1381 AteExtraColon = false;
1382 else {
1383 AteExtraColon = Tok.is(tok::coloncolon);
1384 ConsumeToken();
1385 }
1386
1387 if (!AteExtraColon &&
1388 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001389 return StmtError();
1390 }
1391
1392 assert(Names.size() == Constraints.size() &&
1393 Constraints.size() == Exprs.size() &&
1394 "Input operand size mismatch!");
1395
1396 unsigned NumInputs = Names.size() - NumOutputs;
1397
1398 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001399 if (AteExtraColon || Tok.is(tok::colon)) {
1400 if (!AteExtraColon)
1401 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001402
Chandler Carruth102e1b62010-07-22 07:11:21 +00001403 // Parse the asm-string list for clobbers if present.
1404 if (Tok.isNot(tok::r_paren)) {
1405 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001406 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001407
Chandler Carruth102e1b62010-07-22 07:11:21 +00001408 if (Clobber.isInvalid())
1409 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001410
Chandler Carruth102e1b62010-07-22 07:11:21 +00001411 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001412
Chandler Carruth102e1b62010-07-22 07:11:21 +00001413 if (Tok.isNot(tok::comma)) break;
1414 ConsumeToken();
1415 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001416 }
1417 }
1418
1419 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1420 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001421 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001422 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001423 AsmString.take(), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001424 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001425}
1426
1427/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001428/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001429///
1430/// [GNU] asm-operands:
1431/// asm-operand
1432/// asm-operands ',' asm-operand
1433///
1434/// [GNU] asm-operand:
1435/// asm-string-literal '(' expression ')'
1436/// '[' identifier ']' asm-string-literal '(' expression ')'
1437///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001438//
1439// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001440bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1441 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1442 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001444 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001445 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
1447 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001449 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001452 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 Diag(Tok, diag::err_expected_ident);
1454 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001455 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 }
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Anders Carlssonb235fc22007-11-22 01:36:19 +00001458 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001459 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001460
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001461 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001463 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001464 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001465
John McCall60d7b3a2010-08-24 06:29:42 +00001466 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001467 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001468 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001469 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001470 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001471 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001472
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001473 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001474 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001476 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001478
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001480 SourceLocation OpenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001481 ExprResult Res(ParseExpression());
Eli Friedman72056a22009-05-03 07:49:42 +00001482 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001483 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001484 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001485 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001486 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001487 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001489 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001490 ConsumeToken();
1491 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001492
1493 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001494}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001495
John McCalld226f652010-08-21 09:40:31 +00001496Decl *Parser::ParseFunctionStatementBody(Decl *Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001497 assert(Tok.is(tok::l_brace));
1498 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001499
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001500 if (PP.isCodeCompletionEnabled())
1501 if (trySkippingFunctionBodyForCodeCompletion())
1502 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidis3437f1f2011-01-03 19:44:02 +00001503
John McCallf312b1e2010-08-26 23:41:50 +00001504 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1505 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001507 // Do not enter a scope for the brace, as the arguments are in the same scope
1508 // (the function body) as the body itself. Instead, just read the statement
1509 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001510 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001511
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001512 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001513 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001514 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001515 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001516
John McCall9ae2f072010-08-23 23:25:46 +00001517 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001518}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001519
Sebastian Redld3a413d2009-04-26 20:35:05 +00001520/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1521///
1522/// function-try-block:
1523/// 'try' ctor-initializer[opt] compound-statement handler-seq
1524///
John McCalld226f652010-08-21 09:40:31 +00001525Decl *Parser::ParseFunctionTryBlock(Decl *Decl) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001526 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1527 SourceLocation TryLoc = ConsumeToken();
1528
John McCallf312b1e2010-08-26 23:41:50 +00001529 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1530 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001531
1532 // Constructor initializer list?
1533 if (Tok.is(tok::colon))
1534 ParseConstructorInitializer(Decl);
1535
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001536 if (PP.isCodeCompletionEnabled())
1537 if (trySkippingFunctionBodyForCodeCompletion())
1538 return Actions.ActOnFinishFunctionBody(Decl, 0);
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001539
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001540 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001541 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001542 // If we failed to parse the try-catch, we just give the function an empty
1543 // compound statement as the body.
1544 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001545 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001546 MultiStmtArg(Actions), false);
1547
John McCall9ae2f072010-08-23 23:25:46 +00001548 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001549}
1550
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001551bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001552 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001553 assert(PP.isCodeCompletionEnabled() &&
1554 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001555
1556 // We're in code-completion mode. Skip parsing for all function bodies unless
1557 // the body contains the code-completion point.
1558 TentativeParsingAction PA(*this);
1559 ConsumeBrace();
1560 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1561 /*StopAtCodeCompletion=*/true)) {
1562 PA.Commit();
1563 return true;
1564 }
1565
1566 PA.Revert();
1567 return false;
1568}
1569
Sebastian Redla0fd8652008-12-21 16:41:36 +00001570/// ParseCXXTryBlock - Parse a C++ try-block.
1571///
1572/// try-block:
1573/// 'try' compound-statement handler-seq
1574///
John McCall7f040a92010-12-24 02:08:15 +00001575StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001576 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001577
Sebastian Redla0fd8652008-12-21 16:41:36 +00001578 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1579
1580 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001581 return ParseCXXTryBlockCommon(TryLoc);
1582}
1583
1584/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1585/// function-try-block.
1586///
1587/// try-block:
1588/// 'try' compound-statement handler-seq
1589///
1590/// function-try-block:
1591/// 'try' ctor-initializer[opt] compound-statement handler-seq
1592///
1593/// handler-seq:
1594/// handler handler-seq[opt]
1595///
John McCall60d7b3a2010-08-24 06:29:42 +00001596StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001597 if (Tok.isNot(tok::l_brace))
1598 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001599 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall7f040a92010-12-24 02:08:15 +00001600 ParsedAttributesWithRange attrs;
1601 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001602 if (TryBlock.isInvalid())
1603 return move(TryBlock);
1604
1605 StmtVector Handlers(Actions);
John McCall7f040a92010-12-24 02:08:15 +00001606 MaybeParseCXX0XAttributes(attrs);
1607 ProhibitAttributes(attrs);
1608
Sebastian Redla0fd8652008-12-21 16:41:36 +00001609 if (Tok.isNot(tok::kw_catch))
1610 return StmtError(Diag(Tok, diag::err_expected_catch));
1611 while (Tok.is(tok::kw_catch)) {
John McCall60d7b3a2010-08-24 06:29:42 +00001612 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001613 if (!Handler.isInvalid())
1614 Handlers.push_back(Handler.release());
1615 }
1616 // Don't bother creating the full statement if we don't have any usable
1617 // handlers.
1618 if (Handlers.empty())
1619 return StmtError();
1620
John McCall9ae2f072010-08-23 23:25:46 +00001621 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001622}
1623
1624/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1625///
1626/// handler:
1627/// 'catch' '(' exception-declaration ')' compound-statement
1628///
1629/// exception-declaration:
1630/// type-specifier-seq declarator
1631/// type-specifier-seq abstract-declarator
1632/// type-specifier-seq
1633/// '...'
1634///
John McCall60d7b3a2010-08-24 06:29:42 +00001635StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001636 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1637
1638 SourceLocation CatchLoc = ConsumeToken();
1639
1640 SourceLocation LParenLoc = Tok.getLocation();
1641 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1642 return StmtError();
1643
1644 // C++ 3.3.2p3:
1645 // The name in a catch exception-declaration is local to the handler and
1646 // shall not be redeclared in the outermost block of the handler.
1647 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1648
1649 // exception-declaration is equivalent to '...' or a parameter-declaration
1650 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00001651 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001652 if (Tok.isNot(tok::ellipsis)) {
1653 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001654 if (ParseCXXTypeSpecifierSeq(DS))
1655 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001656 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1657 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001658 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001659 } else
1660 ConsumeToken();
1661
1662 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1663 return StmtError();
1664
1665 if (Tok.isNot(tok::l_brace))
1666 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1667
Sean Huntbbd37c62009-11-21 08:43:09 +00001668 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall7f040a92010-12-24 02:08:15 +00001669 ParsedAttributes attrs;
1670 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001671 if (Block.isInvalid())
1672 return move(Block);
1673
John McCall9ae2f072010-08-23 23:25:46 +00001674 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001675}