blob: 54f1b51f7e9a7ca9f4bdd2faa5765ea11cf868c8 [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);
Richard Smith1002d102012-02-17 01:35:32 +0000599 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
600 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner34a22092009-03-04 04:23:07 +0000601 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000602 }
Mike Stump11289f42009-09-09 15:08:12 +0000603
Chris Lattner34a22092009-03-04 04:23:07 +0000604 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000605 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000606 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000607
Chris Lattner34a22092009-03-04 04:23:07 +0000608 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000609 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000610
Chris Lattner34a22092009-03-04 04:23:07 +0000611 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000612 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000613}
614
615/// ParseDefaultStatement
616/// labeled-statement:
617/// 'default' ':' statement
618/// Note that this does not parse the 'statement' at the end.
619///
John McCall53fa7142010-12-24 02:08:15 +0000620StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000621 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000622
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000623 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000624 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000625
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000626 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000627 if (Tok.is(tok::colon)) {
628 ColonLoc = ConsumeToken();
629
630 // Treat "default;" as a typo for "default:".
631 } else if (Tok.is(tok::semi)) {
632 ColonLoc = ConsumeToken();
633 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
634 << FixItHint::CreateReplacement(ColonLoc, ":");
635 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000636 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
637 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
638 << FixItHint::CreateInsertion(ExpectedLoc, ":");
639 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000640 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000641
Richard Smith1002d102012-02-17 01:35:32 +0000642 StmtResult SubStmt;
643
644 if (Tok.isNot(tok::r_brace)) {
645 SubStmt = ParseStatement();
646 } else {
647 // Diagnose the common error "switch (X) {... default: }", which is
648 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000649 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000650 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
651 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
652 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000653 }
654
Richard Smith1002d102012-02-17 01:35:32 +0000655 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000656 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000657 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000658
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000659 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000660 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000661}
662
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000663StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
664 bool isStmtExpr) {
665 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
666}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000667
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000668/// ParseCompoundStatement - Parse a "{}" block.
669///
670/// compound-statement: [C99 6.8.2]
671/// { block-item-list[opt] }
672/// [GNU] { label-declarations block-item-list } [TODO]
673///
674/// block-item-list:
675/// block-item
676/// block-item-list block-item
677///
678/// block-item:
679/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000680/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000681/// statement
682/// [OMP] openmp-directive [TODO]
683///
684/// [GNU] label-declarations:
685/// [GNU] label-declaration
686/// [GNU] label-declarations label-declaration
687///
688/// [GNU] label-declaration:
689/// [GNU] '__label__' identifier-list ';'
690///
691/// [OMP] openmp-directive: [TODO]
692/// [OMP] barrier-directive
693/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000694///
John McCall53fa7142010-12-24 02:08:15 +0000695StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000696 bool isStmtExpr,
697 unsigned ScopeFlags) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000698 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000699
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000700 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000701
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000702 // Enter a scope to hold everything within the compound stmt. Compound
703 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000704 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000705
706 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000707 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000708}
709
Chris Lattnerf2978802007-01-21 06:52:16 +0000710/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000711/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000712/// consume the '}' at the end of the block. It does not manipulate the scope
713/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000714StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000715 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000716 Tok.getLocation(),
717 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000718 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000719 BalancedDelimiterTracker T(*this, tok::l_brace);
720 if (T.consumeOpen())
721 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000722
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000723 Sema::CompoundScopeRAII CompoundScope(Actions);
724
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000725 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000726
Chris Lattner43e7f312011-02-18 02:08:43 +0000727 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
728 // only allowed at the start of a compound stmt regardless of the language.
729 while (Tok.is(tok::kw___label__)) {
730 SourceLocation LabelLoc = ConsumeToken();
731 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000732
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000733 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000734 while (1) {
735 if (Tok.isNot(tok::identifier)) {
736 Diag(Tok, diag::err_expected_ident);
737 break;
738 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000739
Chris Lattner43e7f312011-02-18 02:08:43 +0000740 IdentifierInfo *II = Tok.getIdentifierInfo();
741 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000742 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000743
Chris Lattner43e7f312011-02-18 02:08:43 +0000744 if (!Tok.is(tok::comma))
745 break;
746 ConsumeToken();
747 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000748
John McCall084e83d2011-03-24 11:26:52 +0000749 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000750 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
751 DeclsInGroup.data(), DeclsInGroup.size());
752 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000753
Chris Lattner43e7f312011-02-18 02:08:43 +0000754 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
755 if (R.isUsable())
756 Stmts.push_back(R.release());
757 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000758
Chris Lattner43e7f312011-02-18 02:08:43 +0000759 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000760 if (Tok.is(tok::annot_pragma_unused)) {
761 HandlePragmaUnused();
762 continue;
763 }
764
Francois Pichet0706d202011-09-17 17:15:52 +0000765 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000766 Tok.is(tok::kw___if_not_exists))) {
767 ParseMicrosoftIfExistsStatement(Stmts);
768 continue;
769 }
770
John McCalldadc5752010-08-24 06:29:42 +0000771 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000772 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000773 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000774 } else {
775 // __extension__ can start declarations and it can also be a unary
776 // operator for expressions. Consume multiple __extension__ markers here
777 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000778 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000779 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000780 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000781 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000782
John McCall084e83d2011-03-24 11:26:52 +0000783 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000784 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000785
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000786 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000787 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000788 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000789 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000790 ExtensionRAIIObject O(Diags);
791
Chris Lattner49836b42009-04-02 04:16:50 +0000792 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000793 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
794 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000795 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000796 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000797 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000798 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000799 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000800
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000801 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000802 SkipUntil(tok::semi);
803 continue;
804 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000805
Alexis Hunt96d5c762009-11-21 08:43:09 +0000806 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000807 // Eat the semicolon at the end of stmt and convert the expr into a
808 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000809 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000810 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000811 }
812 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000813
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000814 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000815 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000816 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000817
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000818 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000819 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000820 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000821 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000822 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000823 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000824
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000825 if (T.consumeClose())
826 return StmtError();
827
828 return Actions.ActOnCompoundStmt(T.getOpenLocation(), T.getCloseLocation(),
829 move_arg(Stmts), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000830}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000831
Chris Lattnerc0081db2008-12-12 06:31:07 +0000832/// ParseParenExprOrCondition:
833/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000834/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000835///
836/// This function parses and performs error recovery on the specified condition
837/// or expression (depending on whether we're in C++ or C mode). This function
838/// goes out of its way to recover well. It returns true if there was a parser
839/// error (the right paren couldn't be found), which indicates that the caller
840/// should try to recover harder. It returns false if the condition is
841/// successfully parsed. Note that a successful parse can still have semantic
842/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000843bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000844 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000845 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000846 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000847 BalancedDelimiterTracker T(*this, tok::l_paren);
848 T.consumeOpen();
849
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000850 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000851 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000852 else {
853 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000854 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000855
Douglas Gregore60e41a2010-05-06 17:25:47 +0000856 // If required, convert to a boolean value.
857 if (!ExprResult.isInvalid() && ConvertToBoolean)
858 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000859 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000860 }
Mike Stump11289f42009-09-09 15:08:12 +0000861
Chris Lattnerc0081db2008-12-12 06:31:07 +0000862 // If the parser was confused by the condition and we don't have a ')', try to
863 // recover by skipping ahead to a semi and bailing out. If condexp is
864 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000865 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000866 SkipUntil(tok::semi);
867 // Skipping may have stopped if it found the containing ')'. If so, we can
868 // continue parsing the if statement.
869 if (Tok.isNot(tok::r_paren))
870 return true;
871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattnerc0081db2008-12-12 06:31:07 +0000873 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000874 T.consumeClose();
Chris Lattnerc0081db2008-12-12 06:31:07 +0000875 return false;
876}
877
878
Chris Lattnerc951dae2006-08-10 04:23:57 +0000879/// ParseIfStatement
880/// if-statement: [C99 6.8.4.1]
881/// 'if' '(' expression ')' statement
882/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000883/// [C++] 'if' '(' condition ')' statement
884/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000885///
Nico Weber3cef1082011-12-22 23:26:17 +0000886StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs,
887 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000888 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000889
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000890 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000891 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000892
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000893 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000894 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000895 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000896 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000897 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000898
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000899 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
900
Chris Lattner2dd1b722007-08-26 23:08:06 +0000901 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
902 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000903 //
904 // C++ 6.4p3:
905 // A name introduced by a declaration in a condition is in scope from its
906 // point of declaration until the end of the substatements controlled by the
907 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000908 // C++ 3.3.2p4:
909 // Names declared in the for-init-statement, and in the condition of if,
910 // while, for, and switch statements are local to the if, while, for, or
911 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000912 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000913 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000914
Chris Lattnerc951dae2006-08-10 04:23:57 +0000915 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000916 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000917 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000918 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000919 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000920
John McCallb268a282010-08-23 23:25:46 +0000921 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000922
Chris Lattner8fb26252007-08-22 05:28:50 +0000923 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000924 // there is no compound stmt. C90 does not have this clause. We only do this
925 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000926 //
927 // C++ 6.4p1:
928 // The substatement in a selection-statement (each substatement, in the else
929 // form of the if statement) implicitly defines a local scope.
930 //
931 // For C++ we create a scope for the condition and a new scope for
932 // substatements because:
933 // -When the 'then' scope exits, we want the condition declaration to still be
934 // active for the 'else' scope too.
935 // -Sema will detect name clashes by considering declarations of a
936 // 'ControlScope' as part of its direct subscope.
937 // -If we wanted the condition and substatement to be in the same scope, we
938 // would have to notify ParseStatement not to create a new scope. It's
939 // simpler to let it create a new scope.
940 //
Mike Stump11289f42009-09-09 15:08:12 +0000941 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000942 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000943
Chris Lattner5c5808a2007-10-29 05:08:52 +0000944 // Read the 'then' stmt.
945 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +0000946
947 SourceLocation InnerStatementTrailingElseLoc;
948 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +0000949
Chris Lattner37e54f42007-08-22 05:16:28 +0000950 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000951 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000952
Chris Lattnerc951dae2006-08-10 04:23:57 +0000953 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000954 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000955 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000956 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000957
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000958 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +0000959 if (TrailingElseLoc)
960 *TrailingElseLoc = Tok.getLocation();
961
Chris Lattneraf635312006-10-16 06:06:51 +0000962 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000963 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000964
Chris Lattner8fb26252007-08-22 05:28:50 +0000965 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000966 // there is no compound stmt. C90 does not have this clause. We only do
967 // this if the body isn't a compound statement to avoid push/pop in common
968 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000969 //
970 // C++ 6.4p1:
971 // The substatement in a selection-statement (each substatement, in the else
972 // form of the if statement) implicitly defines a local scope.
973 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000974 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000975 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000976
Chris Lattner30f910e2006-10-16 05:52:41 +0000977 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000978
Chris Lattner37e54f42007-08-22 05:16:28 +0000979 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000980 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +0000981 } else if (Tok.is(tok::code_completion)) {
982 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000983 cutOffParsing();
984 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +0000985 } else if (InnerStatementTrailingElseLoc.isValid()) {
986 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000987 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000988
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000989 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000990
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000991 // If the condition was invalid, discard the if statement. We could recover
992 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000993 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000994 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000995
Chris Lattner5c5808a2007-10-29 05:08:52 +0000996 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000997 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000998 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000999 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1000 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1001 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001002 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001003 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001004 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001005
Chris Lattner5c5808a2007-10-29 05:08:52 +00001006 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001007 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001008 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001009 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001010 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001011
John McCallb268a282010-08-23 23:25:46 +00001012 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001013 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001014}
1015
Chris Lattner9075bd72006-08-10 04:59:57 +00001016/// ParseSwitchStatement
1017/// switch-statement:
1018/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001019/// [C++] 'switch' '(' condition ')' statement
Nico Weber3cef1082011-12-22 23:26:17 +00001020StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs,
1021 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001022 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001023
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001024 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001025 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001026
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001027 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001028 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001029 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001030 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001031 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001032
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001033 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1034
Chris Lattner2dd1b722007-08-26 23:08:06 +00001035 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1036 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001037 //
1038 // C++ 6.4p3:
1039 // A name introduced by a declaration in a condition is in scope from its
1040 // point of declaration until the end of the substatements controlled by the
1041 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001042 // C++ 3.3.2p4:
1043 // Names declared in the for-init-statement, and in the condition of if,
1044 // while, for, and switch statements are local to the if, while, for, or
1045 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001046 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001047 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001048 if (C99orCXX)
1049 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001050 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001051
Chris Lattner9075bd72006-08-10 04:59:57 +00001052 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001053 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001054 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001055 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001056 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001057
John McCalldadc5752010-08-24 06:29:42 +00001058 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001059 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001060
Douglas Gregore60e41a2010-05-06 17:25:47 +00001061 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001062 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001063 // FIXME: This is not optimal recovery, but parsing the body is more
1064 // dangerous due to the presence of case and default statements, which
1065 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001066 if (Tok.is(tok::l_brace)) {
1067 ConsumeBrace();
1068 SkipUntil(tok::r_brace, false, false);
1069 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001070 SkipUntil(tok::semi);
1071 return move(Switch);
1072 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001073
Chris Lattner8fb26252007-08-22 05:28:50 +00001074 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001075 // there is no compound stmt. C90 does not have this clause. We only do this
1076 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001077 //
1078 // C++ 6.4p1:
1079 // The substatement in a selection-statement (each substatement, in the else
1080 // form of the if statement) implicitly defines a local scope.
1081 //
1082 // See comments in ParseIfStatement for why we create a scope for the
1083 // condition and a new scope for substatement in C++.
1084 //
Mike Stump11289f42009-09-09 15:08:12 +00001085 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001086 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001087
Chris Lattner9075bd72006-08-10 04:59:57 +00001088 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001089 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001090
Chris Lattner8fd2d012010-01-24 01:50:29 +00001091 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001092 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001093 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001094
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001095 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001096 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001097
1098 // Put the synthesized null statement on the same line as the end of switch
1099 // condition.
1100 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1101 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1102 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001103
John McCallb268a282010-08-23 23:25:46 +00001104 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001105}
1106
1107/// ParseWhileStatement
1108/// while-statement: [C99 6.8.5.1]
1109/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001110/// [C++] 'while' '(' condition ')' statement
Nico Weber3cef1082011-12-22 23:26:17 +00001111StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs,
1112 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001113 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001114
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001115 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001116 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001117 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001118
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001119 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001120 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001121 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001122 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001123 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001124
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001125 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1126
Chris Lattner2dd1b722007-08-26 23:08:06 +00001127 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1128 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001129 //
1130 // C++ 6.4p3:
1131 // A name introduced by a declaration in a condition is in scope from its
1132 // point of declaration until the end of the substatements controlled by the
1133 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001134 // C++ 3.3.2p4:
1135 // Names declared in the for-init-statement, and in the condition of if,
1136 // while, for, and switch statements are local to the if, while, for, or
1137 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001138 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001139 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001140 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001141 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1142 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001143 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001144 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1145 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001146
Chris Lattner9075bd72006-08-10 04:59:57 +00001147 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001148 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001149 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001150 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001151 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001152
John McCallb268a282010-08-23 23:25:46 +00001153 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001154
Chris Lattner8fb26252007-08-22 05:28:50 +00001155 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001156 // there is no compound stmt. C90 does not have this clause. We only do this
1157 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001158 //
1159 // C++ 6.5p2:
1160 // The substatement in an iteration-statement implicitly defines a local scope
1161 // which is entered and exited each time through the loop.
1162 //
1163 // See comments in ParseIfStatement for why we create a scope for the
1164 // condition and a new scope for substatement in C++.
1165 //
Mike Stump11289f42009-09-09 15:08:12 +00001166 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001167 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001168
Chris Lattner9075bd72006-08-10 04:59:57 +00001169 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001170 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001171
Chris Lattner8fb26252007-08-22 05:28:50 +00001172 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001173 InnerScope.Exit();
1174 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001175
John McCall48871652010-08-21 09:40:31 +00001176 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001177 return StmtError();
1178
John McCallb268a282010-08-23 23:25:46 +00001179 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001180}
1181
1182/// ParseDoStatement
1183/// do-statement: [C99 6.8.5.2]
1184/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001185/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001186StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001187 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001188
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001189 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001190 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001191
Chris Lattner2dd1b722007-08-26 23:08:06 +00001192 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1193 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001194 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001195 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001196 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001197 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001198 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001199
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001200 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001201
Chris Lattner8fb26252007-08-22 05:28:50 +00001202 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001203 // there is no compound stmt. C90 does not have this clause. We only do this
1204 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001205 //
1206 // C++ 6.5p2:
1207 // The substatement in an iteration-statement implicitly defines a local scope
1208 // which is entered and exited each time through the loop.
1209 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001210 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001211 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001212 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001213
Chris Lattner9075bd72006-08-10 04:59:57 +00001214 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001215 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001216
Chris Lattner8fb26252007-08-22 05:28:50 +00001217 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001218 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001219
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001220 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001221 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001222 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001223 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001224 SkipUntil(tok::semi, false, true);
1225 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001226 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001227 }
Chris Lattneraf635312006-10-16 06:06:51 +00001228 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001229
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001230 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001231 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001232 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001233 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001234 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001235
Chris Lattner10da53c2008-12-12 06:35:28 +00001236 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001237 BalancedDelimiterTracker T(*this, tok::l_paren);
1238 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001239 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001240 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001241 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001242
Sebastian Redlb62406f2008-12-11 19:48:14 +00001243 if (Cond.isInvalid() || Body.isInvalid())
1244 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001245
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001246 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1247 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001248}
1249
1250/// ParseForStatement
1251/// for-statement: [C99 6.8.5.3]
1252/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1253/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001254/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1255/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001256/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001257/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1258/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001259///
1260/// [C++] for-init-statement:
1261/// [C++] expression-statement
1262/// [C++] simple-declaration
1263///
Richard Smith02e85f32011-04-14 22:09:26 +00001264/// [C++0x] for-range-declaration:
1265/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1266/// [C++0x] for-range-initializer:
1267/// [C++0x] expression
1268/// [C++0x] braced-init-list [TODO]
Nico Weber3cef1082011-12-22 23:26:17 +00001269StmtResult Parser::ParseForStatement(ParsedAttributes &attrs,
1270 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001271 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001272
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001273 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001274 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001275
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001276 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001277 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001278 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001279 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001280 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001281
Chris Lattner934074c2009-04-22 00:54:41 +00001282 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001283
Chris Lattner2dd1b722007-08-26 23:08:06 +00001284 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1285 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001286 //
1287 // C++ 6.4p3:
1288 // A name introduced by a declaration in a condition is in scope from its
1289 // point of declaration until the end of the substatements controlled by the
1290 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001291 // C++ 3.3.2p4:
1292 // Names declared in the for-init-statement, and in the condition of if,
1293 // while, for, and switch statements are local to the if, while, for, or
1294 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001295 // C++ 6.5.3p1:
1296 // Names declared in the for-init-statement are in the same declarative-region
1297 // as those declared in the condition.
1298 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001299 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001300 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001301 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1302 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001303 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001304 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1305
1306 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001307
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001308 BalancedDelimiterTracker T(*this, tok::l_paren);
1309 T.consumeOpen();
1310
John McCalldadc5752010-08-24 06:29:42 +00001311 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001312
Richard Smith02e85f32011-04-14 22:09:26 +00001313 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001314 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001315 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001316 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001317 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001318 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001319 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001320 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001321
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001322 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001323 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001324 C99orCXXorObjC? Sema::PCC_ForInit
1325 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001326 cutOffParsing();
1327 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001328 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001329
Chris Lattner9075bd72006-08-10 04:59:57 +00001330 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001331 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001332 // no first part, eat the ';'.
1333 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001334 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001335 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001336 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001337 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001338
John McCall084e83d2011-03-24 11:26:52 +00001339 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001340 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001341
Richard Smith02e85f32011-04-14 22:09:26 +00001342 // In C++0x, "for (T NS:a" might not be a typo for ::
1343 bool MightBeForRangeStmt = getLang().CPlusPlus;
1344 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1345
Chris Lattner49836b42009-04-02 04:16:50 +00001346 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001347 StmtVector Stmts(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001348 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001349 DeclEnd, attrs, false,
1350 MightBeForRangeStmt ?
1351 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001352 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001353
Richard Smith02e85f32011-04-14 22:09:26 +00001354 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith5d164bc2011-10-15 05:09:34 +00001355 Diag(ForRangeInit.ColonLoc, getLang().CPlusPlus0x ?
1356 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001357
Richard Smith02e85f32011-04-14 22:09:26 +00001358 ForRange = true;
1359 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001360 ConsumeToken();
1361 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001362 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001363 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001364 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001365
Douglas Gregor68762e72010-08-23 21:17:50 +00001366 if (Tok.is(tok::code_completion)) {
1367 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001368 cutOffParsing();
1369 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001370 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001371 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001372 } else {
1373 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001374 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001375 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001376 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001377
John McCall34376a62010-12-04 03:47:34 +00001378 ForEach = isTokIdentifier_in();
1379
Chris Lattnercd68f642007-06-27 01:06:29 +00001380 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001381 if (!Value.isInvalid()) {
1382 if (ForEach)
1383 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1384 else
1385 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1386 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001387
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001388 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001389 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001390 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001391 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001392
Douglas Gregor68762e72010-08-23 21:17:50 +00001393 if (Tok.is(tok::code_completion)) {
1394 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001395 cutOffParsing();
1396 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001397 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001398 Collection = ParseExpression();
Richard Smith9a2b7e82012-02-21 20:01:35 +00001399 } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001400 // User tried to write the reasonable, but ill-formed, for-range-statement
1401 // for (expr : expr) { ... }
1402 Diag(Tok, diag::err_for_range_expected_decl)
1403 << FirstPart.get()->getSourceRange();
1404 SkipUntil(tok::r_paren, false, true);
1405 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001406 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001407 if (!Value.isInvalid()) {
1408 Diag(Tok, diag::err_expected_semi_for);
1409 } else {
1410 // Skip until semicolon or rparen, don't consume it.
1411 SkipUntil(tok::r_paren, true, true);
1412 if (Tok.is(tok::semi))
1413 ConsumeToken();
1414 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001415 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001416 }
Richard Smith02e85f32011-04-14 22:09:26 +00001417 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001418 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001419 // Parse the second part of the for specifier.
1420 if (Tok.is(tok::semi)) { // for (...;;
1421 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001422 } else if (Tok.is(tok::r_paren)) {
1423 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001424 } else {
John McCalldadc5752010-08-24 06:29:42 +00001425 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001426 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001427 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1428 else {
1429 Second = ParseExpression();
1430 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001431 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001432 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001433 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001434 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001435 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001436 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001437
Douglas Gregor230a7e62011-02-17 03:38:46 +00001438 if (Tok.isNot(tok::semi)) {
1439 if (!SecondPartIsInvalid || SecondVar)
1440 Diag(Tok, diag::err_expected_semi_for);
1441 else
1442 // Skip until semicolon or rparen, don't consume it.
1443 SkipUntil(tok::r_paren, true, true);
1444 }
1445
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001446 if (Tok.is(tok::semi)) {
1447 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001448 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001449
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001450 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001451 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001452 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001453 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001454 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001455 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001456 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001457 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001458
Richard Smith02e85f32011-04-14 22:09:26 +00001459 // We need to perform most of the semantic analysis for a C++0x for-range
1460 // statememt before parsing the body, in order to be able to deduce the type
1461 // of an auto-typed loop variable.
1462 StmtResult ForRangeStmt;
John McCall53848232011-07-27 01:07:15 +00001463 if (ForRange) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001464 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smith02e85f32011-04-14 22:09:26 +00001465 FirstPart.take(),
1466 ForRangeInit.ColonLoc,
1467 ForRangeInit.RangeExpr.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001468 T.getCloseLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001469
John McCall53848232011-07-27 01:07:15 +00001470
1471 // Similarly, we need to do the semantic analysis for a for-range
1472 // statement immediately in order to close over temporaries correctly.
1473 } else if (ForEach) {
1474 if (!Collection.isInvalid())
1475 Collection =
1476 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1477 }
1478
Chris Lattner8fb26252007-08-22 05:28:50 +00001479 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001480 // there is no compound stmt. C90 does not have this clause. We only do this
1481 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001482 //
1483 // C++ 6.5p2:
1484 // The substatement in an iteration-statement implicitly defines a local scope
1485 // which is entered and exited each time through the loop.
1486 //
1487 // See comments in ParseIfStatement for why we create a scope for
1488 // for-init-statement/condition and a new scope for substatement in C++.
1489 //
Mike Stump11289f42009-09-09 15:08:12 +00001490 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001491 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001492
Chris Lattner9075bd72006-08-10 04:59:57 +00001493 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001494 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001495
Chris Lattner8fb26252007-08-22 05:28:50 +00001496 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001497 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001498
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001499 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001500 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001501
1502 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001503 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001504
Richard Smith02e85f32011-04-14 22:09:26 +00001505 if (ForEach)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001506 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1507 FirstPart.take(),
1508 Collection.take(),
1509 T.getCloseLocation(),
1510 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001511
Richard Smith02e85f32011-04-14 22:09:26 +00001512 if (ForRange)
1513 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1514
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001515 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1516 SecondPart, SecondVar, ThirdPart,
1517 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001518}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001519
Chris Lattner503fadc2006-08-10 05:45:44 +00001520/// ParseGotoStatement
1521/// jump-statement:
1522/// 'goto' identifier ';'
1523/// [GNU] 'goto' '*' expression ';'
1524///
1525/// Note: this lets the caller parse the end ';'.
1526///
John McCall53fa7142010-12-24 02:08:15 +00001527StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001528 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001529
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001530 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001531 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001532
John McCalldadc5752010-08-24 06:29:42 +00001533 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001534 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001535 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1536 Tok.getLocation());
1537 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001538 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001539 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001540 // GNU indirect goto extension.
1541 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001542 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001543 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001544 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001545 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001546 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001547 }
John McCallb268a282010-08-23 23:25:46 +00001548 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001549 } else {
1550 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001551 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001552 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001553
Sebastian Redlb62406f2008-12-11 19:48:14 +00001554 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001555}
1556
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001557/// ParseContinueStatement
1558/// jump-statement:
1559/// 'continue' ';'
1560///
1561/// Note: this lets the caller parse the end ';'.
1562///
John McCall53fa7142010-12-24 02:08:15 +00001563StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001564 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001565
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001566 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001567 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001568}
1569
1570/// ParseBreakStatement
1571/// jump-statement:
1572/// 'break' ';'
1573///
1574/// Note: this lets the caller parse the end ';'.
1575///
John McCall53fa7142010-12-24 02:08:15 +00001576StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001577 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001578
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001579 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001580 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001581}
1582
Chris Lattner503fadc2006-08-10 05:45:44 +00001583/// ParseReturnStatement
1584/// jump-statement:
1585/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001586StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001587 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001588
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001589 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001590 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001591
John McCalldadc5752010-08-24 06:29:42 +00001592 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001593 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001594 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001595 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001596 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001597 return StmtError();
1598 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001599
Douglas Gregore9e27d92011-03-11 23:10:44 +00001600 // FIXME: This is a hack to allow something like C++0x's generalized
1601 // initializer lists, but only enough of this feature to allow Clang to
1602 // parse libstdc++ 4.5's headers.
1603 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1604 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001605 if (R.isUsable())
1606 Diag(R.get()->getLocStart(), getLang().CPlusPlus0x ?
1607 diag::warn_cxx98_compat_generalized_initializer_lists :
1608 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001609 << R.get()->getSourceRange();
1610 } else
1611 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001612 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001613 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001614 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001615 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001616 }
John McCallb268a282010-08-23 23:25:46 +00001617 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001618}
Chris Lattner0116c472006-08-15 06:03:28 +00001619
Eli Friedmana4b02c32011-09-30 01:13:51 +00001620/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1621/// this routine is called to collect the tokens for an MS asm statement.
1622StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1623 SourceManager &SrcMgr = PP.getSourceManager();
1624 SourceLocation EndLoc = AsmLoc;
1625 do {
1626 bool InBraces = false;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001627 unsigned short savedBraceCount = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001628 bool InAsmComment = false;
1629 FileID FID;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001630 unsigned LineNo = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001631 unsigned NumTokensRead = 0;
1632 SourceLocation LBraceLoc;
1633
1634 if (Tok.is(tok::l_brace)) {
1635 // Braced inline asm: consume the opening brace.
1636 InBraces = true;
1637 savedBraceCount = BraceCount;
1638 EndLoc = LBraceLoc = ConsumeBrace();
1639 ++NumTokensRead;
1640 } else {
1641 // Single-line inline asm; compute which line it is on.
1642 std::pair<FileID, unsigned> ExpAsmLoc =
1643 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1644 FID = ExpAsmLoc.first;
1645 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1646 }
1647
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001648 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001649 do {
Eli Friedmana4b02c32011-09-30 01:13:51 +00001650 // If we hit EOF, we're done, period.
1651 if (Tok.is(tok::eof))
1652 break;
1653 // When we consume the closing brace, we're done.
1654 if (InBraces && BraceCount == savedBraceCount)
1655 break;
1656
1657 if (!InAsmComment && Tok.is(tok::semi)) {
1658 // A semicolon in an asm is the start of a comment.
1659 InAsmComment = true;
1660 if (InBraces) {
1661 // Compute which line the comment is on.
1662 std::pair<FileID, unsigned> ExpSemiLoc =
1663 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1664 FID = ExpSemiLoc.first;
1665 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1666 }
1667 } else if (!InBraces || InAsmComment) {
1668 // If end-of-line is significant, check whether this token is on a
1669 // new line.
1670 std::pair<FileID, unsigned> ExpLoc =
1671 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1672 if (ExpLoc.first != FID ||
1673 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1674 // If this is a single-line __asm, we're done.
1675 if (!InBraces)
1676 break;
1677 // We're no longer in a comment.
1678 InAsmComment = false;
1679 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1680 // Single-line asm always ends when a closing brace is seen.
1681 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1682 // does MSVC do here?
1683 break;
1684 }
1685 }
1686
1687 // Consume the next token; make sure we don't modify the brace count etc.
1688 // if we are in a comment.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001689 EndLoc = TokLoc;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001690 if (InAsmComment)
1691 PP.Lex(Tok);
1692 else
1693 ConsumeAnyToken();
Steve Naroff8c099c32008-02-08 18:01:27 +00001694 TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001695 ++NumTokensRead;
1696 } while (1);
1697
1698 if (InBraces && BraceCount != savedBraceCount) {
1699 // __asm without closing brace (this can happen at EOF).
1700 Diag(Tok, diag::err_expected_rbrace);
1701 Diag(LBraceLoc, diag::note_matching) << "{";
1702 return StmtError();
1703 } else if (NumTokensRead == 0) {
1704 // Empty __asm.
1705 Diag(Tok, diag::err_expected_lbrace);
1706 return StmtError();
1707 }
1708 // Multiple adjacent asm's form together into a single asm statement
1709 // in the AST.
1710 if (!Tok.is(tok::kw_asm))
1711 break;
1712 EndLoc = ConsumeToken();
1713 } while (1);
1714 // FIXME: Need to actually grab the data and pass it on to Sema. Ideally,
1715 // what Sema wants is a string of the entire inline asm, with one instruction
1716 // per line and all the __asm keywords stripped out, and a way of mapping
1717 // from any character of that string to its location in the original source
1718 // code. I'm not entirely sure how to go about that, though.
Mike Stump6da5d752009-12-11 00:04:56 +00001719 Token t;
1720 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001721 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001722 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001723 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001724 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001725 ExprVector Constraints(Actions);
1726 ExprVector Exprs(Actions);
1727 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001728 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001729 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001730 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001731 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001732}
1733
Chris Lattner0116c472006-08-15 06:03:28 +00001734/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001735/// asm-statement:
1736/// gnu-asm-statement
1737/// ms-asm-statement
1738///
1739/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001740/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1741///
1742/// [GNU] asm-argument:
1743/// asm-string-literal
1744/// asm-string-literal ':' asm-operands[opt]
1745/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1746/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1747/// ':' asm-clobbers
1748///
1749/// [GNU] asm-clobbers:
1750/// asm-string-literal
1751/// asm-clobbers ',' asm-string-literal
1752///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001753/// [MS] ms-asm-statement:
Eli Friedmana4b02c32011-09-30 01:13:51 +00001754/// ms-asm-block
1755/// ms-asm-block ms-asm-statement
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001756///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001757/// [MS] ms-asm-block:
1758/// '__asm' ms-asm-line '\n'
1759/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1760///
1761/// [MS] ms-asm-instruction-block
1762/// ms-asm-line
1763/// ms-asm-line '\n' ms-asm-instruction-block
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001764///
John McCalldadc5752010-08-24 06:29:42 +00001765StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001766 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001767 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001768
Francois Pichet0706d202011-09-17 17:15:52 +00001769 if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001770 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001771 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001772 }
John McCall084e83d2011-03-24 11:26:52 +00001773 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001774 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001775 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001776
Chris Lattner0116c472006-08-15 06:03:28 +00001777 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001778 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001779 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001780 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001781 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001782
Chris Lattner0116c472006-08-15 06:03:28 +00001783 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001784 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001785 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001786 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001787 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001788 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001789 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001790 BalancedDelimiterTracker T(*this, tok::l_paren);
1791 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001792
John McCalldadc5752010-08-24 06:29:42 +00001793 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001794 if (AsmString.isInvalid()) {
1795 // If the reason we are recovering is because of an improper string
1796 // literal, it makes the most sense just to consume to the ')'.
1797 if (isTokenStringLiteral())
1798 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001799 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001800 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001801
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001802 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001803 ExprVector Constraints(Actions);
1804 ExprVector Exprs(Actions);
1805 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001806
Anders Carlsson19fe1162008-02-05 23:03:50 +00001807 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001808 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001809 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001810 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001811 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001812 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001813 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001814 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001815 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001816
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001817 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001818 bool AteExtraColon = false;
1819 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1820 // In C++ mode, parse "::" like ": :".
1821 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001822 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001823
Chris Lattner15768502009-12-20 23:08:04 +00001824 if (!AteExtraColon &&
1825 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001826 return StmtError();
1827 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001828
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001829 unsigned NumOutputs = Names.size();
1830
1831 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001832 if (AteExtraColon ||
1833 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1834 // In C++ mode, parse "::" like ": :".
1835 if (AteExtraColon)
1836 AteExtraColon = false;
1837 else {
1838 AteExtraColon = Tok.is(tok::coloncolon);
1839 ConsumeToken();
1840 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001841
Chris Lattner15768502009-12-20 23:08:04 +00001842 if (!AteExtraColon &&
1843 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001844 return StmtError();
1845 }
1846
1847 assert(Names.size() == Constraints.size() &&
1848 Constraints.size() == Exprs.size() &&
1849 "Input operand size mismatch!");
1850
1851 unsigned NumInputs = Names.size() - NumOutputs;
1852
1853 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001854 if (AteExtraColon || Tok.is(tok::colon)) {
1855 if (!AteExtraColon)
1856 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001857
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001858 // Parse the asm-string list for clobbers if present.
1859 if (Tok.isNot(tok::r_paren)) {
1860 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001861 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001862
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001863 if (Clobber.isInvalid())
1864 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001865
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001866 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001867
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001868 if (Tok.isNot(tok::comma)) break;
1869 ConsumeToken();
1870 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001871 }
1872 }
1873
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001874 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001875 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001876 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001877 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001878 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001879 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001880}
1881
1882/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001883/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001884///
1885/// [GNU] asm-operands:
1886/// asm-operand
1887/// asm-operands ',' asm-operand
1888///
1889/// [GNU] asm-operand:
1890/// asm-string-literal '(' expression ')'
1891/// '[' identifier ']' asm-string-literal '(' expression ')'
1892///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001893//
1894// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001895bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001896 SmallVectorImpl<Expr *> &Constraints,
1897 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001898 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001899 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001900 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001901
1902 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001903 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001904 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001905 BalancedDelimiterTracker T(*this, tok::l_square);
1906 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001907
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001908 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001909 Diag(Tok, diag::err_expected_ident);
1910 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001911 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001912 }
Mike Stump11289f42009-09-09 15:08:12 +00001913
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001914 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001915 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001916
Anders Carlsson9a020f92010-01-30 22:25:16 +00001917 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001918 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001919 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001920 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001921
John McCalldadc5752010-08-24 06:29:42 +00001922 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001923 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001924 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001925 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001926 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001927 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001928
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001929 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001930 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001931 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001932 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001933 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001934
Chris Lattner0116c472006-08-15 06:03:28 +00001935 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001936 BalancedDelimiterTracker T(*this, tok::l_paren);
1937 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001938 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001939 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001940 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001941 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001942 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001943 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001944 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001945 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001946 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001947 ConsumeToken();
1948 }
1949}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001950
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001951Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001952 assert(Tok.is(tok::l_brace));
1953 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001954
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001955 if (PP.isCodeCompletionEnabled()) {
1956 if (trySkippingFunctionBodyForCodeCompletion()) {
1957 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001958 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001959 }
1960 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001961
John McCallfaf5fb42010-08-26 23:41:50 +00001962 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1963 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001964
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001965 // Do not enter a scope for the brace, as the arguments are in the same scope
1966 // (the function body) as the body itself. Instead, just read the statement
1967 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001968 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001969
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001970 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001971 if (FnBody.isInvalid()) {
1972 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001973 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001974 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001975 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001976
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001977 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001978 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001979}
Sebastian Redlb219c902008-12-21 16:41:36 +00001980
Sebastian Redla7b98a72009-04-26 20:35:05 +00001981/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1982///
1983/// function-try-block:
1984/// 'try' ctor-initializer[opt] compound-statement handler-seq
1985///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001986Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001987 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1988 SourceLocation TryLoc = ConsumeToken();
1989
John McCallfaf5fb42010-08-26 23:41:50 +00001990 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1991 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001992
1993 // Constructor initializer list?
1994 if (Tok.is(tok::colon))
1995 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001996 else
1997 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001998
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001999 if (PP.isCodeCompletionEnabled()) {
2000 if (trySkippingFunctionBodyForCodeCompletion()) {
2001 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00002002 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002003 }
2004 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002005
Sebastian Redld98ecd62009-04-26 21:08:36 +00002006 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00002007 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002008 // If we failed to parse the try-catch, we just give the function an empty
2009 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002010 if (FnBody.isInvalid()) {
2011 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002012 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00002013 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002014 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002015
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002016 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002017 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002018}
2019
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00002020bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002021 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00002022 assert(PP.isCodeCompletionEnabled() &&
2023 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002024
2025 // We're in code-completion mode. Skip parsing for all function bodies unless
2026 // the body contains the code-completion point.
2027 TentativeParsingAction PA(*this);
2028 ConsumeBrace();
2029 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
2030 /*StopAtCodeCompletion=*/true)) {
2031 PA.Commit();
2032 return true;
2033 }
2034
2035 PA.Revert();
2036 return false;
2037}
2038
Sebastian Redlb219c902008-12-21 16:41:36 +00002039/// ParseCXXTryBlock - Parse a C++ try-block.
2040///
2041/// try-block:
2042/// 'try' compound-statement handler-seq
2043///
John McCall53fa7142010-12-24 02:08:15 +00002044StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002045 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002046
Sebastian Redlb219c902008-12-21 16:41:36 +00002047 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2048
2049 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002050 return ParseCXXTryBlockCommon(TryLoc);
2051}
2052
2053/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2054/// function-try-block.
2055///
2056/// try-block:
2057/// 'try' compound-statement handler-seq
2058///
2059/// function-try-block:
2060/// 'try' ctor-initializer[opt] compound-statement handler-seq
2061///
2062/// handler-seq:
2063/// handler handler-seq[opt]
2064///
John Wiegley1c0675e2011-04-28 01:08:34 +00002065/// [Borland] try-block:
2066/// 'try' compound-statement seh-except-block
2067/// 'try' compound-statment seh-finally-block
2068///
John McCalldadc5752010-08-24 06:29:42 +00002069StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002070 if (Tok.isNot(tok::l_brace))
2071 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002072 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002073 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002074 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
2075 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002076 if (TryBlock.isInvalid())
2077 return move(TryBlock);
2078
John Wiegley1c0675e2011-04-28 01:08:34 +00002079 // Borland allows SEH-handlers with 'try'
Douglas Gregor60060d62011-10-21 03:57:52 +00002080
2081 if((Tok.is(tok::identifier) &&
2082 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2083 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002084 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2085 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002086 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002087 SourceLocation Loc = ConsumeToken();
2088 Handler = ParseSEHExceptBlock(Loc);
2089 }
2090 else {
2091 SourceLocation Loc = ConsumeToken();
2092 Handler = ParseSEHFinallyBlock(Loc);
2093 }
2094 if(Handler.isInvalid())
2095 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00002096
John Wiegley1c0675e2011-04-28 01:08:34 +00002097 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2098 TryLoc,
2099 TryBlock.take(),
2100 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002101 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002102 else {
2103 StmtVector Handlers(Actions);
2104 MaybeParseCXX0XAttributes(attrs);
2105 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002106
John Wiegley1c0675e2011-04-28 01:08:34 +00002107 if (Tok.isNot(tok::kw_catch))
2108 return StmtError(Diag(Tok, diag::err_expected_catch));
2109 while (Tok.is(tok::kw_catch)) {
2110 StmtResult Handler(ParseCXXCatchBlock());
2111 if (!Handler.isInvalid())
2112 Handlers.push_back(Handler.release());
2113 }
2114 // Don't bother creating the full statement if we don't have any usable
2115 // handlers.
2116 if (Handlers.empty())
2117 return StmtError();
2118
2119 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2120 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002121}
2122
2123/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2124///
2125/// handler:
2126/// 'catch' '(' exception-declaration ')' compound-statement
2127///
2128/// exception-declaration:
2129/// type-specifier-seq declarator
2130/// type-specifier-seq abstract-declarator
2131/// type-specifier-seq
2132/// '...'
2133///
John McCalldadc5752010-08-24 06:29:42 +00002134StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002135 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2136
2137 SourceLocation CatchLoc = ConsumeToken();
2138
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002139 BalancedDelimiterTracker T(*this, tok::l_paren);
2140 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002141 return StmtError();
2142
2143 // C++ 3.3.2p3:
2144 // The name in a catch exception-declaration is local to the handler and
2145 // shall not be redeclared in the outermost block of the handler.
2146 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2147
2148 // exception-declaration is equivalent to '...' or a parameter-declaration
2149 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002150 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002151 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002152 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002153 if (ParseCXXTypeSpecifierSeq(DS))
2154 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002155 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2156 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002157 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002158 } else
2159 ConsumeToken();
2160
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002161 T.consumeClose();
2162 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002163 return StmtError();
2164
2165 if (Tok.isNot(tok::l_brace))
2166 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2167
Alexis Hunt96d5c762009-11-21 08:43:09 +00002168 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002169 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002170 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00002171 if (Block.isInvalid())
2172 return move(Block);
2173
John McCallb268a282010-08-23 23:25:46 +00002174 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002175}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002176
2177void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002178 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002179 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002180 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002181
Douglas Gregor43edb322011-10-24 22:31:10 +00002182 // Handle dependent statements by parsing the braces as a compound statement.
2183 // This is not the same behavior as Visual C++, which don't treat this as a
2184 // compound statement, but for Clang's type checking we can't have anything
2185 // inside these braces escaping to the surrounding code.
2186 if (Result.Behavior == IEB_Dependent) {
2187 if (!Tok.is(tok::l_brace)) {
2188 Diag(Tok, diag::err_expected_lbrace);
2189 return;
2190 }
2191
2192 ParsedAttributes Attrs(AttrFactory);
2193 StmtResult Compound = ParseCompoundStatement(Attrs);
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002194 if (Compound.isInvalid())
2195 return;
2196
2197 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2198 Result.IsIfExists,
2199 Result.SS,
2200 Result.Name,
2201 Compound.get());
2202 if (DepResult.isUsable())
2203 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002204 return;
2205 }
2206
2207 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2208 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002209 Diag(Tok, diag::err_expected_lbrace);
2210 return;
2211 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002212
Douglas Gregor43edb322011-10-24 22:31:10 +00002213 switch (Result.Behavior) {
2214 case IEB_Parse:
2215 // Parse the statements below.
2216 break;
2217
2218 case IEB_Dependent:
2219 llvm_unreachable("Dependent case handled above");
Douglas Gregor43edb322011-10-24 22:31:10 +00002220
2221 case IEB_Skip:
2222 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002223 return;
2224 }
2225
2226 // Condition is true, parse the statements.
2227 while (Tok.isNot(tok::r_brace)) {
2228 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2229 if (R.isUsable())
2230 Stmts.push_back(R.release());
2231 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002232 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002233}