blob: cd467dd720f6ae829f880f3a888b38d3ce93426c [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
John Wiegley28bbe4b2011-04-28 01:08:34 +000043/// [MS] seh-try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000044/// [OBC] objc-throw-statement
45/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000046/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000047/// [GNU] asm-statement
48/// [OMP] openmp-construct [TODO]
49///
50/// labeled-statement:
51/// identifier ':' statement
52/// 'case' constant-expression ':' statement
53/// 'default' ':' statement
54///
55/// selection-statement:
56/// if-statement
57/// switch-statement
58///
59/// iteration-statement:
60/// while-statement
61/// do-statement
62/// for-statement
63///
64/// expression-statement:
65/// expression[opt] ';'
66///
67/// jump-statement:
68/// 'goto' identifier ';'
69/// 'continue' ';'
70/// 'break' ';'
71/// 'return' expression[opt] ';'
72/// [GNU] 'goto' '*' expression ';'
73///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000074/// [OBC] objc-throw-statement:
75/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000076/// [OBC] '@' 'throw' ';'
77///
John McCall60d7b3a2010-08-24 06:29:42 +000078StmtResult
Nico Weber5cb94a72011-12-22 23:26:17 +000079Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
80 SourceLocation *TrailingElseLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 const char *SemiError = 0;
John McCall60d7b3a2010-08-24 06:29:42 +000082 StmtResult Res;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000083
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000085
John McCall0b7e6782011-03-24 11:26:52 +000086 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000087 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +000088
Reid Spencer5f016e22007-07-11 17:01:13 +000089 // Cases in this switch statement should fall through if the parser expects
90 // the token to end in a semicolon (in which case SemiError should be set),
91 // or they directly 'return;' if not.
Douglas Gregor312eadb2011-04-24 05:37:28 +000092Retry:
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000093 tok::TokenKind Kind = Tok.getKind();
94 SourceLocation AtLoc;
95 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000096 case tok::at: // May be a @try or @throw statement
97 {
98 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000099 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000100 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000101
Douglas Gregor791215b2009-09-21 20:51:25 +0000102 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000103 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000104 cutOffParsing();
105 return StmtError();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000106
Douglas Gregor312eadb2011-04-24 05:37:28 +0000107 case tok::identifier: {
108 Token Next = NextToken();
109 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000110 // identifier ':' statement
John McCall7f040a92010-12-24 02:08:15 +0000111 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000112 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000113
Douglas Gregor3b887352011-04-27 04:48:22 +0000114 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000115 CXXScopeSpec SS;
116 IdentifierInfo *Name = Tok.getIdentifierInfo();
117 SourceLocation NameLoc = Tok.getLocation();
Richard Trieu950be712011-09-19 19:01:00 +0000118
119 if (getLang().CPlusPlus)
120 CheckForTemplateAndDigraph(Next, ParsedType(),
121 /*EnteringContext=*/false, *Name, SS);
122
Douglas Gregor312eadb2011-04-24 05:37:28 +0000123 Sema::NameClassification Classification
124 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
125 switch (Classification.getKind()) {
126 case Sema::NC_Keyword:
127 // The identifier was corrected to a keyword. Update the token
128 // to this keyword, and try again.
129 if (Name->getTokenID() != tok::identifier) {
130 Tok.setIdentifierInfo(Name);
131 Tok.setKind(Name->getTokenID());
132 goto Retry;
133 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000134
Douglas Gregor312eadb2011-04-24 05:37:28 +0000135 // Fall through via the normal error path.
136 // FIXME: This seems like it could only happen for context-sensitive
137 // keywords.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000138
Douglas Gregor312eadb2011-04-24 05:37:28 +0000139 case Sema::NC_Error:
140 // Handle errors here by skipping up to the next semicolon or '}', and
141 // eat the semicolon if that's what stopped us.
142 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
143 if (Tok.is(tok::semi))
144 ConsumeToken();
145 return StmtError();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000146
Douglas Gregor312eadb2011-04-24 05:37:28 +0000147 case Sema::NC_Unknown:
148 // Either we don't know anything about this identifier, or we know that
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000149 // we're in a syntactic context we haven't handled yet.
150 break;
151
Douglas Gregord9d75e52011-04-27 05:41:15 +0000152 case Sema::NC_Type:
Douglas Gregor3b887352011-04-27 04:48:22 +0000153 Tok.setKind(tok::annot_typename);
154 setTypeAnnotation(Tok, Classification.getType());
155 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor3b887352011-04-27 04:48:22 +0000156 PP.AnnotateCachedTokens(Tok);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000157 break;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000158
Douglas Gregor312eadb2011-04-24 05:37:28 +0000159 case Sema::NC_Expression:
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000160 Tok.setKind(tok::annot_primary_expr);
161 setExprAnnotation(Tok, Classification.getExpression());
162 Tok.setAnnotationEndLoc(NameLoc);
163 PP.AnnotateCachedTokens(Tok);
164 break;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000165
Douglas Gregor312eadb2011-04-24 05:37:28 +0000166 case Sema::NC_TypeTemplate:
167 case Sema::NC_FunctionTemplate: {
168 ConsumeToken(); // the identifier
169 UnqualifiedId Id;
170 Id.setIdentifier(Name, NameLoc);
171 if (AnnotateTemplateIdToken(
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000172 TemplateTy::make(Classification.getTemplateName()),
Douglas Gregor312eadb2011-04-24 05:37:28 +0000173 Classification.getTemplateNameKind(),
Douglas Gregor3b887352011-04-27 04:48:22 +0000174 SS, Id, SourceLocation(),
175 /*AllowTypeAnnotation=*/false)) {
176 // Handle errors here by skipping up to the next semicolon or '}', and
177 // eat the semicolon if that's what stopped us.
178 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
179 if (Tok.is(tok::semi))
180 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000181 return StmtError();
Douglas Gregor3b887352011-04-27 04:48:22 +0000182 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000183
184 // If the next token is '::', jump right into parsing a
Douglas Gregor3b887352011-04-27 04:48:22 +0000185 // nested-name-specifier. We don't want to leave the template-id
186 // hanging.
187 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor312eadb2011-04-24 05:37:28 +0000188 // Handle errors here by skipping up to the next semicolon or '}', and
189 // eat the semicolon if that's what stopped us.
190 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
191 if (Tok.is(tok::semi))
192 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000193 return StmtError();
Douglas Gregor312eadb2011-04-24 05:37:28 +0000194 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000195
Douglas Gregor312eadb2011-04-24 05:37:28 +0000196 // We've annotated a template-id, so try again now.
197 goto Retry;
198 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000199
Douglas Gregor312eadb2011-04-24 05:37:28 +0000200 case Sema::NC_NestedNameSpecifier:
201 // FIXME: Implement this!
202 break;
203 }
204 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000205
Douglas Gregor312eadb2011-04-24 05:37:28 +0000206 // Fall through
207 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000208
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000209 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000210 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000211 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000212 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall7f040a92010-12-24 02:08:15 +0000213 DeclEnd, attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000214 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000215 }
216
217 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000219 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000222 return ParseExprStatement(attrs);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000223 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000224
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000226 return ParseCaseStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000228 return ParseDefaultStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall7f040a92010-12-24 02:08:15 +0000231 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000232 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidise2ca8282011-09-01 21:53:45 +0000233 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
234 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000235 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 case tok::kw_if: // C99 6.8.4.1: if-statement
Nico Weber5cb94a72011-12-22 23:26:17 +0000238 return ParseIfStatement(attrs, TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Nico Weber5cb94a72011-12-22 23:26:17 +0000240 return ParseSwitchStatement(attrs, TrailingElseLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000241
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 case tok::kw_while: // C99 6.8.5.1: while-statement
Nico Weber5cb94a72011-12-22 23:26:17 +0000243 return ParseWhileStatement(attrs, TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000245 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000246 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 break;
248 case tok::kw_for: // C99 6.8.5.3: for-statement
Nico Weber5cb94a72011-12-22 23:26:17 +0000249 return ParseForStatement(attrs, TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250
251 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000252 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000253 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 break;
255 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000256 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000257 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 break;
259 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000260 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 break;
263 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000264 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000265 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000267
Sebastian Redla0fd8652008-12-21 16:41:36 +0000268 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000269 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000270 bool msAsm = false;
271 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000272 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000273 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000274 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 break;
276 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000277
Sebastian Redla0fd8652008-12-21 16:41:36 +0000278 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000279 return ParseCXXTryBlock(attrs);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000280
281 case tok::kw___try:
282 return ParseSEHTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000283 }
284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000286 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000288 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000289 // If the result was valid, then we do want to diagnose this. Use
290 // ExpectAndConsume to emit the diagnostic, even though we know it won't
291 // succeed.
292 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000293 // Skip until we see a } or ;, but don't eat it.
294 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Sebastian Redl61364dd2008-12-11 19:30:53 +0000297 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298}
299
Douglas Gregor312eadb2011-04-24 05:37:28 +0000300/// \brief Parse an expression statement.
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000301StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000302 // If a case keyword is missing, this is where it should be inserted.
303 Token OldToken = Tok;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000304
Douglas Gregor312eadb2011-04-24 05:37:28 +0000305 // FIXME: Use the attributes
306 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000307 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000308 if (Expr.isInvalid()) {
309 // If the expression is invalid, skip ahead to the next semicolon or '}'.
310 // Not doing this opens us up to the possibility of infinite loops if
311 // ParseExpression does not consume any tokens.
312 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
313 if (Tok.is(tok::semi))
314 ConsumeToken();
315 return StmtError();
316 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000317
Douglas Gregor312eadb2011-04-24 05:37:28 +0000318 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
319 Actions.CheckCaseExpression(Expr.get())) {
320 // If a constant expression is followed by a colon inside a switch block,
321 // suggest a missing case keyword.
322 Diag(OldToken, diag::err_expected_case_before_expression)
323 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000324
Douglas Gregor312eadb2011-04-24 05:37:28 +0000325 // Recover parsing as a case statement.
326 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
327 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000328
Douglas Gregor312eadb2011-04-24 05:37:28 +0000329 // Otherwise, eat the semicolon.
330 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
331 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley28bbe4b2011-04-28 01:08:34 +0000332}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000333
John Wiegley28bbe4b2011-04-28 01:08:34 +0000334StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
335 assert(Tok.is(tok::kw___try) && "Expected '__try'");
336 SourceLocation Loc = ConsumeToken();
337 return ParseSEHTryBlockCommon(Loc);
338}
339
340/// ParseSEHTryBlockCommon
341///
342/// seh-try-block:
343/// '__try' compound-statement seh-handler
344///
345/// seh-handler:
346/// seh-except-block
347/// seh-finally-block
348///
349StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
350 if(Tok.isNot(tok::l_brace))
351 return StmtError(Diag(Tok,diag::err_expected_lbrace));
352
353 ParsedAttributesWithRange attrs(AttrFactory);
354 StmtResult TryBlock(ParseCompoundStatement(attrs));
355 if(TryBlock.isInvalid())
356 return move(TryBlock);
357
358 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +0000359 if (Tok.is(tok::identifier) &&
360 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000361 SourceLocation Loc = ConsumeToken();
362 Handler = ParseSEHExceptBlock(Loc);
363 } else if (Tok.is(tok::kw___finally)) {
364 SourceLocation Loc = ConsumeToken();
365 Handler = ParseSEHFinallyBlock(Loc);
366 } else {
367 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
368 }
369
370 if(Handler.isInvalid())
371 return move(Handler);
372
373 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
374 TryLoc,
375 TryBlock.take(),
376 Handler.take());
377}
378
379/// ParseSEHExceptBlock - Handle __except
380///
381/// seh-except-block:
382/// '__except' '(' seh-filter-expression ')' compound-statement
383///
384StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
385 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
386 raii2(Ident___exception_code, false),
387 raii3(Ident_GetExceptionCode, false);
388
389 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
390 return StmtError();
391
392 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
393
Francois Pichetd7f02df2011-04-28 03:14:31 +0000394 if (getLang().Borland) {
395 Ident__exception_info->setIsPoisoned(false);
396 Ident___exception_info->setIsPoisoned(false);
397 Ident_GetExceptionInfo->setIsPoisoned(false);
398 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000399 ExprResult FilterExpr(ParseExpression());
Francois Pichetd7f02df2011-04-28 03:14:31 +0000400
401 if (getLang().Borland) {
402 Ident__exception_info->setIsPoisoned(true);
403 Ident___exception_info->setIsPoisoned(true);
404 Ident_GetExceptionInfo->setIsPoisoned(true);
405 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000406
407 if(FilterExpr.isInvalid())
408 return StmtError();
409
410 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
411 return StmtError();
412
413 ParsedAttributesWithRange attrs(AttrFactory);
414 StmtResult Block(ParseCompoundStatement(attrs));
415
416 if(Block.isInvalid())
417 return move(Block);
418
419 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
420}
421
422/// ParseSEHFinallyBlock - Handle __finally
423///
424/// seh-finally-block:
425/// '__finally' compound-statement
426///
427StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
428 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
429 raii2(Ident___abnormal_termination, false),
430 raii3(Ident_AbnormalTermination, false);
431
432 ParsedAttributesWithRange attrs(AttrFactory);
433 StmtResult Block(ParseCompoundStatement(attrs));
434 if(Block.isInvalid())
435 return move(Block);
436
437 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000438}
439
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000440/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000441///
442/// labeled-statement:
443/// identifier ':' statement
444/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000445///
John McCall7f040a92010-12-24 02:08:15 +0000446StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000447 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
448 "Not an identifier!");
449
450 Token IdentTok = Tok; // Save the whole token.
451 ConsumeToken(); // eat the identifier.
452
453 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000454
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000455 // identifier ':' statement
456 SourceLocation ColonLoc = ConsumeToken();
457
458 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000459 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000460
John McCall60d7b3a2010-08-24 06:29:42 +0000461 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000462
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000463 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000464 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000465 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000466
Chris Lattner337e5502011-02-18 01:27:55 +0000467 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
468 IdentTok.getLocation());
469 if (AttributeList *Attrs = attrs.getList())
470 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000471
Chris Lattner337e5502011-02-18 01:27:55 +0000472 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
473 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000474}
Reid Spencer5f016e22007-07-11 17:01:13 +0000475
476/// ParseCaseStatement
477/// labeled-statement:
478/// 'case' constant-expression ':' statement
479/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
480///
Richard Trieubb9b80c2011-04-21 21:44:26 +0000481StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
482 ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000483 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000484 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner24e1e702009-03-04 04:23:07 +0000486 // It is very very common for code to contain many case statements recursively
487 // nested, as in (but usually without indentation):
488 // case 1:
489 // case 2:
490 // case 3:
491 // case 4:
492 // case 5: etc.
493 //
494 // Parsing this naively works, but is both inefficient and can cause us to run
495 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000496 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000497 // but all the grossness is constrained to ParseCaseStatement (and some
498 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattner24e1e702009-03-04 04:23:07 +0000500 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
501 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000502 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner24e1e702009-03-04 04:23:07 +0000504 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
505 // gets updated each time a new case is parsed, and whose body is unset so
506 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieub2fc6902011-09-09 02:16:15 +0000507 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner24e1e702009-03-04 04:23:07 +0000509 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000510 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000511 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000512 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
513 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000515 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000516 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000517 cutOffParsing();
518 return StmtError();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000519 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000520
Chris Lattner6fb09c82009-12-10 00:38:54 +0000521 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
522 /// Disable this form of error recovery while we're parsing the case
523 /// expression.
524 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000525
Richard Trieubb9b80c2011-04-21 21:44:26 +0000526 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
527 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000528 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000530 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000532
Chris Lattner24e1e702009-03-04 04:23:07 +0000533 // GNU case range extension.
534 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000535 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000536 if (Tok.is(tok::ellipsis)) {
537 Diag(Tok, diag::ext_gnu_case_range);
538 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000539
Chris Lattner24e1e702009-03-04 04:23:07 +0000540 RHS = ParseConstantExpression();
541 if (RHS.isInvalid()) {
542 SkipUntil(tok::colon);
543 return StmtError();
544 }
545 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000546
Chris Lattner6fb09c82009-12-10 00:38:54 +0000547 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000548
John McCallf6a3ab02011-01-22 09:28:32 +0000549 if (Tok.is(tok::colon)) {
550 ColonLoc = ConsumeToken();
551
552 // Treat "case blah;" as a typo for "case blah:".
553 } else if (Tok.is(tok::semi)) {
554 ColonLoc = ConsumeToken();
555 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
556 << FixItHint::CreateReplacement(ColonLoc, ":");
557 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000558 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
559 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
560 << FixItHint::CreateInsertion(ExpectedLoc, ":");
561 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000562 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000563
John McCall60d7b3a2010-08-24 06:29:42 +0000564 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000565 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
566 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner24e1e702009-03-04 04:23:07 +0000568 // If we had a sema error parsing this case, then just ignore it and
569 // continue parsing the sub-stmt.
570 if (Case.isInvalid()) {
571 if (TopLevelCase.isInvalid()) // No parsed case stmts.
572 return ParseStatement();
573 // Otherwise, just don't add it as a nested case.
574 } else {
575 // If this is the first case statement we parsed, it becomes TopLevelCase.
576 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000577 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000578 if (TopLevelCase.isInvalid())
579 TopLevelCase = move(Case);
580 else
John McCall9ae2f072010-08-23 23:25:46 +0000581 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000582 DeepestParsedCaseStmt = NextDeepest;
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner24e1e702009-03-04 04:23:07 +0000585 // Handle all case statements.
586 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattner24e1e702009-03-04 04:23:07 +0000588 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Chris Lattner24e1e702009-03-04 04:23:07 +0000590 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000591 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattner24e1e702009-03-04 04:23:07 +0000593 if (Tok.isNot(tok::r_brace)) {
594 SubStmt = ParseStatement();
595 } else {
596 // Nicely diagnose the common error "switch (X) { case 4: }", which is
597 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000598 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
599 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000600 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Chris Lattner24e1e702009-03-04 04:23:07 +0000603 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000604 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000605 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattner24e1e702009-03-04 04:23:07 +0000607 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000608 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000609
Chris Lattner24e1e702009-03-04 04:23:07 +0000610 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000611 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612}
613
614/// ParseDefaultStatement
615/// labeled-statement:
616/// 'default' ':' statement
617/// Note that this does not parse the 'statement' at the end.
618///
John McCall7f040a92010-12-24 02:08:15 +0000619StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000620 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000621
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000622 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
624
Douglas Gregor662a4822010-12-23 22:56:40 +0000625 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000626 if (Tok.is(tok::colon)) {
627 ColonLoc = ConsumeToken();
628
629 // Treat "default;" as a typo for "default:".
630 } else if (Tok.is(tok::semi)) {
631 ColonLoc = ConsumeToken();
632 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
633 << FixItHint::CreateReplacement(ColonLoc, ":");
634 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000635 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
636 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
637 << FixItHint::CreateInsertion(ExpectedLoc, ":");
638 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000642 if (Tok.is(tok::r_brace)) {
David Majnemer63f04ab2011-06-14 15:24:38 +0000643 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
644 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000645 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 }
647
John McCall60d7b3a2010-08-24 06:29:42 +0000648 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000649 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000650 return StmtError();
651
Sebastian Redl117054a2008-12-28 16:13:43 +0000652 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000653 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000654}
655
Douglas Gregorbca01b42011-07-06 22:04:06 +0000656StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
657 bool isStmtExpr) {
658 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
659}
Reid Spencer5f016e22007-07-11 17:01:13 +0000660
661/// ParseCompoundStatement - Parse a "{}" block.
662///
663/// compound-statement: [C99 6.8.2]
664/// { block-item-list[opt] }
665/// [GNU] { label-declarations block-item-list } [TODO]
666///
667/// block-item-list:
668/// block-item
669/// block-item-list block-item
670///
671/// block-item:
672/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000673/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000674/// statement
675/// [OMP] openmp-directive [TODO]
676///
677/// [GNU] label-declarations:
678/// [GNU] label-declaration
679/// [GNU] label-declarations label-declaration
680///
681/// [GNU] label-declaration:
682/// [GNU] '__label__' identifier-list ';'
683///
684/// [OMP] openmp-directive: [TODO]
685/// [OMP] barrier-directive
686/// [OMP] flush-directive
687///
John McCall7f040a92010-12-24 02:08:15 +0000688StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000689 bool isStmtExpr,
690 unsigned ScopeFlags) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000691 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000692
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000693 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000694
Chris Lattner31e05722007-08-26 06:24:45 +0000695 // Enter a scope to hold everything within the compound stmt. Compound
696 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000697 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000698
699 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000700 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000701}
702
703
704/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000705/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000706/// consume the '}' at the end of the block. It does not manipulate the scope
707/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000708StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000709 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000710 Tok.getLocation(),
711 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000712 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000713 BalancedDelimiterTracker T(*this, tok::l_brace);
714 if (T.consumeOpen())
715 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000716
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000717 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000718
Chris Lattner4ae493c2011-02-18 02:08:43 +0000719 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
720 // only allowed at the start of a compound stmt regardless of the language.
721 while (Tok.is(tok::kw___label__)) {
722 SourceLocation LabelLoc = ConsumeToken();
723 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000724
Chris Lattner5f9e2722011-07-23 10:55:15 +0000725 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000726 while (1) {
727 if (Tok.isNot(tok::identifier)) {
728 Diag(Tok, diag::err_expected_ident);
729 break;
730 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000731
Chris Lattner4ae493c2011-02-18 02:08:43 +0000732 IdentifierInfo *II = Tok.getIdentifierInfo();
733 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000734 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000735
Chris Lattner4ae493c2011-02-18 02:08:43 +0000736 if (!Tok.is(tok::comma))
737 break;
738 ConsumeToken();
739 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000740
John McCall0b7e6782011-03-24 11:26:52 +0000741 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000742 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
743 DeclsInGroup.data(), DeclsInGroup.size());
744 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000745
Chris Lattner4ae493c2011-02-18 02:08:43 +0000746 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
747 if (R.isUsable())
748 Stmts.push_back(R.release());
749 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000750
Chris Lattner4ae493c2011-02-18 02:08:43 +0000751 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000752 if (Tok.is(tok::annot_pragma_unused)) {
753 HandlePragmaUnused();
754 continue;
755 }
756
Francois Pichet62ec1f22011-09-17 17:15:52 +0000757 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet1e862692011-05-06 20:48:22 +0000758 Tok.is(tok::kw___if_not_exists))) {
759 ParseMicrosoftIfExistsStatement(Stmts);
760 continue;
761 }
762
John McCall60d7b3a2010-08-24 06:29:42 +0000763 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000764 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000765 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000766 } else {
767 // __extension__ can start declarations and it can also be a unary
768 // operator for expressions. Consume multiple __extension__ markers here
769 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000770 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000771 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000772 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000773 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000774
John McCall0b7e6782011-03-24 11:26:52 +0000775 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000776 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000777
Chris Lattner45a566c2007-08-27 01:01:57 +0000778 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000779 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000780 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000781 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000782 ExtensionRAIIObject O(Diags);
783
Chris Lattner97144fc2009-04-02 04:16:50 +0000784 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000785 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
786 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000787 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000788 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000789 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000790 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000791 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000792
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000793 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000794 SkipUntil(tok::semi);
795 continue;
796 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000797
Sean Huntbbd37c62009-11-21 08:43:09 +0000798 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000799 // Eat the semicolon at the end of stmt and convert the expr into a
800 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000801 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000802 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000803 }
804 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000805
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000806 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000807 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000811 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000813 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000814 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000816
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000817 if (T.consumeClose())
818 return StmtError();
819
820 return Actions.ActOnCompoundStmt(T.getOpenLocation(), T.getCloseLocation(),
821 move_arg(Stmts), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000822}
823
Chris Lattner15ff1112008-12-12 06:31:07 +0000824/// ParseParenExprOrCondition:
825/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000826/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000827///
828/// This function parses and performs error recovery on the specified condition
829/// or expression (depending on whether we're in C++ or C mode). This function
830/// goes out of its way to recover well. It returns true if there was a parser
831/// error (the right paren couldn't be found), which indicates that the caller
832/// should try to recover harder. It returns false if the condition is
833/// successfully parsed. Note that a successful parse can still have semantic
834/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000835bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000836 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000837 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000838 bool ConvertToBoolean) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000839 BalancedDelimiterTracker T(*this, tok::l_paren);
840 T.consumeOpen();
841
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000842 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000843 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000844 else {
845 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000846 DeclResult = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000847
Douglas Gregor586596f2010-05-06 17:25:47 +0000848 // If required, convert to a boolean value.
849 if (!ExprResult.isInvalid() && ConvertToBoolean)
850 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000851 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner15ff1112008-12-12 06:31:07 +0000854 // If the parser was confused by the condition and we don't have a ')', try to
855 // recover by skipping ahead to a semi and bailing out. If condexp is
856 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000857 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000858 SkipUntil(tok::semi);
859 // Skipping may have stopped if it found the containing ')'. If so, we can
860 // continue parsing the if statement.
861 if (Tok.isNot(tok::r_paren))
862 return true;
863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattner15ff1112008-12-12 06:31:07 +0000865 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000866 T.consumeClose();
Chris Lattner15ff1112008-12-12 06:31:07 +0000867 return false;
868}
869
870
Reid Spencer5f016e22007-07-11 17:01:13 +0000871/// ParseIfStatement
872/// if-statement: [C99 6.8.4.1]
873/// 'if' '(' expression ')' statement
874/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000875/// [C++] 'if' '(' condition ')' statement
876/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000877///
Nico Weber5cb94a72011-12-22 23:26:17 +0000878StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs,
879 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000880 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000881
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000882 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
884
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000885 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000886 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000888 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000890
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000891 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
892
Chris Lattner22153252007-08-26 23:08:06 +0000893 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
894 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000895 //
896 // C++ 6.4p3:
897 // A name introduced by a declaration in a condition is in scope from its
898 // point of declaration until the end of the substatements controlled by the
899 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000900 // C++ 3.3.2p4:
901 // Names declared in the for-init-statement, and in the condition of if,
902 // while, for, and switch statements are local to the if, while, for, or
903 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000904 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000905 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000906
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000908 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000909 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000910 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000911 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000912
John McCall9ae2f072010-08-23 23:25:46 +0000913 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Chris Lattner0ecea032007-08-22 05:28:50 +0000915 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000916 // there is no compound stmt. C90 does not have this clause. We only do this
917 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000918 //
919 // C++ 6.4p1:
920 // The substatement in a selection-statement (each substatement, in the else
921 // form of the if statement) implicitly defines a local scope.
922 //
923 // For C++ we create a scope for the condition and a new scope for
924 // substatements because:
925 // -When the 'then' scope exits, we want the condition declaration to still be
926 // active for the 'else' scope too.
927 // -Sema will detect name clashes by considering declarations of a
928 // 'ControlScope' as part of its direct subscope.
929 // -If we wanted the condition and substatement to be in the same scope, we
930 // would have to notify ParseStatement not to create a new scope. It's
931 // simpler to let it create a new scope.
932 //
Mike Stump1eb44332009-09-09 15:08:12 +0000933 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000934 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000935
Chris Lattnerb96728d2007-10-29 05:08:52 +0000936 // Read the 'then' stmt.
937 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber5cb94a72011-12-22 23:26:17 +0000938
939 SourceLocation InnerStatementTrailingElseLoc;
940 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000941
Chris Lattnera36ce712007-08-22 05:16:28 +0000942 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000943 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000944
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 // If it has an else, parse it.
946 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000947 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000948 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000949
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000950 if (Tok.is(tok::kw_else)) {
Nico Weber5cb94a72011-12-22 23:26:17 +0000951 if (TrailingElseLoc)
952 *TrailingElseLoc = Tok.getLocation();
953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000955 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000956
Chris Lattner0ecea032007-08-22 05:28:50 +0000957 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000958 // there is no compound stmt. C90 does not have this clause. We only do
959 // this if the body isn't a compound statement to avoid push/pop in common
960 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000961 //
962 // C++ 6.4p1:
963 // The substatement in a selection-statement (each substatement, in the else
964 // form of the if statement) implicitly defines a local scope.
965 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000966 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000967 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000968
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 ElseStmt = ParseStatement();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000970
Chris Lattnera36ce712007-08-22 05:16:28 +0000971 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000972 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +0000973 } else if (Tok.is(tok::code_completion)) {
974 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000975 cutOffParsing();
976 return StmtError();
Nico Weber5cb94a72011-12-22 23:26:17 +0000977 } else if (InnerStatementTrailingElseLoc.isValid()) {
978 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000980
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000981 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Chris Lattner18914bc2008-12-12 06:19:11 +0000983 // If the condition was invalid, discard the if statement. We could recover
984 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000985 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000986 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000987
Chris Lattnerb96728d2007-10-29 05:08:52 +0000988 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000989 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000990 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000991 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
992 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
993 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000994 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000995 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000996 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000997
Chris Lattnerb96728d2007-10-29 05:08:52 +0000998 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000999 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001000 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001001 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001002 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001003
John McCall9ae2f072010-08-23 23:25:46 +00001004 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001005 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001006}
1007
1008/// ParseSwitchStatement
1009/// switch-statement:
1010/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001011/// [C++] 'switch' '(' condition ')' statement
Nico Weber5cb94a72011-12-22 23:26:17 +00001012StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs,
1013 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001014 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001015
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001016 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1018
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001019 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001020 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001022 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 }
Chris Lattner22153252007-08-26 23:08:06 +00001024
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001025 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1026
Chris Lattner22153252007-08-26 23:08:06 +00001027 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1028 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001029 //
1030 // C++ 6.4p3:
1031 // A name introduced by a declaration in a condition is in scope from its
1032 // point of declaration until the end of the substatements controlled by the
1033 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001034 // C++ 3.3.2p4:
1035 // Names declared in the for-init-statement, and in the condition of if,
1036 // while, for, and switch statements are local to the if, while, for, or
1037 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001038 //
Richard Trieubb9b80c2011-04-21 21:44:26 +00001039 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001040 if (C99orCXX)
1041 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001042 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001043
1044 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001045 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001046 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001047 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001048 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001049
John McCall60d7b3a2010-08-24 06:29:42 +00001050 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001051 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001052
Douglas Gregor586596f2010-05-06 17:25:47 +00001053 if (Switch.isInvalid()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001054 // Skip the switch body.
Douglas Gregor586596f2010-05-06 17:25:47 +00001055 // FIXME: This is not optimal recovery, but parsing the body is more
1056 // dangerous due to the presence of case and default statements, which
1057 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001058 if (Tok.is(tok::l_brace)) {
1059 ConsumeBrace();
1060 SkipUntil(tok::r_brace, false, false);
1061 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001062 SkipUntil(tok::semi);
1063 return move(Switch);
1064 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001065
Chris Lattner0ecea032007-08-22 05:28:50 +00001066 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001067 // there is no compound stmt. C90 does not have this clause. We only do this
1068 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001069 //
1070 // C++ 6.4p1:
1071 // The substatement in a selection-statement (each substatement, in the else
1072 // form of the if statement) implicitly defines a local scope.
1073 //
1074 // See comments in ParseIfStatement for why we create a scope for the
1075 // condition and a new scope for substatement in C++.
1076 //
Mike Stump1eb44332009-09-09 15:08:12 +00001077 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001078 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001079
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001081 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001082
Chris Lattner7e52de42010-01-24 01:50:29 +00001083 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001084 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001085 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001086
Chris Lattner7e52de42010-01-24 01:50:29 +00001087 if (Body.isInvalid())
1088 // FIXME: Remove the case statement list from the Switch statement.
1089 Body = Actions.ActOnNullStmt(Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001090
John McCall9ae2f072010-08-23 23:25:46 +00001091 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001092}
1093
1094/// ParseWhileStatement
1095/// while-statement: [C99 6.8.5.1]
1096/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001097/// [C++] 'while' '(' condition ')' statement
Nico Weber5cb94a72011-12-22 23:26:17 +00001098StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs,
1099 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001100 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001101
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001102 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 SourceLocation WhileLoc = Tok.getLocation();
1104 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001105
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001106 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001107 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001109 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001111
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001112 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1113
Chris Lattner22153252007-08-26 23:08:06 +00001114 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1115 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001116 //
1117 // C++ 6.4p3:
1118 // A name introduced by a declaration in a condition is in scope from its
1119 // point of declaration until the end of the substatements controlled by the
1120 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001121 // C++ 3.3.2p4:
1122 // Names declared in the for-init-statement, and in the condition of if,
1123 // while, for, and switch statements are local to the if, while, for, or
1124 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001125 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001126 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001127 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001128 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1129 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001130 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001131 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1132 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001133
1134 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001135 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001136 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001137 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001138 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001139
John McCall9ae2f072010-08-23 23:25:46 +00001140 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Chris Lattner0ecea032007-08-22 05:28:50 +00001142 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001143 // there is no compound stmt. C90 does not have this clause. We only do this
1144 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001145 //
1146 // C++ 6.5p2:
1147 // The substatement in an iteration-statement implicitly defines a local scope
1148 // which is entered and exited each time through the loop.
1149 //
1150 // See comments in ParseIfStatement for why we create a scope for the
1151 // condition and a new scope for substatement in C++.
1152 //
Mike Stump1eb44332009-09-09 15:08:12 +00001153 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001154 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001155
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001157 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001158
Chris Lattner0ecea032007-08-22 05:28:50 +00001159 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001160 InnerScope.Exit();
1161 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001162
John McCalld226f652010-08-21 09:40:31 +00001163 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001164 return StmtError();
1165
John McCall9ae2f072010-08-23 23:25:46 +00001166 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001167}
1168
1169/// ParseDoStatement
1170/// do-statement: [C99 6.8.5.2]
1171/// 'do' statement 'while' '(' expression ')' ';'
1172/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +00001173StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001174 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001175
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001176 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001178
Chris Lattner22153252007-08-26 23:08:06 +00001179 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1180 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001181 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +00001182 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001183 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001184 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001185 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001186
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001187 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188
Chris Lattner0ecea032007-08-22 05:28:50 +00001189 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001190 // there is no compound stmt. C90 does not have this clause. We only do this
1191 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001192 //
1193 // C++ 6.5p2:
1194 // The substatement in an iteration-statement implicitly defines a local scope
1195 // which is entered and exited each time through the loop.
1196 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001197 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +00001198 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001199 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001200
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001202 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001203
Chris Lattner0ecea032007-08-22 05:28:50 +00001204 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001205 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001206
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001207 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001208 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001209 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001210 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001211 SkipUntil(tok::semi, false, true);
1212 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001213 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 }
1215 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001216
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001217 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001218 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001219 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001220 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001222
Chris Lattnerff871fb2008-12-12 06:35:28 +00001223 // Parse the parenthesized condition.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001224 BalancedDelimiterTracker T(*this, tok::l_paren);
1225 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001226 ExprResult Cond = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001227 T.consumeClose();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001228 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001229
Sebastian Redl9a920342008-12-11 19:48:14 +00001230 if (Cond.isInvalid() || Body.isInvalid())
1231 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001232
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001233 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1234 Cond.get(), T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001235}
1236
1237/// ParseForStatement
1238/// for-statement: [C99 6.8.5.3]
1239/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1240/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001241/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1242/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001243/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001244/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1245/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001246///
1247/// [C++] for-init-statement:
1248/// [C++] expression-statement
1249/// [C++] simple-declaration
1250///
Richard Smithad762fc2011-04-14 22:09:26 +00001251/// [C++0x] for-range-declaration:
1252/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1253/// [C++0x] for-range-initializer:
1254/// [C++0x] expression
1255/// [C++0x] braced-init-list [TODO]
Nico Weber5cb94a72011-12-22 23:26:17 +00001256StmtResult Parser::ParseForStatement(ParsedAttributes &attrs,
1257 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001258 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001259
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001260 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001262
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001263 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001264 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001266 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001268
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001269 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001270
Chris Lattner22153252007-08-26 23:08:06 +00001271 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1272 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001273 //
1274 // C++ 6.4p3:
1275 // A name introduced by a declaration in a condition is in scope from its
1276 // point of declaration until the end of the substatements controlled by the
1277 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001278 // C++ 3.3.2p4:
1279 // Names declared in the for-init-statement, and in the condition of if,
1280 // while, for, and switch statements are local to the if, while, for, or
1281 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001282 // C++ 6.5.3p1:
1283 // Names declared in the for-init-statement are in the same declarative-region
1284 // as those declared in the condition.
1285 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001286 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001287 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001288 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1289 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001290 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001291 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1292
1293 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001294
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001295 BalancedDelimiterTracker T(*this, tok::l_paren);
1296 T.consumeOpen();
1297
John McCall60d7b3a2010-08-24 06:29:42 +00001298 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001299
Richard Smithad762fc2011-04-14 22:09:26 +00001300 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001301 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001302 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001303 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001304 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001305 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001306 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001307 Decl *SecondVar = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001308
Douglas Gregor791215b2009-09-21 20:51:25 +00001309 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001310 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001311 C99orCXXorObjC? Sema::PCC_ForInit
1312 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001313 cutOffParsing();
1314 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001315 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001316
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001318 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 // no first part, eat the ';'.
1320 ConsumeToken();
Eli Friedman9490ab42011-12-20 01:50:37 +00001321 } else if (isForInitDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001323 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001325
John McCall0b7e6782011-03-24 11:26:52 +00001326 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001327 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001328
Richard Smithad762fc2011-04-14 22:09:26 +00001329 // In C++0x, "for (T NS:a" might not be a typo for ::
1330 bool MightBeForRangeStmt = getLang().CPlusPlus;
1331 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1332
Chris Lattner97144fc2009-04-02 04:16:50 +00001333 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001334 StmtVector Stmts(Actions);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001335 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001336 DeclEnd, attrs, false,
1337 MightBeForRangeStmt ?
1338 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001339 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Richard Smithad762fc2011-04-14 22:09:26 +00001341 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith7fe62082011-10-15 05:09:34 +00001342 Diag(ForRangeInit.ColonLoc, getLang().CPlusPlus0x ?
1343 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8f4fb192011-09-04 19:54:14 +00001344
Richard Smithad762fc2011-04-14 22:09:26 +00001345 ForRange = true;
1346 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001347 ConsumeToken();
1348 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001349 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001350 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001351 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001352
Douglas Gregorfb629412010-08-23 21:17:50 +00001353 if (Tok.is(tok::code_completion)) {
1354 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001355 cutOffParsing();
1356 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001357 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001358 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001359 } else {
1360 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001361 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 } else {
1363 Value = ParseExpression();
1364
John McCallf6a16482010-12-04 03:47:34 +00001365 ForEach = isTokIdentifier_in();
1366
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001368 if (!Value.isInvalid()) {
1369 if (ForEach)
1370 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1371 else
1372 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1373 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001374
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001375 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001377 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001378 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001379
Douglas Gregorfb629412010-08-23 21:17:50 +00001380 if (Tok.is(tok::code_completion)) {
1381 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001382 cutOffParsing();
1383 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001384 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001385 Collection = ParseExpression();
Richard Smitha44854a2011-12-20 22:56:20 +00001386 } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) &&
1387 !FirstPart.isInvalid()) {
1388 // User tried to write the reasonable, but ill-formed, for-range-statement
1389 // for (expr : expr) { ... }
1390 Diag(Tok, diag::err_for_range_expected_decl)
1391 << FirstPart.get()->getSourceRange();
1392 SkipUntil(tok::r_paren, false, true);
1393 SecondPartIsInvalid = true;
Chris Lattner682bf922009-03-29 16:50:03 +00001394 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001395 if (!Value.isInvalid()) {
1396 Diag(Tok, diag::err_expected_semi_for);
1397 } else {
1398 // Skip until semicolon or rparen, don't consume it.
1399 SkipUntil(tok::r_paren, true, true);
1400 if (Tok.is(tok::semi))
1401 ConsumeToken();
1402 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 }
1404 }
Richard Smithad762fc2011-04-14 22:09:26 +00001405 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001406 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001407 // Parse the second part of the for specifier.
1408 if (Tok.is(tok::semi)) { // for (...;;
1409 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001410 } else if (Tok.is(tok::r_paren)) {
1411 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001412 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001413 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001414 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001415 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1416 else {
1417 Second = ParseExpression();
1418 if (!Second.isInvalid())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001419 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001420 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001421 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001422 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001423 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001424 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001425
Douglas Gregorb72c7782011-02-17 03:38:46 +00001426 if (Tok.isNot(tok::semi)) {
1427 if (!SecondPartIsInvalid || SecondVar)
1428 Diag(Tok, diag::err_expected_semi_for);
1429 else
1430 // Skip until semicolon or rparen, don't consume it.
1431 SkipUntil(tok::r_paren, true, true);
1432 }
1433
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001434 if (Tok.is(tok::semi)) {
1435 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001436 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001437
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001438 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001439 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001440 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001441 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001442 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001445 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001446
Richard Smithad762fc2011-04-14 22:09:26 +00001447 // We need to perform most of the semantic analysis for a C++0x for-range
1448 // statememt before parsing the body, in order to be able to deduce the type
1449 // of an auto-typed loop variable.
1450 StmtResult ForRangeStmt;
John McCall990567c2011-07-27 01:07:15 +00001451 if (ForRange) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001452 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smithad762fc2011-04-14 22:09:26 +00001453 FirstPart.take(),
1454 ForRangeInit.ColonLoc,
1455 ForRangeInit.RangeExpr.get(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001456 T.getCloseLocation());
Richard Smithad762fc2011-04-14 22:09:26 +00001457
John McCall990567c2011-07-27 01:07:15 +00001458
1459 // Similarly, we need to do the semantic analysis for a for-range
1460 // statement immediately in order to close over temporaries correctly.
1461 } else if (ForEach) {
1462 if (!Collection.isInvalid())
1463 Collection =
1464 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1465 }
1466
Chris Lattner0ecea032007-08-22 05:28:50 +00001467 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001468 // there is no compound stmt. C90 does not have this clause. We only do this
1469 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001470 //
1471 // C++ 6.5p2:
1472 // The substatement in an iteration-statement implicitly defines a local scope
1473 // which is entered and exited each time through the loop.
1474 //
1475 // See comments in ParseIfStatement for why we create a scope for
1476 // for-init-statement/condition and a new scope for substatement in C++.
1477 //
Mike Stump1eb44332009-09-09 15:08:12 +00001478 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001479 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001480
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001482 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001483
Chris Lattner0ecea032007-08-22 05:28:50 +00001484 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001485 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001486
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001488 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001489
1490 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001491 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001492
Richard Smithad762fc2011-04-14 22:09:26 +00001493 if (ForEach)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001494 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1495 FirstPart.take(),
1496 Collection.take(),
1497 T.getCloseLocation(),
1498 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Richard Smithad762fc2011-04-14 22:09:26 +00001500 if (ForRange)
1501 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1502
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001503 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1504 SecondPart, SecondVar, ThirdPart,
1505 T.getCloseLocation(), Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001506}
1507
1508/// ParseGotoStatement
1509/// jump-statement:
1510/// 'goto' identifier ';'
1511/// [GNU] 'goto' '*' expression ';'
1512///
1513/// Note: this lets the caller parse the end ';'.
1514///
John McCall7f040a92010-12-24 02:08:15 +00001515StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001516 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001517
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001518 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001520
John McCall60d7b3a2010-08-24 06:29:42 +00001521 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001522 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001523 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1524 Tok.getLocation());
1525 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001527 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 // GNU indirect goto extension.
1529 Diag(Tok, diag::ext_gnu_indirect_goto);
1530 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001531 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001532 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001534 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 }
John McCall9ae2f072010-08-23 23:25:46 +00001536 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001537 } else {
1538 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001539 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001541
Sebastian Redl9a920342008-12-11 19:48:14 +00001542 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001543}
1544
1545/// ParseContinueStatement
1546/// jump-statement:
1547/// 'continue' ';'
1548///
1549/// Note: this lets the caller parse the end ';'.
1550///
John McCall7f040a92010-12-24 02:08:15 +00001551StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001552 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001553
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001555 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001556}
1557
1558/// ParseBreakStatement
1559/// jump-statement:
1560/// 'break' ';'
1561///
1562/// Note: this lets the caller parse the end ';'.
1563///
John McCall7f040a92010-12-24 02:08:15 +00001564StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001565 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001566
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001568 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001569}
1570
1571/// ParseReturnStatement
1572/// jump-statement:
1573/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001574StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001575 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001576
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001577 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001579
John McCall60d7b3a2010-08-24 06:29:42 +00001580 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001581 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001582 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001583 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001584 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001585 return StmtError();
1586 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001587
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001588 // FIXME: This is a hack to allow something like C++0x's generalized
1589 // initializer lists, but only enough of this feature to allow Clang to
1590 // parse libstdc++ 4.5's headers.
1591 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1592 R = ParseInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001593 if (R.isUsable())
1594 Diag(R.get()->getLocStart(), getLang().CPlusPlus0x ?
1595 diag::warn_cxx98_compat_generalized_initializer_lists :
1596 diag::ext_generalized_initializer_lists)
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001597 << R.get()->getSourceRange();
1598 } else
1599 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001600 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001602 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 }
1604 }
John McCall9ae2f072010-08-23 23:25:46 +00001605 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001606}
1607
Eli Friedman3fedbe12011-09-30 01:13:51 +00001608/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1609/// this routine is called to collect the tokens for an MS asm statement.
1610StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1611 SourceManager &SrcMgr = PP.getSourceManager();
1612 SourceLocation EndLoc = AsmLoc;
1613 do {
1614 bool InBraces = false;
NAKAMURA Takumi96e21712011-10-08 11:31:53 +00001615 unsigned short savedBraceCount = 0;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001616 bool InAsmComment = false;
1617 FileID FID;
NAKAMURA Takumi96e21712011-10-08 11:31:53 +00001618 unsigned LineNo = 0;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001619 unsigned NumTokensRead = 0;
1620 SourceLocation LBraceLoc;
1621
1622 if (Tok.is(tok::l_brace)) {
1623 // Braced inline asm: consume the opening brace.
1624 InBraces = true;
1625 savedBraceCount = BraceCount;
1626 EndLoc = LBraceLoc = ConsumeBrace();
1627 ++NumTokensRead;
1628 } else {
1629 // Single-line inline asm; compute which line it is on.
1630 std::pair<FileID, unsigned> ExpAsmLoc =
1631 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1632 FID = ExpAsmLoc.first;
1633 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1634 }
1635
Steve Naroff03d6bc62008-02-08 03:36:19 +00001636 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001637 do {
Eli Friedman3fedbe12011-09-30 01:13:51 +00001638 // If we hit EOF, we're done, period.
1639 if (Tok.is(tok::eof))
1640 break;
1641 // When we consume the closing brace, we're done.
1642 if (InBraces && BraceCount == savedBraceCount)
1643 break;
1644
1645 if (!InAsmComment && Tok.is(tok::semi)) {
1646 // A semicolon in an asm is the start of a comment.
1647 InAsmComment = true;
1648 if (InBraces) {
1649 // Compute which line the comment is on.
1650 std::pair<FileID, unsigned> ExpSemiLoc =
1651 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1652 FID = ExpSemiLoc.first;
1653 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1654 }
1655 } else if (!InBraces || InAsmComment) {
1656 // If end-of-line is significant, check whether this token is on a
1657 // new line.
1658 std::pair<FileID, unsigned> ExpLoc =
1659 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1660 if (ExpLoc.first != FID ||
1661 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1662 // If this is a single-line __asm, we're done.
1663 if (!InBraces)
1664 break;
1665 // We're no longer in a comment.
1666 InAsmComment = false;
1667 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1668 // Single-line asm always ends when a closing brace is seen.
1669 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1670 // does MSVC do here?
1671 break;
1672 }
1673 }
1674
1675 // Consume the next token; make sure we don't modify the brace count etc.
1676 // if we are in a comment.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001677 EndLoc = TokLoc;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001678 if (InAsmComment)
1679 PP.Lex(Tok);
1680 else
1681 ConsumeAnyToken();
Steve Naroff36280972008-02-08 18:01:27 +00001682 TokLoc = Tok.getLocation();
Eli Friedman3fedbe12011-09-30 01:13:51 +00001683 ++NumTokensRead;
1684 } while (1);
1685
1686 if (InBraces && BraceCount != savedBraceCount) {
1687 // __asm without closing brace (this can happen at EOF).
1688 Diag(Tok, diag::err_expected_rbrace);
1689 Diag(LBraceLoc, diag::note_matching) << "{";
1690 return StmtError();
1691 } else if (NumTokensRead == 0) {
1692 // Empty __asm.
1693 Diag(Tok, diag::err_expected_lbrace);
1694 return StmtError();
1695 }
1696 // Multiple adjacent asm's form together into a single asm statement
1697 // in the AST.
1698 if (!Tok.is(tok::kw_asm))
1699 break;
1700 EndLoc = ConsumeToken();
1701 } while (1);
1702 // FIXME: Need to actually grab the data and pass it on to Sema. Ideally,
1703 // what Sema wants is a string of the entire inline asm, with one instruction
1704 // per line and all the __asm keywords stripped out, and a way of mapping
1705 // from any character of that string to its location in the original source
1706 // code. I'm not entirely sure how to go about that, though.
Mike Stump95059b52009-12-11 00:04:56 +00001707 Token t;
1708 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001709 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001710 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001711 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001712 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001713 ExprVector Constraints(Actions);
1714 ExprVector Exprs(Actions);
1715 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001716 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001717 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001718 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001719 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001720}
1721
Reid Spencer5f016e22007-07-11 17:01:13 +00001722/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001723/// asm-statement:
1724/// gnu-asm-statement
1725/// ms-asm-statement
1726///
1727/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001728/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1729///
1730/// [GNU] asm-argument:
1731/// asm-string-literal
1732/// asm-string-literal ':' asm-operands[opt]
1733/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1734/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1735/// ':' asm-clobbers
1736///
1737/// [GNU] asm-clobbers:
1738/// asm-string-literal
1739/// asm-clobbers ',' asm-string-literal
1740///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001741/// [MS] ms-asm-statement:
Eli Friedman3fedbe12011-09-30 01:13:51 +00001742/// ms-asm-block
1743/// ms-asm-block ms-asm-statement
Steve Naroff5f8aa692008-02-11 23:15:56 +00001744///
Eli Friedman3fedbe12011-09-30 01:13:51 +00001745/// [MS] ms-asm-block:
1746/// '__asm' ms-asm-line '\n'
1747/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1748///
1749/// [MS] ms-asm-instruction-block
1750/// ms-asm-line
1751/// ms-asm-line '\n' ms-asm-instruction-block
Steve Naroff5f8aa692008-02-11 23:15:56 +00001752///
John McCall60d7b3a2010-08-24 06:29:42 +00001753StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001754 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001755 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001756
Francois Pichet62ec1f22011-09-17 17:15:52 +00001757 if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001758 msAsm = true;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001759 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001760 }
John McCall0b7e6782011-03-24 11:26:52 +00001761 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001763 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001764
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1766 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001767 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001769 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001770
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001772 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001773 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001774 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001776 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001778 BalancedDelimiterTracker T(*this, tok::l_paren);
1779 T.consumeOpen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001780
John McCall60d7b3a2010-08-24 06:29:42 +00001781 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001782 if (AsmString.isInvalid()) {
1783 // If the reason we are recovering is because of an improper string
1784 // literal, it makes the most sense just to consume to the ')'.
1785 if (isTokenStringLiteral())
1786 T.skipToEnd();
Sebastian Redl9a920342008-12-11 19:48:14 +00001787 return StmtError();
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001788 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001789
Chris Lattner5f9e2722011-07-23 10:55:15 +00001790 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001791 ExprVector Constraints(Actions);
1792 ExprVector Exprs(Actions);
1793 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001794
Anders Carlssondfab34a2008-02-05 23:03:50 +00001795 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001796 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001797 T.consumeClose();
Chris Lattner64cb4752009-12-20 23:00:41 +00001798 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001799 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattner64cb4752009-12-20 23:00:41 +00001800 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001801 AsmString.take(), move_arg(Clobbers),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001802 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001804
Chris Lattner64cb4752009-12-20 23:00:41 +00001805 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001806 bool AteExtraColon = false;
1807 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1808 // In C++ mode, parse "::" like ": :".
1809 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001810 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001811
Chris Lattner64056462009-12-20 23:08:04 +00001812 if (!AteExtraColon &&
1813 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001814 return StmtError();
1815 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001816
Chris Lattner64cb4752009-12-20 23:00:41 +00001817 unsigned NumOutputs = Names.size();
1818
1819 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001820 if (AteExtraColon ||
1821 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1822 // In C++ mode, parse "::" like ": :".
1823 if (AteExtraColon)
1824 AteExtraColon = false;
1825 else {
1826 AteExtraColon = Tok.is(tok::coloncolon);
1827 ConsumeToken();
1828 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001829
Chris Lattner64056462009-12-20 23:08:04 +00001830 if (!AteExtraColon &&
1831 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001832 return StmtError();
1833 }
1834
1835 assert(Names.size() == Constraints.size() &&
1836 Constraints.size() == Exprs.size() &&
1837 "Input operand size mismatch!");
1838
1839 unsigned NumInputs = Names.size() - NumOutputs;
1840
1841 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001842 if (AteExtraColon || Tok.is(tok::colon)) {
1843 if (!AteExtraColon)
1844 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001845
Chandler Carruth102e1b62010-07-22 07:11:21 +00001846 // Parse the asm-string list for clobbers if present.
1847 if (Tok.isNot(tok::r_paren)) {
1848 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001849 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001850
Chandler Carruth102e1b62010-07-22 07:11:21 +00001851 if (Clobber.isInvalid())
1852 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001853
Chandler Carruth102e1b62010-07-22 07:11:21 +00001854 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001855
Chandler Carruth102e1b62010-07-22 07:11:21 +00001856 if (Tok.isNot(tok::comma)) break;
1857 ConsumeToken();
1858 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001859 }
1860 }
1861
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001862 T.consumeClose();
Chris Lattner64cb4752009-12-20 23:00:41 +00001863 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001864 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001865 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001866 AsmString.take(), move_arg(Clobbers),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001867 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001868}
1869
1870/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001871/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001872///
1873/// [GNU] asm-operands:
1874/// asm-operand
1875/// asm-operands ',' asm-operand
1876///
1877/// [GNU] asm-operand:
1878/// asm-string-literal '(' expression ')'
1879/// '[' identifier ']' asm-string-literal '(' expression ')'
1880///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001881//
1882// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001883bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieuf81e5a92011-09-09 02:00:50 +00001884 SmallVectorImpl<Expr *> &Constraints,
1885 SmallVectorImpl<Expr *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001886 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001887 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001888 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001889
1890 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001891 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001892 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001893 BalancedDelimiterTracker T(*this, tok::l_square);
1894 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001896 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 Diag(Tok, diag::err_expected_ident);
1898 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001899 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Anders Carlssonb235fc22007-11-22 01:36:19 +00001902 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001903 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001904
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001905 Names.push_back(II);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001906 T.consumeClose();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001907 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001908 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001909
John McCall60d7b3a2010-08-24 06:29:42 +00001910 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001911 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001912 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001913 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001914 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001915 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001916
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001917 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001918 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001920 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001922
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 // Read the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001924 BalancedDelimiterTracker T(*this, tok::l_paren);
1925 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001926 ExprResult Res(ParseExpression());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001927 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001928 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001930 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001932 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001934 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001935 ConsumeToken();
1936 }
1937}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001938
Douglas Gregorc9977d02011-03-16 17:05:57 +00001939Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001940 assert(Tok.is(tok::l_brace));
1941 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001942
Douglas Gregorc9977d02011-03-16 17:05:57 +00001943 if (PP.isCodeCompletionEnabled()) {
1944 if (trySkippingFunctionBodyForCodeCompletion()) {
1945 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001946 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001947 }
1948 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001949
John McCallf312b1e2010-08-26 23:41:50 +00001950 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1951 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001953 // Do not enter a scope for the brace, as the arguments are in the same scope
1954 // (the function body) as the body itself. Instead, just read the statement
1955 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001956 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001957
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001958 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001959 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001960 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001961 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001962
Douglas Gregorc9977d02011-03-16 17:05:57 +00001963 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001964 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001965}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001966
Sebastian Redld3a413d2009-04-26 20:35:05 +00001967/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1968///
1969/// function-try-block:
1970/// 'try' ctor-initializer[opt] compound-statement handler-seq
1971///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001972Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001973 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1974 SourceLocation TryLoc = ConsumeToken();
1975
John McCallf312b1e2010-08-26 23:41:50 +00001976 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1977 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001978
1979 // Constructor initializer list?
1980 if (Tok.is(tok::colon))
1981 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00001982 else
1983 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001984
Douglas Gregorc9977d02011-03-16 17:05:57 +00001985 if (PP.isCodeCompletionEnabled()) {
1986 if (trySkippingFunctionBodyForCodeCompletion()) {
1987 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001988 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001989 }
1990 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001991
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001992 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001993 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001994 // If we failed to parse the try-catch, we just give the function an empty
1995 // compound statement as the body.
1996 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001997 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001998 MultiStmtArg(Actions), false);
1999
Douglas Gregorc9977d02011-03-16 17:05:57 +00002000 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00002001 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00002002}
2003
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00002004bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002005 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00002006 assert(PP.isCodeCompletionEnabled() &&
2007 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002008
2009 // We're in code-completion mode. Skip parsing for all function bodies unless
2010 // the body contains the code-completion point.
2011 TentativeParsingAction PA(*this);
2012 ConsumeBrace();
2013 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
2014 /*StopAtCodeCompletion=*/true)) {
2015 PA.Commit();
2016 return true;
2017 }
2018
2019 PA.Revert();
2020 return false;
2021}
2022
Sebastian Redla0fd8652008-12-21 16:41:36 +00002023/// ParseCXXTryBlock - Parse a C++ try-block.
2024///
2025/// try-block:
2026/// 'try' compound-statement handler-seq
2027///
John McCall7f040a92010-12-24 02:08:15 +00002028StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002029 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00002030
Sebastian Redla0fd8652008-12-21 16:41:36 +00002031 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2032
2033 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00002034 return ParseCXXTryBlockCommon(TryLoc);
2035}
2036
2037/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2038/// function-try-block.
2039///
2040/// try-block:
2041/// 'try' compound-statement handler-seq
2042///
2043/// function-try-block:
2044/// 'try' ctor-initializer[opt] compound-statement handler-seq
2045///
2046/// handler-seq:
2047/// handler handler-seq[opt]
2048///
John Wiegley28bbe4b2011-04-28 01:08:34 +00002049/// [Borland] try-block:
2050/// 'try' compound-statement seh-except-block
2051/// 'try' compound-statment seh-finally-block
2052///
John McCall60d7b3a2010-08-24 06:29:42 +00002053StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002054 if (Tok.isNot(tok::l_brace))
2055 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00002056 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002057 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregorbca01b42011-07-06 22:04:06 +00002058 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
2059 Scope::DeclScope|Scope::TryScope));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002060 if (TryBlock.isInvalid())
2061 return move(TryBlock);
2062
John Wiegley28bbe4b2011-04-28 01:08:34 +00002063 // Borland allows SEH-handlers with 'try'
Douglas Gregorb57791e2011-10-21 03:57:52 +00002064
2065 if((Tok.is(tok::identifier) &&
2066 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2067 Tok.is(tok::kw___finally)) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002068 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2069 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +00002070 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002071 SourceLocation Loc = ConsumeToken();
2072 Handler = ParseSEHExceptBlock(Loc);
2073 }
2074 else {
2075 SourceLocation Loc = ConsumeToken();
2076 Handler = ParseSEHFinallyBlock(Loc);
2077 }
2078 if(Handler.isInvalid())
2079 return move(Handler);
John McCall7f040a92010-12-24 02:08:15 +00002080
John Wiegley28bbe4b2011-04-28 01:08:34 +00002081 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2082 TryLoc,
2083 TryBlock.take(),
2084 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002085 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00002086 else {
2087 StmtVector Handlers(Actions);
2088 MaybeParseCXX0XAttributes(attrs);
2089 ProhibitAttributes(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002090
John Wiegley28bbe4b2011-04-28 01:08:34 +00002091 if (Tok.isNot(tok::kw_catch))
2092 return StmtError(Diag(Tok, diag::err_expected_catch));
2093 while (Tok.is(tok::kw_catch)) {
2094 StmtResult Handler(ParseCXXCatchBlock());
2095 if (!Handler.isInvalid())
2096 Handlers.push_back(Handler.release());
2097 }
2098 // Don't bother creating the full statement if we don't have any usable
2099 // handlers.
2100 if (Handlers.empty())
2101 return StmtError();
2102
2103 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2104 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00002105}
2106
2107/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2108///
2109/// handler:
2110/// 'catch' '(' exception-declaration ')' compound-statement
2111///
2112/// exception-declaration:
2113/// type-specifier-seq declarator
2114/// type-specifier-seq abstract-declarator
2115/// type-specifier-seq
2116/// '...'
2117///
John McCall60d7b3a2010-08-24 06:29:42 +00002118StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002119 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2120
2121 SourceLocation CatchLoc = ConsumeToken();
2122
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002123 BalancedDelimiterTracker T(*this, tok::l_paren);
2124 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redla0fd8652008-12-21 16:41:36 +00002125 return StmtError();
2126
2127 // C++ 3.3.2p3:
2128 // The name in a catch exception-declaration is local to the handler and
2129 // shall not be redeclared in the outermost block of the handler.
2130 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2131
2132 // exception-declaration is equivalent to '...' or a parameter-declaration
2133 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00002134 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002135 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00002136 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00002137 if (ParseCXXTypeSpecifierSeq(DS))
2138 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00002139 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2140 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002141 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002142 } else
2143 ConsumeToken();
2144
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002145 T.consumeClose();
2146 if (T.getCloseLocation().isInvalid())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002147 return StmtError();
2148
2149 if (Tok.isNot(tok::l_brace))
2150 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2151
Sean Huntbbd37c62009-11-21 08:43:09 +00002152 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002153 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002154 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002155 if (Block.isInvalid())
2156 return move(Block);
2157
John McCall9ae2f072010-08-23 23:25:46 +00002158 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002159}
Francois Pichet1e862692011-05-06 20:48:22 +00002160
2161void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002162 IfExistsCondition Result;
Francois Pichetf9860382011-05-07 17:30:27 +00002163 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002164 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002165
Douglas Gregor3896fc52011-10-24 22:31:10 +00002166 // Handle dependent statements by parsing the braces as a compound statement.
2167 // This is not the same behavior as Visual C++, which don't treat this as a
2168 // compound statement, but for Clang's type checking we can't have anything
2169 // inside these braces escaping to the surrounding code.
2170 if (Result.Behavior == IEB_Dependent) {
2171 if (!Tok.is(tok::l_brace)) {
2172 Diag(Tok, diag::err_expected_lbrace);
2173 return;
2174 }
2175
2176 ParsedAttributes Attrs(AttrFactory);
2177 StmtResult Compound = ParseCompoundStatement(Attrs);
Douglas Gregorba0513d2011-10-25 01:33:02 +00002178 if (Compound.isInvalid())
2179 return;
2180
2181 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2182 Result.IsIfExists,
2183 Result.SS,
2184 Result.Name,
2185 Compound.get());
2186 if (DepResult.isUsable())
2187 Stmts.push_back(DepResult.get());
Douglas Gregor3896fc52011-10-24 22:31:10 +00002188 return;
2189 }
2190
2191 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2192 if (Braces.consumeOpen()) {
Francois Pichet1e862692011-05-06 20:48:22 +00002193 Diag(Tok, diag::err_expected_lbrace);
2194 return;
2195 }
Francois Pichet1e862692011-05-06 20:48:22 +00002196
Douglas Gregor3896fc52011-10-24 22:31:10 +00002197 switch (Result.Behavior) {
2198 case IEB_Parse:
2199 // Parse the statements below.
2200 break;
2201
2202 case IEB_Dependent:
2203 llvm_unreachable("Dependent case handled above");
Douglas Gregor3896fc52011-10-24 22:31:10 +00002204
2205 case IEB_Skip:
2206 Braces.skipToEnd();
Francois Pichet1e862692011-05-06 20:48:22 +00002207 return;
2208 }
2209
2210 // Condition is true, parse the statements.
2211 while (Tok.isNot(tok::r_brace)) {
2212 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2213 if (R.isUsable())
2214 Stmts.push_back(R.release());
2215 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002216 Braces.consumeClose();
Francois Pichet1e862692011-05-06 20:48:22 +00002217}