blob: 28b34fe89ab22e60252d7c4c603f7cc860296a48 [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 Kyrtzidisf7620e42011-04-27 05:04:02 +0000227 SourceLocation LeadingEmptyMacroLoc;
228 if (Tok.hasLeadingEmptyMacro())
229 LeadingEmptyMacroLoc = PP.getLastEmptyMacroInstantiationLoc();
230 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacroLoc);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000231 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000232
Chris Lattner9075bd72006-08-10 04:59:57 +0000233 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall53fa7142010-12-24 02:08:15 +0000234 return ParseIfStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000235 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall53fa7142010-12-24 02:08:15 +0000236 return ParseSwitchStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000237
Chris Lattner9075bd72006-08-10 04:59:57 +0000238 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall53fa7142010-12-24 02:08:15 +0000239 return ParseWhileStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000240 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000241 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000242 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000243 break;
244 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall53fa7142010-12-24 02:08:15 +0000245 return ParseForStatement(attrs);
Chris Lattner503fadc2006-08-10 05:45:44 +0000246
247 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000248 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000249 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000250 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000251 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000252 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000253 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000254 break;
255 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000256 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000257 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000258 break;
259 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000260 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000261 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000262 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000263
Sebastian Redlb219c902008-12-21 16:41:36 +0000264 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000265 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000266 bool msAsm = false;
267 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000268 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000269 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000270 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000271 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000272 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000273
Sebastian Redlb219c902008-12-21 16:41:36 +0000274 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000275 return ParseCXXTryBlock(attrs);
John Wiegley1c0675e2011-04-28 01:08:34 +0000276
277 case tok::kw___try:
278 return ParseSEHTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000279 }
280
Chris Lattner503fadc2006-08-10 05:45:44 +0000281 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000282 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000283 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000284 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000285 // If the result was valid, then we do want to diagnose this. Use
286 // ExpectAndConsume to emit the diagnostic, even though we know it won't
287 // succeed.
288 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000289 // Skip until we see a } or ;, but don't eat it.
290 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000291 }
Mike Stump11289f42009-09-09 15:08:12 +0000292
Sebastian Redl042ad952008-12-11 19:30:53 +0000293 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000294}
295
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000296/// \brief Parse an expression statement.
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000297StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000298 // If a case keyword is missing, this is where it should be inserted.
299 Token OldToken = Tok;
300
301 // FIXME: Use the attributes
302 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000303 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000304 if (Expr.isInvalid()) {
305 // If the expression is invalid, skip ahead to the next semicolon or '}'.
306 // Not doing this opens us up to the possibility of infinite loops if
307 // ParseExpression does not consume any tokens.
308 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
309 if (Tok.is(tok::semi))
310 ConsumeToken();
311 return StmtError();
312 }
313
314 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
315 Actions.CheckCaseExpression(Expr.get())) {
316 // If a constant expression is followed by a colon inside a switch block,
317 // suggest a missing case keyword.
318 Diag(OldToken, diag::err_expected_case_before_expression)
319 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
320
321 // Recover parsing as a case statement.
322 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
323 }
324
325 // Otherwise, eat the semicolon.
326 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
327 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley1c0675e2011-04-28 01:08:34 +0000328}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000329
John Wiegley1c0675e2011-04-28 01:08:34 +0000330StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
331 assert(Tok.is(tok::kw___try) && "Expected '__try'");
332 SourceLocation Loc = ConsumeToken();
333 return ParseSEHTryBlockCommon(Loc);
334}
335
336/// ParseSEHTryBlockCommon
337///
338/// seh-try-block:
339/// '__try' compound-statement seh-handler
340///
341/// seh-handler:
342/// seh-except-block
343/// seh-finally-block
344///
345StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
346 if(Tok.isNot(tok::l_brace))
347 return StmtError(Diag(Tok,diag::err_expected_lbrace));
348
349 ParsedAttributesWithRange attrs(AttrFactory);
350 StmtResult TryBlock(ParseCompoundStatement(attrs));
351 if(TryBlock.isInvalid())
352 return move(TryBlock);
353
354 StmtResult Handler;
355 if(Tok.is(tok::kw___except)) {
356 SourceLocation Loc = ConsumeToken();
357 Handler = ParseSEHExceptBlock(Loc);
358 } else if (Tok.is(tok::kw___finally)) {
359 SourceLocation Loc = ConsumeToken();
360 Handler = ParseSEHFinallyBlock(Loc);
361 } else {
362 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
363 }
364
365 if(Handler.isInvalid())
366 return move(Handler);
367
368 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
369 TryLoc,
370 TryBlock.take(),
371 Handler.take());
372}
373
374/// ParseSEHExceptBlock - Handle __except
375///
376/// seh-except-block:
377/// '__except' '(' seh-filter-expression ')' compound-statement
378///
379StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
380 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
381 raii2(Ident___exception_code, false),
382 raii3(Ident_GetExceptionCode, false);
383
384 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
385 return StmtError();
386
387 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
388
Francois Pichetbfaf4772011-04-28 03:14:31 +0000389 if (getLang().Borland) {
390 Ident__exception_info->setIsPoisoned(false);
391 Ident___exception_info->setIsPoisoned(false);
392 Ident_GetExceptionInfo->setIsPoisoned(false);
393 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000394 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000395
396 if (getLang().Borland) {
397 Ident__exception_info->setIsPoisoned(true);
398 Ident___exception_info->setIsPoisoned(true);
399 Ident_GetExceptionInfo->setIsPoisoned(true);
400 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000401
402 if(FilterExpr.isInvalid())
403 return StmtError();
404
405 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
406 return StmtError();
407
408 ParsedAttributesWithRange attrs(AttrFactory);
409 StmtResult Block(ParseCompoundStatement(attrs));
410
411 if(Block.isInvalid())
412 return move(Block);
413
414 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
415}
416
417/// ParseSEHFinallyBlock - Handle __finally
418///
419/// seh-finally-block:
420/// '__finally' compound-statement
421///
422StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
423 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
424 raii2(Ident___abnormal_termination, false),
425 raii3(Ident_AbnormalTermination, false);
426
427 ParsedAttributesWithRange attrs(AttrFactory);
428 StmtResult Block(ParseCompoundStatement(attrs));
429 if(Block.isInvalid())
430 return move(Block);
431
432 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000433}
434
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000435/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000436///
437/// labeled-statement:
438/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000439/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000440///
John McCall53fa7142010-12-24 02:08:15 +0000441StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000442 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
443 "Not an identifier!");
444
445 Token IdentTok = Tok; // Save the whole token.
446 ConsumeToken(); // eat the identifier.
447
448 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000449
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000450 // identifier ':' statement
451 SourceLocation ColonLoc = ConsumeToken();
452
453 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000454 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000455
John McCalldadc5752010-08-24 06:29:42 +0000456 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000457
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000458 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000459 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000460 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000461
462 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
463 IdentTok.getLocation());
464 if (AttributeList *Attrs = attrs.getList())
465 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
466
467 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
468 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000469}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000470
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000471/// ParseCaseStatement
472/// labeled-statement:
473/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000474/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000475///
Richard Trieu2c850c02011-04-21 21:44:26 +0000476StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
477 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000478 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000479 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000480
Chris Lattner34a22092009-03-04 04:23:07 +0000481 // It is very very common for code to contain many case statements recursively
482 // nested, as in (but usually without indentation):
483 // case 1:
484 // case 2:
485 // case 3:
486 // case 4:
487 // case 5: etc.
488 //
489 // Parsing this naively works, but is both inefficient and can cause us to run
490 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000491 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000492 // but all the grossness is constrained to ParseCaseStatement (and some
493 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattner34a22092009-03-04 04:23:07 +0000495 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
496 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000497 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattner34a22092009-03-04 04:23:07 +0000499 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
500 // gets updated each time a new case is parsed, and whose body is unset so
501 // far. When parsing 'case 4', this is the 'case 3' node.
502 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattner34a22092009-03-04 04:23:07 +0000504 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000505 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000506 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000507 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
508 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000509
Douglas Gregord328d572009-09-21 18:10:23 +0000510 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000511 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000512 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000513 }
514
Chris Lattner125c0ee2009-12-10 00:38:54 +0000515 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
516 /// Disable this form of error recovery while we're parsing the case
517 /// expression.
518 ColonProtectionRAIIObject ColonProtection(*this);
519
Richard Trieu2c850c02011-04-21 21:44:26 +0000520 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
521 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000522 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000523 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000524 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000525 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000526
Chris Lattner34a22092009-03-04 04:23:07 +0000527 // GNU case range extension.
528 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000529 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000530 if (Tok.is(tok::ellipsis)) {
531 Diag(Tok, diag::ext_gnu_case_range);
532 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000533
Chris Lattner34a22092009-03-04 04:23:07 +0000534 RHS = ParseConstantExpression();
535 if (RHS.isInvalid()) {
536 SkipUntil(tok::colon);
537 return StmtError();
538 }
539 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000540
541 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000542
John McCall0140bfe2011-01-22 09:28:32 +0000543 if (Tok.is(tok::colon)) {
544 ColonLoc = ConsumeToken();
545
546 // Treat "case blah;" as a typo for "case blah:".
547 } else if (Tok.is(tok::semi)) {
548 ColonLoc = ConsumeToken();
549 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
550 << FixItHint::CreateReplacement(ColonLoc, ":");
551 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000552 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
553 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
554 << FixItHint::CreateInsertion(ExpectedLoc, ":");
555 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000556 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000557
John McCalldadc5752010-08-24 06:29:42 +0000558 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000559 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
560 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000561
Chris Lattner34a22092009-03-04 04:23:07 +0000562 // If we had a sema error parsing this case, then just ignore it and
563 // continue parsing the sub-stmt.
564 if (Case.isInvalid()) {
565 if (TopLevelCase.isInvalid()) // No parsed case stmts.
566 return ParseStatement();
567 // Otherwise, just don't add it as a nested case.
568 } else {
569 // If this is the first case statement we parsed, it becomes TopLevelCase.
570 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000571 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000572 if (TopLevelCase.isInvalid())
573 TopLevelCase = move(Case);
574 else
John McCallb268a282010-08-23 23:25:46 +0000575 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000576 DeepestParsedCaseStmt = NextDeepest;
577 }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner34a22092009-03-04 04:23:07 +0000579 // Handle all case statements.
580 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000581
Chris Lattner34a22092009-03-04 04:23:07 +0000582 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000583
Chris Lattner34a22092009-03-04 04:23:07 +0000584 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000585 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattner34a22092009-03-04 04:23:07 +0000587 if (Tok.isNot(tok::r_brace)) {
588 SubStmt = ParseStatement();
589 } else {
590 // Nicely diagnose the common error "switch (X) { case 4: }", which is
591 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000592 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
593 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000594 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattner34a22092009-03-04 04:23:07 +0000597 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000598 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000599 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000600
Chris Lattner34a22092009-03-04 04:23:07 +0000601 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000602 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000603
Chris Lattner34a22092009-03-04 04:23:07 +0000604 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000605 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000606}
607
608/// ParseDefaultStatement
609/// labeled-statement:
610/// 'default' ':' statement
611/// Note that this does not parse the 'statement' at the end.
612///
John McCall53fa7142010-12-24 02:08:15 +0000613StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000614 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000615
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000616 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000617 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000618
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000619 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000620 if (Tok.is(tok::colon)) {
621 ColonLoc = ConsumeToken();
622
623 // Treat "default;" as a typo for "default:".
624 } else if (Tok.is(tok::semi)) {
625 ColonLoc = ConsumeToken();
626 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
627 << FixItHint::CreateReplacement(ColonLoc, ":");
628 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000629 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
630 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
631 << FixItHint::CreateInsertion(ExpectedLoc, ":");
632 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000633 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000634
Chris Lattner30f910e2006-10-16 05:52:41 +0000635 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000636 if (Tok.is(tok::r_brace)) {
David Majnemerc6a99872011-06-14 15:24:38 +0000637 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
638 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000639 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000640 }
641
John McCalldadc5752010-08-24 06:29:42 +0000642 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000643 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000644 return StmtError();
645
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000646 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000647 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000648}
649
650
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000651/// ParseCompoundStatement - Parse a "{}" block.
652///
653/// compound-statement: [C99 6.8.2]
654/// { block-item-list[opt] }
655/// [GNU] { label-declarations block-item-list } [TODO]
656///
657/// block-item-list:
658/// block-item
659/// block-item-list block-item
660///
661/// block-item:
662/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000663/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000664/// statement
665/// [OMP] openmp-directive [TODO]
666///
667/// [GNU] label-declarations:
668/// [GNU] label-declaration
669/// [GNU] label-declarations label-declaration
670///
671/// [GNU] label-declaration:
672/// [GNU] '__label__' identifier-list ';'
673///
674/// [OMP] openmp-directive: [TODO]
675/// [OMP] barrier-directive
676/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000677///
John McCall53fa7142010-12-24 02:08:15 +0000678StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000679 bool isStmtExpr) {
680 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000681
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000682 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000683
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000684 // Enter a scope to hold everything within the compound stmt. Compound
685 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000686 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000687
688 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000689 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000690}
691
692
693/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000694/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000695/// consume the '}' at the end of the block. It does not manipulate the scope
696/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000697StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000698 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000699 Tok.getLocation(),
700 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000701 InMessageExpressionRAIIObject InMessage(*this, false);
702
Chris Lattnerf2978802007-01-21 06:52:16 +0000703 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
704
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000705 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000706
Chris Lattner43e7f312011-02-18 02:08:43 +0000707 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
708 // only allowed at the start of a compound stmt regardless of the language.
709 while (Tok.is(tok::kw___label__)) {
710 SourceLocation LabelLoc = ConsumeToken();
711 Diag(LabelLoc, diag::ext_gnu_local_label);
712
713 llvm::SmallVector<Decl *, 8> DeclsInGroup;
714 while (1) {
715 if (Tok.isNot(tok::identifier)) {
716 Diag(Tok, diag::err_expected_ident);
717 break;
718 }
719
720 IdentifierInfo *II = Tok.getIdentifierInfo();
721 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000722 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner43e7f312011-02-18 02:08:43 +0000723
724 if (!Tok.is(tok::comma))
725 break;
726 ConsumeToken();
727 }
728
John McCall084e83d2011-03-24 11:26:52 +0000729 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000730 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
731 DeclsInGroup.data(), DeclsInGroup.size());
732 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
733
734 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
735 if (R.isUsable())
736 Stmts.push_back(R.release());
737 }
738
739 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000740 if (Tok.is(tok::annot_pragma_unused)) {
741 HandlePragmaUnused();
742 continue;
743 }
744
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000745 if (getLang().Microsoft && (Tok.is(tok::kw___if_exists) ||
746 Tok.is(tok::kw___if_not_exists))) {
747 ParseMicrosoftIfExistsStatement(Stmts);
748 continue;
749 }
750
John McCalldadc5752010-08-24 06:29:42 +0000751 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000752 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000753 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000754 } else {
755 // __extension__ can start declarations and it can also be a unary
756 // operator for expressions. Consume multiple __extension__ markers here
757 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000758 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000759 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000760 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000761 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000762
John McCall084e83d2011-03-24 11:26:52 +0000763 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000764 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000765
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000766 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000767 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000768 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000769 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000770 ExtensionRAIIObject O(Diags);
771
Chris Lattner49836b42009-04-02 04:16:50 +0000772 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000773 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
774 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000775 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000776 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000777 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000778 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000779 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000780
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000781 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000782 SkipUntil(tok::semi);
783 continue;
784 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000785
Alexis Hunt96d5c762009-11-21 08:43:09 +0000786 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000787 // Eat the semicolon at the end of stmt and convert the expr into a
788 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000789 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000790 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000791 }
792 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000793
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000794 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000795 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000796 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000797
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000798 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000799 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000800 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000801 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000802 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000803 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000804
Chris Lattner04132372006-10-16 06:12:55 +0000805 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000806 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000807 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000808}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000809
Chris Lattnerc0081db2008-12-12 06:31:07 +0000810/// ParseParenExprOrCondition:
811/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000812/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000813///
814/// This function parses and performs error recovery on the specified condition
815/// or expression (depending on whether we're in C++ or C mode). This function
816/// goes out of its way to recover well. It returns true if there was a parser
817/// error (the right paren couldn't be found), which indicates that the caller
818/// should try to recover harder. It returns false if the condition is
819/// successfully parsed. Note that a successful parse can still have semantic
820/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000821bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000822 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000823 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000824 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000825 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000826 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000827 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000828 else {
829 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000830 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000831
832 // If required, convert to a boolean value.
833 if (!ExprResult.isInvalid() && ConvertToBoolean)
834 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000835 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000836 }
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattnerc0081db2008-12-12 06:31:07 +0000838 // If the parser was confused by the condition and we don't have a ')', try to
839 // recover by skipping ahead to a semi and bailing out. If condexp is
840 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000841 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000842 SkipUntil(tok::semi);
843 // Skipping may have stopped if it found the containing ')'. If so, we can
844 // continue parsing the if statement.
845 if (Tok.isNot(tok::r_paren))
846 return true;
847 }
Mike Stump11289f42009-09-09 15:08:12 +0000848
Chris Lattnerc0081db2008-12-12 06:31:07 +0000849 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000850 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000851 return false;
852}
853
854
Chris Lattnerc951dae2006-08-10 04:23:57 +0000855/// ParseIfStatement
856/// if-statement: [C99 6.8.4.1]
857/// 'if' '(' expression ')' statement
858/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000859/// [C++] 'if' '(' condition ')' statement
860/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000861///
John McCall53fa7142010-12-24 02:08:15 +0000862StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000863 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000864
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000865 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000866 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000867
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000868 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000869 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000870 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000871 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000872 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000873
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000874 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
875
Chris Lattner2dd1b722007-08-26 23:08:06 +0000876 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
877 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000878 //
879 // C++ 6.4p3:
880 // A name introduced by a declaration in a condition is in scope from its
881 // point of declaration until the end of the substatements controlled by the
882 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000883 // C++ 3.3.2p4:
884 // Names declared in the for-init-statement, and in the condition of if,
885 // while, for, and switch statements are local to the if, while, for, or
886 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000887 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000888 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000889
Chris Lattnerc951dae2006-08-10 04:23:57 +0000890 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000891 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000892 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000893 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000894 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000895
John McCallb268a282010-08-23 23:25:46 +0000896 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000897
Chris Lattner8fb26252007-08-22 05:28:50 +0000898 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000899 // there is no compound stmt. C90 does not have this clause. We only do this
900 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000901 //
902 // C++ 6.4p1:
903 // The substatement in a selection-statement (each substatement, in the else
904 // form of the if statement) implicitly defines a local scope.
905 //
906 // For C++ we create a scope for the condition and a new scope for
907 // substatements because:
908 // -When the 'then' scope exits, we want the condition declaration to still be
909 // active for the 'else' scope too.
910 // -Sema will detect name clashes by considering declarations of a
911 // 'ControlScope' as part of its direct subscope.
912 // -If we wanted the condition and substatement to be in the same scope, we
913 // would have to notify ParseStatement not to create a new scope. It's
914 // simpler to let it create a new scope.
915 //
Mike Stump11289f42009-09-09 15:08:12 +0000916 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000917 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000918
Chris Lattner5c5808a2007-10-29 05:08:52 +0000919 // Read the 'then' stmt.
920 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000921 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000922
Chris Lattner37e54f42007-08-22 05:16:28 +0000923 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000924 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000925
Chris Lattnerc951dae2006-08-10 04:23:57 +0000926 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000927 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000928 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000929 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000930
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000931 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000932 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000933 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000934
Chris Lattner8fb26252007-08-22 05:28:50 +0000935 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000936 // there is no compound stmt. C90 does not have this clause. We only do
937 // this if the body isn't a compound statement to avoid push/pop in common
938 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000939 //
940 // C++ 6.4p1:
941 // The substatement in a selection-statement (each substatement, in the else
942 // form of the if statement) implicitly defines a local scope.
943 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000944 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000945 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000946
Chris Lattner30f910e2006-10-16 05:52:41 +0000947 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000948
Chris Lattner37e54f42007-08-22 05:16:28 +0000949 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000950 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000951 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000952
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000953 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000954
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000955 // If the condition was invalid, discard the if statement. We could recover
956 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000957 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000958 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000959
Chris Lattner5c5808a2007-10-29 05:08:52 +0000960 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000961 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000962 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000963 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
964 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
965 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000966 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000967 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000968 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000969
Chris Lattner5c5808a2007-10-29 05:08:52 +0000970 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000971 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000972 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000973 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000974 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000975
John McCallb268a282010-08-23 23:25:46 +0000976 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000977 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000978}
979
Chris Lattner9075bd72006-08-10 04:59:57 +0000980/// ParseSwitchStatement
981/// switch-statement:
982/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000983/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000984StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000985 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000986
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000987 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000988 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000989
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000990 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000991 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000992 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000993 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000994 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000995
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000996 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
997
Chris Lattner2dd1b722007-08-26 23:08:06 +0000998 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
999 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001000 //
1001 // C++ 6.4p3:
1002 // A name introduced by a declaration in a condition is in scope from its
1003 // point of declaration until the end of the substatements controlled by the
1004 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001005 // C++ 3.3.2p4:
1006 // Names declared in the for-init-statement, and in the condition of if,
1007 // while, for, and switch statements are local to the if, while, for, or
1008 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001009 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001010 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001011 if (C99orCXX)
1012 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001013 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001014
Chris Lattner9075bd72006-08-10 04:59:57 +00001015 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001016 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001017 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001018 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001019 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001020
John McCalldadc5752010-08-24 06:29:42 +00001021 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001022 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001023
Douglas Gregore60e41a2010-05-06 17:25:47 +00001024 if (Switch.isInvalid()) {
1025 // Skip the switch body.
1026 // FIXME: This is not optimal recovery, but parsing the body is more
1027 // dangerous due to the presence of case and default statements, which
1028 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001029 if (Tok.is(tok::l_brace)) {
1030 ConsumeBrace();
1031 SkipUntil(tok::r_brace, false, false);
1032 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001033 SkipUntil(tok::semi);
1034 return move(Switch);
1035 }
1036
Chris Lattner8fb26252007-08-22 05:28:50 +00001037 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001038 // there is no compound stmt. C90 does not have this clause. We only do this
1039 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001040 //
1041 // C++ 6.4p1:
1042 // The substatement in a selection-statement (each substatement, in the else
1043 // form of the if statement) implicitly defines a local scope.
1044 //
1045 // See comments in ParseIfStatement for why we create a scope for the
1046 // condition and a new scope for substatement in C++.
1047 //
Mike Stump11289f42009-09-09 15:08:12 +00001048 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001049 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001050
Chris Lattner9075bd72006-08-10 04:59:57 +00001051 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001052 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001053
Chris Lattner8fd2d012010-01-24 01:50:29 +00001054 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001055 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001056 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001057
Chris Lattner8fd2d012010-01-24 01:50:29 +00001058 if (Body.isInvalid())
1059 // FIXME: Remove the case statement list from the Switch statement.
1060 Body = Actions.ActOnNullStmt(Tok.getLocation());
1061
John McCallb268a282010-08-23 23:25:46 +00001062 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001063}
1064
1065/// ParseWhileStatement
1066/// while-statement: [C99 6.8.5.1]
1067/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001068/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +00001069StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001070 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001071
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001072 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001073 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001074 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001075
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001076 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001077 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001078 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001079 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001080 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001081
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001082 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1083
Chris Lattner2dd1b722007-08-26 23:08:06 +00001084 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1085 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001086 //
1087 // C++ 6.4p3:
1088 // A name introduced by a declaration in a condition is in scope from its
1089 // point of declaration until the end of the substatements controlled by the
1090 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001091 // C++ 3.3.2p4:
1092 // Names declared in the for-init-statement, and in the condition of if,
1093 // while, for, and switch statements are local to the if, while, for, or
1094 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001095 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001096 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001097 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001098 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1099 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001100 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001101 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1102 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001103
Chris Lattner9075bd72006-08-10 04:59:57 +00001104 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001105 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001106 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001107 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001108 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001109
John McCallb268a282010-08-23 23:25:46 +00001110 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattner8fb26252007-08-22 05:28:50 +00001112 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001113 // there is no compound stmt. C90 does not have this clause. We only do this
1114 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001115 //
1116 // C++ 6.5p2:
1117 // The substatement in an iteration-statement implicitly defines a local scope
1118 // which is entered and exited each time through the loop.
1119 //
1120 // See comments in ParseIfStatement for why we create a scope for the
1121 // condition and a new scope for substatement in C++.
1122 //
Mike Stump11289f42009-09-09 15:08:12 +00001123 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001124 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001125
Chris Lattner9075bd72006-08-10 04:59:57 +00001126 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001127 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001128
Chris Lattner8fb26252007-08-22 05:28:50 +00001129 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001130 InnerScope.Exit();
1131 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001132
John McCall48871652010-08-21 09:40:31 +00001133 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001134 return StmtError();
1135
John McCallb268a282010-08-23 23:25:46 +00001136 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001137}
1138
1139/// ParseDoStatement
1140/// do-statement: [C99 6.8.5.2]
1141/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001142/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001143StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001144 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001145
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001146 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001147 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001148
Chris Lattner2dd1b722007-08-26 23:08:06 +00001149 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1150 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001151 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001152 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001153 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001154 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001155 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001156
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001157 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001158
Chris Lattner8fb26252007-08-22 05:28:50 +00001159 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001160 // there is no compound stmt. C90 does not have this clause. We only do this
1161 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001162 //
1163 // C++ 6.5p2:
1164 // The substatement in an iteration-statement implicitly defines a local scope
1165 // which is entered and exited each time through the loop.
1166 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001167 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001168 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001169 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001170
Chris Lattner9075bd72006-08-10 04:59:57 +00001171 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001172 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001173
Chris Lattner8fb26252007-08-22 05:28:50 +00001174 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001175 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001176
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001177 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001178 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001179 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001180 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001181 SkipUntil(tok::semi, false, true);
1182 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001183 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001184 }
Chris Lattneraf635312006-10-16 06:06:51 +00001185 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001186
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001187 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001188 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001189 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001190 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001191 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001192
Chris Lattner10da53c2008-12-12 06:35:28 +00001193 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +00001194 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001195 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +00001196 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001197 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001198
Sebastian Redlb62406f2008-12-11 19:48:14 +00001199 if (Cond.isInvalid() || Body.isInvalid())
1200 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001201
John McCallb268a282010-08-23 23:25:46 +00001202 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1203 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +00001204}
1205
1206/// ParseForStatement
1207/// for-statement: [C99 6.8.5.3]
1208/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1209/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001210/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1211/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001212/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001213/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1214/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001215///
1216/// [C++] for-init-statement:
1217/// [C++] expression-statement
1218/// [C++] simple-declaration
1219///
Richard Smith02e85f32011-04-14 22:09:26 +00001220/// [C++0x] for-range-declaration:
1221/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1222/// [C++0x] for-range-initializer:
1223/// [C++0x] expression
1224/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001225StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001226 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001227
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001228 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001229 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001230
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001231 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001232 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001233 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001234 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001235 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001236
Chris Lattner934074c2009-04-22 00:54:41 +00001237 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001238
Chris Lattner2dd1b722007-08-26 23:08:06 +00001239 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1240 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001241 //
1242 // C++ 6.4p3:
1243 // A name introduced by a declaration in a condition is in scope from its
1244 // point of declaration until the end of the substatements controlled by the
1245 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001246 // C++ 3.3.2p4:
1247 // Names declared in the for-init-statement, and in the condition of if,
1248 // while, for, and switch statements are local to the if, while, for, or
1249 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001250 // C++ 6.5.3p1:
1251 // Names declared in the for-init-statement are in the same declarative-region
1252 // as those declared in the condition.
1253 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001254 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001255 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001256 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1257 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001258 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001259 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1260
1261 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001262
Chris Lattner04132372006-10-16 06:12:55 +00001263 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001264 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001265
Richard Smith02e85f32011-04-14 22:09:26 +00001266 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001267 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001268 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001269 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001270 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001271 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001272 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001273 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001274
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001275 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001276 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001277 C99orCXXorObjC? Sema::PCC_ForInit
1278 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001279 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001280 }
1281
Chris Lattner9075bd72006-08-10 04:59:57 +00001282 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001283 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001284 // no first part, eat the ';'.
1285 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001286 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001287 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001288 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001289 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001290
John McCall084e83d2011-03-24 11:26:52 +00001291 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001292 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001293
Richard Smith02e85f32011-04-14 22:09:26 +00001294 // In C++0x, "for (T NS:a" might not be a typo for ::
1295 bool MightBeForRangeStmt = getLang().CPlusPlus;
1296 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1297
Chris Lattner49836b42009-04-02 04:16:50 +00001298 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001299 StmtVector Stmts(Actions);
1300 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001301 DeclEnd, attrs, false,
1302 MightBeForRangeStmt ?
1303 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001304 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001305
Richard Smith02e85f32011-04-14 22:09:26 +00001306 if (ForRangeInit.ParsedForRangeDecl()) {
1307 ForRange = true;
1308 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001309 ConsumeToken();
1310 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001311 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001312 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001313 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001314
1315 if (Tok.is(tok::code_completion)) {
1316 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1317 ConsumeCodeCompletionToken();
1318 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001319 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001320 } else {
1321 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001322 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001323 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001324 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001325
John McCall34376a62010-12-04 03:47:34 +00001326 ForEach = isTokIdentifier_in();
1327
Chris Lattnercd68f642007-06-27 01:06:29 +00001328 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001329 if (!Value.isInvalid()) {
1330 if (ForEach)
1331 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1332 else
1333 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1334 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001335
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001336 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001337 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001338 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001339 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001340
1341 if (Tok.is(tok::code_completion)) {
1342 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1343 ConsumeCodeCompletionToken();
1344 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001345 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001346 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001347 if (!Value.isInvalid()) {
1348 Diag(Tok, diag::err_expected_semi_for);
1349 } else {
1350 // Skip until semicolon or rparen, don't consume it.
1351 SkipUntil(tok::r_paren, true, true);
1352 if (Tok.is(tok::semi))
1353 ConsumeToken();
1354 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001355 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001356 }
Richard Smith02e85f32011-04-14 22:09:26 +00001357 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001358 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001359 // Parse the second part of the for specifier.
1360 if (Tok.is(tok::semi)) { // for (...;;
1361 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001362 } else if (Tok.is(tok::r_paren)) {
1363 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001364 } else {
John McCalldadc5752010-08-24 06:29:42 +00001365 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001366 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001367 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1368 else {
1369 Second = ParseExpression();
1370 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001371 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001372 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001373 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001374 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001375 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001376 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001377
Douglas Gregor230a7e62011-02-17 03:38:46 +00001378 if (Tok.isNot(tok::semi)) {
1379 if (!SecondPartIsInvalid || SecondVar)
1380 Diag(Tok, diag::err_expected_semi_for);
1381 else
1382 // Skip until semicolon or rparen, don't consume it.
1383 SkipUntil(tok::r_paren, true, true);
1384 }
1385
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001386 if (Tok.is(tok::semi)) {
1387 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001388 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001389
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001390 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001391 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001392 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001393 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001394 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001395 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001396 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001397 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001398
Richard Smith02e85f32011-04-14 22:09:26 +00001399 // We need to perform most of the semantic analysis for a C++0x for-range
1400 // statememt before parsing the body, in order to be able to deduce the type
1401 // of an auto-typed loop variable.
1402 StmtResult ForRangeStmt;
1403 if (ForRange)
1404 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1405 FirstPart.take(),
1406 ForRangeInit.ColonLoc,
1407 ForRangeInit.RangeExpr.get(),
1408 RParenLoc);
1409
Chris Lattner8fb26252007-08-22 05:28:50 +00001410 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001411 // there is no compound stmt. C90 does not have this clause. We only do this
1412 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001413 //
1414 // C++ 6.5p2:
1415 // The substatement in an iteration-statement implicitly defines a local scope
1416 // which is entered and exited each time through the loop.
1417 //
1418 // See comments in ParseIfStatement for why we create a scope for
1419 // for-init-statement/condition and a new scope for substatement in C++.
1420 //
Mike Stump11289f42009-09-09 15:08:12 +00001421 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001422 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001423
Chris Lattner9075bd72006-08-10 04:59:57 +00001424 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001425 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001426
Chris Lattner8fb26252007-08-22 05:28:50 +00001427 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001428 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001429
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001430 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001431 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001432
1433 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001434 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001435
Richard Smith02e85f32011-04-14 22:09:26 +00001436 if (ForEach)
1437 // FIXME: It isn't clear how to communicate the late destruction of
1438 // C++ temporaries used to create the collection.
1439 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1440 FirstPart.take(),
1441 Collection.take(), RParenLoc,
1442 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001443
Richard Smith02e85f32011-04-14 22:09:26 +00001444 if (ForRange)
1445 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1446
1447 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1448 SecondVar, ThirdPart, RParenLoc, Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001449}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001450
Chris Lattner503fadc2006-08-10 05:45:44 +00001451/// ParseGotoStatement
1452/// jump-statement:
1453/// 'goto' identifier ';'
1454/// [GNU] 'goto' '*' expression ';'
1455///
1456/// Note: this lets the caller parse the end ';'.
1457///
John McCall53fa7142010-12-24 02:08:15 +00001458StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001459 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001460
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001461 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001462 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001463
John McCalldadc5752010-08-24 06:29:42 +00001464 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001465 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001466 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1467 Tok.getLocation());
1468 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001469 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001470 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001471 // GNU indirect goto extension.
1472 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001473 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001474 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001475 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001476 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001477 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001478 }
John McCallb268a282010-08-23 23:25:46 +00001479 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001480 } else {
1481 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001482 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001483 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001484
Sebastian Redlb62406f2008-12-11 19:48:14 +00001485 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001486}
1487
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001488/// ParseContinueStatement
1489/// jump-statement:
1490/// 'continue' ';'
1491///
1492/// Note: this lets the caller parse the end ';'.
1493///
John McCall53fa7142010-12-24 02:08:15 +00001494StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001495 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001496
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001497 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001498 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001499}
1500
1501/// ParseBreakStatement
1502/// jump-statement:
1503/// 'break' ';'
1504///
1505/// Note: this lets the caller parse the end ';'.
1506///
John McCall53fa7142010-12-24 02:08:15 +00001507StmtResult Parser::ParseBreakStatement(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 BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001511 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001512}
1513
Chris Lattner503fadc2006-08-10 05:45:44 +00001514/// ParseReturnStatement
1515/// jump-statement:
1516/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001517StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001518 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001519
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001520 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001521 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001522
John McCalldadc5752010-08-24 06:29:42 +00001523 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001524 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001525 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001526 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001527 ConsumeCodeCompletionToken();
1528 SkipUntil(tok::semi, false, true);
1529 return StmtError();
1530 }
1531
Douglas Gregore9e27d92011-03-11 23:10:44 +00001532 // FIXME: This is a hack to allow something like C++0x's generalized
1533 // initializer lists, but only enough of this feature to allow Clang to
1534 // parse libstdc++ 4.5's headers.
1535 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1536 R = ParseInitializer();
1537 if (R.isUsable() && !getLang().CPlusPlus0x)
1538 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1539 << R.get()->getSourceRange();
1540 } else
1541 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001542 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001543 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001544 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001545 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001546 }
John McCallb268a282010-08-23 23:25:46 +00001547 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001548}
Chris Lattner0116c472006-08-15 06:03:28 +00001549
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001550/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1551/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001552StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1553 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001554 if (Tok.is(tok::l_brace)) {
1555 unsigned short savedBraceCount = BraceCount;
1556 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001557 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001558 ConsumeAnyToken();
1559 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001560 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001561 // From the MS website: If used without braces, the __asm keyword means
1562 // that the rest of the line is an assembly-language statement.
1563 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001564 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001565 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001566 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001567 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001568 ConsumeAnyToken();
1569 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001570 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1571 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001572 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001573 }
Mike Stump6da5d752009-12-11 00:04:56 +00001574 Token t;
1575 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001576 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001577 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001578 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001579 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001580 ExprVector Constraints(Actions);
1581 ExprVector Exprs(Actions);
1582 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001583 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001584 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001585 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001586 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001587}
1588
Chris Lattner0116c472006-08-15 06:03:28 +00001589/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001590/// asm-statement:
1591/// gnu-asm-statement
1592/// ms-asm-statement
1593///
1594/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001595/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1596///
1597/// [GNU] asm-argument:
1598/// asm-string-literal
1599/// asm-string-literal ':' asm-operands[opt]
1600/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1601/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1602/// ':' asm-clobbers
1603///
1604/// [GNU] asm-clobbers:
1605/// asm-string-literal
1606/// asm-clobbers ',' asm-string-literal
1607///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001608/// [MS] ms-asm-statement:
1609/// '__asm' assembly-instruction ';'[opt]
1610/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1611///
1612/// [MS] assembly-instruction-list:
1613/// assembly-instruction ';'[opt]
1614/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1615///
John McCalldadc5752010-08-24 06:29:42 +00001616StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001617 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001618 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001619
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001620 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001621 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001622 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001623 }
John McCall084e83d2011-03-24 11:26:52 +00001624 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001625 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001626 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001627
Chris Lattner0116c472006-08-15 06:03:28 +00001628 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001629 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001630 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001631 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001632 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001633
Chris Lattner0116c472006-08-15 06:03:28 +00001634 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001635 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001636 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001637 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001638 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001639 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001640 }
Chris Lattner04132372006-10-16 06:12:55 +00001641 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001642
John McCalldadc5752010-08-24 06:29:42 +00001643 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001644 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001645 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001646
Anders Carlsson9a020f92010-01-30 22:25:16 +00001647 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001648 ExprVector Constraints(Actions);
1649 ExprVector Exprs(Actions);
1650 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001651
Anders Carlsson19fe1162008-02-05 23:03:50 +00001652 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001653 // We have a simple asm expression like 'asm("foo")'.
1654 SourceLocation RParenLoc = ConsumeParen();
1655 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1656 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1657 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001658 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001659 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001660 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001661
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001662 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001663 bool AteExtraColon = false;
1664 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1665 // In C++ mode, parse "::" like ": :".
1666 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001667 ConsumeToken();
1668
Chris Lattner15768502009-12-20 23:08:04 +00001669 if (!AteExtraColon &&
1670 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001671 return StmtError();
1672 }
Chris Lattner15768502009-12-20 23:08:04 +00001673
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001674 unsigned NumOutputs = Names.size();
1675
1676 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001677 if (AteExtraColon ||
1678 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1679 // In C++ mode, parse "::" like ": :".
1680 if (AteExtraColon)
1681 AteExtraColon = false;
1682 else {
1683 AteExtraColon = Tok.is(tok::coloncolon);
1684 ConsumeToken();
1685 }
1686
1687 if (!AteExtraColon &&
1688 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001689 return StmtError();
1690 }
1691
1692 assert(Names.size() == Constraints.size() &&
1693 Constraints.size() == Exprs.size() &&
1694 "Input operand size mismatch!");
1695
1696 unsigned NumInputs = Names.size() - NumOutputs;
1697
1698 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001699 if (AteExtraColon || Tok.is(tok::colon)) {
1700 if (!AteExtraColon)
1701 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001702
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001703 // Parse the asm-string list for clobbers if present.
1704 if (Tok.isNot(tok::r_paren)) {
1705 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001706 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001707
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001708 if (Clobber.isInvalid())
1709 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001710
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001711 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001712
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001713 if (Tok.isNot(tok::comma)) break;
1714 ConsumeToken();
1715 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001716 }
1717 }
1718
1719 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1720 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001721 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001722 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001723 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001724 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001725}
1726
1727/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001728/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001729///
1730/// [GNU] asm-operands:
1731/// asm-operand
1732/// asm-operands ',' asm-operand
1733///
1734/// [GNU] asm-operand:
1735/// asm-string-literal '(' expression ')'
1736/// '[' identifier ']' asm-string-literal '(' expression ')'
1737///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001738//
1739// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001740bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1741 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1742 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001743 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001744 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001745 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001746
1747 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001748 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001749 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001750 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001751
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001752 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001753 Diag(Tok, diag::err_expected_ident);
1754 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001755 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001756 }
Mike Stump11289f42009-09-09 15:08:12 +00001757
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001758 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001759 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001760
Anders Carlsson9a020f92010-01-30 22:25:16 +00001761 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001762 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001763 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001764 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001765
John McCalldadc5752010-08-24 06:29:42 +00001766 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001767 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001768 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001769 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001770 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001771 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001772
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001773 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001774 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001775 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001776 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001777 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001778
Chris Lattner0116c472006-08-15 06:03:28 +00001779 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001780 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001781 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001782 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001783 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001784 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001785 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001786 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001787 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001788 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001789 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001790 ConsumeToken();
1791 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001792
1793 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001794}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001795
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001796Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001797 assert(Tok.is(tok::l_brace));
1798 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001799
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001800 if (PP.isCodeCompletionEnabled()) {
1801 if (trySkippingFunctionBodyForCodeCompletion()) {
1802 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001803 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001804 }
1805 }
1806
John McCallfaf5fb42010-08-26 23:41:50 +00001807 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1808 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001809
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001810 // Do not enter a scope for the brace, as the arguments are in the same scope
1811 // (the function body) as the body itself. Instead, just read the statement
1812 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001813 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001814
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001815 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001816 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001817 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001818 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001819
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001820 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001821 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001822}
Sebastian Redlb219c902008-12-21 16:41:36 +00001823
Sebastian Redla7b98a72009-04-26 20:35:05 +00001824/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1825///
1826/// function-try-block:
1827/// 'try' ctor-initializer[opt] compound-statement handler-seq
1828///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001829Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001830 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1831 SourceLocation TryLoc = ConsumeToken();
1832
John McCallfaf5fb42010-08-26 23:41:50 +00001833 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1834 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001835
1836 // Constructor initializer list?
1837 if (Tok.is(tok::colon))
1838 ParseConstructorInitializer(Decl);
1839
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001840 if (PP.isCodeCompletionEnabled()) {
1841 if (trySkippingFunctionBodyForCodeCompletion()) {
1842 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001843 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001844 }
1845 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001846
Sebastian Redld98ecd62009-04-26 21:08:36 +00001847 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001848 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001849 // If we failed to parse the try-catch, we just give the function an empty
1850 // compound statement as the body.
1851 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001852 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001853 MultiStmtArg(Actions), false);
1854
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001855 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001856 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001857}
1858
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001859bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001860 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001861 assert(PP.isCodeCompletionEnabled() &&
1862 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001863
1864 // We're in code-completion mode. Skip parsing for all function bodies unless
1865 // the body contains the code-completion point.
1866 TentativeParsingAction PA(*this);
1867 ConsumeBrace();
1868 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1869 /*StopAtCodeCompletion=*/true)) {
1870 PA.Commit();
1871 return true;
1872 }
1873
1874 PA.Revert();
1875 return false;
1876}
1877
Sebastian Redlb219c902008-12-21 16:41:36 +00001878/// ParseCXXTryBlock - Parse a C++ try-block.
1879///
1880/// try-block:
1881/// 'try' compound-statement handler-seq
1882///
John McCall53fa7142010-12-24 02:08:15 +00001883StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001884 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001885
Sebastian Redlb219c902008-12-21 16:41:36 +00001886 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1887
1888 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001889 return ParseCXXTryBlockCommon(TryLoc);
1890}
1891
1892/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1893/// function-try-block.
1894///
1895/// try-block:
1896/// 'try' compound-statement handler-seq
1897///
1898/// function-try-block:
1899/// 'try' ctor-initializer[opt] compound-statement handler-seq
1900///
1901/// handler-seq:
1902/// handler handler-seq[opt]
1903///
John Wiegley1c0675e2011-04-28 01:08:34 +00001904/// [Borland] try-block:
1905/// 'try' compound-statement seh-except-block
1906/// 'try' compound-statment seh-finally-block
1907///
John McCalldadc5752010-08-24 06:29:42 +00001908StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001909 if (Tok.isNot(tok::l_brace))
1910 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001911 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001912 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001913 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001914 if (TryBlock.isInvalid())
1915 return move(TryBlock);
1916
John Wiegley1c0675e2011-04-28 01:08:34 +00001917 // Borland allows SEH-handlers with 'try'
1918 if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
1919 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1920 StmtResult Handler;
1921 if(Tok.is(tok::kw___except)) {
1922 SourceLocation Loc = ConsumeToken();
1923 Handler = ParseSEHExceptBlock(Loc);
1924 }
1925 else {
1926 SourceLocation Loc = ConsumeToken();
1927 Handler = ParseSEHFinallyBlock(Loc);
1928 }
1929 if(Handler.isInvalid())
1930 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00001931
John Wiegley1c0675e2011-04-28 01:08:34 +00001932 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1933 TryLoc,
1934 TryBlock.take(),
1935 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001936 }
John Wiegley1c0675e2011-04-28 01:08:34 +00001937 else {
1938 StmtVector Handlers(Actions);
1939 MaybeParseCXX0XAttributes(attrs);
1940 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00001941
John Wiegley1c0675e2011-04-28 01:08:34 +00001942 if (Tok.isNot(tok::kw_catch))
1943 return StmtError(Diag(Tok, diag::err_expected_catch));
1944 while (Tok.is(tok::kw_catch)) {
1945 StmtResult Handler(ParseCXXCatchBlock());
1946 if (!Handler.isInvalid())
1947 Handlers.push_back(Handler.release());
1948 }
1949 // Don't bother creating the full statement if we don't have any usable
1950 // handlers.
1951 if (Handlers.empty())
1952 return StmtError();
1953
1954 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
1955 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001956}
1957
1958/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1959///
1960/// handler:
1961/// 'catch' '(' exception-declaration ')' compound-statement
1962///
1963/// exception-declaration:
1964/// type-specifier-seq declarator
1965/// type-specifier-seq abstract-declarator
1966/// type-specifier-seq
1967/// '...'
1968///
John McCalldadc5752010-08-24 06:29:42 +00001969StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001970 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1971
1972 SourceLocation CatchLoc = ConsumeToken();
1973
1974 SourceLocation LParenLoc = Tok.getLocation();
1975 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1976 return StmtError();
1977
1978 // C++ 3.3.2p3:
1979 // The name in a catch exception-declaration is local to the handler and
1980 // shall not be redeclared in the outermost block of the handler.
1981 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1982
1983 // exception-declaration is equivalent to '...' or a parameter-declaration
1984 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001985 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001986 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001987 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00001988 if (ParseCXXTypeSpecifierSeq(DS))
1989 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001990 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1991 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001992 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001993 } else
1994 ConsumeToken();
1995
1996 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1997 return StmtError();
1998
1999 if (Tok.isNot(tok::l_brace))
2000 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2001
Alexis Hunt96d5c762009-11-21 08:43:09 +00002002 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002003 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002004 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00002005 if (Block.isInvalid())
2006 return move(Block);
2007
John McCallb268a282010-08-23 23:25:46 +00002008 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002009}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002010
2011void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002012 bool Result;
2013 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002014 return;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002015
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002016 if (Tok.isNot(tok::l_brace)) {
2017 Diag(Tok, diag::err_expected_lbrace);
2018 return;
2019 }
2020 ConsumeBrace();
2021
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002022 // Condition is false skip all inside the {}.
2023 if (!Result) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002024 SkipUntil(tok::r_brace, false);
2025 return;
2026 }
2027
2028 // Condition is true, parse the statements.
2029 while (Tok.isNot(tok::r_brace)) {
2030 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2031 if (R.isUsable())
2032 Stmts.push_back(R.release());
2033 }
2034
2035 if (Tok.isNot(tok::r_brace)) {
2036 Diag(Tok, diag::err_expected_rbrace);
2037 return;
2038 }
2039 ConsumeBrace();
2040}