blob: 7ad48a1f5d9b09e82c4290968d2d9a6cefcb987c [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
Fariborz Jahanian1db5c942010-09-28 20:42:35 +000079Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000080 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000081 StmtResult Res;
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000082
83 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000084
John McCall084e83d2011-03-24 11:26:52 +000085 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000086 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +000087
Chris Lattner503fadc2006-08-10 05:45:44 +000088 // Cases in this switch statement should fall through if the parser expects
89 // the token to end in a semicolon (in which case SemiError should be set),
90 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +000091Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000092 tok::TokenKind Kind = Tok.getKind();
93 SourceLocation AtLoc;
94 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000095 case tok::at: // May be a @try or @throw statement
96 {
97 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000098 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000099 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000100
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000101 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000102 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000103 cutOffParsing();
104 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000105
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000106 case tok::identifier: {
107 Token Next = NextToken();
108 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000109 // identifier ':' statement
John McCall53fa7142010-12-24 02:08:15 +0000110 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000111 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000112
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000113 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000114 CXXScopeSpec SS;
115 IdentifierInfo *Name = Tok.getIdentifierInfo();
116 SourceLocation NameLoc = Tok.getLocation();
117 Sema::NameClassification Classification
118 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
119 switch (Classification.getKind()) {
120 case Sema::NC_Keyword:
121 // The identifier was corrected to a keyword. Update the token
122 // to this keyword, and try again.
123 if (Name->getTokenID() != tok::identifier) {
124 Tok.setIdentifierInfo(Name);
125 Tok.setKind(Name->getTokenID());
126 goto Retry;
127 }
128
129 // Fall through via the normal error path.
130 // FIXME: This seems like it could only happen for context-sensitive
131 // keywords.
132
133 case Sema::NC_Error:
134 // Handle errors here by skipping up to the next semicolon or '}', and
135 // eat the semicolon if that's what stopped us.
136 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
137 if (Tok.is(tok::semi))
138 ConsumeToken();
139 return StmtError();
140
141 case Sema::NC_Unknown:
142 // Either we don't know anything about this identifier, or we know that
143 // we're in a syntactic context we haven't handled yet.
144 break;
145
Douglas Gregor19b7acf2011-04-27 05:41:15 +0000146 case Sema::NC_Type:
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000147 Tok.setKind(tok::annot_typename);
148 setTypeAnnotation(Tok, Classification.getType());
149 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000150 PP.AnnotateCachedTokens(Tok);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000151 break;
152
153 case Sema::NC_Expression:
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000154 Tok.setKind(tok::annot_primary_expr);
155 setExprAnnotation(Tok, Classification.getExpression());
156 Tok.setAnnotationEndLoc(NameLoc);
157 PP.AnnotateCachedTokens(Tok);
158 break;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000159
160 case Sema::NC_TypeTemplate:
161 case Sema::NC_FunctionTemplate: {
162 ConsumeToken(); // the identifier
163 UnqualifiedId Id;
164 Id.setIdentifier(Name, NameLoc);
165 if (AnnotateTemplateIdToken(
166 TemplateTy::make(Classification.getTemplateName()),
167 Classification.getTemplateNameKind(),
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000168 SS, Id, SourceLocation(),
169 /*AllowTypeAnnotation=*/false)) {
170 // Handle errors here by skipping up to the next semicolon or '}', and
171 // eat the semicolon if that's what stopped us.
172 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
173 if (Tok.is(tok::semi))
174 ConsumeToken();
175 return StmtError();
176 }
177
178 // If the next token is '::', jump right into parsing a
179 // nested-name-specifier. We don't want to leave the template-id
180 // hanging.
181 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000182 // Handle errors here by skipping up to the next semicolon or '}', and
183 // eat the semicolon if that's what stopped us.
184 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
185 if (Tok.is(tok::semi))
186 ConsumeToken();
187 return StmtError();
188 }
189
190 // We've annotated a template-id, so try again now.
191 goto Retry;
192 }
193
194 case Sema::NC_NestedNameSpecifier:
195 // FIXME: Implement this!
196 break;
197 }
198 }
199
200 // Fall through
201 }
202
Chris Lattner803802d2009-03-24 17:04:48 +0000203 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000204 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000205 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000206 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000207 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000208 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000209 }
210
211 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000212 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000213 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000214 }
Mike Stump11289f42009-09-09 15:08:12 +0000215
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000216 return ParseExprStatement(attrs);
Chris Lattner803802d2009-03-24 17:04:48 +0000217 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000218
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000219 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000220 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000221 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000222 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000223
Chris Lattner9075bd72006-08-10 04:59:57 +0000224 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000225 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000226 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000227 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
228 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000229 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000230
Chris Lattner9075bd72006-08-10 04:59:57 +0000231 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall53fa7142010-12-24 02:08:15 +0000232 return ParseIfStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000233 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall53fa7142010-12-24 02:08:15 +0000234 return ParseSwitchStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000235
Chris Lattner9075bd72006-08-10 04:59:57 +0000236 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall53fa7142010-12-24 02:08:15 +0000237 return ParseWhileStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000238 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000239 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000240 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000241 break;
242 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall53fa7142010-12-24 02:08:15 +0000243 return ParseForStatement(attrs);
Chris Lattner503fadc2006-08-10 05:45:44 +0000244
245 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000246 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000247 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000248 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000249 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000250 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000251 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000252 break;
253 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000254 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000255 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000256 break;
257 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000258 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000259 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000260 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000261
Sebastian Redlb219c902008-12-21 16:41:36 +0000262 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000263 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000264 bool msAsm = false;
265 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000266 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000267 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000268 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000269 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000270 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000271
Sebastian Redlb219c902008-12-21 16:41:36 +0000272 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000273 return ParseCXXTryBlock(attrs);
John Wiegley1c0675e2011-04-28 01:08:34 +0000274
275 case tok::kw___try:
276 return ParseSEHTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000277 }
278
Chris Lattner503fadc2006-08-10 05:45:44 +0000279 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000280 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000281 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000282 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000283 // If the result was valid, then we do want to diagnose this. Use
284 // ExpectAndConsume to emit the diagnostic, even though we know it won't
285 // succeed.
286 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000287 // Skip until we see a } or ;, but don't eat it.
288 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000289 }
Mike Stump11289f42009-09-09 15:08:12 +0000290
Sebastian Redl042ad952008-12-11 19:30:53 +0000291 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000292}
293
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000294/// \brief Parse an expression statement.
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000295StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000296 // If a case keyword is missing, this is where it should be inserted.
297 Token OldToken = Tok;
298
299 // FIXME: Use the attributes
300 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000301 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000302 if (Expr.isInvalid()) {
303 // If the expression is invalid, skip ahead to the next semicolon or '}'.
304 // Not doing this opens us up to the possibility of infinite loops if
305 // ParseExpression does not consume any tokens.
306 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
307 if (Tok.is(tok::semi))
308 ConsumeToken();
309 return StmtError();
310 }
311
312 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
313 Actions.CheckCaseExpression(Expr.get())) {
314 // If a constant expression is followed by a colon inside a switch block,
315 // suggest a missing case keyword.
316 Diag(OldToken, diag::err_expected_case_before_expression)
317 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
318
319 // Recover parsing as a case statement.
320 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
321 }
322
323 // Otherwise, eat the semicolon.
324 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
325 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley1c0675e2011-04-28 01:08:34 +0000326}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000327
John Wiegley1c0675e2011-04-28 01:08:34 +0000328StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
329 assert(Tok.is(tok::kw___try) && "Expected '__try'");
330 SourceLocation Loc = ConsumeToken();
331 return ParseSEHTryBlockCommon(Loc);
332}
333
334/// ParseSEHTryBlockCommon
335///
336/// seh-try-block:
337/// '__try' compound-statement seh-handler
338///
339/// seh-handler:
340/// seh-except-block
341/// seh-finally-block
342///
343StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
344 if(Tok.isNot(tok::l_brace))
345 return StmtError(Diag(Tok,diag::err_expected_lbrace));
346
347 ParsedAttributesWithRange attrs(AttrFactory);
348 StmtResult TryBlock(ParseCompoundStatement(attrs));
349 if(TryBlock.isInvalid())
350 return move(TryBlock);
351
352 StmtResult Handler;
353 if(Tok.is(tok::kw___except)) {
354 SourceLocation Loc = ConsumeToken();
355 Handler = ParseSEHExceptBlock(Loc);
356 } else if (Tok.is(tok::kw___finally)) {
357 SourceLocation Loc = ConsumeToken();
358 Handler = ParseSEHFinallyBlock(Loc);
359 } else {
360 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
361 }
362
363 if(Handler.isInvalid())
364 return move(Handler);
365
366 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
367 TryLoc,
368 TryBlock.take(),
369 Handler.take());
370}
371
372/// ParseSEHExceptBlock - Handle __except
373///
374/// seh-except-block:
375/// '__except' '(' seh-filter-expression ')' compound-statement
376///
377StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
378 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
379 raii2(Ident___exception_code, false),
380 raii3(Ident_GetExceptionCode, false);
381
382 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
383 return StmtError();
384
385 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
386
Francois Pichetbfaf4772011-04-28 03:14:31 +0000387 if (getLang().Borland) {
388 Ident__exception_info->setIsPoisoned(false);
389 Ident___exception_info->setIsPoisoned(false);
390 Ident_GetExceptionInfo->setIsPoisoned(false);
391 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000392 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000393
394 if (getLang().Borland) {
395 Ident__exception_info->setIsPoisoned(true);
396 Ident___exception_info->setIsPoisoned(true);
397 Ident_GetExceptionInfo->setIsPoisoned(true);
398 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000399
400 if(FilterExpr.isInvalid())
401 return StmtError();
402
403 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
404 return StmtError();
405
406 ParsedAttributesWithRange attrs(AttrFactory);
407 StmtResult Block(ParseCompoundStatement(attrs));
408
409 if(Block.isInvalid())
410 return move(Block);
411
412 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
413}
414
415/// ParseSEHFinallyBlock - Handle __finally
416///
417/// seh-finally-block:
418/// '__finally' compound-statement
419///
420StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
421 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
422 raii2(Ident___abnormal_termination, false),
423 raii3(Ident_AbnormalTermination, false);
424
425 ParsedAttributesWithRange attrs(AttrFactory);
426 StmtResult Block(ParseCompoundStatement(attrs));
427 if(Block.isInvalid())
428 return move(Block);
429
430 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000431}
432
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000433/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000434///
435/// labeled-statement:
436/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000437/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000438///
John McCall53fa7142010-12-24 02:08:15 +0000439StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000440 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
441 "Not an identifier!");
442
443 Token IdentTok = Tok; // Save the whole token.
444 ConsumeToken(); // eat the identifier.
445
446 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000447
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000448 // identifier ':' statement
449 SourceLocation ColonLoc = ConsumeToken();
450
451 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000452 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000453
John McCalldadc5752010-08-24 06:29:42 +0000454 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000455
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000456 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000457 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000458 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000459
460 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
461 IdentTok.getLocation());
462 if (AttributeList *Attrs = attrs.getList())
463 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
464
465 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
466 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000467}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000468
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000469/// ParseCaseStatement
470/// labeled-statement:
471/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000472/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000473///
Richard Trieu2c850c02011-04-21 21:44:26 +0000474StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
475 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000476 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000477 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner34a22092009-03-04 04:23:07 +0000479 // It is very very common for code to contain many case statements recursively
480 // nested, as in (but usually without indentation):
481 // case 1:
482 // case 2:
483 // case 3:
484 // case 4:
485 // case 5: etc.
486 //
487 // Parsing this naively works, but is both inefficient and can cause us to run
488 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000489 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000490 // but all the grossness is constrained to ParseCaseStatement (and some
491 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattner34a22092009-03-04 04:23:07 +0000493 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
494 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000495 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattner34a22092009-03-04 04:23:07 +0000497 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
498 // gets updated each time a new case is parsed, and whose body is unset so
499 // far. When parsing 'case 4', this is the 'case 3' node.
500 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner34a22092009-03-04 04:23:07 +0000502 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000503 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000504 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000505 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
506 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregord328d572009-09-21 18:10:23 +0000508 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000509 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000510 cutOffParsing();
511 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000512 }
513
Chris Lattner125c0ee2009-12-10 00:38:54 +0000514 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
515 /// Disable this form of error recovery while we're parsing the case
516 /// expression.
517 ColonProtectionRAIIObject ColonProtection(*this);
518
Richard Trieu2c850c02011-04-21 21:44:26 +0000519 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
520 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000521 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000522 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000523 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000524 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000525
Chris Lattner34a22092009-03-04 04:23:07 +0000526 // GNU case range extension.
527 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000528 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000529 if (Tok.is(tok::ellipsis)) {
530 Diag(Tok, diag::ext_gnu_case_range);
531 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000532
Chris Lattner34a22092009-03-04 04:23:07 +0000533 RHS = ParseConstantExpression();
534 if (RHS.isInvalid()) {
535 SkipUntil(tok::colon);
536 return StmtError();
537 }
538 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000539
540 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000541
John McCall0140bfe2011-01-22 09:28:32 +0000542 if (Tok.is(tok::colon)) {
543 ColonLoc = ConsumeToken();
544
545 // Treat "case blah;" as a typo for "case blah:".
546 } else if (Tok.is(tok::semi)) {
547 ColonLoc = ConsumeToken();
548 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
549 << FixItHint::CreateReplacement(ColonLoc, ":");
550 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000551 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
552 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
553 << FixItHint::CreateInsertion(ExpectedLoc, ":");
554 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000555 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000556
John McCalldadc5752010-08-24 06:29:42 +0000557 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000558 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
559 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattner34a22092009-03-04 04:23:07 +0000561 // If we had a sema error parsing this case, then just ignore it and
562 // continue parsing the sub-stmt.
563 if (Case.isInvalid()) {
564 if (TopLevelCase.isInvalid()) // No parsed case stmts.
565 return ParseStatement();
566 // Otherwise, just don't add it as a nested case.
567 } else {
568 // If this is the first case statement we parsed, it becomes TopLevelCase.
569 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000570 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000571 if (TopLevelCase.isInvalid())
572 TopLevelCase = move(Case);
573 else
John McCallb268a282010-08-23 23:25:46 +0000574 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000575 DeepestParsedCaseStmt = NextDeepest;
576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner34a22092009-03-04 04:23:07 +0000578 // Handle all case statements.
579 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner34a22092009-03-04 04:23:07 +0000581 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000582
Chris Lattner34a22092009-03-04 04:23:07 +0000583 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000584 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner34a22092009-03-04 04:23:07 +0000586 if (Tok.isNot(tok::r_brace)) {
587 SubStmt = ParseStatement();
588 } else {
589 // Nicely diagnose the common error "switch (X) { case 4: }", which is
590 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000591 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
592 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000593 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Chris Lattner34a22092009-03-04 04:23:07 +0000596 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000597 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000598 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner34a22092009-03-04 04:23:07 +0000600 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000601 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000602
Chris Lattner34a22092009-03-04 04:23:07 +0000603 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000604 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000605}
606
607/// ParseDefaultStatement
608/// labeled-statement:
609/// 'default' ':' statement
610/// Note that this does not parse the 'statement' at the end.
611///
John McCall53fa7142010-12-24 02:08:15 +0000612StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000613 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000614
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000615 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000616 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000617
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000618 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000619 if (Tok.is(tok::colon)) {
620 ColonLoc = ConsumeToken();
621
622 // Treat "default;" as a typo for "default:".
623 } else if (Tok.is(tok::semi)) {
624 ColonLoc = ConsumeToken();
625 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
626 << FixItHint::CreateReplacement(ColonLoc, ":");
627 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000628 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
629 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
630 << FixItHint::CreateInsertion(ExpectedLoc, ":");
631 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000632 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000633
Chris Lattner30f910e2006-10-16 05:52:41 +0000634 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000635 if (Tok.is(tok::r_brace)) {
David Majnemerc6a99872011-06-14 15:24:38 +0000636 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
637 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000638 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000639 }
640
John McCalldadc5752010-08-24 06:29:42 +0000641 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000642 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000643 return StmtError();
644
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000645 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000646 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000647}
648
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000649StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
650 bool isStmtExpr) {
651 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
652}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000653
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000654/// ParseCompoundStatement - Parse a "{}" block.
655///
656/// compound-statement: [C99 6.8.2]
657/// { block-item-list[opt] }
658/// [GNU] { label-declarations block-item-list } [TODO]
659///
660/// block-item-list:
661/// block-item
662/// block-item-list block-item
663///
664/// block-item:
665/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000666/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000667/// statement
668/// [OMP] openmp-directive [TODO]
669///
670/// [GNU] label-declarations:
671/// [GNU] label-declaration
672/// [GNU] label-declarations label-declaration
673///
674/// [GNU] label-declaration:
675/// [GNU] '__label__' identifier-list ';'
676///
677/// [OMP] openmp-directive: [TODO]
678/// [OMP] barrier-directive
679/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000680///
John McCall53fa7142010-12-24 02:08:15 +0000681StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000682 bool isStmtExpr,
683 unsigned ScopeFlags) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000684 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000685
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000686 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000687
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000688 // Enter a scope to hold everything within the compound stmt. Compound
689 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000690 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000691
692 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000693 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000694}
695
696
697/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000698/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000699/// consume the '}' at the end of the block. It does not manipulate the scope
700/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000701StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000702 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000703 Tok.getLocation(),
704 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000705 InMessageExpressionRAIIObject InMessage(*this, false);
706
Chris Lattnerf2978802007-01-21 06:52:16 +0000707 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
708
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000709 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000710
Chris Lattner43e7f312011-02-18 02:08:43 +0000711 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
712 // only allowed at the start of a compound stmt regardless of the language.
713 while (Tok.is(tok::kw___label__)) {
714 SourceLocation LabelLoc = ConsumeToken();
715 Diag(LabelLoc, diag::ext_gnu_local_label);
716
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000717 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000718 while (1) {
719 if (Tok.isNot(tok::identifier)) {
720 Diag(Tok, diag::err_expected_ident);
721 break;
722 }
723
724 IdentifierInfo *II = Tok.getIdentifierInfo();
725 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000726 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner43e7f312011-02-18 02:08:43 +0000727
728 if (!Tok.is(tok::comma))
729 break;
730 ConsumeToken();
731 }
732
John McCall084e83d2011-03-24 11:26:52 +0000733 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000734 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
735 DeclsInGroup.data(), DeclsInGroup.size());
736 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
737
738 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
739 if (R.isUsable())
740 Stmts.push_back(R.release());
741 }
742
743 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000744 if (Tok.is(tok::annot_pragma_unused)) {
745 HandlePragmaUnused();
746 continue;
747 }
748
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000749 if (getLang().Microsoft && (Tok.is(tok::kw___if_exists) ||
750 Tok.is(tok::kw___if_not_exists))) {
751 ParseMicrosoftIfExistsStatement(Stmts);
752 continue;
753 }
754
John McCalldadc5752010-08-24 06:29:42 +0000755 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000756 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000757 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000758 } else {
759 // __extension__ can start declarations and it can also be a unary
760 // operator for expressions. Consume multiple __extension__ markers here
761 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000762 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000763 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000764 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000765 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000766
John McCall084e83d2011-03-24 11:26:52 +0000767 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000768 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000769
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000770 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000771 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000772 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000773 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000774 ExtensionRAIIObject O(Diags);
775
Chris Lattner49836b42009-04-02 04:16:50 +0000776 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000777 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
778 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000779 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000780 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000781 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000782 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000783 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000784
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000785 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000786 SkipUntil(tok::semi);
787 continue;
788 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000789
Alexis Hunt96d5c762009-11-21 08:43:09 +0000790 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000791 // Eat the semicolon at the end of stmt and convert the expr into a
792 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000793 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000794 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000795 }
796 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000797
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000798 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000799 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000800 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000801
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000802 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000803 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000804 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000805 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000806 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000807 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000808
Chris Lattner04132372006-10-16 06:12:55 +0000809 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000810 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000811 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000812}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000813
Chris Lattnerc0081db2008-12-12 06:31:07 +0000814/// ParseParenExprOrCondition:
815/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000816/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000817///
818/// This function parses and performs error recovery on the specified condition
819/// or expression (depending on whether we're in C++ or C mode). This function
820/// goes out of its way to recover well. It returns true if there was a parser
821/// error (the right paren couldn't be found), which indicates that the caller
822/// should try to recover harder. It returns false if the condition is
823/// successfully parsed. Note that a successful parse can still have semantic
824/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000825bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000826 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000827 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000828 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000829 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000830 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000831 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000832 else {
833 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000834 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000835
836 // If required, convert to a boolean value.
837 if (!ExprResult.isInvalid() && ConvertToBoolean)
838 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000839 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000840 }
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattnerc0081db2008-12-12 06:31:07 +0000842 // If the parser was confused by the condition and we don't have a ')', try to
843 // recover by skipping ahead to a semi and bailing out. If condexp is
844 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000845 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000846 SkipUntil(tok::semi);
847 // Skipping may have stopped if it found the containing ')'. If so, we can
848 // continue parsing the if statement.
849 if (Tok.isNot(tok::r_paren))
850 return true;
851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattnerc0081db2008-12-12 06:31:07 +0000853 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000854 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000855 return false;
856}
857
858
Chris Lattnerc951dae2006-08-10 04:23:57 +0000859/// ParseIfStatement
860/// if-statement: [C99 6.8.4.1]
861/// 'if' '(' expression ')' statement
862/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000863/// [C++] 'if' '(' condition ')' statement
864/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000865///
John McCall53fa7142010-12-24 02:08:15 +0000866StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000867 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000868
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000869 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000870 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000871
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000872 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000873 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000874 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000875 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000876 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000877
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000878 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
879
Chris Lattner2dd1b722007-08-26 23:08:06 +0000880 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
881 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000882 //
883 // C++ 6.4p3:
884 // A name introduced by a declaration in a condition is in scope from its
885 // point of declaration until the end of the substatements controlled by the
886 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000887 // C++ 3.3.2p4:
888 // Names declared in the for-init-statement, and in the condition of if,
889 // while, for, and switch statements are local to the if, while, for, or
890 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000891 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000892 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000893
Chris Lattnerc951dae2006-08-10 04:23:57 +0000894 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000895 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000896 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000897 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000898 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000899
John McCallb268a282010-08-23 23:25:46 +0000900 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000901
Chris Lattner8fb26252007-08-22 05:28:50 +0000902 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000903 // there is no compound stmt. C90 does not have this clause. We only do this
904 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000905 //
906 // C++ 6.4p1:
907 // The substatement in a selection-statement (each substatement, in the else
908 // form of the if statement) implicitly defines a local scope.
909 //
910 // For C++ we create a scope for the condition and a new scope for
911 // substatements because:
912 // -When the 'then' scope exits, we want the condition declaration to still be
913 // active for the 'else' scope too.
914 // -Sema will detect name clashes by considering declarations of a
915 // 'ControlScope' as part of its direct subscope.
916 // -If we wanted the condition and substatement to be in the same scope, we
917 // would have to notify ParseStatement not to create a new scope. It's
918 // simpler to let it create a new scope.
919 //
Mike Stump11289f42009-09-09 15:08:12 +0000920 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000921 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000922
Chris Lattner5c5808a2007-10-29 05:08:52 +0000923 // Read the 'then' stmt.
924 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000925 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000926
Chris Lattner37e54f42007-08-22 05:16:28 +0000927 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000928 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000929
Chris Lattnerc951dae2006-08-10 04:23:57 +0000930 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000931 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000932 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000933 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000934
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000935 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000936 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000937 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000938
Chris Lattner8fb26252007-08-22 05:28:50 +0000939 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000940 // there is no compound stmt. C90 does not have this clause. We only do
941 // this if the body isn't a compound statement to avoid push/pop in common
942 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000943 //
944 // C++ 6.4p1:
945 // The substatement in a selection-statement (each substatement, in the else
946 // form of the if statement) implicitly defines a local scope.
947 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000948 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000949 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000950
Chris Lattner30f910e2006-10-16 05:52:41 +0000951 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000952
Chris Lattner37e54f42007-08-22 05:16:28 +0000953 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000954 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +0000955 } else if (Tok.is(tok::code_completion)) {
956 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000957 cutOffParsing();
958 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000959 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000960
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000961 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000962
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000963 // If the condition was invalid, discard the if statement. We could recover
964 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000965 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000966 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000967
Chris Lattner5c5808a2007-10-29 05:08:52 +0000968 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000969 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000970 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000971 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
972 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
973 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000974 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000975 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000976 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000977
Chris Lattner5c5808a2007-10-29 05:08:52 +0000978 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000979 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000980 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000981 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000982 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000983
John McCallb268a282010-08-23 23:25:46 +0000984 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000985 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000986}
987
Chris Lattner9075bd72006-08-10 04:59:57 +0000988/// ParseSwitchStatement
989/// switch-statement:
990/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000991/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000992StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000993 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000994
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000995 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000996 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000997
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000998 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000999 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001000 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001001 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001002 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001003
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001004 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1005
Chris Lattner2dd1b722007-08-26 23:08:06 +00001006 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1007 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001008 //
1009 // C++ 6.4p3:
1010 // A name introduced by a declaration in a condition is in scope from its
1011 // point of declaration until the end of the substatements controlled by the
1012 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001013 // C++ 3.3.2p4:
1014 // Names declared in the for-init-statement, and in the condition of if,
1015 // while, for, and switch statements are local to the if, while, for, or
1016 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001017 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001018 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001019 if (C99orCXX)
1020 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001021 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001022
Chris Lattner9075bd72006-08-10 04:59:57 +00001023 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001024 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001025 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001026 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001027 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001028
John McCalldadc5752010-08-24 06:29:42 +00001029 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001030 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001031
Douglas Gregore60e41a2010-05-06 17:25:47 +00001032 if (Switch.isInvalid()) {
1033 // Skip the switch body.
1034 // FIXME: This is not optimal recovery, but parsing the body is more
1035 // dangerous due to the presence of case and default statements, which
1036 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001037 if (Tok.is(tok::l_brace)) {
1038 ConsumeBrace();
1039 SkipUntil(tok::r_brace, false, false);
1040 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001041 SkipUntil(tok::semi);
1042 return move(Switch);
1043 }
1044
Chris Lattner8fb26252007-08-22 05:28:50 +00001045 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001046 // there is no compound stmt. C90 does not have this clause. We only do this
1047 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001048 //
1049 // C++ 6.4p1:
1050 // The substatement in a selection-statement (each substatement, in the else
1051 // form of the if statement) implicitly defines a local scope.
1052 //
1053 // See comments in ParseIfStatement for why we create a scope for the
1054 // condition and a new scope for substatement in C++.
1055 //
Mike Stump11289f42009-09-09 15:08:12 +00001056 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001057 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001058
Chris Lattner9075bd72006-08-10 04:59:57 +00001059 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001060 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001061
Chris Lattner8fd2d012010-01-24 01:50:29 +00001062 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001063 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001064 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001065
Chris Lattner8fd2d012010-01-24 01:50:29 +00001066 if (Body.isInvalid())
1067 // FIXME: Remove the case statement list from the Switch statement.
1068 Body = Actions.ActOnNullStmt(Tok.getLocation());
1069
John McCallb268a282010-08-23 23:25:46 +00001070 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001071}
1072
1073/// ParseWhileStatement
1074/// while-statement: [C99 6.8.5.1]
1075/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001076/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +00001077StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001078 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001079
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001080 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001081 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001082 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001083
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001084 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001085 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001086 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001087 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001088 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001089
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001090 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1091
Chris Lattner2dd1b722007-08-26 23:08:06 +00001092 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1093 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001094 //
1095 // C++ 6.4p3:
1096 // A name introduced by a declaration in a condition is in scope from its
1097 // point of declaration until the end of the substatements controlled by the
1098 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001099 // C++ 3.3.2p4:
1100 // Names declared in the for-init-statement, and in the condition of if,
1101 // while, for, and switch statements are local to the if, while, for, or
1102 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001103 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001104 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001105 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001106 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1107 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001108 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001109 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1110 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001111
Chris Lattner9075bd72006-08-10 04:59:57 +00001112 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001113 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001114 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001115 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001116 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001117
John McCallb268a282010-08-23 23:25:46 +00001118 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001119
Chris Lattner8fb26252007-08-22 05:28:50 +00001120 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001121 // there is no compound stmt. C90 does not have this clause. We only do this
1122 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001123 //
1124 // C++ 6.5p2:
1125 // The substatement in an iteration-statement implicitly defines a local scope
1126 // which is entered and exited each time through the loop.
1127 //
1128 // See comments in ParseIfStatement for why we create a scope for the
1129 // condition and a new scope for substatement in C++.
1130 //
Mike Stump11289f42009-09-09 15:08:12 +00001131 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001132 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001133
Chris Lattner9075bd72006-08-10 04:59:57 +00001134 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001135 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001136
Chris Lattner8fb26252007-08-22 05:28:50 +00001137 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001138 InnerScope.Exit();
1139 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001140
John McCall48871652010-08-21 09:40:31 +00001141 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001142 return StmtError();
1143
John McCallb268a282010-08-23 23:25:46 +00001144 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001145}
1146
1147/// ParseDoStatement
1148/// do-statement: [C99 6.8.5.2]
1149/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001150/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001151StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001152 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001153
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001154 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001155 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001156
Chris Lattner2dd1b722007-08-26 23:08:06 +00001157 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1158 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001159 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001160 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001161 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001162 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001163 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001164
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001165 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001166
Chris Lattner8fb26252007-08-22 05:28:50 +00001167 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001168 // there is no compound stmt. C90 does not have this clause. We only do this
1169 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001170 //
1171 // C++ 6.5p2:
1172 // The substatement in an iteration-statement implicitly defines a local scope
1173 // which is entered and exited each time through the loop.
1174 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001175 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001176 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001177 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001178
Chris Lattner9075bd72006-08-10 04:59:57 +00001179 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001180 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001181
Chris Lattner8fb26252007-08-22 05:28:50 +00001182 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001183 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001184
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001185 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001186 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001187 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001188 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001189 SkipUntil(tok::semi, false, true);
1190 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001191 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001192 }
Chris Lattneraf635312006-10-16 06:06:51 +00001193 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001194
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001195 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001196 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001197 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001198 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001199 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001200
Chris Lattner10da53c2008-12-12 06:35:28 +00001201 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +00001202 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001203 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +00001204 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001205 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001206
Sebastian Redlb62406f2008-12-11 19:48:14 +00001207 if (Cond.isInvalid() || Body.isInvalid())
1208 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001209
John McCallb268a282010-08-23 23:25:46 +00001210 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1211 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +00001212}
1213
1214/// ParseForStatement
1215/// for-statement: [C99 6.8.5.3]
1216/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1217/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001218/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1219/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001220/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001221/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1222/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001223///
1224/// [C++] for-init-statement:
1225/// [C++] expression-statement
1226/// [C++] simple-declaration
1227///
Richard Smith02e85f32011-04-14 22:09:26 +00001228/// [C++0x] for-range-declaration:
1229/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1230/// [C++0x] for-range-initializer:
1231/// [C++0x] expression
1232/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001233StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001234 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001235
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001236 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001237 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001238
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001239 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001240 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001241 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001242 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001243 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001244
Chris Lattner934074c2009-04-22 00:54:41 +00001245 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001246
Chris Lattner2dd1b722007-08-26 23:08:06 +00001247 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1248 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001249 //
1250 // C++ 6.4p3:
1251 // A name introduced by a declaration in a condition is in scope from its
1252 // point of declaration until the end of the substatements controlled by the
1253 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001254 // C++ 3.3.2p4:
1255 // Names declared in the for-init-statement, and in the condition of if,
1256 // while, for, and switch statements are local to the if, while, for, or
1257 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001258 // C++ 6.5.3p1:
1259 // Names declared in the for-init-statement are in the same declarative-region
1260 // as those declared in the condition.
1261 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001262 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001263 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001264 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1265 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001266 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001267 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1268
1269 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001270
Chris Lattner04132372006-10-16 06:12:55 +00001271 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001272 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001273
Richard Smith02e85f32011-04-14 22:09:26 +00001274 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001275 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001276 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001277 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001278 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001279 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001280 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001281 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001282
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001283 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001284 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001285 C99orCXXorObjC? Sema::PCC_ForInit
1286 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001287 cutOffParsing();
1288 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001289 }
1290
Chris Lattner9075bd72006-08-10 04:59:57 +00001291 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001292 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001293 // no first part, eat the ';'.
1294 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001295 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001296 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001297 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001298 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001299
John McCall084e83d2011-03-24 11:26:52 +00001300 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001301 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001302
Richard Smith02e85f32011-04-14 22:09:26 +00001303 // In C++0x, "for (T NS:a" might not be a typo for ::
1304 bool MightBeForRangeStmt = getLang().CPlusPlus;
1305 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1306
Chris Lattner49836b42009-04-02 04:16:50 +00001307 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001308 StmtVector Stmts(Actions);
1309 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001310 DeclEnd, attrs, false,
1311 MightBeForRangeStmt ?
1312 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001313 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001314
Richard Smith02e85f32011-04-14 22:09:26 +00001315 if (ForRangeInit.ParsedForRangeDecl()) {
1316 ForRange = true;
1317 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001318 ConsumeToken();
1319 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001320 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001321 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001322 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001323
1324 if (Tok.is(tok::code_completion)) {
1325 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001326 cutOffParsing();
1327 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001328 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001329 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001330 } else {
1331 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001332 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001333 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001334 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001335
John McCall34376a62010-12-04 03:47:34 +00001336 ForEach = isTokIdentifier_in();
1337
Chris Lattnercd68f642007-06-27 01:06:29 +00001338 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001339 if (!Value.isInvalid()) {
1340 if (ForEach)
1341 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1342 else
1343 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1344 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001345
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001346 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001347 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001348 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001349 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001350
1351 if (Tok.is(tok::code_completion)) {
1352 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001353 cutOffParsing();
1354 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001355 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001356 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001357 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001358 if (!Value.isInvalid()) {
1359 Diag(Tok, diag::err_expected_semi_for);
1360 } else {
1361 // Skip until semicolon or rparen, don't consume it.
1362 SkipUntil(tok::r_paren, true, true);
1363 if (Tok.is(tok::semi))
1364 ConsumeToken();
1365 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001366 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001367 }
Richard Smith02e85f32011-04-14 22:09:26 +00001368 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001369 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001370 // Parse the second part of the for specifier.
1371 if (Tok.is(tok::semi)) { // for (...;;
1372 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001373 } else if (Tok.is(tok::r_paren)) {
1374 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001375 } else {
John McCalldadc5752010-08-24 06:29:42 +00001376 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001377 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001378 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1379 else {
1380 Second = ParseExpression();
1381 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001382 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001383 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001384 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001385 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001386 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001387 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001388
Douglas Gregor230a7e62011-02-17 03:38:46 +00001389 if (Tok.isNot(tok::semi)) {
1390 if (!SecondPartIsInvalid || SecondVar)
1391 Diag(Tok, diag::err_expected_semi_for);
1392 else
1393 // Skip until semicolon or rparen, don't consume it.
1394 SkipUntil(tok::r_paren, true, true);
1395 }
1396
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001397 if (Tok.is(tok::semi)) {
1398 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001399 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001400
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001401 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001402 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001403 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001404 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001405 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001406 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001407 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001408 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001409
Richard Smith02e85f32011-04-14 22:09:26 +00001410 // We need to perform most of the semantic analysis for a C++0x for-range
1411 // statememt before parsing the body, in order to be able to deduce the type
1412 // of an auto-typed loop variable.
1413 StmtResult ForRangeStmt;
John McCall53848232011-07-27 01:07:15 +00001414 if (ForRange) {
Richard Smith02e85f32011-04-14 22:09:26 +00001415 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1416 FirstPart.take(),
1417 ForRangeInit.ColonLoc,
1418 ForRangeInit.RangeExpr.get(),
1419 RParenLoc);
1420
John McCall53848232011-07-27 01:07:15 +00001421
1422 // Similarly, we need to do the semantic analysis for a for-range
1423 // statement immediately in order to close over temporaries correctly.
1424 } else if (ForEach) {
1425 if (!Collection.isInvalid())
1426 Collection =
1427 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1428 }
1429
Chris Lattner8fb26252007-08-22 05:28:50 +00001430 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001431 // there is no compound stmt. C90 does not have this clause. We only do this
1432 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001433 //
1434 // C++ 6.5p2:
1435 // The substatement in an iteration-statement implicitly defines a local scope
1436 // which is entered and exited each time through the loop.
1437 //
1438 // See comments in ParseIfStatement for why we create a scope for
1439 // for-init-statement/condition and a new scope for substatement in C++.
1440 //
Mike Stump11289f42009-09-09 15:08:12 +00001441 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001442 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001443
Chris Lattner9075bd72006-08-10 04:59:57 +00001444 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001445 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001446
Chris Lattner8fb26252007-08-22 05:28:50 +00001447 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001448 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001449
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001450 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001451 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001452
1453 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001454 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001455
Richard Smith02e85f32011-04-14 22:09:26 +00001456 if (ForEach)
Richard Smith02e85f32011-04-14 22:09:26 +00001457 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1458 FirstPart.take(),
1459 Collection.take(), RParenLoc,
1460 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001461
Richard Smith02e85f32011-04-14 22:09:26 +00001462 if (ForRange)
1463 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1464
1465 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1466 SecondVar, ThirdPart, RParenLoc, Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001467}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001468
Chris Lattner503fadc2006-08-10 05:45:44 +00001469/// ParseGotoStatement
1470/// jump-statement:
1471/// 'goto' identifier ';'
1472/// [GNU] 'goto' '*' expression ';'
1473///
1474/// Note: this lets the caller parse the end ';'.
1475///
John McCall53fa7142010-12-24 02:08:15 +00001476StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001477 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001478
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001479 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001480 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001481
John McCalldadc5752010-08-24 06:29:42 +00001482 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001483 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001484 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1485 Tok.getLocation());
1486 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001487 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001488 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001489 // GNU indirect goto extension.
1490 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001491 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001492 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001493 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001494 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001495 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001496 }
John McCallb268a282010-08-23 23:25:46 +00001497 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001498 } else {
1499 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001500 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001501 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001502
Sebastian Redlb62406f2008-12-11 19:48:14 +00001503 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001504}
1505
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001506/// ParseContinueStatement
1507/// jump-statement:
1508/// 'continue' ';'
1509///
1510/// Note: this lets the caller parse the end ';'.
1511///
John McCall53fa7142010-12-24 02:08:15 +00001512StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001513 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001514
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001515 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001516 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001517}
1518
1519/// ParseBreakStatement
1520/// jump-statement:
1521/// 'break' ';'
1522///
1523/// Note: this lets the caller parse the end ';'.
1524///
John McCall53fa7142010-12-24 02:08:15 +00001525StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001526 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001527
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001528 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001529 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001530}
1531
Chris Lattner503fadc2006-08-10 05:45:44 +00001532/// ParseReturnStatement
1533/// jump-statement:
1534/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001535StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001536 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001537
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001538 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001539 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001540
John McCalldadc5752010-08-24 06:29:42 +00001541 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001542 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001543 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001544 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001545 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001546 return StmtError();
1547 }
1548
Douglas Gregore9e27d92011-03-11 23:10:44 +00001549 // FIXME: This is a hack to allow something like C++0x's generalized
1550 // initializer lists, but only enough of this feature to allow Clang to
1551 // parse libstdc++ 4.5's headers.
1552 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1553 R = ParseInitializer();
1554 if (R.isUsable() && !getLang().CPlusPlus0x)
1555 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1556 << R.get()->getSourceRange();
1557 } else
1558 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001559 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001560 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001561 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001562 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001563 }
John McCallb268a282010-08-23 23:25:46 +00001564 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001565}
Chris Lattner0116c472006-08-15 06:03:28 +00001566
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001567/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1568/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001569StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1570 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001571 if (Tok.is(tok::l_brace)) {
1572 unsigned short savedBraceCount = BraceCount;
1573 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001574 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001575 ConsumeAnyToken();
1576 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001577 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001578 // From the MS website: If used without braces, the __asm keyword means
1579 // that the rest of the line is an assembly-language statement.
1580 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001581 SourceLocation TokLoc = Tok.getLocation();
Chandler Carruthd48db212011-07-25 21:09:52 +00001582 unsigned LineNo = SrcMgr.getExpansionLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001583 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001584 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001585 ConsumeAnyToken();
1586 TokLoc = Tok.getLocation();
Chandler Carruthd48db212011-07-25 21:09:52 +00001587 } while ((SrcMgr.getExpansionLineNumber(TokLoc) == LineNo) &&
Mike Stump11289f42009-09-09 15:08:12 +00001588 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001589 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001590 }
Mike Stump6da5d752009-12-11 00:04:56 +00001591 Token t;
1592 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001593 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001594 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001595 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001596 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001597 ExprVector Constraints(Actions);
1598 ExprVector Exprs(Actions);
1599 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001600 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001601 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001602 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001603 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001604}
1605
Chris Lattner0116c472006-08-15 06:03:28 +00001606/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001607/// asm-statement:
1608/// gnu-asm-statement
1609/// ms-asm-statement
1610///
1611/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001612/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1613///
1614/// [GNU] asm-argument:
1615/// asm-string-literal
1616/// asm-string-literal ':' asm-operands[opt]
1617/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1618/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1619/// ':' asm-clobbers
1620///
1621/// [GNU] asm-clobbers:
1622/// asm-string-literal
1623/// asm-clobbers ',' asm-string-literal
1624///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001625/// [MS] ms-asm-statement:
1626/// '__asm' assembly-instruction ';'[opt]
1627/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1628///
1629/// [MS] assembly-instruction-list:
1630/// assembly-instruction ';'[opt]
1631/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1632///
John McCalldadc5752010-08-24 06:29:42 +00001633StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001634 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001635 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001636
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001637 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001638 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001639 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001640 }
John McCall084e83d2011-03-24 11:26:52 +00001641 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001642 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001643 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001644
Chris Lattner0116c472006-08-15 06:03:28 +00001645 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001646 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001647 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001648 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001649 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001650
Chris Lattner0116c472006-08-15 06:03:28 +00001651 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001652 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001653 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001654 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001655 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001656 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001657 }
Chris Lattner04132372006-10-16 06:12:55 +00001658 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001659
John McCalldadc5752010-08-24 06:29:42 +00001660 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001661 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001662 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001663
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001664 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001665 ExprVector Constraints(Actions);
1666 ExprVector Exprs(Actions);
1667 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001668
Anders Carlsson19fe1162008-02-05 23:03:50 +00001669 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001670 // We have a simple asm expression like 'asm("foo")'.
1671 SourceLocation RParenLoc = ConsumeParen();
1672 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1673 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1674 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001675 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001676 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001677 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001678
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001679 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001680 bool AteExtraColon = false;
1681 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1682 // In C++ mode, parse "::" like ": :".
1683 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001684 ConsumeToken();
1685
Chris Lattner15768502009-12-20 23:08:04 +00001686 if (!AteExtraColon &&
1687 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001688 return StmtError();
1689 }
Chris Lattner15768502009-12-20 23:08:04 +00001690
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001691 unsigned NumOutputs = Names.size();
1692
1693 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001694 if (AteExtraColon ||
1695 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1696 // In C++ mode, parse "::" like ": :".
1697 if (AteExtraColon)
1698 AteExtraColon = false;
1699 else {
1700 AteExtraColon = Tok.is(tok::coloncolon);
1701 ConsumeToken();
1702 }
1703
1704 if (!AteExtraColon &&
1705 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001706 return StmtError();
1707 }
1708
1709 assert(Names.size() == Constraints.size() &&
1710 Constraints.size() == Exprs.size() &&
1711 "Input operand size mismatch!");
1712
1713 unsigned NumInputs = Names.size() - NumOutputs;
1714
1715 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001716 if (AteExtraColon || Tok.is(tok::colon)) {
1717 if (!AteExtraColon)
1718 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001719
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001720 // Parse the asm-string list for clobbers if present.
1721 if (Tok.isNot(tok::r_paren)) {
1722 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001723 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001724
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001725 if (Clobber.isInvalid())
1726 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001727
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001728 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001729
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001730 if (Tok.isNot(tok::comma)) break;
1731 ConsumeToken();
1732 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001733 }
1734 }
1735
1736 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1737 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001738 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001739 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001740 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001741 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001742}
1743
1744/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001745/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001746///
1747/// [GNU] asm-operands:
1748/// asm-operand
1749/// asm-operands ',' asm-operand
1750///
1751/// [GNU] asm-operand:
1752/// asm-string-literal '(' expression ')'
1753/// '[' identifier ']' asm-string-literal '(' expression ')'
1754///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001755//
1756// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001757bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
1758 SmallVectorImpl<ExprTy *> &Constraints,
1759 SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001760 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001761 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001762 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001763
1764 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001765 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001766 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001767 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001768
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001769 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001770 Diag(Tok, diag::err_expected_ident);
1771 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001772 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001773 }
Mike Stump11289f42009-09-09 15:08:12 +00001774
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001775 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001776 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001777
Anders Carlsson9a020f92010-01-30 22:25:16 +00001778 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001779 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001780 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001781 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001782
John McCalldadc5752010-08-24 06:29:42 +00001783 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001784 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001785 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001786 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001787 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001788 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001789
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001790 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001791 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001792 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001793 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001794 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001795
Chris Lattner0116c472006-08-15 06:03:28 +00001796 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001797 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001798 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001799 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001800 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001801 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001802 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001803 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001804 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001805 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001806 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001807 ConsumeToken();
1808 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001809
1810 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001811}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001812
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001813Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001814 assert(Tok.is(tok::l_brace));
1815 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001816
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001817 if (PP.isCodeCompletionEnabled()) {
1818 if (trySkippingFunctionBodyForCodeCompletion()) {
1819 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001820 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001821 }
1822 }
1823
John McCallfaf5fb42010-08-26 23:41:50 +00001824 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1825 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001826
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001827 // Do not enter a scope for the brace, as the arguments are in the same scope
1828 // (the function body) as the body itself. Instead, just read the statement
1829 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001830 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001831
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001832 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001833 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001834 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001835 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001836
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001837 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001838 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001839}
Sebastian Redlb219c902008-12-21 16:41:36 +00001840
Sebastian Redla7b98a72009-04-26 20:35:05 +00001841/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1842///
1843/// function-try-block:
1844/// 'try' ctor-initializer[opt] compound-statement handler-seq
1845///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001846Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001847 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1848 SourceLocation TryLoc = ConsumeToken();
1849
John McCallfaf5fb42010-08-26 23:41:50 +00001850 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1851 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001852
1853 // Constructor initializer list?
1854 if (Tok.is(tok::colon))
1855 ParseConstructorInitializer(Decl);
1856
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001857 if (PP.isCodeCompletionEnabled()) {
1858 if (trySkippingFunctionBodyForCodeCompletion()) {
1859 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001860 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001861 }
1862 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001863
Sebastian Redld98ecd62009-04-26 21:08:36 +00001864 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001865 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001866 // If we failed to parse the try-catch, we just give the function an empty
1867 // compound statement as the body.
1868 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001869 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001870 MultiStmtArg(Actions), false);
1871
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001872 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001873 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001874}
1875
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001876bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001877 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001878 assert(PP.isCodeCompletionEnabled() &&
1879 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001880
1881 // We're in code-completion mode. Skip parsing for all function bodies unless
1882 // the body contains the code-completion point.
1883 TentativeParsingAction PA(*this);
1884 ConsumeBrace();
1885 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1886 /*StopAtCodeCompletion=*/true)) {
1887 PA.Commit();
1888 return true;
1889 }
1890
1891 PA.Revert();
1892 return false;
1893}
1894
Sebastian Redlb219c902008-12-21 16:41:36 +00001895/// ParseCXXTryBlock - Parse a C++ try-block.
1896///
1897/// try-block:
1898/// 'try' compound-statement handler-seq
1899///
John McCall53fa7142010-12-24 02:08:15 +00001900StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001901 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001902
Sebastian Redlb219c902008-12-21 16:41:36 +00001903 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1904
1905 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001906 return ParseCXXTryBlockCommon(TryLoc);
1907}
1908
1909/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1910/// function-try-block.
1911///
1912/// try-block:
1913/// 'try' compound-statement handler-seq
1914///
1915/// function-try-block:
1916/// 'try' ctor-initializer[opt] compound-statement handler-seq
1917///
1918/// handler-seq:
1919/// handler handler-seq[opt]
1920///
John Wiegley1c0675e2011-04-28 01:08:34 +00001921/// [Borland] try-block:
1922/// 'try' compound-statement seh-except-block
1923/// 'try' compound-statment seh-finally-block
1924///
John McCalldadc5752010-08-24 06:29:42 +00001925StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001926 if (Tok.isNot(tok::l_brace))
1927 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001928 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001929 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001930 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
1931 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00001932 if (TryBlock.isInvalid())
1933 return move(TryBlock);
1934
John Wiegley1c0675e2011-04-28 01:08:34 +00001935 // Borland allows SEH-handlers with 'try'
1936 if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
1937 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1938 StmtResult Handler;
1939 if(Tok.is(tok::kw___except)) {
1940 SourceLocation Loc = ConsumeToken();
1941 Handler = ParseSEHExceptBlock(Loc);
1942 }
1943 else {
1944 SourceLocation Loc = ConsumeToken();
1945 Handler = ParseSEHFinallyBlock(Loc);
1946 }
1947 if(Handler.isInvalid())
1948 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00001949
John Wiegley1c0675e2011-04-28 01:08:34 +00001950 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1951 TryLoc,
1952 TryBlock.take(),
1953 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001954 }
John Wiegley1c0675e2011-04-28 01:08:34 +00001955 else {
1956 StmtVector Handlers(Actions);
1957 MaybeParseCXX0XAttributes(attrs);
1958 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00001959
John Wiegley1c0675e2011-04-28 01:08:34 +00001960 if (Tok.isNot(tok::kw_catch))
1961 return StmtError(Diag(Tok, diag::err_expected_catch));
1962 while (Tok.is(tok::kw_catch)) {
1963 StmtResult Handler(ParseCXXCatchBlock());
1964 if (!Handler.isInvalid())
1965 Handlers.push_back(Handler.release());
1966 }
1967 // Don't bother creating the full statement if we don't have any usable
1968 // handlers.
1969 if (Handlers.empty())
1970 return StmtError();
1971
1972 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
1973 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001974}
1975
1976/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1977///
1978/// handler:
1979/// 'catch' '(' exception-declaration ')' compound-statement
1980///
1981/// exception-declaration:
1982/// type-specifier-seq declarator
1983/// type-specifier-seq abstract-declarator
1984/// type-specifier-seq
1985/// '...'
1986///
John McCalldadc5752010-08-24 06:29:42 +00001987StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001988 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1989
1990 SourceLocation CatchLoc = ConsumeToken();
1991
1992 SourceLocation LParenLoc = Tok.getLocation();
1993 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1994 return StmtError();
1995
1996 // C++ 3.3.2p3:
1997 // The name in a catch exception-declaration is local to the handler and
1998 // shall not be redeclared in the outermost block of the handler.
1999 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2000
2001 // exception-declaration is equivalent to '...' or a parameter-declaration
2002 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002003 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002004 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002005 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002006 if (ParseCXXTypeSpecifierSeq(DS))
2007 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002008 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2009 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002010 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002011 } else
2012 ConsumeToken();
2013
2014 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
2015 return StmtError();
2016
2017 if (Tok.isNot(tok::l_brace))
2018 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2019
Alexis Hunt96d5c762009-11-21 08:43:09 +00002020 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002021 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002022 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00002023 if (Block.isInvalid())
2024 return move(Block);
2025
John McCallb268a282010-08-23 23:25:46 +00002026 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002027}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002028
2029void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002030 bool Result;
2031 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002032 return;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002033
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002034 if (Tok.isNot(tok::l_brace)) {
2035 Diag(Tok, diag::err_expected_lbrace);
2036 return;
2037 }
2038 ConsumeBrace();
2039
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002040 // Condition is false skip all inside the {}.
2041 if (!Result) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002042 SkipUntil(tok::r_brace, false);
2043 return;
2044 }
2045
2046 // Condition is true, parse the statements.
2047 while (Tok.isNot(tok::r_brace)) {
2048 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2049 if (R.isUsable())
2050 Stmts.push_back(R.release());
2051 }
2052
2053 if (Tok.isNot(tok::r_brace)) {
2054 Diag(Tok, diag::err_expected_rbrace);
2055 return;
2056 }
2057 ConsumeBrace();
2058}