blob: c256f7842deab5f4ae7a7c1193cb5c0be7dbb2f6 [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(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000174 SS, SourceLocation(), Id,
Douglas Gregor3b887352011-04-27 04:48:22 +0000175 /*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);
Richard Smith85b29a42012-02-17 01:35:32 +0000599 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
600 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner24e1e702009-03-04 04:23:07 +0000601 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Chris Lattner24e1e702009-03-04 04:23:07 +0000604 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000605 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000606 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Chris Lattner24e1e702009-03-04 04:23:07 +0000608 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000609 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000610
Chris Lattner24e1e702009-03-04 04:23:07 +0000611 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000612 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613}
614
615/// ParseDefaultStatement
616/// labeled-statement:
617/// 'default' ':' statement
618/// Note that this does not parse the 'statement' at the end.
619///
John McCall7f040a92010-12-24 02:08:15 +0000620StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000621 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000622
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000623 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
625
Douglas Gregor662a4822010-12-23 22:56:40 +0000626 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000627 if (Tok.is(tok::colon)) {
628 ColonLoc = ConsumeToken();
629
630 // Treat "default;" as a typo for "default:".
631 } else if (Tok.is(tok::semi)) {
632 ColonLoc = ConsumeToken();
633 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
634 << FixItHint::CreateReplacement(ColonLoc, ":");
635 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000636 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
637 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
638 << FixItHint::CreateInsertion(ExpectedLoc, ":");
639 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000641
Richard Smith85b29a42012-02-17 01:35:32 +0000642 StmtResult SubStmt;
643
644 if (Tok.isNot(tok::r_brace)) {
645 SubStmt = ParseStatement();
646 } else {
647 // Diagnose the common error "switch (X) {... default: }", which is
648 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000649 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000650 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
651 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
652 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 }
654
Richard Smith85b29a42012-02-17 01:35:32 +0000655 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000656 if (SubStmt.isInvalid())
Richard Smith85b29a42012-02-17 01:35:32 +0000657 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000658
Sebastian Redl117054a2008-12-28 16:13:43 +0000659 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000660 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000661}
662
Douglas Gregorbca01b42011-07-06 22:04:06 +0000663StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
664 bool isStmtExpr) {
665 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
666}
Reid Spencer5f016e22007-07-11 17:01:13 +0000667
668/// ParseCompoundStatement - Parse a "{}" block.
669///
670/// compound-statement: [C99 6.8.2]
671/// { block-item-list[opt] }
672/// [GNU] { label-declarations block-item-list } [TODO]
673///
674/// block-item-list:
675/// block-item
676/// block-item-list block-item
677///
678/// block-item:
679/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000680/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000681/// statement
682/// [OMP] openmp-directive [TODO]
683///
684/// [GNU] label-declarations:
685/// [GNU] label-declaration
686/// [GNU] label-declarations label-declaration
687///
688/// [GNU] label-declaration:
689/// [GNU] '__label__' identifier-list ';'
690///
691/// [OMP] openmp-directive: [TODO]
692/// [OMP] barrier-directive
693/// [OMP] flush-directive
694///
John McCall7f040a92010-12-24 02:08:15 +0000695StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000696 bool isStmtExpr,
697 unsigned ScopeFlags) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000698 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000699
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000700 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000701
Chris Lattner31e05722007-08-26 06:24:45 +0000702 // Enter a scope to hold everything within the compound stmt. Compound
703 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000704 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705
706 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000707 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000708}
709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000711/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000712/// consume the '}' at the end of the block. It does not manipulate the scope
713/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000714StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000715 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000716 Tok.getLocation(),
717 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000718 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000719 BalancedDelimiterTracker T(*this, tok::l_brace);
720 if (T.consumeOpen())
721 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000722
Dmitri Gribenko625bb562012-02-14 22:14:32 +0000723 Sema::CompoundScopeRAII CompoundScope(Actions);
724
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000725 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000726
Chris Lattner4ae493c2011-02-18 02:08:43 +0000727 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
728 // only allowed at the start of a compound stmt regardless of the language.
729 while (Tok.is(tok::kw___label__)) {
730 SourceLocation LabelLoc = ConsumeToken();
731 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000732
Chris Lattner5f9e2722011-07-23 10:55:15 +0000733 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000734 while (1) {
735 if (Tok.isNot(tok::identifier)) {
736 Diag(Tok, diag::err_expected_ident);
737 break;
738 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000739
Chris Lattner4ae493c2011-02-18 02:08:43 +0000740 IdentifierInfo *II = Tok.getIdentifierInfo();
741 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000742 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000743
Chris Lattner4ae493c2011-02-18 02:08:43 +0000744 if (!Tok.is(tok::comma))
745 break;
746 ConsumeToken();
747 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000748
John McCall0b7e6782011-03-24 11:26:52 +0000749 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000750 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
751 DeclsInGroup.data(), DeclsInGroup.size());
752 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000753
Chris Lattner4ae493c2011-02-18 02:08:43 +0000754 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
755 if (R.isUsable())
756 Stmts.push_back(R.release());
757 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000758
Chris Lattner4ae493c2011-02-18 02:08:43 +0000759 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000760 if (Tok.is(tok::annot_pragma_unused)) {
761 HandlePragmaUnused();
762 continue;
763 }
764
Francois Pichet62ec1f22011-09-17 17:15:52 +0000765 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet1e862692011-05-06 20:48:22 +0000766 Tok.is(tok::kw___if_not_exists))) {
767 ParseMicrosoftIfExistsStatement(Stmts);
768 continue;
769 }
770
John McCall60d7b3a2010-08-24 06:29:42 +0000771 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000772 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000773 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000774 } else {
775 // __extension__ can start declarations and it can also be a unary
776 // operator for expressions. Consume multiple __extension__ markers here
777 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000778 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000779 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000780 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000781 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000782
John McCall0b7e6782011-03-24 11:26:52 +0000783 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000784 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000785
Chris Lattner45a566c2007-08-27 01:01:57 +0000786 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000787 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000788 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000789 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000790 ExtensionRAIIObject O(Diags);
791
Chris Lattner97144fc2009-04-02 04:16:50 +0000792 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000793 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
794 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000795 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000796 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000797 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000798 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000799 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000800
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000801 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000802 SkipUntil(tok::semi);
803 continue;
804 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000805
Sean Huntbbd37c62009-11-21 08:43:09 +0000806 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000807 // Eat the semicolon at the end of stmt and convert the expr into a
808 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000809 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000810 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000811 }
812 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000813
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000814 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000815 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000819 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000821 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000822 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000824
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000825 if (T.consumeClose())
826 return StmtError();
827
828 return Actions.ActOnCompoundStmt(T.getOpenLocation(), T.getCloseLocation(),
829 move_arg(Stmts), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830}
831
Chris Lattner15ff1112008-12-12 06:31:07 +0000832/// ParseParenExprOrCondition:
833/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000834/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000835///
836/// This function parses and performs error recovery on the specified condition
837/// or expression (depending on whether we're in C++ or C mode). This function
838/// goes out of its way to recover well. It returns true if there was a parser
839/// error (the right paren couldn't be found), which indicates that the caller
840/// should try to recover harder. It returns false if the condition is
841/// successfully parsed. Note that a successful parse can still have semantic
842/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000843bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000844 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000845 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000846 bool ConvertToBoolean) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000847 BalancedDelimiterTracker T(*this, tok::l_paren);
848 T.consumeOpen();
849
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000850 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000851 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000852 else {
853 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000854 DeclResult = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000855
Douglas Gregor586596f2010-05-06 17:25:47 +0000856 // If required, convert to a boolean value.
857 if (!ExprResult.isInvalid() && ConvertToBoolean)
858 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000859 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000860 }
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Chris Lattner15ff1112008-12-12 06:31:07 +0000862 // If the parser was confused by the condition and we don't have a ')', try to
863 // recover by skipping ahead to a semi and bailing out. If condexp is
864 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000865 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000866 SkipUntil(tok::semi);
867 // Skipping may have stopped if it found the containing ')'. If so, we can
868 // continue parsing the if statement.
869 if (Tok.isNot(tok::r_paren))
870 return true;
871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Chris Lattner15ff1112008-12-12 06:31:07 +0000873 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000874 T.consumeClose();
Chris Lattner15ff1112008-12-12 06:31:07 +0000875 return false;
876}
877
878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879/// ParseIfStatement
880/// if-statement: [C99 6.8.4.1]
881/// 'if' '(' expression ')' statement
882/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000883/// [C++] 'if' '(' condition ')' statement
884/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000885///
Nico Weber5cb94a72011-12-22 23:26:17 +0000886StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs,
887 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000888 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000889
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000890 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
892
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000893 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000894 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000896 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000898
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000899 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
900
Chris Lattner22153252007-08-26 23:08:06 +0000901 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
902 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000903 //
904 // C++ 6.4p3:
905 // A name introduced by a declaration in a condition is in scope from its
906 // point of declaration until the end of the substatements controlled by the
907 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000908 // C++ 3.3.2p4:
909 // Names declared in the for-init-statement, and in the condition of if,
910 // while, for, and switch statements are local to the if, while, for, or
911 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000912 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000913 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000914
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000916 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000917 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000918 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000919 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000920
John McCall9ae2f072010-08-23 23:25:46 +0000921 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Chris Lattner0ecea032007-08-22 05:28:50 +0000923 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000924 // there is no compound stmt. C90 does not have this clause. We only do this
925 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000926 //
927 // C++ 6.4p1:
928 // The substatement in a selection-statement (each substatement, in the else
929 // form of the if statement) implicitly defines a local scope.
930 //
931 // For C++ we create a scope for the condition and a new scope for
932 // substatements because:
933 // -When the 'then' scope exits, we want the condition declaration to still be
934 // active for the 'else' scope too.
935 // -Sema will detect name clashes by considering declarations of a
936 // 'ControlScope' as part of its direct subscope.
937 // -If we wanted the condition and substatement to be in the same scope, we
938 // would have to notify ParseStatement not to create a new scope. It's
939 // simpler to let it create a new scope.
940 //
Mike Stump1eb44332009-09-09 15:08:12 +0000941 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000942 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000943
Chris Lattnerb96728d2007-10-29 05:08:52 +0000944 // Read the 'then' stmt.
945 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber5cb94a72011-12-22 23:26:17 +0000946
947 SourceLocation InnerStatementTrailingElseLoc;
948 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000949
Chris Lattnera36ce712007-08-22 05:16:28 +0000950 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000951 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000952
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 // If it has an else, parse it.
954 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000955 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000956 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000957
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000958 if (Tok.is(tok::kw_else)) {
Nico Weber5cb94a72011-12-22 23:26:17 +0000959 if (TrailingElseLoc)
960 *TrailingElseLoc = Tok.getLocation();
961
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000963 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000964
Chris Lattner0ecea032007-08-22 05:28:50 +0000965 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000966 // there is no compound stmt. C90 does not have this clause. We only do
967 // this if the body isn't a compound statement to avoid push/pop in common
968 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000969 //
970 // C++ 6.4p1:
971 // The substatement in a selection-statement (each substatement, in the else
972 // form of the if statement) implicitly defines a local scope.
973 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000974 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000975 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000976
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 ElseStmt = ParseStatement();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000978
Chris Lattnera36ce712007-08-22 05:16:28 +0000979 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000980 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +0000981 } else if (Tok.is(tok::code_completion)) {
982 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000983 cutOffParsing();
984 return StmtError();
Nico Weber5cb94a72011-12-22 23:26:17 +0000985 } else if (InnerStatementTrailingElseLoc.isValid()) {
986 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000988
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000989 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattner18914bc2008-12-12 06:19:11 +0000991 // If the condition was invalid, discard the if statement. We could recover
992 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000993 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000994 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000995
Chris Lattnerb96728d2007-10-29 05:08:52 +0000996 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000997 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000998 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000999 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1000 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1001 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001002 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001003 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +00001004 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001005
Chris Lattnerb96728d2007-10-29 05:08:52 +00001006 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001007 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001008 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001009 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001010 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001011
John McCall9ae2f072010-08-23 23:25:46 +00001012 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001013 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001014}
1015
1016/// ParseSwitchStatement
1017/// switch-statement:
1018/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001019/// [C++] 'switch' '(' condition ')' statement
Nico Weber5cb94a72011-12-22 23:26:17 +00001020StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs,
1021 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001022 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001023
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001024 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1026
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001027 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001028 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001030 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 }
Chris Lattner22153252007-08-26 23:08:06 +00001032
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001033 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1034
Chris Lattner22153252007-08-26 23:08:06 +00001035 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1036 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001037 //
1038 // C++ 6.4p3:
1039 // A name introduced by a declaration in a condition is in scope from its
1040 // point of declaration until the end of the substatements controlled by the
1041 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001042 // C++ 3.3.2p4:
1043 // Names declared in the for-init-statement, and in the condition of if,
1044 // while, for, and switch statements are local to the if, while, for, or
1045 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001046 //
Richard Trieubb9b80c2011-04-21 21:44:26 +00001047 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001048 if (C99orCXX)
1049 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001050 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051
1052 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001053 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001054 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001055 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001056 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001057
John McCall60d7b3a2010-08-24 06:29:42 +00001058 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001059 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001060
Douglas Gregor586596f2010-05-06 17:25:47 +00001061 if (Switch.isInvalid()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001062 // Skip the switch body.
Douglas Gregor586596f2010-05-06 17:25:47 +00001063 // FIXME: This is not optimal recovery, but parsing the body is more
1064 // dangerous due to the presence of case and default statements, which
1065 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001066 if (Tok.is(tok::l_brace)) {
1067 ConsumeBrace();
1068 SkipUntil(tok::r_brace, false, false);
1069 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001070 SkipUntil(tok::semi);
1071 return move(Switch);
1072 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001073
Chris Lattner0ecea032007-08-22 05:28:50 +00001074 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001075 // there is no compound stmt. C90 does not have this clause. We only do this
1076 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001077 //
1078 // C++ 6.4p1:
1079 // The substatement in a selection-statement (each substatement, in the else
1080 // form of the if statement) implicitly defines a local scope.
1081 //
1082 // See comments in ParseIfStatement for why we create a scope for the
1083 // condition and a new scope for substatement in C++.
1084 //
Mike Stump1eb44332009-09-09 15:08:12 +00001085 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001086 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001087
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001089 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001090
Chris Lattner7e52de42010-01-24 01:50:29 +00001091 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001092 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001093 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001094
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001095 if (Body.isInvalid()) {
Chris Lattner7e52de42010-01-24 01:50:29 +00001096 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001097
1098 // Put the synthesized null statement on the same line as the end of switch
1099 // condition.
1100 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1101 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1102 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001103
John McCall9ae2f072010-08-23 23:25:46 +00001104 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001105}
1106
1107/// ParseWhileStatement
1108/// while-statement: [C99 6.8.5.1]
1109/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001110/// [C++] 'while' '(' condition ')' statement
Nico Weber5cb94a72011-12-22 23:26:17 +00001111StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs,
1112 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001113 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001114
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001115 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 SourceLocation WhileLoc = Tok.getLocation();
1117 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001118
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001119 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001120 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001122 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001124
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001125 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1126
Chris Lattner22153252007-08-26 23:08:06 +00001127 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1128 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001129 //
1130 // C++ 6.4p3:
1131 // A name introduced by a declaration in a condition is in scope from its
1132 // point of declaration until the end of the substatements controlled by the
1133 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001134 // C++ 3.3.2p4:
1135 // Names declared in the for-init-statement, and in the condition of if,
1136 // while, for, and switch statements are local to the if, while, for, or
1137 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001138 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001139 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001140 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001141 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1142 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001143 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001144 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1145 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001148 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001149 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001150 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001151 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001152
John McCall9ae2f072010-08-23 23:25:46 +00001153 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Chris Lattner0ecea032007-08-22 05:28:50 +00001155 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001156 // there is no compound stmt. C90 does not have this clause. We only do this
1157 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001158 //
1159 // C++ 6.5p2:
1160 // The substatement in an iteration-statement implicitly defines a local scope
1161 // which is entered and exited each time through the loop.
1162 //
1163 // See comments in ParseIfStatement for why we create a scope for the
1164 // condition and a new scope for substatement in C++.
1165 //
Mike Stump1eb44332009-09-09 15:08:12 +00001166 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001167 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001170 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001171
Chris Lattner0ecea032007-08-22 05:28:50 +00001172 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001173 InnerScope.Exit();
1174 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001175
John McCalld226f652010-08-21 09:40:31 +00001176 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001177 return StmtError();
1178
John McCall9ae2f072010-08-23 23:25:46 +00001179 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001180}
1181
1182/// ParseDoStatement
1183/// do-statement: [C99 6.8.5.2]
1184/// 'do' statement 'while' '(' expression ')' ';'
1185/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +00001186StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001187 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001188
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001189 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001191
Chris Lattner22153252007-08-26 23:08:06 +00001192 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1193 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001194 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +00001195 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001196 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001197 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001198 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001199
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001200 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001201
Chris Lattner0ecea032007-08-22 05:28:50 +00001202 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001203 // there is no compound stmt. C90 does not have this clause. We only do this
1204 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001205 //
1206 // C++ 6.5p2:
1207 // The substatement in an iteration-statement implicitly defines a local scope
1208 // which is entered and exited each time through the loop.
1209 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001210 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +00001211 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001212 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001213
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001215 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001216
Chris Lattner0ecea032007-08-22 05:28:50 +00001217 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001218 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001219
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001220 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001221 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001222 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001223 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001224 SkipUntil(tok::semi, false, true);
1225 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001226 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 }
1228 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001229
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001230 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001231 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001232 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001233 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001235
Chris Lattnerff871fb2008-12-12 06:35:28 +00001236 // Parse the parenthesized condition.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001237 BalancedDelimiterTracker T(*this, tok::l_paren);
1238 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001239 ExprResult Cond = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001240 T.consumeClose();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001241 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001242
Sebastian Redl9a920342008-12-11 19:48:14 +00001243 if (Cond.isInvalid() || Body.isInvalid())
1244 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001245
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001246 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1247 Cond.get(), T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001248}
1249
1250/// ParseForStatement
1251/// for-statement: [C99 6.8.5.3]
1252/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1253/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001254/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1255/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001256/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001257/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1258/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001259///
1260/// [C++] for-init-statement:
1261/// [C++] expression-statement
1262/// [C++] simple-declaration
1263///
Richard Smithad762fc2011-04-14 22:09:26 +00001264/// [C++0x] for-range-declaration:
1265/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1266/// [C++0x] for-range-initializer:
1267/// [C++0x] expression
1268/// [C++0x] braced-init-list [TODO]
Nico Weber5cb94a72011-12-22 23:26:17 +00001269StmtResult Parser::ParseForStatement(ParsedAttributes &attrs,
1270 SourceLocation *TrailingElseLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001271 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001272
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001273 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001275
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001276 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001277 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001279 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001281
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001282 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001283
Chris Lattner22153252007-08-26 23:08:06 +00001284 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1285 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001286 //
1287 // C++ 6.4p3:
1288 // A name introduced by a declaration in a condition is in scope from its
1289 // point of declaration until the end of the substatements controlled by the
1290 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001291 // C++ 3.3.2p4:
1292 // Names declared in the for-init-statement, and in the condition of if,
1293 // while, for, and switch statements are local to the if, while, for, or
1294 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001295 // C++ 6.5.3p1:
1296 // Names declared in the for-init-statement are in the same declarative-region
1297 // as those declared in the condition.
1298 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001299 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001300 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001301 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1302 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001303 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001304 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1305
1306 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001307
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001308 BalancedDelimiterTracker T(*this, tok::l_paren);
1309 T.consumeOpen();
1310
John McCall60d7b3a2010-08-24 06:29:42 +00001311 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001312
Richard Smithad762fc2011-04-14 22:09:26 +00001313 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001314 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001315 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001316 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001317 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001318 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001319 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001320 Decl *SecondVar = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001321
Douglas Gregor791215b2009-09-21 20:51:25 +00001322 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001323 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001324 C99orCXXorObjC? Sema::PCC_ForInit
1325 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001326 cutOffParsing();
1327 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001328 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001329
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001331 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 // no first part, eat the ';'.
1333 ConsumeToken();
Eli Friedman9490ab42011-12-20 01:50:37 +00001334 } else if (isForInitDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001336 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001338
John McCall0b7e6782011-03-24 11:26:52 +00001339 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001340 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001341
Richard Smithad762fc2011-04-14 22:09:26 +00001342 // In C++0x, "for (T NS:a" might not be a typo for ::
1343 bool MightBeForRangeStmt = getLang().CPlusPlus;
1344 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1345
Chris Lattner97144fc2009-04-02 04:16:50 +00001346 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001347 StmtVector Stmts(Actions);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001348 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001349 DeclEnd, attrs, false,
1350 MightBeForRangeStmt ?
1351 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001352 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Richard Smithad762fc2011-04-14 22:09:26 +00001354 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith7fe62082011-10-15 05:09:34 +00001355 Diag(ForRangeInit.ColonLoc, getLang().CPlusPlus0x ?
1356 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8f4fb192011-09-04 19:54:14 +00001357
Richard Smithad762fc2011-04-14 22:09:26 +00001358 ForRange = true;
1359 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001360 ConsumeToken();
1361 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001362 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001363 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001364 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001365
Douglas Gregorfb629412010-08-23 21:17:50 +00001366 if (Tok.is(tok::code_completion)) {
1367 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001368 cutOffParsing();
1369 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001370 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001371 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001372 } else {
1373 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001374 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 } else {
1376 Value = ParseExpression();
1377
John McCallf6a16482010-12-04 03:47:34 +00001378 ForEach = isTokIdentifier_in();
1379
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001381 if (!Value.isInvalid()) {
1382 if (ForEach)
1383 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1384 else
1385 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1386 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001387
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001388 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001390 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001391 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001392
Douglas Gregorfb629412010-08-23 21:17:50 +00001393 if (Tok.is(tok::code_completion)) {
1394 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001395 cutOffParsing();
1396 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001397 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001398 Collection = ParseExpression();
Richard Smitha44854a2011-12-20 22:56:20 +00001399 } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) &&
1400 !FirstPart.isInvalid()) {
1401 // User tried to write the reasonable, but ill-formed, for-range-statement
1402 // for (expr : expr) { ... }
1403 Diag(Tok, diag::err_for_range_expected_decl)
1404 << FirstPart.get()->getSourceRange();
1405 SkipUntil(tok::r_paren, false, true);
1406 SecondPartIsInvalid = true;
Chris Lattner682bf922009-03-29 16:50:03 +00001407 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001408 if (!Value.isInvalid()) {
1409 Diag(Tok, diag::err_expected_semi_for);
1410 } else {
1411 // Skip until semicolon or rparen, don't consume it.
1412 SkipUntil(tok::r_paren, true, true);
1413 if (Tok.is(tok::semi))
1414 ConsumeToken();
1415 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 }
1417 }
Richard Smithad762fc2011-04-14 22:09:26 +00001418 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001419 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001420 // Parse the second part of the for specifier.
1421 if (Tok.is(tok::semi)) { // for (...;;
1422 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001423 } else if (Tok.is(tok::r_paren)) {
1424 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001425 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001426 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001427 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001428 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1429 else {
1430 Second = ParseExpression();
1431 if (!Second.isInvalid())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001432 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001433 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001434 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001435 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001436 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001437 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001438
Douglas Gregorb72c7782011-02-17 03:38:46 +00001439 if (Tok.isNot(tok::semi)) {
1440 if (!SecondPartIsInvalid || SecondVar)
1441 Diag(Tok, diag::err_expected_semi_for);
1442 else
1443 // Skip until semicolon or rparen, don't consume it.
1444 SkipUntil(tok::r_paren, true, true);
1445 }
1446
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001447 if (Tok.is(tok::semi)) {
1448 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001449 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001450
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001451 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001452 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001453 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001454 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001455 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001458 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001459
Richard Smithad762fc2011-04-14 22:09:26 +00001460 // We need to perform most of the semantic analysis for a C++0x for-range
1461 // statememt before parsing the body, in order to be able to deduce the type
1462 // of an auto-typed loop variable.
1463 StmtResult ForRangeStmt;
John McCall990567c2011-07-27 01:07:15 +00001464 if (ForRange) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001465 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smithad762fc2011-04-14 22:09:26 +00001466 FirstPart.take(),
1467 ForRangeInit.ColonLoc,
1468 ForRangeInit.RangeExpr.get(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001469 T.getCloseLocation());
Richard Smithad762fc2011-04-14 22:09:26 +00001470
John McCall990567c2011-07-27 01:07:15 +00001471
1472 // Similarly, we need to do the semantic analysis for a for-range
1473 // statement immediately in order to close over temporaries correctly.
1474 } else if (ForEach) {
1475 if (!Collection.isInvalid())
1476 Collection =
1477 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1478 }
1479
Chris Lattner0ecea032007-08-22 05:28:50 +00001480 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001481 // there is no compound stmt. C90 does not have this clause. We only do this
1482 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001483 //
1484 // C++ 6.5p2:
1485 // The substatement in an iteration-statement implicitly defines a local scope
1486 // which is entered and exited each time through the loop.
1487 //
1488 // See comments in ParseIfStatement for why we create a scope for
1489 // for-init-statement/condition and a new scope for substatement in C++.
1490 //
Mike Stump1eb44332009-09-09 15:08:12 +00001491 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001492 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001493
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001495 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001496
Chris Lattner0ecea032007-08-22 05:28:50 +00001497 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001498 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001499
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001501 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001502
1503 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001504 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001505
Richard Smithad762fc2011-04-14 22:09:26 +00001506 if (ForEach)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001507 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1508 FirstPart.take(),
1509 Collection.take(),
1510 T.getCloseLocation(),
1511 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Richard Smithad762fc2011-04-14 22:09:26 +00001513 if (ForRange)
1514 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1515
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001516 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1517 SecondPart, SecondVar, ThirdPart,
1518 T.getCloseLocation(), Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001519}
1520
1521/// ParseGotoStatement
1522/// jump-statement:
1523/// 'goto' identifier ';'
1524/// [GNU] 'goto' '*' expression ';'
1525///
1526/// Note: this lets the caller parse the end ';'.
1527///
John McCall7f040a92010-12-24 02:08:15 +00001528StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001529 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001530
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001531 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001533
John McCall60d7b3a2010-08-24 06:29:42 +00001534 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001535 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001536 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1537 Tok.getLocation());
1538 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001540 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 // GNU indirect goto extension.
1542 Diag(Tok, diag::ext_gnu_indirect_goto);
1543 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001544 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001545 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001547 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 }
John McCall9ae2f072010-08-23 23:25:46 +00001549 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001550 } else {
1551 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001552 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001553 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001554
Sebastian Redl9a920342008-12-11 19:48:14 +00001555 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001556}
1557
1558/// ParseContinueStatement
1559/// jump-statement:
1560/// 'continue' ';'
1561///
1562/// Note: this lets the caller parse the end ';'.
1563///
John McCall7f040a92010-12-24 02:08:15 +00001564StmtResult Parser::ParseContinueStatement(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 ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001568 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001569}
1570
1571/// ParseBreakStatement
1572/// jump-statement:
1573/// 'break' ';'
1574///
1575/// Note: this lets the caller parse the end ';'.
1576///
John McCall7f040a92010-12-24 02:08:15 +00001577StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001578 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001579
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001581 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001582}
1583
1584/// ParseReturnStatement
1585/// jump-statement:
1586/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001587StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001588 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001589
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001590 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001592
John McCall60d7b3a2010-08-24 06:29:42 +00001593 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001594 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001595 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001596 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001597 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001598 return StmtError();
1599 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001600
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001601 // FIXME: This is a hack to allow something like C++0x's generalized
1602 // initializer lists, but only enough of this feature to allow Clang to
1603 // parse libstdc++ 4.5's headers.
1604 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1605 R = ParseInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001606 if (R.isUsable())
1607 Diag(R.get()->getLocStart(), getLang().CPlusPlus0x ?
1608 diag::warn_cxx98_compat_generalized_initializer_lists :
1609 diag::ext_generalized_initializer_lists)
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001610 << R.get()->getSourceRange();
1611 } else
1612 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001613 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001615 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 }
1617 }
John McCall9ae2f072010-08-23 23:25:46 +00001618 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001619}
1620
Eli Friedman3fedbe12011-09-30 01:13:51 +00001621/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1622/// this routine is called to collect the tokens for an MS asm statement.
1623StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1624 SourceManager &SrcMgr = PP.getSourceManager();
1625 SourceLocation EndLoc = AsmLoc;
1626 do {
1627 bool InBraces = false;
NAKAMURA Takumi96e21712011-10-08 11:31:53 +00001628 unsigned short savedBraceCount = 0;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001629 bool InAsmComment = false;
1630 FileID FID;
NAKAMURA Takumi96e21712011-10-08 11:31:53 +00001631 unsigned LineNo = 0;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001632 unsigned NumTokensRead = 0;
1633 SourceLocation LBraceLoc;
1634
1635 if (Tok.is(tok::l_brace)) {
1636 // Braced inline asm: consume the opening brace.
1637 InBraces = true;
1638 savedBraceCount = BraceCount;
1639 EndLoc = LBraceLoc = ConsumeBrace();
1640 ++NumTokensRead;
1641 } else {
1642 // Single-line inline asm; compute which line it is on.
1643 std::pair<FileID, unsigned> ExpAsmLoc =
1644 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1645 FID = ExpAsmLoc.first;
1646 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1647 }
1648
Steve Naroff03d6bc62008-02-08 03:36:19 +00001649 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001650 do {
Eli Friedman3fedbe12011-09-30 01:13:51 +00001651 // If we hit EOF, we're done, period.
1652 if (Tok.is(tok::eof))
1653 break;
1654 // When we consume the closing brace, we're done.
1655 if (InBraces && BraceCount == savedBraceCount)
1656 break;
1657
1658 if (!InAsmComment && Tok.is(tok::semi)) {
1659 // A semicolon in an asm is the start of a comment.
1660 InAsmComment = true;
1661 if (InBraces) {
1662 // Compute which line the comment is on.
1663 std::pair<FileID, unsigned> ExpSemiLoc =
1664 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1665 FID = ExpSemiLoc.first;
1666 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1667 }
1668 } else if (!InBraces || InAsmComment) {
1669 // If end-of-line is significant, check whether this token is on a
1670 // new line.
1671 std::pair<FileID, unsigned> ExpLoc =
1672 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1673 if (ExpLoc.first != FID ||
1674 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1675 // If this is a single-line __asm, we're done.
1676 if (!InBraces)
1677 break;
1678 // We're no longer in a comment.
1679 InAsmComment = false;
1680 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1681 // Single-line asm always ends when a closing brace is seen.
1682 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1683 // does MSVC do here?
1684 break;
1685 }
1686 }
1687
1688 // Consume the next token; make sure we don't modify the brace count etc.
1689 // if we are in a comment.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001690 EndLoc = TokLoc;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001691 if (InAsmComment)
1692 PP.Lex(Tok);
1693 else
1694 ConsumeAnyToken();
Steve Naroff36280972008-02-08 18:01:27 +00001695 TokLoc = Tok.getLocation();
Eli Friedman3fedbe12011-09-30 01:13:51 +00001696 ++NumTokensRead;
1697 } while (1);
1698
1699 if (InBraces && BraceCount != savedBraceCount) {
1700 // __asm without closing brace (this can happen at EOF).
1701 Diag(Tok, diag::err_expected_rbrace);
1702 Diag(LBraceLoc, diag::note_matching) << "{";
1703 return StmtError();
1704 } else if (NumTokensRead == 0) {
1705 // Empty __asm.
1706 Diag(Tok, diag::err_expected_lbrace);
1707 return StmtError();
1708 }
1709 // Multiple adjacent asm's form together into a single asm statement
1710 // in the AST.
1711 if (!Tok.is(tok::kw_asm))
1712 break;
1713 EndLoc = ConsumeToken();
1714 } while (1);
1715 // FIXME: Need to actually grab the data and pass it on to Sema. Ideally,
1716 // what Sema wants is a string of the entire inline asm, with one instruction
1717 // per line and all the __asm keywords stripped out, and a way of mapping
1718 // from any character of that string to its location in the original source
1719 // code. I'm not entirely sure how to go about that, though.
Mike Stump95059b52009-12-11 00:04:56 +00001720 Token t;
1721 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001722 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001723 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001724 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001725 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001726 ExprVector Constraints(Actions);
1727 ExprVector Exprs(Actions);
1728 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001729 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001730 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001731 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001732 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001733}
1734
Reid Spencer5f016e22007-07-11 17:01:13 +00001735/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001736/// asm-statement:
1737/// gnu-asm-statement
1738/// ms-asm-statement
1739///
1740/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001741/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1742///
1743/// [GNU] asm-argument:
1744/// asm-string-literal
1745/// asm-string-literal ':' asm-operands[opt]
1746/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1747/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1748/// ':' asm-clobbers
1749///
1750/// [GNU] asm-clobbers:
1751/// asm-string-literal
1752/// asm-clobbers ',' asm-string-literal
1753///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001754/// [MS] ms-asm-statement:
Eli Friedman3fedbe12011-09-30 01:13:51 +00001755/// ms-asm-block
1756/// ms-asm-block ms-asm-statement
Steve Naroff5f8aa692008-02-11 23:15:56 +00001757///
Eli Friedman3fedbe12011-09-30 01:13:51 +00001758/// [MS] ms-asm-block:
1759/// '__asm' ms-asm-line '\n'
1760/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1761///
1762/// [MS] ms-asm-instruction-block
1763/// ms-asm-line
1764/// ms-asm-line '\n' ms-asm-instruction-block
Steve Naroff5f8aa692008-02-11 23:15:56 +00001765///
John McCall60d7b3a2010-08-24 06:29:42 +00001766StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001767 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001768 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001769
Francois Pichet62ec1f22011-09-17 17:15:52 +00001770 if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001771 msAsm = true;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001772 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001773 }
John McCall0b7e6782011-03-24 11:26:52 +00001774 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001776 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001777
Reid Spencer5f016e22007-07-11 17:01:13 +00001778 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1779 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001780 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001782 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001785 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001786 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001787 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001789 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001791 BalancedDelimiterTracker T(*this, tok::l_paren);
1792 T.consumeOpen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001793
John McCall60d7b3a2010-08-24 06:29:42 +00001794 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001795 if (AsmString.isInvalid()) {
1796 // If the reason we are recovering is because of an improper string
1797 // literal, it makes the most sense just to consume to the ')'.
1798 if (isTokenStringLiteral())
1799 T.skipToEnd();
Sebastian Redl9a920342008-12-11 19:48:14 +00001800 return StmtError();
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001801 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001802
Chris Lattner5f9e2722011-07-23 10:55:15 +00001803 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001804 ExprVector Constraints(Actions);
1805 ExprVector Exprs(Actions);
1806 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001807
Anders Carlssondfab34a2008-02-05 23:03:50 +00001808 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001809 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001810 T.consumeClose();
Chris Lattner64cb4752009-12-20 23:00:41 +00001811 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001812 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattner64cb4752009-12-20 23:00:41 +00001813 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001814 AsmString.take(), move_arg(Clobbers),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001815 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001817
Chris Lattner64cb4752009-12-20 23:00:41 +00001818 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001819 bool AteExtraColon = false;
1820 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1821 // In C++ mode, parse "::" like ": :".
1822 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001823 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001824
Chris Lattner64056462009-12-20 23:08:04 +00001825 if (!AteExtraColon &&
1826 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001827 return StmtError();
1828 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001829
Chris Lattner64cb4752009-12-20 23:00:41 +00001830 unsigned NumOutputs = Names.size();
1831
1832 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001833 if (AteExtraColon ||
1834 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1835 // In C++ mode, parse "::" like ": :".
1836 if (AteExtraColon)
1837 AteExtraColon = false;
1838 else {
1839 AteExtraColon = Tok.is(tok::coloncolon);
1840 ConsumeToken();
1841 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001842
Chris Lattner64056462009-12-20 23:08:04 +00001843 if (!AteExtraColon &&
1844 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001845 return StmtError();
1846 }
1847
1848 assert(Names.size() == Constraints.size() &&
1849 Constraints.size() == Exprs.size() &&
1850 "Input operand size mismatch!");
1851
1852 unsigned NumInputs = Names.size() - NumOutputs;
1853
1854 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001855 if (AteExtraColon || Tok.is(tok::colon)) {
1856 if (!AteExtraColon)
1857 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001858
Chandler Carruth102e1b62010-07-22 07:11:21 +00001859 // Parse the asm-string list for clobbers if present.
1860 if (Tok.isNot(tok::r_paren)) {
1861 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001862 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001863
Chandler Carruth102e1b62010-07-22 07:11:21 +00001864 if (Clobber.isInvalid())
1865 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001866
Chandler Carruth102e1b62010-07-22 07:11:21 +00001867 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001868
Chandler Carruth102e1b62010-07-22 07:11:21 +00001869 if (Tok.isNot(tok::comma)) break;
1870 ConsumeToken();
1871 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001872 }
1873 }
1874
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001875 T.consumeClose();
Chris Lattner64cb4752009-12-20 23:00:41 +00001876 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001877 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001878 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001879 AsmString.take(), move_arg(Clobbers),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001880 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001881}
1882
1883/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001884/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001885///
1886/// [GNU] asm-operands:
1887/// asm-operand
1888/// asm-operands ',' asm-operand
1889///
1890/// [GNU] asm-operand:
1891/// asm-string-literal '(' expression ')'
1892/// '[' identifier ']' asm-string-literal '(' expression ')'
1893///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001894//
1895// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001896bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieuf81e5a92011-09-09 02:00:50 +00001897 SmallVectorImpl<Expr *> &Constraints,
1898 SmallVectorImpl<Expr *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001900 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001902
1903 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001904 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001905 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001906 BalancedDelimiterTracker T(*this, tok::l_square);
1907 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001909 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 Diag(Tok, diag::err_expected_ident);
1911 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001912 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001913 }
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Anders Carlssonb235fc22007-11-22 01:36:19 +00001915 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001916 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001917
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001918 Names.push_back(II);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001919 T.consumeClose();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001920 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001921 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001922
John McCall60d7b3a2010-08-24 06:29:42 +00001923 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001924 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001925 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001926 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001927 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001928 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001929
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001930 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001931 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001933 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001935
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 // Read the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001937 BalancedDelimiterTracker T(*this, tok::l_paren);
1938 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001939 ExprResult Res(ParseExpression());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001940 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001941 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001943 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001944 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001945 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001946 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001947 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001948 ConsumeToken();
1949 }
1950}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001951
Douglas Gregorc9977d02011-03-16 17:05:57 +00001952Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001953 assert(Tok.is(tok::l_brace));
1954 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001955
Douglas Gregorc9977d02011-03-16 17:05:57 +00001956 if (PP.isCodeCompletionEnabled()) {
1957 if (trySkippingFunctionBodyForCodeCompletion()) {
1958 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001959 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001960 }
1961 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001962
John McCallf312b1e2010-08-26 23:41:50 +00001963 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1964 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001966 // Do not enter a scope for the brace, as the arguments are in the same scope
1967 // (the function body) as the body itself. Instead, just read the statement
1968 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001969 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001970
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001971 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001972 if (FnBody.isInvalid()) {
1973 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00001974 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001975 MultiStmtArg(Actions), false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001976 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001977
Douglas Gregorc9977d02011-03-16 17:05:57 +00001978 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001979 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001980}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001981
Sebastian Redld3a413d2009-04-26 20:35:05 +00001982/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1983///
1984/// function-try-block:
1985/// 'try' ctor-initializer[opt] compound-statement handler-seq
1986///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001987Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001988 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1989 SourceLocation TryLoc = ConsumeToken();
1990
John McCallf312b1e2010-08-26 23:41:50 +00001991 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1992 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001993
1994 // Constructor initializer list?
1995 if (Tok.is(tok::colon))
1996 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00001997 else
1998 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001999
Douglas Gregorc9977d02011-03-16 17:05:57 +00002000 if (PP.isCodeCompletionEnabled()) {
2001 if (trySkippingFunctionBodyForCodeCompletion()) {
2002 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00002003 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00002004 }
2005 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002006
Sebastian Redlde1b60a2009-04-26 21:08:36 +00002007 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00002008 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00002009 // If we failed to parse the try-catch, we just give the function an empty
2010 // compound statement as the body.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002011 if (FnBody.isInvalid()) {
2012 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redlde1b60a2009-04-26 21:08:36 +00002013 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00002014 MultiStmtArg(Actions), false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002015 }
Sebastian Redld3a413d2009-04-26 20:35:05 +00002016
Douglas Gregorc9977d02011-03-16 17:05:57 +00002017 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00002018 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00002019}
2020
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00002021bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002022 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00002023 assert(PP.isCodeCompletionEnabled() &&
2024 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002025
2026 // We're in code-completion mode. Skip parsing for all function bodies unless
2027 // the body contains the code-completion point.
2028 TentativeParsingAction PA(*this);
2029 ConsumeBrace();
2030 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
2031 /*StopAtCodeCompletion=*/true)) {
2032 PA.Commit();
2033 return true;
2034 }
2035
2036 PA.Revert();
2037 return false;
2038}
2039
Sebastian Redla0fd8652008-12-21 16:41:36 +00002040/// ParseCXXTryBlock - Parse a C++ try-block.
2041///
2042/// try-block:
2043/// 'try' compound-statement handler-seq
2044///
John McCall7f040a92010-12-24 02:08:15 +00002045StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002046 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00002047
Sebastian Redla0fd8652008-12-21 16:41:36 +00002048 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2049
2050 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00002051 return ParseCXXTryBlockCommon(TryLoc);
2052}
2053
2054/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2055/// function-try-block.
2056///
2057/// try-block:
2058/// 'try' compound-statement handler-seq
2059///
2060/// function-try-block:
2061/// 'try' ctor-initializer[opt] compound-statement handler-seq
2062///
2063/// handler-seq:
2064/// handler handler-seq[opt]
2065///
John Wiegley28bbe4b2011-04-28 01:08:34 +00002066/// [Borland] try-block:
2067/// 'try' compound-statement seh-except-block
2068/// 'try' compound-statment seh-finally-block
2069///
John McCall60d7b3a2010-08-24 06:29:42 +00002070StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002071 if (Tok.isNot(tok::l_brace))
2072 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00002073 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002074 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregorbca01b42011-07-06 22:04:06 +00002075 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
2076 Scope::DeclScope|Scope::TryScope));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002077 if (TryBlock.isInvalid())
2078 return move(TryBlock);
2079
John Wiegley28bbe4b2011-04-28 01:08:34 +00002080 // Borland allows SEH-handlers with 'try'
Douglas Gregorb57791e2011-10-21 03:57:52 +00002081
2082 if((Tok.is(tok::identifier) &&
2083 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2084 Tok.is(tok::kw___finally)) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002085 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2086 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +00002087 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002088 SourceLocation Loc = ConsumeToken();
2089 Handler = ParseSEHExceptBlock(Loc);
2090 }
2091 else {
2092 SourceLocation Loc = ConsumeToken();
2093 Handler = ParseSEHFinallyBlock(Loc);
2094 }
2095 if(Handler.isInvalid())
2096 return move(Handler);
John McCall7f040a92010-12-24 02:08:15 +00002097
John Wiegley28bbe4b2011-04-28 01:08:34 +00002098 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2099 TryLoc,
2100 TryBlock.take(),
2101 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002102 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00002103 else {
2104 StmtVector Handlers(Actions);
2105 MaybeParseCXX0XAttributes(attrs);
2106 ProhibitAttributes(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002107
John Wiegley28bbe4b2011-04-28 01:08:34 +00002108 if (Tok.isNot(tok::kw_catch))
2109 return StmtError(Diag(Tok, diag::err_expected_catch));
2110 while (Tok.is(tok::kw_catch)) {
2111 StmtResult Handler(ParseCXXCatchBlock());
2112 if (!Handler.isInvalid())
2113 Handlers.push_back(Handler.release());
2114 }
2115 // Don't bother creating the full statement if we don't have any usable
2116 // handlers.
2117 if (Handlers.empty())
2118 return StmtError();
2119
2120 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2121 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00002122}
2123
2124/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2125///
2126/// handler:
2127/// 'catch' '(' exception-declaration ')' compound-statement
2128///
2129/// exception-declaration:
2130/// type-specifier-seq declarator
2131/// type-specifier-seq abstract-declarator
2132/// type-specifier-seq
2133/// '...'
2134///
John McCall60d7b3a2010-08-24 06:29:42 +00002135StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002136 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2137
2138 SourceLocation CatchLoc = ConsumeToken();
2139
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002140 BalancedDelimiterTracker T(*this, tok::l_paren);
2141 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redla0fd8652008-12-21 16:41:36 +00002142 return StmtError();
2143
2144 // C++ 3.3.2p3:
2145 // The name in a catch exception-declaration is local to the handler and
2146 // shall not be redeclared in the outermost block of the handler.
2147 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2148
2149 // exception-declaration is equivalent to '...' or a parameter-declaration
2150 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00002151 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002152 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00002153 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00002154 if (ParseCXXTypeSpecifierSeq(DS))
2155 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00002156 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2157 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002158 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002159 } else
2160 ConsumeToken();
2161
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002162 T.consumeClose();
2163 if (T.getCloseLocation().isInvalid())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002164 return StmtError();
2165
2166 if (Tok.isNot(tok::l_brace))
2167 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2168
Sean Huntbbd37c62009-11-21 08:43:09 +00002169 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002170 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002171 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002172 if (Block.isInvalid())
2173 return move(Block);
2174
John McCall9ae2f072010-08-23 23:25:46 +00002175 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002176}
Francois Pichet1e862692011-05-06 20:48:22 +00002177
2178void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002179 IfExistsCondition Result;
Francois Pichetf9860382011-05-07 17:30:27 +00002180 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002181 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002182
Douglas Gregor3896fc52011-10-24 22:31:10 +00002183 // Handle dependent statements by parsing the braces as a compound statement.
2184 // This is not the same behavior as Visual C++, which don't treat this as a
2185 // compound statement, but for Clang's type checking we can't have anything
2186 // inside these braces escaping to the surrounding code.
2187 if (Result.Behavior == IEB_Dependent) {
2188 if (!Tok.is(tok::l_brace)) {
2189 Diag(Tok, diag::err_expected_lbrace);
2190 return;
2191 }
2192
2193 ParsedAttributes Attrs(AttrFactory);
2194 StmtResult Compound = ParseCompoundStatement(Attrs);
Douglas Gregorba0513d2011-10-25 01:33:02 +00002195 if (Compound.isInvalid())
2196 return;
2197
2198 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2199 Result.IsIfExists,
2200 Result.SS,
2201 Result.Name,
2202 Compound.get());
2203 if (DepResult.isUsable())
2204 Stmts.push_back(DepResult.get());
Douglas Gregor3896fc52011-10-24 22:31:10 +00002205 return;
2206 }
2207
2208 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2209 if (Braces.consumeOpen()) {
Francois Pichet1e862692011-05-06 20:48:22 +00002210 Diag(Tok, diag::err_expected_lbrace);
2211 return;
2212 }
Francois Pichet1e862692011-05-06 20:48:22 +00002213
Douglas Gregor3896fc52011-10-24 22:31:10 +00002214 switch (Result.Behavior) {
2215 case IEB_Parse:
2216 // Parse the statements below.
2217 break;
2218
2219 case IEB_Dependent:
2220 llvm_unreachable("Dependent case handled above");
Douglas Gregor3896fc52011-10-24 22:31:10 +00002221
2222 case IEB_Skip:
2223 Braces.skipToEnd();
Francois Pichet1e862692011-05-06 20:48:22 +00002224 return;
2225 }
2226
2227 // Condition is true, parse the statements.
2228 while (Tok.isNot(tok::r_brace)) {
2229 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2230 if (R.isUsable())
2231 Stmts.push_back(R.release());
2232 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002233 Braces.consumeClose();
Francois Pichet1e862692011-05-06 20:48:22 +00002234}