blob: df768c9a0655ff0ee9b66daf391543fd70dcbcb3 [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);
Douglas Gregorf4c33342010-05-28 00:22:41 +0000103 ConsumeCodeCompletionToken();
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000104 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
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());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000510 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000511 }
512
Chris Lattner125c0ee2009-12-10 00:38:54 +0000513 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
514 /// Disable this form of error recovery while we're parsing the case
515 /// expression.
516 ColonProtectionRAIIObject ColonProtection(*this);
517
Richard Trieu2c850c02011-04-21 21:44:26 +0000518 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
519 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000520 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000521 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000522 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000523 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000524
Chris Lattner34a22092009-03-04 04:23:07 +0000525 // GNU case range extension.
526 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000527 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000528 if (Tok.is(tok::ellipsis)) {
529 Diag(Tok, diag::ext_gnu_case_range);
530 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000531
Chris Lattner34a22092009-03-04 04:23:07 +0000532 RHS = ParseConstantExpression();
533 if (RHS.isInvalid()) {
534 SkipUntil(tok::colon);
535 return StmtError();
536 }
537 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000538
539 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000540
John McCall0140bfe2011-01-22 09:28:32 +0000541 if (Tok.is(tok::colon)) {
542 ColonLoc = ConsumeToken();
543
544 // Treat "case blah;" as a typo for "case blah:".
545 } else if (Tok.is(tok::semi)) {
546 ColonLoc = ConsumeToken();
547 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
548 << FixItHint::CreateReplacement(ColonLoc, ":");
549 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000550 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
551 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
552 << FixItHint::CreateInsertion(ExpectedLoc, ":");
553 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000554 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000555
John McCalldadc5752010-08-24 06:29:42 +0000556 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000557 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
558 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner34a22092009-03-04 04:23:07 +0000560 // If we had a sema error parsing this case, then just ignore it and
561 // continue parsing the sub-stmt.
562 if (Case.isInvalid()) {
563 if (TopLevelCase.isInvalid()) // No parsed case stmts.
564 return ParseStatement();
565 // Otherwise, just don't add it as a nested case.
566 } else {
567 // If this is the first case statement we parsed, it becomes TopLevelCase.
568 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000569 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000570 if (TopLevelCase.isInvalid())
571 TopLevelCase = move(Case);
572 else
John McCallb268a282010-08-23 23:25:46 +0000573 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000574 DeepestParsedCaseStmt = NextDeepest;
575 }
Mike Stump11289f42009-09-09 15:08:12 +0000576
Chris Lattner34a22092009-03-04 04:23:07 +0000577 // Handle all case statements.
578 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000579
Chris Lattner34a22092009-03-04 04:23:07 +0000580 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000581
Chris Lattner34a22092009-03-04 04:23:07 +0000582 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000583 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000584
Chris Lattner34a22092009-03-04 04:23:07 +0000585 if (Tok.isNot(tok::r_brace)) {
586 SubStmt = ParseStatement();
587 } else {
588 // Nicely diagnose the common error "switch (X) { case 4: }", which is
589 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000590 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
591 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000592 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000593 }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner34a22092009-03-04 04:23:07 +0000595 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000596 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000597 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000598
Chris Lattner34a22092009-03-04 04:23:07 +0000599 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000600 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000601
Chris Lattner34a22092009-03-04 04:23:07 +0000602 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000603 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000604}
605
606/// ParseDefaultStatement
607/// labeled-statement:
608/// 'default' ':' statement
609/// Note that this does not parse the 'statement' at the end.
610///
John McCall53fa7142010-12-24 02:08:15 +0000611StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000612 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000613
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000614 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000615 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000616
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000617 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000618 if (Tok.is(tok::colon)) {
619 ColonLoc = ConsumeToken();
620
621 // Treat "default;" as a typo for "default:".
622 } else if (Tok.is(tok::semi)) {
623 ColonLoc = ConsumeToken();
624 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
625 << FixItHint::CreateReplacement(ColonLoc, ":");
626 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000627 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
628 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
629 << FixItHint::CreateInsertion(ExpectedLoc, ":");
630 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000631 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000632
Chris Lattner30f910e2006-10-16 05:52:41 +0000633 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000634 if (Tok.is(tok::r_brace)) {
David Majnemerc6a99872011-06-14 15:24:38 +0000635 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
636 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000637 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000638 }
639
John McCalldadc5752010-08-24 06:29:42 +0000640 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000641 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000642 return StmtError();
643
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000644 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000645 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000646}
647
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000648StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
649 bool isStmtExpr) {
650 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
651}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000652
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000653/// ParseCompoundStatement - Parse a "{}" block.
654///
655/// compound-statement: [C99 6.8.2]
656/// { block-item-list[opt] }
657/// [GNU] { label-declarations block-item-list } [TODO]
658///
659/// block-item-list:
660/// block-item
661/// block-item-list block-item
662///
663/// block-item:
664/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000665/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000666/// statement
667/// [OMP] openmp-directive [TODO]
668///
669/// [GNU] label-declarations:
670/// [GNU] label-declaration
671/// [GNU] label-declarations label-declaration
672///
673/// [GNU] label-declaration:
674/// [GNU] '__label__' identifier-list ';'
675///
676/// [OMP] openmp-directive: [TODO]
677/// [OMP] barrier-directive
678/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000679///
John McCall53fa7142010-12-24 02:08:15 +0000680StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000681 bool isStmtExpr,
682 unsigned ScopeFlags) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000683 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000684
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000685 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000686
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000687 // Enter a scope to hold everything within the compound stmt. Compound
688 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000689 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000690
691 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000692 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000693}
694
695
696/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000697/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000698/// consume the '}' at the end of the block. It does not manipulate the scope
699/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000700StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000701 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000702 Tok.getLocation(),
703 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000704 InMessageExpressionRAIIObject InMessage(*this, false);
705
Chris Lattnerf2978802007-01-21 06:52:16 +0000706 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
707
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000708 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000709
Chris Lattner43e7f312011-02-18 02:08:43 +0000710 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
711 // only allowed at the start of a compound stmt regardless of the language.
712 while (Tok.is(tok::kw___label__)) {
713 SourceLocation LabelLoc = ConsumeToken();
714 Diag(LabelLoc, diag::ext_gnu_local_label);
715
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000716 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000717 while (1) {
718 if (Tok.isNot(tok::identifier)) {
719 Diag(Tok, diag::err_expected_ident);
720 break;
721 }
722
723 IdentifierInfo *II = Tok.getIdentifierInfo();
724 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000725 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner43e7f312011-02-18 02:08:43 +0000726
727 if (!Tok.is(tok::comma))
728 break;
729 ConsumeToken();
730 }
731
John McCall084e83d2011-03-24 11:26:52 +0000732 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000733 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
734 DeclsInGroup.data(), DeclsInGroup.size());
735 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
736
737 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
738 if (R.isUsable())
739 Stmts.push_back(R.release());
740 }
741
742 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000743 if (Tok.is(tok::annot_pragma_unused)) {
744 HandlePragmaUnused();
745 continue;
746 }
747
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000748 if (getLang().Microsoft && (Tok.is(tok::kw___if_exists) ||
749 Tok.is(tok::kw___if_not_exists))) {
750 ParseMicrosoftIfExistsStatement(Stmts);
751 continue;
752 }
753
John McCalldadc5752010-08-24 06:29:42 +0000754 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000755 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000756 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000757 } else {
758 // __extension__ can start declarations and it can also be a unary
759 // operator for expressions. Consume multiple __extension__ markers here
760 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000761 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000762 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000763 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000764 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000765
John McCall084e83d2011-03-24 11:26:52 +0000766 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000767 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000768
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000769 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000770 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000771 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000772 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000773 ExtensionRAIIObject O(Diags);
774
Chris Lattner49836b42009-04-02 04:16:50 +0000775 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000776 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
777 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000778 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000779 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000780 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000781 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000782 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000783
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000784 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000785 SkipUntil(tok::semi);
786 continue;
787 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000788
Alexis Hunt96d5c762009-11-21 08:43:09 +0000789 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000790 // Eat the semicolon at the end of stmt and convert the expr into a
791 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000792 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000793 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000794 }
795 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000796
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000797 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000798 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000799 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000800
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000801 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000802 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000803 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000804 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000805 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000806 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000807
Chris Lattner04132372006-10-16 06:12:55 +0000808 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000809 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000810 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000811}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000812
Chris Lattnerc0081db2008-12-12 06:31:07 +0000813/// ParseParenExprOrCondition:
814/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000815/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000816///
817/// This function parses and performs error recovery on the specified condition
818/// or expression (depending on whether we're in C++ or C mode). This function
819/// goes out of its way to recover well. It returns true if there was a parser
820/// error (the right paren couldn't be found), which indicates that the caller
821/// should try to recover harder. It returns false if the condition is
822/// successfully parsed. Note that a successful parse can still have semantic
823/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000824bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000825 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000826 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000827 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000828 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000829 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000830 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000831 else {
832 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000833 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000834
835 // If required, convert to a boolean value.
836 if (!ExprResult.isInvalid() && ConvertToBoolean)
837 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000838 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000839 }
Mike Stump11289f42009-09-09 15:08:12 +0000840
Chris Lattnerc0081db2008-12-12 06:31:07 +0000841 // If the parser was confused by the condition and we don't have a ')', try to
842 // recover by skipping ahead to a semi and bailing out. If condexp is
843 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000844 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000845 SkipUntil(tok::semi);
846 // Skipping may have stopped if it found the containing ')'. If so, we can
847 // continue parsing the if statement.
848 if (Tok.isNot(tok::r_paren))
849 return true;
850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattnerc0081db2008-12-12 06:31:07 +0000852 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000853 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000854 return false;
855}
856
857
Chris Lattnerc951dae2006-08-10 04:23:57 +0000858/// ParseIfStatement
859/// if-statement: [C99 6.8.4.1]
860/// 'if' '(' expression ')' statement
861/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000862/// [C++] 'if' '(' condition ')' statement
863/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000864///
John McCall53fa7142010-12-24 02:08:15 +0000865StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000866 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000867
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000868 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000869 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000870
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000871 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000872 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000873 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000874 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000875 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000876
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000877 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
878
Chris Lattner2dd1b722007-08-26 23:08:06 +0000879 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
880 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000881 //
882 // C++ 6.4p3:
883 // A name introduced by a declaration in a condition is in scope from its
884 // point of declaration until the end of the substatements controlled by the
885 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000886 // C++ 3.3.2p4:
887 // Names declared in the for-init-statement, and in the condition of if,
888 // while, for, and switch statements are local to the if, while, for, or
889 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000890 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000891 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000892
Chris Lattnerc951dae2006-08-10 04:23:57 +0000893 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000894 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000895 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000896 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000897 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000898
John McCallb268a282010-08-23 23:25:46 +0000899 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000900
Chris Lattner8fb26252007-08-22 05:28:50 +0000901 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000902 // there is no compound stmt. C90 does not have this clause. We only do this
903 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000904 //
905 // C++ 6.4p1:
906 // The substatement in a selection-statement (each substatement, in the else
907 // form of the if statement) implicitly defines a local scope.
908 //
909 // For C++ we create a scope for the condition and a new scope for
910 // substatements because:
911 // -When the 'then' scope exits, we want the condition declaration to still be
912 // active for the 'else' scope too.
913 // -Sema will detect name clashes by considering declarations of a
914 // 'ControlScope' as part of its direct subscope.
915 // -If we wanted the condition and substatement to be in the same scope, we
916 // would have to notify ParseStatement not to create a new scope. It's
917 // simpler to let it create a new scope.
918 //
Mike Stump11289f42009-09-09 15:08:12 +0000919 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000920 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000921
Chris Lattner5c5808a2007-10-29 05:08:52 +0000922 // Read the 'then' stmt.
923 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000924 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000925
Chris Lattner37e54f42007-08-22 05:16:28 +0000926 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000927 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000928
Chris Lattnerc951dae2006-08-10 04:23:57 +0000929 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000930 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000931 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000932 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000933
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000934 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000935 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000936 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000937
Chris Lattner8fb26252007-08-22 05:28:50 +0000938 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000939 // there is no compound stmt. C90 does not have this clause. We only do
940 // this if the body isn't a compound statement to avoid push/pop in common
941 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000942 //
943 // C++ 6.4p1:
944 // The substatement in a selection-statement (each substatement, in the else
945 // form of the if statement) implicitly defines a local scope.
946 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000947 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000948 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000949
Chris Lattner30f910e2006-10-16 05:52:41 +0000950 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000951
Chris Lattner37e54f42007-08-22 05:16:28 +0000952 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000953 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +0000954 } else if (Tok.is(tok::code_completion)) {
955 Actions.CodeCompleteAfterIf(getCurScope());
956 ConsumeCodeCompletionToken();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000957 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000958
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000959 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000960
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000961 // If the condition was invalid, discard the if statement. We could recover
962 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000963 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000964 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000965
Chris Lattner5c5808a2007-10-29 05:08:52 +0000966 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000967 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000968 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000969 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
970 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
971 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000972 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000973 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000974 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000975
Chris Lattner5c5808a2007-10-29 05:08:52 +0000976 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000977 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000978 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000979 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000980 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000981
John McCallb268a282010-08-23 23:25:46 +0000982 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000983 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000984}
985
Chris Lattner9075bd72006-08-10 04:59:57 +0000986/// ParseSwitchStatement
987/// switch-statement:
988/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000989/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000990StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000991 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000992
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000993 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000994 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000995
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000996 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000997 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000998 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000999 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001000 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001001
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001002 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1003
Chris Lattner2dd1b722007-08-26 23:08:06 +00001004 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1005 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001006 //
1007 // C++ 6.4p3:
1008 // A name introduced by a declaration in a condition is in scope from its
1009 // point of declaration until the end of the substatements controlled by the
1010 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001011 // C++ 3.3.2p4:
1012 // Names declared in the for-init-statement, and in the condition of if,
1013 // while, for, and switch statements are local to the if, while, for, or
1014 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001015 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001016 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001017 if (C99orCXX)
1018 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001019 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001020
Chris Lattner9075bd72006-08-10 04:59:57 +00001021 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001022 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001023 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001024 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001025 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001026
John McCalldadc5752010-08-24 06:29:42 +00001027 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001028 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001029
Douglas Gregore60e41a2010-05-06 17:25:47 +00001030 if (Switch.isInvalid()) {
1031 // Skip the switch body.
1032 // FIXME: This is not optimal recovery, but parsing the body is more
1033 // dangerous due to the presence of case and default statements, which
1034 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001035 if (Tok.is(tok::l_brace)) {
1036 ConsumeBrace();
1037 SkipUntil(tok::r_brace, false, false);
1038 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001039 SkipUntil(tok::semi);
1040 return move(Switch);
1041 }
1042
Chris Lattner8fb26252007-08-22 05:28:50 +00001043 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001044 // there is no compound stmt. C90 does not have this clause. We only do this
1045 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001046 //
1047 // C++ 6.4p1:
1048 // The substatement in a selection-statement (each substatement, in the else
1049 // form of the if statement) implicitly defines a local scope.
1050 //
1051 // See comments in ParseIfStatement for why we create a scope for the
1052 // condition and a new scope for substatement in C++.
1053 //
Mike Stump11289f42009-09-09 15:08:12 +00001054 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001055 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001056
Chris Lattner9075bd72006-08-10 04:59:57 +00001057 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001058 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001059
Chris Lattner8fd2d012010-01-24 01:50:29 +00001060 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001061 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001062 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001063
Chris Lattner8fd2d012010-01-24 01:50:29 +00001064 if (Body.isInvalid())
1065 // FIXME: Remove the case statement list from the Switch statement.
1066 Body = Actions.ActOnNullStmt(Tok.getLocation());
1067
John McCallb268a282010-08-23 23:25:46 +00001068 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001069}
1070
1071/// ParseWhileStatement
1072/// while-statement: [C99 6.8.5.1]
1073/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001074/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +00001075StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001076 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001077
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001078 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001079 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001080 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001081
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001082 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001083 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001084 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001085 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001086 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001087
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001088 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1089
Chris Lattner2dd1b722007-08-26 23:08:06 +00001090 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1091 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001092 //
1093 // C++ 6.4p3:
1094 // A name introduced by a declaration in a condition is in scope from its
1095 // point of declaration until the end of the substatements controlled by the
1096 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001097 // C++ 3.3.2p4:
1098 // Names declared in the for-init-statement, and in the condition of if,
1099 // while, for, and switch statements are local to the if, while, for, or
1100 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001101 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001102 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001103 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001104 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1105 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001106 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001107 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1108 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001109
Chris Lattner9075bd72006-08-10 04:59:57 +00001110 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001111 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001112 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001113 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001114 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001115
John McCallb268a282010-08-23 23:25:46 +00001116 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner8fb26252007-08-22 05:28:50 +00001118 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001119 // there is no compound stmt. C90 does not have this clause. We only do this
1120 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001121 //
1122 // C++ 6.5p2:
1123 // The substatement in an iteration-statement implicitly defines a local scope
1124 // which is entered and exited each time through the loop.
1125 //
1126 // See comments in ParseIfStatement for why we create a scope for the
1127 // condition and a new scope for substatement in C++.
1128 //
Mike Stump11289f42009-09-09 15:08:12 +00001129 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001130 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001131
Chris Lattner9075bd72006-08-10 04:59:57 +00001132 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001133 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001134
Chris Lattner8fb26252007-08-22 05:28:50 +00001135 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001136 InnerScope.Exit();
1137 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001138
John McCall48871652010-08-21 09:40:31 +00001139 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001140 return StmtError();
1141
John McCallb268a282010-08-23 23:25:46 +00001142 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001143}
1144
1145/// ParseDoStatement
1146/// do-statement: [C99 6.8.5.2]
1147/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001148/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001149StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001150 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001151
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001152 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001153 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001154
Chris Lattner2dd1b722007-08-26 23:08:06 +00001155 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1156 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001157 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001158 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001159 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001160 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001161 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001162
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001163 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001164
Chris Lattner8fb26252007-08-22 05:28:50 +00001165 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001166 // there is no compound stmt. C90 does not have this clause. We only do this
1167 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001168 //
1169 // C++ 6.5p2:
1170 // The substatement in an iteration-statement implicitly defines a local scope
1171 // which is entered and exited each time through the loop.
1172 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001173 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001174 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001175 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001176
Chris Lattner9075bd72006-08-10 04:59:57 +00001177 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001178 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001179
Chris Lattner8fb26252007-08-22 05:28:50 +00001180 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001181 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001182
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001183 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001184 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001185 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001186 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001187 SkipUntil(tok::semi, false, true);
1188 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001189 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001190 }
Chris Lattneraf635312006-10-16 06:06:51 +00001191 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001192
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001193 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001194 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001195 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001196 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001197 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001198
Chris Lattner10da53c2008-12-12 06:35:28 +00001199 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +00001200 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001201 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +00001202 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001203 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001204
Sebastian Redlb62406f2008-12-11 19:48:14 +00001205 if (Cond.isInvalid() || Body.isInvalid())
1206 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001207
John McCallb268a282010-08-23 23:25:46 +00001208 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1209 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +00001210}
1211
1212/// ParseForStatement
1213/// for-statement: [C99 6.8.5.3]
1214/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1215/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001216/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1217/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001218/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001219/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1220/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001221///
1222/// [C++] for-init-statement:
1223/// [C++] expression-statement
1224/// [C++] simple-declaration
1225///
Richard Smith02e85f32011-04-14 22:09:26 +00001226/// [C++0x] for-range-declaration:
1227/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1228/// [C++0x] for-range-initializer:
1229/// [C++0x] expression
1230/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001231StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001232 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001233
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001234 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001235 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001236
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001237 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001238 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001239 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001240 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001241 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001242
Chris Lattner934074c2009-04-22 00:54:41 +00001243 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001244
Chris Lattner2dd1b722007-08-26 23:08:06 +00001245 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1246 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001247 //
1248 // C++ 6.4p3:
1249 // A name introduced by a declaration in a condition is in scope from its
1250 // point of declaration until the end of the substatements controlled by the
1251 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001252 // C++ 3.3.2p4:
1253 // Names declared in the for-init-statement, and in the condition of if,
1254 // while, for, and switch statements are local to the if, while, for, or
1255 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001256 // C++ 6.5.3p1:
1257 // Names declared in the for-init-statement are in the same declarative-region
1258 // as those declared in the condition.
1259 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001260 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001261 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001262 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1263 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001264 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001265 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1266
1267 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001268
Chris Lattner04132372006-10-16 06:12:55 +00001269 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001270 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001271
Richard Smith02e85f32011-04-14 22:09:26 +00001272 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001273 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001274 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001275 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001276 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001277 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001278 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001279 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001280
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001281 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001282 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001283 C99orCXXorObjC? Sema::PCC_ForInit
1284 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001285 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001286 }
1287
Chris Lattner9075bd72006-08-10 04:59:57 +00001288 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001289 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001290 // no first part, eat the ';'.
1291 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001292 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001293 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001294 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001295 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001296
John McCall084e83d2011-03-24 11:26:52 +00001297 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001298 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001299
Richard Smith02e85f32011-04-14 22:09:26 +00001300 // In C++0x, "for (T NS:a" might not be a typo for ::
1301 bool MightBeForRangeStmt = getLang().CPlusPlus;
1302 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1303
Chris Lattner49836b42009-04-02 04:16:50 +00001304 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001305 StmtVector Stmts(Actions);
1306 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001307 DeclEnd, attrs, false,
1308 MightBeForRangeStmt ?
1309 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001310 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001311
Richard Smith02e85f32011-04-14 22:09:26 +00001312 if (ForRangeInit.ParsedForRangeDecl()) {
1313 ForRange = true;
1314 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001315 ConsumeToken();
1316 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001317 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001318 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001319 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001320
1321 if (Tok.is(tok::code_completion)) {
1322 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1323 ConsumeCodeCompletionToken();
1324 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001325 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001326 } else {
1327 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001328 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001329 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001330 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001331
John McCall34376a62010-12-04 03:47:34 +00001332 ForEach = isTokIdentifier_in();
1333
Chris Lattnercd68f642007-06-27 01:06:29 +00001334 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001335 if (!Value.isInvalid()) {
1336 if (ForEach)
1337 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1338 else
1339 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1340 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001341
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001342 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001343 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001344 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001345 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001346
1347 if (Tok.is(tok::code_completion)) {
1348 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1349 ConsumeCodeCompletionToken();
1350 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001351 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001352 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001353 if (!Value.isInvalid()) {
1354 Diag(Tok, diag::err_expected_semi_for);
1355 } else {
1356 // Skip until semicolon or rparen, don't consume it.
1357 SkipUntil(tok::r_paren, true, true);
1358 if (Tok.is(tok::semi))
1359 ConsumeToken();
1360 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001361 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001362 }
Richard Smith02e85f32011-04-14 22:09:26 +00001363 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001364 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001365 // Parse the second part of the for specifier.
1366 if (Tok.is(tok::semi)) { // for (...;;
1367 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001368 } else if (Tok.is(tok::r_paren)) {
1369 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001370 } else {
John McCalldadc5752010-08-24 06:29:42 +00001371 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001372 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001373 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1374 else {
1375 Second = ParseExpression();
1376 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001377 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001378 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001379 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001380 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001381 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001382 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001383
Douglas Gregor230a7e62011-02-17 03:38:46 +00001384 if (Tok.isNot(tok::semi)) {
1385 if (!SecondPartIsInvalid || SecondVar)
1386 Diag(Tok, diag::err_expected_semi_for);
1387 else
1388 // Skip until semicolon or rparen, don't consume it.
1389 SkipUntil(tok::r_paren, true, true);
1390 }
1391
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001392 if (Tok.is(tok::semi)) {
1393 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001394 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001395
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001396 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001397 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001398 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001399 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001400 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001401 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001402 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001403 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001404
Richard Smith02e85f32011-04-14 22:09:26 +00001405 // We need to perform most of the semantic analysis for a C++0x for-range
1406 // statememt before parsing the body, in order to be able to deduce the type
1407 // of an auto-typed loop variable.
1408 StmtResult ForRangeStmt;
John McCall53848232011-07-27 01:07:15 +00001409 if (ForRange) {
Richard Smith02e85f32011-04-14 22:09:26 +00001410 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1411 FirstPart.take(),
1412 ForRangeInit.ColonLoc,
1413 ForRangeInit.RangeExpr.get(),
1414 RParenLoc);
1415
John McCall53848232011-07-27 01:07:15 +00001416
1417 // Similarly, we need to do the semantic analysis for a for-range
1418 // statement immediately in order to close over temporaries correctly.
1419 } else if (ForEach) {
1420 if (!Collection.isInvalid())
1421 Collection =
1422 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1423 }
1424
Chris Lattner8fb26252007-08-22 05:28:50 +00001425 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001426 // there is no compound stmt. C90 does not have this clause. We only do this
1427 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001428 //
1429 // C++ 6.5p2:
1430 // The substatement in an iteration-statement implicitly defines a local scope
1431 // which is entered and exited each time through the loop.
1432 //
1433 // See comments in ParseIfStatement for why we create a scope for
1434 // for-init-statement/condition and a new scope for substatement in C++.
1435 //
Mike Stump11289f42009-09-09 15:08:12 +00001436 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001437 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001438
Chris Lattner9075bd72006-08-10 04:59:57 +00001439 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001440 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001441
Chris Lattner8fb26252007-08-22 05:28:50 +00001442 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001443 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001444
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001445 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001446 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001447
1448 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001449 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001450
Richard Smith02e85f32011-04-14 22:09:26 +00001451 if (ForEach)
Richard Smith02e85f32011-04-14 22:09:26 +00001452 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1453 FirstPart.take(),
1454 Collection.take(), RParenLoc,
1455 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001456
Richard Smith02e85f32011-04-14 22:09:26 +00001457 if (ForRange)
1458 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1459
1460 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1461 SecondVar, ThirdPart, RParenLoc, Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001462}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001463
Chris Lattner503fadc2006-08-10 05:45:44 +00001464/// ParseGotoStatement
1465/// jump-statement:
1466/// 'goto' identifier ';'
1467/// [GNU] 'goto' '*' expression ';'
1468///
1469/// Note: this lets the caller parse the end ';'.
1470///
John McCall53fa7142010-12-24 02:08:15 +00001471StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001472 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001473
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001474 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001475 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001476
John McCalldadc5752010-08-24 06:29:42 +00001477 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001478 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001479 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1480 Tok.getLocation());
1481 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001482 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001483 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001484 // GNU indirect goto extension.
1485 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001486 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001487 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001488 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001489 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001490 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001491 }
John McCallb268a282010-08-23 23:25:46 +00001492 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001493 } else {
1494 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001495 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001496 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001497
Sebastian Redlb62406f2008-12-11 19:48:14 +00001498 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001499}
1500
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001501/// ParseContinueStatement
1502/// jump-statement:
1503/// 'continue' ';'
1504///
1505/// Note: this lets the caller parse the end ';'.
1506///
John McCall53fa7142010-12-24 02:08:15 +00001507StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001508 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001509
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001510 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001511 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001512}
1513
1514/// ParseBreakStatement
1515/// jump-statement:
1516/// 'break' ';'
1517///
1518/// Note: this lets the caller parse the end ';'.
1519///
John McCall53fa7142010-12-24 02:08:15 +00001520StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001521 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001522
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001523 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001524 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001525}
1526
Chris Lattner503fadc2006-08-10 05:45:44 +00001527/// ParseReturnStatement
1528/// jump-statement:
1529/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001530StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001531 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001532
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001533 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001534 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001535
John McCalldadc5752010-08-24 06:29:42 +00001536 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001537 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001538 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001539 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001540 ConsumeCodeCompletionToken();
1541 SkipUntil(tok::semi, false, true);
1542 return StmtError();
1543 }
1544
Douglas Gregore9e27d92011-03-11 23:10:44 +00001545 // FIXME: This is a hack to allow something like C++0x's generalized
1546 // initializer lists, but only enough of this feature to allow Clang to
1547 // parse libstdc++ 4.5's headers.
1548 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1549 R = ParseInitializer();
1550 if (R.isUsable() && !getLang().CPlusPlus0x)
1551 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1552 << R.get()->getSourceRange();
1553 } else
1554 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001555 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001556 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001557 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001558 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001559 }
John McCallb268a282010-08-23 23:25:46 +00001560 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001561}
Chris Lattner0116c472006-08-15 06:03:28 +00001562
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001563/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1564/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001565StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1566 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001567 if (Tok.is(tok::l_brace)) {
1568 unsigned short savedBraceCount = BraceCount;
1569 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001570 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001571 ConsumeAnyToken();
1572 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001573 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001574 // From the MS website: If used without braces, the __asm keyword means
1575 // that the rest of the line is an assembly-language statement.
1576 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001577 SourceLocation TokLoc = Tok.getLocation();
Chandler Carruthd48db212011-07-25 21:09:52 +00001578 unsigned LineNo = SrcMgr.getExpansionLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001579 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001580 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001581 ConsumeAnyToken();
1582 TokLoc = Tok.getLocation();
Chandler Carruthd48db212011-07-25 21:09:52 +00001583 } while ((SrcMgr.getExpansionLineNumber(TokLoc) == LineNo) &&
Mike Stump11289f42009-09-09 15:08:12 +00001584 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001585 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001586 }
Mike Stump6da5d752009-12-11 00:04:56 +00001587 Token t;
1588 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001589 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001590 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001591 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001592 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001593 ExprVector Constraints(Actions);
1594 ExprVector Exprs(Actions);
1595 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001596 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001597 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001598 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001599 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001600}
1601
Chris Lattner0116c472006-08-15 06:03:28 +00001602/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001603/// asm-statement:
1604/// gnu-asm-statement
1605/// ms-asm-statement
1606///
1607/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001608/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1609///
1610/// [GNU] asm-argument:
1611/// asm-string-literal
1612/// asm-string-literal ':' asm-operands[opt]
1613/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1614/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1615/// ':' asm-clobbers
1616///
1617/// [GNU] asm-clobbers:
1618/// asm-string-literal
1619/// asm-clobbers ',' asm-string-literal
1620///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001621/// [MS] ms-asm-statement:
1622/// '__asm' assembly-instruction ';'[opt]
1623/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1624///
1625/// [MS] assembly-instruction-list:
1626/// assembly-instruction ';'[opt]
1627/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1628///
John McCalldadc5752010-08-24 06:29:42 +00001629StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001630 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001631 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001632
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001633 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001634 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001635 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001636 }
John McCall084e83d2011-03-24 11:26:52 +00001637 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001638 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001639 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001640
Chris Lattner0116c472006-08-15 06:03:28 +00001641 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001642 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001643 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001644 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001645 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001646
Chris Lattner0116c472006-08-15 06:03:28 +00001647 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001648 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001649 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001650 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001651 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001652 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001653 }
Chris Lattner04132372006-10-16 06:12:55 +00001654 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001655
John McCalldadc5752010-08-24 06:29:42 +00001656 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001657 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001658 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001659
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001660 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001661 ExprVector Constraints(Actions);
1662 ExprVector Exprs(Actions);
1663 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001664
Anders Carlsson19fe1162008-02-05 23:03:50 +00001665 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001666 // We have a simple asm expression like 'asm("foo")'.
1667 SourceLocation RParenLoc = ConsumeParen();
1668 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1669 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1670 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001671 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001672 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001673 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001674
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001675 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001676 bool AteExtraColon = false;
1677 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1678 // In C++ mode, parse "::" like ": :".
1679 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001680 ConsumeToken();
1681
Chris Lattner15768502009-12-20 23:08:04 +00001682 if (!AteExtraColon &&
1683 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001684 return StmtError();
1685 }
Chris Lattner15768502009-12-20 23:08:04 +00001686
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001687 unsigned NumOutputs = Names.size();
1688
1689 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001690 if (AteExtraColon ||
1691 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1692 // In C++ mode, parse "::" like ": :".
1693 if (AteExtraColon)
1694 AteExtraColon = false;
1695 else {
1696 AteExtraColon = Tok.is(tok::coloncolon);
1697 ConsumeToken();
1698 }
1699
1700 if (!AteExtraColon &&
1701 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001702 return StmtError();
1703 }
1704
1705 assert(Names.size() == Constraints.size() &&
1706 Constraints.size() == Exprs.size() &&
1707 "Input operand size mismatch!");
1708
1709 unsigned NumInputs = Names.size() - NumOutputs;
1710
1711 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001712 if (AteExtraColon || Tok.is(tok::colon)) {
1713 if (!AteExtraColon)
1714 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001715
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001716 // Parse the asm-string list for clobbers if present.
1717 if (Tok.isNot(tok::r_paren)) {
1718 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001719 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001720
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001721 if (Clobber.isInvalid())
1722 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001723
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001724 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001725
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001726 if (Tok.isNot(tok::comma)) break;
1727 ConsumeToken();
1728 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001729 }
1730 }
1731
1732 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1733 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001734 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001735 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001736 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001737 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001738}
1739
1740/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001741/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001742///
1743/// [GNU] asm-operands:
1744/// asm-operand
1745/// asm-operands ',' asm-operand
1746///
1747/// [GNU] asm-operand:
1748/// asm-string-literal '(' expression ')'
1749/// '[' identifier ']' asm-string-literal '(' expression ')'
1750///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001751//
1752// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001753bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
1754 SmallVectorImpl<ExprTy *> &Constraints,
1755 SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001756 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001757 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001758 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001759
1760 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001761 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001762 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001763 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001764
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001765 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001766 Diag(Tok, diag::err_expected_ident);
1767 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001768 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001769 }
Mike Stump11289f42009-09-09 15:08:12 +00001770
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001771 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001772 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001773
Anders Carlsson9a020f92010-01-30 22:25:16 +00001774 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001775 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001776 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001777 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001778
John McCalldadc5752010-08-24 06:29:42 +00001779 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001780 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001781 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001782 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001783 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001784 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001785
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001786 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001787 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001788 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001789 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001790 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001791
Chris Lattner0116c472006-08-15 06:03:28 +00001792 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001793 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001794 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001795 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001796 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001797 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001798 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001799 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001800 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001801 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001802 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001803 ConsumeToken();
1804 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001805
1806 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001807}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001808
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001809Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001810 assert(Tok.is(tok::l_brace));
1811 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001812
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001813 if (PP.isCodeCompletionEnabled()) {
1814 if (trySkippingFunctionBodyForCodeCompletion()) {
1815 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001816 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001817 }
1818 }
1819
John McCallfaf5fb42010-08-26 23:41:50 +00001820 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1821 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001822
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001823 // Do not enter a scope for the brace, as the arguments are in the same scope
1824 // (the function body) as the body itself. Instead, just read the statement
1825 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001826 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001827
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001828 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001829 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001830 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001831 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001832
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001833 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001834 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001835}
Sebastian Redlb219c902008-12-21 16:41:36 +00001836
Sebastian Redla7b98a72009-04-26 20:35:05 +00001837/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1838///
1839/// function-try-block:
1840/// 'try' ctor-initializer[opt] compound-statement handler-seq
1841///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001842Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001843 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1844 SourceLocation TryLoc = ConsumeToken();
1845
John McCallfaf5fb42010-08-26 23:41:50 +00001846 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1847 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001848
1849 // Constructor initializer list?
1850 if (Tok.is(tok::colon))
1851 ParseConstructorInitializer(Decl);
1852
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001853 if (PP.isCodeCompletionEnabled()) {
1854 if (trySkippingFunctionBodyForCodeCompletion()) {
1855 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001856 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001857 }
1858 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001859
Sebastian Redld98ecd62009-04-26 21:08:36 +00001860 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001861 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001862 // If we failed to parse the try-catch, we just give the function an empty
1863 // compound statement as the body.
1864 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001865 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001866 MultiStmtArg(Actions), false);
1867
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001868 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001869 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001870}
1871
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001872bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001873 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001874 assert(PP.isCodeCompletionEnabled() &&
1875 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001876
1877 // We're in code-completion mode. Skip parsing for all function bodies unless
1878 // the body contains the code-completion point.
1879 TentativeParsingAction PA(*this);
1880 ConsumeBrace();
1881 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1882 /*StopAtCodeCompletion=*/true)) {
1883 PA.Commit();
1884 return true;
1885 }
1886
1887 PA.Revert();
1888 return false;
1889}
1890
Sebastian Redlb219c902008-12-21 16:41:36 +00001891/// ParseCXXTryBlock - Parse a C++ try-block.
1892///
1893/// try-block:
1894/// 'try' compound-statement handler-seq
1895///
John McCall53fa7142010-12-24 02:08:15 +00001896StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001897 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001898
Sebastian Redlb219c902008-12-21 16:41:36 +00001899 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1900
1901 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001902 return ParseCXXTryBlockCommon(TryLoc);
1903}
1904
1905/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1906/// function-try-block.
1907///
1908/// try-block:
1909/// 'try' compound-statement handler-seq
1910///
1911/// function-try-block:
1912/// 'try' ctor-initializer[opt] compound-statement handler-seq
1913///
1914/// handler-seq:
1915/// handler handler-seq[opt]
1916///
John Wiegley1c0675e2011-04-28 01:08:34 +00001917/// [Borland] try-block:
1918/// 'try' compound-statement seh-except-block
1919/// 'try' compound-statment seh-finally-block
1920///
John McCalldadc5752010-08-24 06:29:42 +00001921StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001922 if (Tok.isNot(tok::l_brace))
1923 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001924 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001925 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00001926 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
1927 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00001928 if (TryBlock.isInvalid())
1929 return move(TryBlock);
1930
John Wiegley1c0675e2011-04-28 01:08:34 +00001931 // Borland allows SEH-handlers with 'try'
1932 if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
1933 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1934 StmtResult Handler;
1935 if(Tok.is(tok::kw___except)) {
1936 SourceLocation Loc = ConsumeToken();
1937 Handler = ParseSEHExceptBlock(Loc);
1938 }
1939 else {
1940 SourceLocation Loc = ConsumeToken();
1941 Handler = ParseSEHFinallyBlock(Loc);
1942 }
1943 if(Handler.isInvalid())
1944 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00001945
John Wiegley1c0675e2011-04-28 01:08:34 +00001946 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1947 TryLoc,
1948 TryBlock.take(),
1949 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001950 }
John Wiegley1c0675e2011-04-28 01:08:34 +00001951 else {
1952 StmtVector Handlers(Actions);
1953 MaybeParseCXX0XAttributes(attrs);
1954 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00001955
John Wiegley1c0675e2011-04-28 01:08:34 +00001956 if (Tok.isNot(tok::kw_catch))
1957 return StmtError(Diag(Tok, diag::err_expected_catch));
1958 while (Tok.is(tok::kw_catch)) {
1959 StmtResult Handler(ParseCXXCatchBlock());
1960 if (!Handler.isInvalid())
1961 Handlers.push_back(Handler.release());
1962 }
1963 // Don't bother creating the full statement if we don't have any usable
1964 // handlers.
1965 if (Handlers.empty())
1966 return StmtError();
1967
1968 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
1969 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001970}
1971
1972/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1973///
1974/// handler:
1975/// 'catch' '(' exception-declaration ')' compound-statement
1976///
1977/// exception-declaration:
1978/// type-specifier-seq declarator
1979/// type-specifier-seq abstract-declarator
1980/// type-specifier-seq
1981/// '...'
1982///
John McCalldadc5752010-08-24 06:29:42 +00001983StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001984 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1985
1986 SourceLocation CatchLoc = ConsumeToken();
1987
1988 SourceLocation LParenLoc = Tok.getLocation();
1989 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1990 return StmtError();
1991
1992 // C++ 3.3.2p3:
1993 // The name in a catch exception-declaration is local to the handler and
1994 // shall not be redeclared in the outermost block of the handler.
1995 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1996
1997 // exception-declaration is equivalent to '...' or a parameter-declaration
1998 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001999 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002000 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002001 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002002 if (ParseCXXTypeSpecifierSeq(DS))
2003 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002004 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2005 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002006 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002007 } else
2008 ConsumeToken();
2009
2010 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
2011 return StmtError();
2012
2013 if (Tok.isNot(tok::l_brace))
2014 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2015
Alexis Hunt96d5c762009-11-21 08:43:09 +00002016 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002017 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002018 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00002019 if (Block.isInvalid())
2020 return move(Block);
2021
John McCallb268a282010-08-23 23:25:46 +00002022 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002023}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002024
2025void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002026 bool Result;
2027 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002028 return;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002029
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002030 if (Tok.isNot(tok::l_brace)) {
2031 Diag(Tok, diag::err_expected_lbrace);
2032 return;
2033 }
2034 ConsumeBrace();
2035
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002036 // Condition is false skip all inside the {}.
2037 if (!Result) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002038 SkipUntil(tok::r_brace, false);
2039 return;
2040 }
2041
2042 // Condition is true, parse the statements.
2043 while (Tok.isNot(tok::r_brace)) {
2044 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2045 if (R.isUsable())
2046 Stmts.push_back(R.release());
2047 }
2048
2049 if (Tok.isNot(tok::r_brace)) {
2050 Diag(Tok, diag::err_expected_rbrace);
2051 return;
2052 }
2053 ConsumeBrace();
2054}