blob: 8af4da6a846445ed074595e26c25743254183192 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +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 Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +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 Kyrtzidisdee82912008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000042/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000043/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000044/// [OBC] objc-throw-statement
45/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000046/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000047/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// [OMP] openmp-construct [TODO]
49///
50/// labeled-statement:
51/// identifier ':' statement
52/// 'case' constant-expression ':' statement
53/// 'default' ':' statement
54///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000055/// selection-statement:
56/// if-statement
57/// switch-statement
58///
59/// iteration-statement:
60/// while-statement
61/// do-statement
62/// for-statement
63///
Chris Lattner9075bd72006-08-10 04:59:57 +000064/// expression-statement:
65/// expression[opt] ';'
66///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000067/// jump-statement:
68/// 'goto' identifier ';'
69/// 'continue' ';'
70/// 'break' ';'
71/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000072/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000073///
Fariborz Jahanian90814572007-10-04 20:19:06 +000074/// [OBC] objc-throw-statement:
75/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000076/// [OBC] '@' 'throw' ';'
77///
John McCalldadc5752010-08-24 06:29:42 +000078StmtResult
Nico Weber3cef1082011-12-22 23:26:17 +000079Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
80 SourceLocation *TrailingElseLoc) {
Chris Lattner503fadc2006-08-10 05:45:44 +000081 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000082 StmtResult Res;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +000083
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000085
John McCall084e83d2011-03-24 11:26:52 +000086 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000087 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +000088
Chris Lattner503fadc2006-08-10 05:45:44 +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 Gregor0e7dde52011-04-24 05:37:28 +000092Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000093 tok::TokenKind Kind = Tok.getKind();
94 SourceLocation AtLoc;
95 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000096 case tok::at: // May be a @try or @throw statement
97 {
98 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000099 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000100 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000101
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000102 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000103 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000104 cutOffParsing();
105 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000106
Douglas Gregor0e7dde52011-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 Kyrtzidis07b8b632008-07-12 21:04:42 +0000110 // identifier ':' statement
John McCall53fa7142010-12-24 02:08:15 +0000111 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000112 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000113
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000114 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000115 CXXScopeSpec SS;
116 IdentifierInfo *Name = Tok.getIdentifierInfo();
117 SourceLocation NameLoc = Tok.getLocation();
Richard Trieu01fc0012011-09-19 19:01:00 +0000118
119 if (getLang().CPlusPlus)
120 CheckForTemplateAndDigraph(Next, ParsedType(),
121 /*EnteringContext=*/false, *Name, SS);
122
Douglas Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000134
Douglas Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000138
Douglas Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000146
Douglas Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000149 // we're in a syntactic context we haven't handled yet.
150 break;
151
Douglas Gregor19b7acf2011-04-27 05:41:15 +0000152 case Sema::NC_Type:
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000153 Tok.setKind(tok::annot_typename);
154 setTypeAnnotation(Tok, Classification.getType());
155 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000156 PP.AnnotateCachedTokens(Tok);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000157 break;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000158
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000159 case Sema::NC_Expression:
Douglas Gregorda6c89d2011-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 Takumi82a35112011-10-08 11:31:46 +0000165
Douglas Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000172 TemplateTy::make(Classification.getTemplateName()),
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000173 Classification.getTemplateNameKind(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000174 SS, SourceLocation(), Id,
Douglas Gregor8b02cd02011-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 Takumi82a35112011-10-08 11:31:46 +0000181 return StmtError();
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000182 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000183
184 // If the next token is '::', jump right into parsing a
Douglas Gregor8b02cd02011-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 Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000193 return StmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000194 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000195
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000196 // We've annotated a template-id, so try again now.
197 goto Retry;
198 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000199
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000200 case Sema::NC_NestedNameSpecifier:
201 // FIXME: Implement this!
202 break;
203 }
204 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000205
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000206 // Fall through
207 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000208
Chris Lattner803802d2009-03-24 17:04:48 +0000209 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000210 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000211 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000212 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000213 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000214 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000215 }
216
217 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000218 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000219 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000222 return ParseExprStatement(attrs);
Chris Lattner803802d2009-03-24 17:04:48 +0000223 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000224
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000225 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000226 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000227 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000228 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000229
Chris Lattner9075bd72006-08-10 04:59:57 +0000230 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000231 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000232 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000233 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
234 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000235 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000236
Chris Lattner9075bd72006-08-10 04:59:57 +0000237 case tok::kw_if: // C99 6.8.4.1: if-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000238 return ParseIfStatement(attrs, TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000239 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000240 return ParseSwitchStatement(attrs, TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000241
Chris Lattner9075bd72006-08-10 04:59:57 +0000242 case tok::kw_while: // C99 6.8.5.1: while-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000243 return ParseWhileStatement(attrs, TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000244 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000245 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000246 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000247 break;
248 case tok::kw_for: // C99 6.8.5.3: for-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000249 return ParseForStatement(attrs, TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000250
251 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000252 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000253 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000254 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000255 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000256 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000257 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000258 break;
259 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000260 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000261 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000262 break;
263 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000264 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000265 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000266 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000267
Sebastian Redlb219c902008-12-21 16:41:36 +0000268 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000269 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000270 bool msAsm = false;
271 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000272 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000273 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000274 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000275 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000276 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000277
Sebastian Redlb219c902008-12-21 16:41:36 +0000278 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000279 return ParseCXXTryBlock(attrs);
John Wiegley1c0675e2011-04-28 01:08:34 +0000280
281 case tok::kw___try:
282 return ParseSEHTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000283 }
284
Chris Lattner503fadc2006-08-10 05:45:44 +0000285 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000286 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000287 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000288 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-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 Lattner0046de12008-11-13 18:52:53 +0000293 // Skip until we see a } or ;, but don't eat it.
294 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000295 }
Mike Stump11289f42009-09-09 15:08:12 +0000296
Sebastian Redl042ad952008-12-11 19:30:53 +0000297 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000298}
299
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000300/// \brief Parse an expression statement.
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000301StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000302 // If a case keyword is missing, this is where it should be inserted.
303 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000304
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000305 // FIXME: Use the attributes
306 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000307 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000317
Douglas Gregor0e7dde52011-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 Takumi82a35112011-10-08 11:31:46 +0000324
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000325 // Recover parsing as a case statement.
326 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
327 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000328
Douglas Gregor0e7dde52011-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 Wiegley1c0675e2011-04-28 01:08:34 +0000332}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000333
John Wiegley1c0675e2011-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 Gregor60060d62011-10-21 03:57:52 +0000359 if (Tok.is(tok::identifier) &&
360 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-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 Pichetbfaf4772011-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 Wiegley1c0675e2011-04-28 01:08:34 +0000399 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-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 Wiegley1c0675e2011-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 Gregor0e7dde52011-04-24 05:37:28 +0000438}
439
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000440/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000441///
442/// labeled-statement:
443/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000444/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000445///
John McCall53fa7142010-12-24 02:08:15 +0000446StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-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 Redl042ad952008-12-11 19:30:53 +0000454
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000455 // identifier ':' statement
456 SourceLocation ColonLoc = ConsumeToken();
457
458 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000459 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000460
John McCalldadc5752010-08-24 06:29:42 +0000461 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000462
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000463 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000464 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000465 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000466
Chris Lattnerebb5c6c2011-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 Takumi82a35112011-10-08 11:31:46 +0000471
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000472 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
473 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000474}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000475
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000476/// ParseCaseStatement
477/// labeled-statement:
478/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000479/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000480///
Richard Trieu2c850c02011-04-21 21:44:26 +0000481StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
482 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000483 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000484 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chris Lattner34a22092009-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 Lattner2b19a6582009-03-04 18:24:58 +0000496 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-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 Stump11289f42009-09-09 15:08:12 +0000499
Chris Lattner34a22092009-03-04 04:23:07 +0000500 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
501 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000502 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattner34a22092009-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 Trieu3481fcd2011-09-09 02:16:15 +0000507 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattner34a22092009-03-04 04:23:07 +0000509 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000510 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000511 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000512 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
513 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregord328d572009-09-21 18:10:23 +0000515 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000516 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000517 cutOffParsing();
518 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000519 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000520
Chris Lattner125c0ee2009-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 Takumi82a35112011-10-08 11:31:46 +0000525
Richard Trieu2c850c02011-04-21 21:44:26 +0000526 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
527 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000528 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000529 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000530 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000531 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000532
Chris Lattner34a22092009-03-04 04:23:07 +0000533 // GNU case range extension.
534 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000535 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000536 if (Tok.is(tok::ellipsis)) {
537 Diag(Tok, diag::ext_gnu_case_range);
538 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000539
Chris Lattner34a22092009-03-04 04:23:07 +0000540 RHS = ParseConstantExpression();
541 if (RHS.isInvalid()) {
542 SkipUntil(tok::colon);
543 return StmtError();
544 }
545 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000546
Chris Lattner125c0ee2009-12-10 00:38:54 +0000547 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000548
John McCall0140bfe2011-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 Gregor0d0a9652010-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 Lattner34a22092009-03-04 04:23:07 +0000562 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000563
John McCalldadc5752010-08-24 06:29:42 +0000564 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000565 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
566 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner34a22092009-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 McCall37ad5512010-08-23 06:44:23 +0000577 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000578 if (TopLevelCase.isInvalid())
579 TopLevelCase = move(Case);
580 else
John McCallb268a282010-08-23 23:25:46 +0000581 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000582 DeepestParsedCaseStmt = NextDeepest;
583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Chris Lattner34a22092009-03-04 04:23:07 +0000585 // Handle all case statements.
586 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner34a22092009-03-04 04:23:07 +0000588 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000589
Chris Lattner34a22092009-03-04 04:23:07 +0000590 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000591 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner34a22092009-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 Majnemerc6a99872011-06-14 15:24:38 +0000598 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
599 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000600 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000601 }
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattner34a22092009-03-04 04:23:07 +0000603 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000604 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000605 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattner34a22092009-03-04 04:23:07 +0000607 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000608 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000609
Chris Lattner34a22092009-03-04 04:23:07 +0000610 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000611 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000612}
613
614/// ParseDefaultStatement
615/// labeled-statement:
616/// 'default' ':' statement
617/// Note that this does not parse the 'statement' at the end.
618///
John McCall53fa7142010-12-24 02:08:15 +0000619StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000620 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000621
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000622 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000623 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000624
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000625 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000626 if (Tok.is(tok::colon)) {
627 ColonLoc = ConsumeToken();
628
629 // Treat "default;" as a typo for "default:".
630 } else if (Tok.is(tok::semi)) {
631 ColonLoc = ConsumeToken();
632 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
633 << FixItHint::CreateReplacement(ColonLoc, ":");
634 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000635 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
636 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
637 << FixItHint::CreateInsertion(ExpectedLoc, ":");
638 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000639 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000640
Chris Lattner30f910e2006-10-16 05:52:41 +0000641 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000642 if (Tok.is(tok::r_brace)) {
David Majnemerc6a99872011-06-14 15:24:38 +0000643 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
644 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000645 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000646 }
647
John McCalldadc5752010-08-24 06:29:42 +0000648 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000649 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000650 return StmtError();
651
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000652 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000653 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000654}
655
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000656StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
657 bool isStmtExpr) {
658 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
659}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000660
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000661/// ParseCompoundStatement - Parse a "{}" block.
662///
663/// compound-statement: [C99 6.8.2]
664/// { block-item-list[opt] }
665/// [GNU] { label-declarations block-item-list } [TODO]
666///
667/// block-item-list:
668/// block-item
669/// block-item-list block-item
670///
671/// block-item:
672/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000673/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000674/// statement
675/// [OMP] openmp-directive [TODO]
676///
677/// [GNU] label-declarations:
678/// [GNU] label-declaration
679/// [GNU] label-declarations label-declaration
680///
681/// [GNU] label-declaration:
682/// [GNU] '__label__' identifier-list ';'
683///
684/// [OMP] openmp-directive: [TODO]
685/// [OMP] barrier-directive
686/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000687///
John McCall53fa7142010-12-24 02:08:15 +0000688StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000689 bool isStmtExpr,
690 unsigned ScopeFlags) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000691 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000692
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000693 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000694
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000695 // Enter a scope to hold everything within the compound stmt. Compound
696 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000697 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000698
699 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000700 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000701}
702
Chris Lattnerf2978802007-01-21 06:52:16 +0000703/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000704/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000705/// consume the '}' at the end of the block. It does not manipulate the scope
706/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000707StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000708 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000709 Tok.getLocation(),
710 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000711 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000712 BalancedDelimiterTracker T(*this, tok::l_brace);
713 if (T.consumeOpen())
714 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000715
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000716 Sema::CompoundScopeRAII CompoundScope(Actions);
717
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000718 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000719
Chris Lattner43e7f312011-02-18 02:08:43 +0000720 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
721 // only allowed at the start of a compound stmt regardless of the language.
722 while (Tok.is(tok::kw___label__)) {
723 SourceLocation LabelLoc = ConsumeToken();
724 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000725
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000726 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000727 while (1) {
728 if (Tok.isNot(tok::identifier)) {
729 Diag(Tok, diag::err_expected_ident);
730 break;
731 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000732
Chris Lattner43e7f312011-02-18 02:08:43 +0000733 IdentifierInfo *II = Tok.getIdentifierInfo();
734 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000735 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000736
Chris Lattner43e7f312011-02-18 02:08:43 +0000737 if (!Tok.is(tok::comma))
738 break;
739 ConsumeToken();
740 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000741
John McCall084e83d2011-03-24 11:26:52 +0000742 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000743 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
744 DeclsInGroup.data(), DeclsInGroup.size());
745 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000746
Chris Lattner43e7f312011-02-18 02:08:43 +0000747 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
748 if (R.isUsable())
749 Stmts.push_back(R.release());
750 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000751
Chris Lattner43e7f312011-02-18 02:08:43 +0000752 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000753 if (Tok.is(tok::annot_pragma_unused)) {
754 HandlePragmaUnused();
755 continue;
756 }
757
Francois Pichet0706d202011-09-17 17:15:52 +0000758 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000759 Tok.is(tok::kw___if_not_exists))) {
760 ParseMicrosoftIfExistsStatement(Stmts);
761 continue;
762 }
763
John McCalldadc5752010-08-24 06:29:42 +0000764 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000765 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000766 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000767 } else {
768 // __extension__ can start declarations and it can also be a unary
769 // operator for expressions. Consume multiple __extension__ markers here
770 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000771 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000772 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000773 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000774 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000775
John McCall084e83d2011-03-24 11:26:52 +0000776 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000777 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000778
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000779 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000780 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000781 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000782 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000783 ExtensionRAIIObject O(Diags);
784
Chris Lattner49836b42009-04-02 04:16:50 +0000785 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000786 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
787 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000788 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000789 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000790 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000791 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000792 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000793
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000794 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000795 SkipUntil(tok::semi);
796 continue;
797 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000798
Alexis Hunt96d5c762009-11-21 08:43:09 +0000799 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000800 // Eat the semicolon at the end of stmt and convert the expr into a
801 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000802 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000803 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000804 }
805 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000806
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000807 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000808 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000809 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000810
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000811 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000812 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000813 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000814 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000815 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000816 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000817
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000818 if (T.consumeClose())
819 return StmtError();
820
821 return Actions.ActOnCompoundStmt(T.getOpenLocation(), T.getCloseLocation(),
822 move_arg(Stmts), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000823}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000824
Chris Lattnerc0081db2008-12-12 06:31:07 +0000825/// ParseParenExprOrCondition:
826/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000827/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000828///
829/// This function parses and performs error recovery on the specified condition
830/// or expression (depending on whether we're in C++ or C mode). This function
831/// goes out of its way to recover well. It returns true if there was a parser
832/// error (the right paren couldn't be found), which indicates that the caller
833/// should try to recover harder. It returns false if the condition is
834/// successfully parsed. Note that a successful parse can still have semantic
835/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000836bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000837 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000838 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000839 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000840 BalancedDelimiterTracker T(*this, tok::l_paren);
841 T.consumeOpen();
842
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000843 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000844 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000845 else {
846 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000847 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000848
Douglas Gregore60e41a2010-05-06 17:25:47 +0000849 // If required, convert to a boolean value.
850 if (!ExprResult.isInvalid() && ConvertToBoolean)
851 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000852 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Chris Lattnerc0081db2008-12-12 06:31:07 +0000855 // If the parser was confused by the condition and we don't have a ')', try to
856 // recover by skipping ahead to a semi and bailing out. If condexp is
857 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000858 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000859 SkipUntil(tok::semi);
860 // Skipping may have stopped if it found the containing ')'. If so, we can
861 // continue parsing the if statement.
862 if (Tok.isNot(tok::r_paren))
863 return true;
864 }
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattnerc0081db2008-12-12 06:31:07 +0000866 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000867 T.consumeClose();
Chris Lattnerc0081db2008-12-12 06:31:07 +0000868 return false;
869}
870
871
Chris Lattnerc951dae2006-08-10 04:23:57 +0000872/// ParseIfStatement
873/// if-statement: [C99 6.8.4.1]
874/// 'if' '(' expression ')' statement
875/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000876/// [C++] 'if' '(' condition ')' statement
877/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000878///
Nico Weber3cef1082011-12-22 23:26:17 +0000879StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs,
880 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000881 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000882
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000883 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000884 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000885
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000886 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000887 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000888 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000889 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000890 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000891
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000892 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
893
Chris Lattner2dd1b722007-08-26 23:08:06 +0000894 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
895 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000896 //
897 // C++ 6.4p3:
898 // A name introduced by a declaration in a condition is in scope from its
899 // point of declaration until the end of the substatements controlled by the
900 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000901 // C++ 3.3.2p4:
902 // Names declared in the for-init-statement, and in the condition of if,
903 // while, for, and switch statements are local to the if, while, for, or
904 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000905 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000906 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000907
Chris Lattnerc951dae2006-08-10 04:23:57 +0000908 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000909 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000910 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000911 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000912 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000913
John McCallb268a282010-08-23 23:25:46 +0000914 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner8fb26252007-08-22 05:28:50 +0000916 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000917 // there is no compound stmt. C90 does not have this clause. We only do this
918 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000919 //
920 // C++ 6.4p1:
921 // The substatement in a selection-statement (each substatement, in the else
922 // form of the if statement) implicitly defines a local scope.
923 //
924 // For C++ we create a scope for the condition and a new scope for
925 // substatements because:
926 // -When the 'then' scope exits, we want the condition declaration to still be
927 // active for the 'else' scope too.
928 // -Sema will detect name clashes by considering declarations of a
929 // 'ControlScope' as part of its direct subscope.
930 // -If we wanted the condition and substatement to be in the same scope, we
931 // would have to notify ParseStatement not to create a new scope. It's
932 // simpler to let it create a new scope.
933 //
Mike Stump11289f42009-09-09 15:08:12 +0000934 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000935 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000936
Chris Lattner5c5808a2007-10-29 05:08:52 +0000937 // Read the 'then' stmt.
938 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +0000939
940 SourceLocation InnerStatementTrailingElseLoc;
941 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +0000942
Chris Lattner37e54f42007-08-22 05:16:28 +0000943 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000944 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000945
Chris Lattnerc951dae2006-08-10 04:23:57 +0000946 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000947 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000948 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000949 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000950
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000951 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +0000952 if (TrailingElseLoc)
953 *TrailingElseLoc = Tok.getLocation();
954
Chris Lattneraf635312006-10-16 06:06:51 +0000955 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000956 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000957
Chris Lattner8fb26252007-08-22 05:28:50 +0000958 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000959 // there is no compound stmt. C90 does not have this clause. We only do
960 // this if the body isn't a compound statement to avoid push/pop in common
961 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000962 //
963 // C++ 6.4p1:
964 // The substatement in a selection-statement (each substatement, in the else
965 // form of the if statement) implicitly defines a local scope.
966 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000967 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000968 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000969
Chris Lattner30f910e2006-10-16 05:52:41 +0000970 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000971
Chris Lattner37e54f42007-08-22 05:16:28 +0000972 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000973 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +0000974 } else if (Tok.is(tok::code_completion)) {
975 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000976 cutOffParsing();
977 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +0000978 } else if (InnerStatementTrailingElseLoc.isValid()) {
979 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000980 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000981
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000982 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000983
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000984 // If the condition was invalid, discard the if statement. We could recover
985 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000986 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000987 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000988
Chris Lattner5c5808a2007-10-29 05:08:52 +0000989 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000990 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000991 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000992 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
993 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
994 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000995 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000996 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000997 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000998
Chris Lattner5c5808a2007-10-29 05:08:52 +0000999 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001000 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001001 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001002 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001003 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001004
John McCallb268a282010-08-23 23:25:46 +00001005 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001006 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001007}
1008
Chris Lattner9075bd72006-08-10 04:59:57 +00001009/// ParseSwitchStatement
1010/// switch-statement:
1011/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001012/// [C++] 'switch' '(' condition ')' statement
Nico Weber3cef1082011-12-22 23:26:17 +00001013StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs,
1014 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001015 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001016
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001017 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001018 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001019
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001020 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001021 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001022 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001023 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001024 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001025
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001026 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1027
Chris Lattner2dd1b722007-08-26 23:08:06 +00001028 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1029 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001030 //
1031 // C++ 6.4p3:
1032 // A name introduced by a declaration in a condition is in scope from its
1033 // point of declaration until the end of the substatements controlled by the
1034 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001035 // C++ 3.3.2p4:
1036 // Names declared in the for-init-statement, and in the condition of if,
1037 // while, for, and switch statements are local to the if, while, for, or
1038 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001039 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001040 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001041 if (C99orCXX)
1042 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001043 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001044
Chris Lattner9075bd72006-08-10 04:59:57 +00001045 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001046 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001047 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001048 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001049 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001050
John McCalldadc5752010-08-24 06:29:42 +00001051 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001052 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001053
Douglas Gregore60e41a2010-05-06 17:25:47 +00001054 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001055 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001056 // FIXME: This is not optimal recovery, but parsing the body is more
1057 // dangerous due to the presence of case and default statements, which
1058 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001059 if (Tok.is(tok::l_brace)) {
1060 ConsumeBrace();
1061 SkipUntil(tok::r_brace, false, false);
1062 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001063 SkipUntil(tok::semi);
1064 return move(Switch);
1065 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001066
Chris Lattner8fb26252007-08-22 05:28:50 +00001067 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001068 // there is no compound stmt. C90 does not have this clause. We only do this
1069 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001070 //
1071 // C++ 6.4p1:
1072 // The substatement in a selection-statement (each substatement, in the else
1073 // form of the if statement) implicitly defines a local scope.
1074 //
1075 // See comments in ParseIfStatement for why we create a scope for the
1076 // condition and a new scope for substatement in C++.
1077 //
Mike Stump11289f42009-09-09 15:08:12 +00001078 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001079 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001080
Chris Lattner9075bd72006-08-10 04:59:57 +00001081 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001082 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001083
Chris Lattner8fd2d012010-01-24 01:50:29 +00001084 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001085 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001086 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001087
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001088 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001089 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001090
1091 // Put the synthesized null statement on the same line as the end of switch
1092 // condition.
1093 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1094 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1095 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001096
John McCallb268a282010-08-23 23:25:46 +00001097 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001098}
1099
1100/// ParseWhileStatement
1101/// while-statement: [C99 6.8.5.1]
1102/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001103/// [C++] 'while' '(' condition ')' statement
Nico Weber3cef1082011-12-22 23:26:17 +00001104StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs,
1105 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001106 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001107
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001108 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001109 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001110 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001111
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001112 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001113 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001114 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001115 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001116 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001117
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001118 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1119
Chris Lattner2dd1b722007-08-26 23:08:06 +00001120 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1121 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001122 //
1123 // C++ 6.4p3:
1124 // A name introduced by a declaration in a condition is in scope from its
1125 // point of declaration until the end of the substatements controlled by the
1126 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001127 // C++ 3.3.2p4:
1128 // Names declared in the for-init-statement, and in the condition of if,
1129 // while, for, and switch statements are local to the if, while, for, or
1130 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001131 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001132 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001133 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001134 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1135 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001136 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001137 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1138 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001139
Chris Lattner9075bd72006-08-10 04:59:57 +00001140 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001141 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001142 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001143 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001144 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001145
John McCallb268a282010-08-23 23:25:46 +00001146 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001147
Chris Lattner8fb26252007-08-22 05:28:50 +00001148 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001149 // there is no compound stmt. C90 does not have this clause. We only do this
1150 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001151 //
1152 // C++ 6.5p2:
1153 // The substatement in an iteration-statement implicitly defines a local scope
1154 // which is entered and exited each time through the loop.
1155 //
1156 // See comments in ParseIfStatement for why we create a scope for the
1157 // condition and a new scope for substatement in C++.
1158 //
Mike Stump11289f42009-09-09 15:08:12 +00001159 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001160 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001161
Chris Lattner9075bd72006-08-10 04:59:57 +00001162 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001163 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001164
Chris Lattner8fb26252007-08-22 05:28:50 +00001165 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001166 InnerScope.Exit();
1167 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001168
John McCall48871652010-08-21 09:40:31 +00001169 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001170 return StmtError();
1171
John McCallb268a282010-08-23 23:25:46 +00001172 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001173}
1174
1175/// ParseDoStatement
1176/// do-statement: [C99 6.8.5.2]
1177/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001178/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001179StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001180 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001181
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001182 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001183 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001184
Chris Lattner2dd1b722007-08-26 23:08:06 +00001185 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1186 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001187 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001188 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001189 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001190 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001191 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001192
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001193 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001194
Chris Lattner8fb26252007-08-22 05:28:50 +00001195 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001196 // there is no compound stmt. C90 does not have this clause. We only do this
1197 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001198 //
1199 // C++ 6.5p2:
1200 // The substatement in an iteration-statement implicitly defines a local scope
1201 // which is entered and exited each time through the loop.
1202 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001203 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001204 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001205 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001206
Chris Lattner9075bd72006-08-10 04:59:57 +00001207 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001208 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001209
Chris Lattner8fb26252007-08-22 05:28:50 +00001210 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001211 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001212
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001213 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001214 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001215 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001216 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001217 SkipUntil(tok::semi, false, true);
1218 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001219 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001220 }
Chris Lattneraf635312006-10-16 06:06:51 +00001221 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001222
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001223 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001224 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001225 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001226 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001227 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001228
Chris Lattner10da53c2008-12-12 06:35:28 +00001229 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001230 BalancedDelimiterTracker T(*this, tok::l_paren);
1231 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001232 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001233 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001234 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001235
Sebastian Redlb62406f2008-12-11 19:48:14 +00001236 if (Cond.isInvalid() || Body.isInvalid())
1237 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001238
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001239 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1240 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001241}
1242
1243/// ParseForStatement
1244/// for-statement: [C99 6.8.5.3]
1245/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1246/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001247/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1248/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001249/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001250/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1251/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001252///
1253/// [C++] for-init-statement:
1254/// [C++] expression-statement
1255/// [C++] simple-declaration
1256///
Richard Smith02e85f32011-04-14 22:09:26 +00001257/// [C++0x] for-range-declaration:
1258/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1259/// [C++0x] for-range-initializer:
1260/// [C++0x] expression
1261/// [C++0x] braced-init-list [TODO]
Nico Weber3cef1082011-12-22 23:26:17 +00001262StmtResult Parser::ParseForStatement(ParsedAttributes &attrs,
1263 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001264 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001265
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001266 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001267 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001268
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001269 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001270 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001271 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001272 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001273 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001274
Chris Lattner934074c2009-04-22 00:54:41 +00001275 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001276
Chris Lattner2dd1b722007-08-26 23:08:06 +00001277 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1278 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001279 //
1280 // C++ 6.4p3:
1281 // A name introduced by a declaration in a condition is in scope from its
1282 // point of declaration until the end of the substatements controlled by the
1283 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001284 // C++ 3.3.2p4:
1285 // Names declared in the for-init-statement, and in the condition of if,
1286 // while, for, and switch statements are local to the if, while, for, or
1287 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001288 // C++ 6.5.3p1:
1289 // Names declared in the for-init-statement are in the same declarative-region
1290 // as those declared in the condition.
1291 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001292 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001293 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001294 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1295 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001296 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001297 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1298
1299 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001300
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001301 BalancedDelimiterTracker T(*this, tok::l_paren);
1302 T.consumeOpen();
1303
John McCalldadc5752010-08-24 06:29:42 +00001304 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001305
Richard Smith02e85f32011-04-14 22:09:26 +00001306 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001307 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001308 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001309 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001310 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001311 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001312 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001313 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001314
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001315 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001316 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001317 C99orCXXorObjC? Sema::PCC_ForInit
1318 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001319 cutOffParsing();
1320 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001321 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001322
Chris Lattner9075bd72006-08-10 04:59:57 +00001323 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001324 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001325 // no first part, eat the ';'.
1326 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001327 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001328 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001329 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001330 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001331
John McCall084e83d2011-03-24 11:26:52 +00001332 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001333 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001334
Richard Smith02e85f32011-04-14 22:09:26 +00001335 // In C++0x, "for (T NS:a" might not be a typo for ::
1336 bool MightBeForRangeStmt = getLang().CPlusPlus;
1337 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1338
Chris Lattner49836b42009-04-02 04:16:50 +00001339 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001340 StmtVector Stmts(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001341 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001342 DeclEnd, attrs, false,
1343 MightBeForRangeStmt ?
1344 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001345 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001346
Richard Smith02e85f32011-04-14 22:09:26 +00001347 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001348 Diag(ForRangeInit.ColonLoc, getLang().CPlusPlus0x ?
1349 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001350
Richard Smith02e85f32011-04-14 22:09:26 +00001351 ForRange = true;
1352 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001353 ConsumeToken();
1354 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001355 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001356 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001357 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001358
Douglas Gregor68762e72010-08-23 21:17:50 +00001359 if (Tok.is(tok::code_completion)) {
1360 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001361 cutOffParsing();
1362 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001363 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001364 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001365 } else {
1366 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001367 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001368 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001369 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001370
John McCall34376a62010-12-04 03:47:34 +00001371 ForEach = isTokIdentifier_in();
1372
Chris Lattnercd68f642007-06-27 01:06:29 +00001373 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001374 if (!Value.isInvalid()) {
1375 if (ForEach)
1376 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1377 else
1378 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1379 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001380
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001381 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001382 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001383 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001384 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001385
Douglas Gregor68762e72010-08-23 21:17:50 +00001386 if (Tok.is(tok::code_completion)) {
1387 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001388 cutOffParsing();
1389 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001390 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001391 Collection = ParseExpression();
Richard Smith4f848f12011-12-20 22:56:20 +00001392 } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) &&
1393 !FirstPart.isInvalid()) {
1394 // User tried to write the reasonable, but ill-formed, for-range-statement
1395 // for (expr : expr) { ... }
1396 Diag(Tok, diag::err_for_range_expected_decl)
1397 << FirstPart.get()->getSourceRange();
1398 SkipUntil(tok::r_paren, false, true);
1399 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001400 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001401 if (!Value.isInvalid()) {
1402 Diag(Tok, diag::err_expected_semi_for);
1403 } else {
1404 // Skip until semicolon or rparen, don't consume it.
1405 SkipUntil(tok::r_paren, true, true);
1406 if (Tok.is(tok::semi))
1407 ConsumeToken();
1408 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001409 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001410 }
Richard Smith02e85f32011-04-14 22:09:26 +00001411 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001412 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001413 // Parse the second part of the for specifier.
1414 if (Tok.is(tok::semi)) { // for (...;;
1415 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001416 } else if (Tok.is(tok::r_paren)) {
1417 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001418 } else {
John McCalldadc5752010-08-24 06:29:42 +00001419 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001420 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001421 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1422 else {
1423 Second = ParseExpression();
1424 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001425 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001426 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001427 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001428 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001429 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001430 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001431
Douglas Gregor230a7e62011-02-17 03:38:46 +00001432 if (Tok.isNot(tok::semi)) {
1433 if (!SecondPartIsInvalid || SecondVar)
1434 Diag(Tok, diag::err_expected_semi_for);
1435 else
1436 // Skip until semicolon or rparen, don't consume it.
1437 SkipUntil(tok::r_paren, true, true);
1438 }
1439
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001440 if (Tok.is(tok::semi)) {
1441 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001442 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001443
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001444 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001445 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001446 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001447 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001448 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001449 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001450 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001451 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001452
Richard Smith02e85f32011-04-14 22:09:26 +00001453 // We need to perform most of the semantic analysis for a C++0x for-range
1454 // statememt before parsing the body, in order to be able to deduce the type
1455 // of an auto-typed loop variable.
1456 StmtResult ForRangeStmt;
John McCall53848232011-07-27 01:07:15 +00001457 if (ForRange) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001458 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smith02e85f32011-04-14 22:09:26 +00001459 FirstPart.take(),
1460 ForRangeInit.ColonLoc,
1461 ForRangeInit.RangeExpr.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001462 T.getCloseLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001463
John McCall53848232011-07-27 01:07:15 +00001464
1465 // Similarly, we need to do the semantic analysis for a for-range
1466 // statement immediately in order to close over temporaries correctly.
1467 } else if (ForEach) {
1468 if (!Collection.isInvalid())
1469 Collection =
1470 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1471 }
1472
Chris Lattner8fb26252007-08-22 05:28:50 +00001473 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001474 // there is no compound stmt. C90 does not have this clause. We only do this
1475 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001476 //
1477 // C++ 6.5p2:
1478 // The substatement in an iteration-statement implicitly defines a local scope
1479 // which is entered and exited each time through the loop.
1480 //
1481 // See comments in ParseIfStatement for why we create a scope for
1482 // for-init-statement/condition and a new scope for substatement in C++.
1483 //
Mike Stump11289f42009-09-09 15:08:12 +00001484 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001485 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001486
Chris Lattner9075bd72006-08-10 04:59:57 +00001487 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001488 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001489
Chris Lattner8fb26252007-08-22 05:28:50 +00001490 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001491 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001492
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001493 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001494 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001495
1496 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001497 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001498
Richard Smith02e85f32011-04-14 22:09:26 +00001499 if (ForEach)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001500 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1501 FirstPart.take(),
1502 Collection.take(),
1503 T.getCloseLocation(),
1504 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001505
Richard Smith02e85f32011-04-14 22:09:26 +00001506 if (ForRange)
1507 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1508
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001509 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1510 SecondPart, SecondVar, ThirdPart,
1511 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001512}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001513
Chris Lattner503fadc2006-08-10 05:45:44 +00001514/// ParseGotoStatement
1515/// jump-statement:
1516/// 'goto' identifier ';'
1517/// [GNU] 'goto' '*' expression ';'
1518///
1519/// Note: this lets the caller parse the end ';'.
1520///
John McCall53fa7142010-12-24 02:08:15 +00001521StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001522 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001523
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001524 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001525 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001526
John McCalldadc5752010-08-24 06:29:42 +00001527 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001528 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001529 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1530 Tok.getLocation());
1531 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001532 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001533 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001534 // GNU indirect goto extension.
1535 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001536 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001537 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001538 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001539 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001540 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001541 }
John McCallb268a282010-08-23 23:25:46 +00001542 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001543 } else {
1544 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001545 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001546 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001547
Sebastian Redlb62406f2008-12-11 19:48:14 +00001548 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001549}
1550
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001551/// ParseContinueStatement
1552/// jump-statement:
1553/// 'continue' ';'
1554///
1555/// Note: this lets the caller parse the end ';'.
1556///
John McCall53fa7142010-12-24 02:08:15 +00001557StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001558 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001559
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001560 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001561 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001562}
1563
1564/// ParseBreakStatement
1565/// jump-statement:
1566/// 'break' ';'
1567///
1568/// Note: this lets the caller parse the end ';'.
1569///
John McCall53fa7142010-12-24 02:08:15 +00001570StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001571 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001572
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001573 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001574 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001575}
1576
Chris Lattner503fadc2006-08-10 05:45:44 +00001577/// ParseReturnStatement
1578/// jump-statement:
1579/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001580StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001581 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001582
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001583 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001584 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001585
John McCalldadc5752010-08-24 06:29:42 +00001586 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001587 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001588 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001589 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001590 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001591 return StmtError();
1592 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001593
Douglas Gregore9e27d92011-03-11 23:10:44 +00001594 // FIXME: This is a hack to allow something like C++0x's generalized
1595 // initializer lists, but only enough of this feature to allow Clang to
1596 // parse libstdc++ 4.5's headers.
1597 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1598 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001599 if (R.isUsable())
1600 Diag(R.get()->getLocStart(), getLang().CPlusPlus0x ?
1601 diag::warn_cxx98_compat_generalized_initializer_lists :
1602 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001603 << R.get()->getSourceRange();
1604 } else
1605 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001606 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001607 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001608 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001609 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001610 }
John McCallb268a282010-08-23 23:25:46 +00001611 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001612}
Chris Lattner0116c472006-08-15 06:03:28 +00001613
Eli Friedmana4b02c32011-09-30 01:13:51 +00001614/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1615/// this routine is called to collect the tokens for an MS asm statement.
1616StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1617 SourceManager &SrcMgr = PP.getSourceManager();
1618 SourceLocation EndLoc = AsmLoc;
1619 do {
1620 bool InBraces = false;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001621 unsigned short savedBraceCount = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001622 bool InAsmComment = false;
1623 FileID FID;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001624 unsigned LineNo = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001625 unsigned NumTokensRead = 0;
1626 SourceLocation LBraceLoc;
1627
1628 if (Tok.is(tok::l_brace)) {
1629 // Braced inline asm: consume the opening brace.
1630 InBraces = true;
1631 savedBraceCount = BraceCount;
1632 EndLoc = LBraceLoc = ConsumeBrace();
1633 ++NumTokensRead;
1634 } else {
1635 // Single-line inline asm; compute which line it is on.
1636 std::pair<FileID, unsigned> ExpAsmLoc =
1637 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1638 FID = ExpAsmLoc.first;
1639 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1640 }
1641
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001642 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001643 do {
Eli Friedmana4b02c32011-09-30 01:13:51 +00001644 // If we hit EOF, we're done, period.
1645 if (Tok.is(tok::eof))
1646 break;
1647 // When we consume the closing brace, we're done.
1648 if (InBraces && BraceCount == savedBraceCount)
1649 break;
1650
1651 if (!InAsmComment && Tok.is(tok::semi)) {
1652 // A semicolon in an asm is the start of a comment.
1653 InAsmComment = true;
1654 if (InBraces) {
1655 // Compute which line the comment is on.
1656 std::pair<FileID, unsigned> ExpSemiLoc =
1657 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1658 FID = ExpSemiLoc.first;
1659 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1660 }
1661 } else if (!InBraces || InAsmComment) {
1662 // If end-of-line is significant, check whether this token is on a
1663 // new line.
1664 std::pair<FileID, unsigned> ExpLoc =
1665 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1666 if (ExpLoc.first != FID ||
1667 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1668 // If this is a single-line __asm, we're done.
1669 if (!InBraces)
1670 break;
1671 // We're no longer in a comment.
1672 InAsmComment = false;
1673 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1674 // Single-line asm always ends when a closing brace is seen.
1675 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1676 // does MSVC do here?
1677 break;
1678 }
1679 }
1680
1681 // Consume the next token; make sure we don't modify the brace count etc.
1682 // if we are in a comment.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001683 EndLoc = TokLoc;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001684 if (InAsmComment)
1685 PP.Lex(Tok);
1686 else
1687 ConsumeAnyToken();
Steve Naroff8c099c32008-02-08 18:01:27 +00001688 TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001689 ++NumTokensRead;
1690 } while (1);
1691
1692 if (InBraces && BraceCount != savedBraceCount) {
1693 // __asm without closing brace (this can happen at EOF).
1694 Diag(Tok, diag::err_expected_rbrace);
1695 Diag(LBraceLoc, diag::note_matching) << "{";
1696 return StmtError();
1697 } else if (NumTokensRead == 0) {
1698 // Empty __asm.
1699 Diag(Tok, diag::err_expected_lbrace);
1700 return StmtError();
1701 }
1702 // Multiple adjacent asm's form together into a single asm statement
1703 // in the AST.
1704 if (!Tok.is(tok::kw_asm))
1705 break;
1706 EndLoc = ConsumeToken();
1707 } while (1);
1708 // FIXME: Need to actually grab the data and pass it on to Sema. Ideally,
1709 // what Sema wants is a string of the entire inline asm, with one instruction
1710 // per line and all the __asm keywords stripped out, and a way of mapping
1711 // from any character of that string to its location in the original source
1712 // code. I'm not entirely sure how to go about that, though.
Mike Stump6da5d752009-12-11 00:04:56 +00001713 Token t;
1714 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001715 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001716 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001717 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001718 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001719 ExprVector Constraints(Actions);
1720 ExprVector Exprs(Actions);
1721 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001722 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001723 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001724 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001725 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001726}
1727
Chris Lattner0116c472006-08-15 06:03:28 +00001728/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001729/// asm-statement:
1730/// gnu-asm-statement
1731/// ms-asm-statement
1732///
1733/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001734/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1735///
1736/// [GNU] asm-argument:
1737/// asm-string-literal
1738/// asm-string-literal ':' asm-operands[opt]
1739/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1740/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1741/// ':' asm-clobbers
1742///
1743/// [GNU] asm-clobbers:
1744/// asm-string-literal
1745/// asm-clobbers ',' asm-string-literal
1746///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001747/// [MS] ms-asm-statement:
Eli Friedmana4b02c32011-09-30 01:13:51 +00001748/// ms-asm-block
1749/// ms-asm-block ms-asm-statement
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001750///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001751/// [MS] ms-asm-block:
1752/// '__asm' ms-asm-line '\n'
1753/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1754///
1755/// [MS] ms-asm-instruction-block
1756/// ms-asm-line
1757/// ms-asm-line '\n' ms-asm-instruction-block
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001758///
John McCalldadc5752010-08-24 06:29:42 +00001759StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001760 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001761 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001762
Francois Pichet0706d202011-09-17 17:15:52 +00001763 if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001764 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001765 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001766 }
John McCall084e83d2011-03-24 11:26:52 +00001767 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001768 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001769 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001770
Chris Lattner0116c472006-08-15 06:03:28 +00001771 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001772 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001773 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001774 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001775 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001776
Chris Lattner0116c472006-08-15 06:03:28 +00001777 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001778 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001779 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001780 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001781 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001782 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001783 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001784 BalancedDelimiterTracker T(*this, tok::l_paren);
1785 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001786
John McCalldadc5752010-08-24 06:29:42 +00001787 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001788 if (AsmString.isInvalid()) {
1789 // If the reason we are recovering is because of an improper string
1790 // literal, it makes the most sense just to consume to the ')'.
1791 if (isTokenStringLiteral())
1792 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001793 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001794 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001795
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001796 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001797 ExprVector Constraints(Actions);
1798 ExprVector Exprs(Actions);
1799 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001800
Anders Carlsson19fe1162008-02-05 23:03:50 +00001801 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001802 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001803 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001804 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001805 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001806 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001807 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001808 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001809 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001810
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001811 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001812 bool AteExtraColon = false;
1813 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1814 // In C++ mode, parse "::" like ": :".
1815 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001816 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001817
Chris Lattner15768502009-12-20 23:08:04 +00001818 if (!AteExtraColon &&
1819 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001820 return StmtError();
1821 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001822
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001823 unsigned NumOutputs = Names.size();
1824
1825 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001826 if (AteExtraColon ||
1827 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1828 // In C++ mode, parse "::" like ": :".
1829 if (AteExtraColon)
1830 AteExtraColon = false;
1831 else {
1832 AteExtraColon = Tok.is(tok::coloncolon);
1833 ConsumeToken();
1834 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001835
Chris Lattner15768502009-12-20 23:08:04 +00001836 if (!AteExtraColon &&
1837 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001838 return StmtError();
1839 }
1840
1841 assert(Names.size() == Constraints.size() &&
1842 Constraints.size() == Exprs.size() &&
1843 "Input operand size mismatch!");
1844
1845 unsigned NumInputs = Names.size() - NumOutputs;
1846
1847 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001848 if (AteExtraColon || Tok.is(tok::colon)) {
1849 if (!AteExtraColon)
1850 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001851
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001852 // Parse the asm-string list for clobbers if present.
1853 if (Tok.isNot(tok::r_paren)) {
1854 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001855 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001856
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001857 if (Clobber.isInvalid())
1858 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001859
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001860 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001861
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001862 if (Tok.isNot(tok::comma)) break;
1863 ConsumeToken();
1864 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001865 }
1866 }
1867
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001868 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001869 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001870 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001871 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001872 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001873 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001874}
1875
1876/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001877/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001878///
1879/// [GNU] asm-operands:
1880/// asm-operand
1881/// asm-operands ',' asm-operand
1882///
1883/// [GNU] asm-operand:
1884/// asm-string-literal '(' expression ')'
1885/// '[' identifier ']' asm-string-literal '(' expression ')'
1886///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001887//
1888// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001889bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001890 SmallVectorImpl<Expr *> &Constraints,
1891 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001892 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001893 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001894 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001895
1896 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001897 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001898 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001899 BalancedDelimiterTracker T(*this, tok::l_square);
1900 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001901
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001902 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001903 Diag(Tok, diag::err_expected_ident);
1904 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001905 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001906 }
Mike Stump11289f42009-09-09 15:08:12 +00001907
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001908 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001909 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001910
Anders Carlsson9a020f92010-01-30 22:25:16 +00001911 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001912 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001913 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001914 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001915
John McCalldadc5752010-08-24 06:29:42 +00001916 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001917 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001918 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001919 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001920 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001921 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001922
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001923 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001924 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001925 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001926 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001927 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001928
Chris Lattner0116c472006-08-15 06:03:28 +00001929 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001930 BalancedDelimiterTracker T(*this, tok::l_paren);
1931 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001932 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001933 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001934 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001935 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001936 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001937 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001938 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001939 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001940 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001941 ConsumeToken();
1942 }
1943}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001944
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001945Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001946 assert(Tok.is(tok::l_brace));
1947 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001948
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001949 if (PP.isCodeCompletionEnabled()) {
1950 if (trySkippingFunctionBodyForCodeCompletion()) {
1951 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001952 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001953 }
1954 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001955
John McCallfaf5fb42010-08-26 23:41:50 +00001956 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1957 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001958
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001959 // Do not enter a scope for the brace, as the arguments are in the same scope
1960 // (the function body) as the body itself. Instead, just read the statement
1961 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001962 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001963
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001964 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001965 if (FnBody.isInvalid()) {
1966 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001967 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001968 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001969 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001970
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001971 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001972 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001973}
Sebastian Redlb219c902008-12-21 16:41:36 +00001974
Sebastian Redla7b98a72009-04-26 20:35:05 +00001975/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1976///
1977/// function-try-block:
1978/// 'try' ctor-initializer[opt] compound-statement handler-seq
1979///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001980Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001981 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1982 SourceLocation TryLoc = ConsumeToken();
1983
John McCallfaf5fb42010-08-26 23:41:50 +00001984 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1985 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001986
1987 // Constructor initializer list?
1988 if (Tok.is(tok::colon))
1989 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001990 else
1991 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001992
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001993 if (PP.isCodeCompletionEnabled()) {
1994 if (trySkippingFunctionBodyForCodeCompletion()) {
1995 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001996 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001997 }
1998 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001999
Sebastian Redld98ecd62009-04-26 21:08:36 +00002000 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00002001 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002002 // If we failed to parse the try-catch, we just give the function an empty
2003 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002004 if (FnBody.isInvalid()) {
2005 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002006 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00002007 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002008 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002009
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002010 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002011 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002012}
2013
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00002014bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002015 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00002016 assert(PP.isCodeCompletionEnabled() &&
2017 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002018
2019 // We're in code-completion mode. Skip parsing for all function bodies unless
2020 // the body contains the code-completion point.
2021 TentativeParsingAction PA(*this);
2022 ConsumeBrace();
2023 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
2024 /*StopAtCodeCompletion=*/true)) {
2025 PA.Commit();
2026 return true;
2027 }
2028
2029 PA.Revert();
2030 return false;
2031}
2032
Sebastian Redlb219c902008-12-21 16:41:36 +00002033/// ParseCXXTryBlock - Parse a C++ try-block.
2034///
2035/// try-block:
2036/// 'try' compound-statement handler-seq
2037///
John McCall53fa7142010-12-24 02:08:15 +00002038StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002039 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002040
Sebastian Redlb219c902008-12-21 16:41:36 +00002041 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2042
2043 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002044 return ParseCXXTryBlockCommon(TryLoc);
2045}
2046
2047/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2048/// function-try-block.
2049///
2050/// try-block:
2051/// 'try' compound-statement handler-seq
2052///
2053/// function-try-block:
2054/// 'try' ctor-initializer[opt] compound-statement handler-seq
2055///
2056/// handler-seq:
2057/// handler handler-seq[opt]
2058///
John Wiegley1c0675e2011-04-28 01:08:34 +00002059/// [Borland] try-block:
2060/// 'try' compound-statement seh-except-block
2061/// 'try' compound-statment seh-finally-block
2062///
John McCalldadc5752010-08-24 06:29:42 +00002063StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002064 if (Tok.isNot(tok::l_brace))
2065 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002066 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002067 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002068 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
2069 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002070 if (TryBlock.isInvalid())
2071 return move(TryBlock);
2072
John Wiegley1c0675e2011-04-28 01:08:34 +00002073 // Borland allows SEH-handlers with 'try'
Douglas Gregor60060d62011-10-21 03:57:52 +00002074
2075 if((Tok.is(tok::identifier) &&
2076 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2077 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002078 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2079 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002080 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002081 SourceLocation Loc = ConsumeToken();
2082 Handler = ParseSEHExceptBlock(Loc);
2083 }
2084 else {
2085 SourceLocation Loc = ConsumeToken();
2086 Handler = ParseSEHFinallyBlock(Loc);
2087 }
2088 if(Handler.isInvalid())
2089 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00002090
John Wiegley1c0675e2011-04-28 01:08:34 +00002091 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2092 TryLoc,
2093 TryBlock.take(),
2094 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002095 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002096 else {
2097 StmtVector Handlers(Actions);
2098 MaybeParseCXX0XAttributes(attrs);
2099 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002100
John Wiegley1c0675e2011-04-28 01:08:34 +00002101 if (Tok.isNot(tok::kw_catch))
2102 return StmtError(Diag(Tok, diag::err_expected_catch));
2103 while (Tok.is(tok::kw_catch)) {
2104 StmtResult Handler(ParseCXXCatchBlock());
2105 if (!Handler.isInvalid())
2106 Handlers.push_back(Handler.release());
2107 }
2108 // Don't bother creating the full statement if we don't have any usable
2109 // handlers.
2110 if (Handlers.empty())
2111 return StmtError();
2112
2113 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2114 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002115}
2116
2117/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2118///
2119/// handler:
2120/// 'catch' '(' exception-declaration ')' compound-statement
2121///
2122/// exception-declaration:
2123/// type-specifier-seq declarator
2124/// type-specifier-seq abstract-declarator
2125/// type-specifier-seq
2126/// '...'
2127///
John McCalldadc5752010-08-24 06:29:42 +00002128StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002129 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2130
2131 SourceLocation CatchLoc = ConsumeToken();
2132
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002133 BalancedDelimiterTracker T(*this, tok::l_paren);
2134 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002135 return StmtError();
2136
2137 // C++ 3.3.2p3:
2138 // The name in a catch exception-declaration is local to the handler and
2139 // shall not be redeclared in the outermost block of the handler.
2140 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2141
2142 // exception-declaration is equivalent to '...' or a parameter-declaration
2143 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002144 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002145 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002146 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002147 if (ParseCXXTypeSpecifierSeq(DS))
2148 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002149 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2150 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002151 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002152 } else
2153 ConsumeToken();
2154
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002155 T.consumeClose();
2156 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002157 return StmtError();
2158
2159 if (Tok.isNot(tok::l_brace))
2160 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2161
Alexis Hunt96d5c762009-11-21 08:43:09 +00002162 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002163 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002164 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00002165 if (Block.isInvalid())
2166 return move(Block);
2167
John McCallb268a282010-08-23 23:25:46 +00002168 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002169}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002170
2171void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002172 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002173 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002174 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002175
Douglas Gregor43edb322011-10-24 22:31:10 +00002176 // Handle dependent statements by parsing the braces as a compound statement.
2177 // This is not the same behavior as Visual C++, which don't treat this as a
2178 // compound statement, but for Clang's type checking we can't have anything
2179 // inside these braces escaping to the surrounding code.
2180 if (Result.Behavior == IEB_Dependent) {
2181 if (!Tok.is(tok::l_brace)) {
2182 Diag(Tok, diag::err_expected_lbrace);
2183 return;
2184 }
2185
2186 ParsedAttributes Attrs(AttrFactory);
2187 StmtResult Compound = ParseCompoundStatement(Attrs);
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002188 if (Compound.isInvalid())
2189 return;
2190
2191 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2192 Result.IsIfExists,
2193 Result.SS,
2194 Result.Name,
2195 Compound.get());
2196 if (DepResult.isUsable())
2197 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002198 return;
2199 }
2200
2201 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2202 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002203 Diag(Tok, diag::err_expected_lbrace);
2204 return;
2205 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002206
Douglas Gregor43edb322011-10-24 22:31:10 +00002207 switch (Result.Behavior) {
2208 case IEB_Parse:
2209 // Parse the statements below.
2210 break;
2211
2212 case IEB_Dependent:
2213 llvm_unreachable("Dependent case handled above");
Douglas Gregor43edb322011-10-24 22:31:10 +00002214
2215 case IEB_Skip:
2216 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002217 return;
2218 }
2219
2220 // Condition is true, parse the statements.
2221 while (Tok.isNot(tok::r_brace)) {
2222 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2223 if (R.isUsable())
2224 Stmts.push_back(R.release());
2225 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002226 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002227}