blob: 3741ccbca15704734d2462c623b18ad6696e4a53 [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
389 Ident__exception_info->setIsPoisoned(false);
390 Ident___exception_info->setIsPoisoned(false);
391 Ident_GetExceptionInfo->setIsPoisoned(false);
392 ExprResult FilterExpr(ParseExpression());
393 Ident__exception_info->setIsPoisoned(true);
394 Ident___exception_info->setIsPoisoned(true);
395 Ident_GetExceptionInfo->setIsPoisoned(true);
396
397 if(FilterExpr.isInvalid())
398 return StmtError();
399
400 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
401 return StmtError();
402
403 ParsedAttributesWithRange attrs(AttrFactory);
404 StmtResult Block(ParseCompoundStatement(attrs));
405
406 if(Block.isInvalid())
407 return move(Block);
408
409 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
410}
411
412/// ParseSEHFinallyBlock - Handle __finally
413///
414/// seh-finally-block:
415/// '__finally' compound-statement
416///
417StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
418 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
419 raii2(Ident___abnormal_termination, false),
420 raii3(Ident_AbnormalTermination, false);
421
422 ParsedAttributesWithRange attrs(AttrFactory);
423 StmtResult Block(ParseCompoundStatement(attrs));
424 if(Block.isInvalid())
425 return move(Block);
426
427 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000428}
429
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000430/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000431///
432/// labeled-statement:
433/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000434/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000435///
John McCall53fa7142010-12-24 02:08:15 +0000436StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000437 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
438 "Not an identifier!");
439
440 Token IdentTok = Tok; // Save the whole token.
441 ConsumeToken(); // eat the identifier.
442
443 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000444
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000445 // identifier ':' statement
446 SourceLocation ColonLoc = ConsumeToken();
447
448 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000449 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000450
John McCalldadc5752010-08-24 06:29:42 +0000451 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000452
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000453 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000454 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000455 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000456
457 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
458 IdentTok.getLocation());
459 if (AttributeList *Attrs = attrs.getList())
460 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
461
462 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
463 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000464}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000465
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000466/// ParseCaseStatement
467/// labeled-statement:
468/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000469/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000470///
Richard Trieu2c850c02011-04-21 21:44:26 +0000471StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
472 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000473 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000474 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000475
Chris Lattner34a22092009-03-04 04:23:07 +0000476 // It is very very common for code to contain many case statements recursively
477 // nested, as in (but usually without indentation):
478 // case 1:
479 // case 2:
480 // case 3:
481 // case 4:
482 // case 5: etc.
483 //
484 // Parsing this naively works, but is both inefficient and can cause us to run
485 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000486 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000487 // but all the grossness is constrained to ParseCaseStatement (and some
488 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner34a22092009-03-04 04:23:07 +0000490 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
491 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000492 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner34a22092009-03-04 04:23:07 +0000494 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
495 // gets updated each time a new case is parsed, and whose body is unset so
496 // far. When parsing 'case 4', this is the 'case 3' node.
497 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattner34a22092009-03-04 04:23:07 +0000499 // While we have case statements, eat and stack them.
500 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000501 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
502 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregord328d572009-09-21 18:10:23 +0000504 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000505 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000506 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000507 }
508
Chris Lattner125c0ee2009-12-10 00:38:54 +0000509 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
510 /// Disable this form of error recovery while we're parsing the case
511 /// expression.
512 ColonProtectionRAIIObject ColonProtection(*this);
513
Richard Trieu2c850c02011-04-21 21:44:26 +0000514 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
515 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000516 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000517 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000518 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000519 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000520
Chris Lattner34a22092009-03-04 04:23:07 +0000521 // GNU case range extension.
522 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000523 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000524 if (Tok.is(tok::ellipsis)) {
525 Diag(Tok, diag::ext_gnu_case_range);
526 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000527
Chris Lattner34a22092009-03-04 04:23:07 +0000528 RHS = ParseConstantExpression();
529 if (RHS.isInvalid()) {
530 SkipUntil(tok::colon);
531 return StmtError();
532 }
533 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000534
535 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000536
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000537 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000538 if (Tok.is(tok::colon)) {
539 ColonLoc = ConsumeToken();
540
541 // Treat "case blah;" as a typo for "case blah:".
542 } else if (Tok.is(tok::semi)) {
543 ColonLoc = ConsumeToken();
544 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
545 << FixItHint::CreateReplacement(ColonLoc, ":");
546 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000547 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
548 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
549 << FixItHint::CreateInsertion(ExpectedLoc, ":");
550 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000551 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000552
John McCalldadc5752010-08-24 06:29:42 +0000553 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000554 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
555 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattner34a22092009-03-04 04:23:07 +0000557 // If we had a sema error parsing this case, then just ignore it and
558 // continue parsing the sub-stmt.
559 if (Case.isInvalid()) {
560 if (TopLevelCase.isInvalid()) // No parsed case stmts.
561 return ParseStatement();
562 // Otherwise, just don't add it as a nested case.
563 } else {
564 // If this is the first case statement we parsed, it becomes TopLevelCase.
565 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000566 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000567 if (TopLevelCase.isInvalid())
568 TopLevelCase = move(Case);
569 else
John McCallb268a282010-08-23 23:25:46 +0000570 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000571 DeepestParsedCaseStmt = NextDeepest;
572 }
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner34a22092009-03-04 04:23:07 +0000574 // Handle all case statements.
575 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000576
Chris Lattner34a22092009-03-04 04:23:07 +0000577 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner34a22092009-03-04 04:23:07 +0000579 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000580 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000581
Chris Lattner34a22092009-03-04 04:23:07 +0000582 if (Tok.isNot(tok::r_brace)) {
583 SubStmt = ParseStatement();
584 } else {
585 // Nicely diagnose the common error "switch (X) { case 4: }", which is
586 // not valid.
587 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000588 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000589 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000590 }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Chris Lattner34a22092009-03-04 04:23:07 +0000592 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000593 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000594 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000595
Chris Lattner34a22092009-03-04 04:23:07 +0000596 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000597 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000598
Chris Lattner34a22092009-03-04 04:23:07 +0000599 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000600 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000601}
602
603/// ParseDefaultStatement
604/// labeled-statement:
605/// 'default' ':' statement
606/// Note that this does not parse the 'statement' at the end.
607///
John McCall53fa7142010-12-24 02:08:15 +0000608StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000609 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000610
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000611 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000612 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000613
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000614 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000615 if (Tok.is(tok::colon)) {
616 ColonLoc = ConsumeToken();
617
618 // Treat "default;" as a typo for "default:".
619 } else if (Tok.is(tok::semi)) {
620 ColonLoc = ConsumeToken();
621 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
622 << FixItHint::CreateReplacement(ColonLoc, ":");
623 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000624 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
625 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
626 << FixItHint::CreateInsertion(ExpectedLoc, ":");
627 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000628 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000629
Chris Lattner30f910e2006-10-16 05:52:41 +0000630 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000631 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000632 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000633 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000634 }
635
John McCalldadc5752010-08-24 06:29:42 +0000636 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000637 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000638 return StmtError();
639
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000640 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000641 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000642}
643
644
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000645/// ParseCompoundStatement - Parse a "{}" block.
646///
647/// compound-statement: [C99 6.8.2]
648/// { block-item-list[opt] }
649/// [GNU] { label-declarations block-item-list } [TODO]
650///
651/// block-item-list:
652/// block-item
653/// block-item-list block-item
654///
655/// block-item:
656/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000657/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000658/// statement
659/// [OMP] openmp-directive [TODO]
660///
661/// [GNU] label-declarations:
662/// [GNU] label-declaration
663/// [GNU] label-declarations label-declaration
664///
665/// [GNU] label-declaration:
666/// [GNU] '__label__' identifier-list ';'
667///
668/// [OMP] openmp-directive: [TODO]
669/// [OMP] barrier-directive
670/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000671///
John McCall53fa7142010-12-24 02:08:15 +0000672StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000673 bool isStmtExpr) {
674 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000675
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000676 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000677
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000678 // Enter a scope to hold everything within the compound stmt. Compound
679 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000680 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000681
682 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000683 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000684}
685
686
687/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000688/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000689/// consume the '}' at the end of the block. It does not manipulate the scope
690/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000691StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000692 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000693 Tok.getLocation(),
694 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000695 InMessageExpressionRAIIObject InMessage(*this, false);
696
Chris Lattnerf2978802007-01-21 06:52:16 +0000697 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
698
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000699 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000700
Chris Lattner43e7f312011-02-18 02:08:43 +0000701 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
702 // only allowed at the start of a compound stmt regardless of the language.
703 while (Tok.is(tok::kw___label__)) {
704 SourceLocation LabelLoc = ConsumeToken();
705 Diag(LabelLoc, diag::ext_gnu_local_label);
706
707 llvm::SmallVector<Decl *, 8> DeclsInGroup;
708 while (1) {
709 if (Tok.isNot(tok::identifier)) {
710 Diag(Tok, diag::err_expected_ident);
711 break;
712 }
713
714 IdentifierInfo *II = Tok.getIdentifierInfo();
715 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000716 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner43e7f312011-02-18 02:08:43 +0000717
718 if (!Tok.is(tok::comma))
719 break;
720 ConsumeToken();
721 }
722
John McCall084e83d2011-03-24 11:26:52 +0000723 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000724 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
725 DeclsInGroup.data(), DeclsInGroup.size());
726 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
727
728 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
729 if (R.isUsable())
730 Stmts.push_back(R.release());
731 }
732
733 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000734 if (Tok.is(tok::annot_pragma_unused)) {
735 HandlePragmaUnused();
736 continue;
737 }
738
John McCalldadc5752010-08-24 06:29:42 +0000739 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000740 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000741 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000742 } else {
743 // __extension__ can start declarations and it can also be a unary
744 // operator for expressions. Consume multiple __extension__ markers here
745 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000746 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000747 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000748 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000749 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000750
John McCall084e83d2011-03-24 11:26:52 +0000751 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000752 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000753
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000754 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000755 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000756 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000757 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000758 ExtensionRAIIObject O(Diags);
759
Chris Lattner49836b42009-04-02 04:16:50 +0000760 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000761 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
762 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000763 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000764 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000765 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000766 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000767 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000768
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000769 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000770 SkipUntil(tok::semi);
771 continue;
772 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000773
Alexis Hunt96d5c762009-11-21 08:43:09 +0000774 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000775 // Eat the semicolon at the end of stmt and convert the expr into a
776 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000777 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000778 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000779 }
780 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000781
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000782 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000783 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000784 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000785
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000786 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000787 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000788 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000789 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000790 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000791 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000792
Chris Lattner04132372006-10-16 06:12:55 +0000793 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000794 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000795 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000796}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000797
Chris Lattnerc0081db2008-12-12 06:31:07 +0000798/// ParseParenExprOrCondition:
799/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000800/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000801///
802/// This function parses and performs error recovery on the specified condition
803/// or expression (depending on whether we're in C++ or C mode). This function
804/// goes out of its way to recover well. It returns true if there was a parser
805/// error (the right paren couldn't be found), which indicates that the caller
806/// should try to recover harder. It returns false if the condition is
807/// successfully parsed. Note that a successful parse can still have semantic
808/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000809bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000810 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000811 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000812 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000813 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000814 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000815 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000816 else {
817 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000818 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000819
820 // If required, convert to a boolean value.
821 if (!ExprResult.isInvalid() && ConvertToBoolean)
822 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000823 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000824 }
Mike Stump11289f42009-09-09 15:08:12 +0000825
Chris Lattnerc0081db2008-12-12 06:31:07 +0000826 // If the parser was confused by the condition and we don't have a ')', try to
827 // recover by skipping ahead to a semi and bailing out. If condexp is
828 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000829 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000830 SkipUntil(tok::semi);
831 // Skipping may have stopped if it found the containing ')'. If so, we can
832 // continue parsing the if statement.
833 if (Tok.isNot(tok::r_paren))
834 return true;
835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Chris Lattnerc0081db2008-12-12 06:31:07 +0000837 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000838 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000839 return false;
840}
841
842
Chris Lattnerc951dae2006-08-10 04:23:57 +0000843/// ParseIfStatement
844/// if-statement: [C99 6.8.4.1]
845/// 'if' '(' expression ')' statement
846/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000847/// [C++] 'if' '(' condition ')' statement
848/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000849///
John McCall53fa7142010-12-24 02:08:15 +0000850StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000851 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000852
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000853 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000854 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000855
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000856 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000857 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000858 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000859 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000860 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000861
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000862 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
863
Chris Lattner2dd1b722007-08-26 23:08:06 +0000864 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
865 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000866 //
867 // C++ 6.4p3:
868 // A name introduced by a declaration in a condition is in scope from its
869 // point of declaration until the end of the substatements controlled by the
870 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000871 // C++ 3.3.2p4:
872 // Names declared in the for-init-statement, and in the condition of if,
873 // while, for, and switch statements are local to the if, while, for, or
874 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000875 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000876 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000877
Chris Lattnerc951dae2006-08-10 04:23:57 +0000878 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000879 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000880 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000881 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000882 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000883
John McCallb268a282010-08-23 23:25:46 +0000884 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000885
Chris Lattner8fb26252007-08-22 05:28:50 +0000886 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000887 // there is no compound stmt. C90 does not have this clause. We only do this
888 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000889 //
890 // C++ 6.4p1:
891 // The substatement in a selection-statement (each substatement, in the else
892 // form of the if statement) implicitly defines a local scope.
893 //
894 // For C++ we create a scope for the condition and a new scope for
895 // substatements because:
896 // -When the 'then' scope exits, we want the condition declaration to still be
897 // active for the 'else' scope too.
898 // -Sema will detect name clashes by considering declarations of a
899 // 'ControlScope' as part of its direct subscope.
900 // -If we wanted the condition and substatement to be in the same scope, we
901 // would have to notify ParseStatement not to create a new scope. It's
902 // simpler to let it create a new scope.
903 //
Mike Stump11289f42009-09-09 15:08:12 +0000904 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000905 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000906
Chris Lattner5c5808a2007-10-29 05:08:52 +0000907 // Read the 'then' stmt.
908 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000909 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000910
Chris Lattner37e54f42007-08-22 05:16:28 +0000911 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000912 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000913
Chris Lattnerc951dae2006-08-10 04:23:57 +0000914 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000915 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000916 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000917 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000918
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000919 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000920 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000921 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000922
Chris Lattner8fb26252007-08-22 05:28:50 +0000923 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000924 // there is no compound stmt. C90 does not have this clause. We only do
925 // this if the body isn't a compound statement to avoid push/pop in common
926 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000927 //
928 // C++ 6.4p1:
929 // The substatement in a selection-statement (each substatement, in the else
930 // form of the if statement) implicitly defines a local scope.
931 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000932 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000933 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000934
Chris Lattner30f910e2006-10-16 05:52:41 +0000935 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000936
Chris Lattner37e54f42007-08-22 05:16:28 +0000937 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000938 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000939 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000940
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000941 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000942
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000943 // If the condition was invalid, discard the if statement. We could recover
944 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000945 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000946 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000947
Chris Lattner5c5808a2007-10-29 05:08:52 +0000948 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000949 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000950 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000951 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
952 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
953 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000954 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000955 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000956 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000957
Chris Lattner5c5808a2007-10-29 05:08:52 +0000958 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000959 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000960 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000961 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000962 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000963
John McCallb268a282010-08-23 23:25:46 +0000964 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000965 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000966}
967
Chris Lattner9075bd72006-08-10 04:59:57 +0000968/// ParseSwitchStatement
969/// switch-statement:
970/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000971/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000972StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000973 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000974
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000975 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000976 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000977
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000978 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000979 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000980 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000981 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000982 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000983
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000984 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
985
Chris Lattner2dd1b722007-08-26 23:08:06 +0000986 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
987 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000988 //
989 // C++ 6.4p3:
990 // A name introduced by a declaration in a condition is in scope from its
991 // point of declaration until the end of the substatements controlled by the
992 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000993 // C++ 3.3.2p4:
994 // Names declared in the for-init-statement, and in the condition of if,
995 // while, for, and switch statements are local to the if, while, for, or
996 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000997 //
Richard Trieu2c850c02011-04-21 21:44:26 +0000998 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +0000999 if (C99orCXX)
1000 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001001 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001002
Chris Lattner9075bd72006-08-10 04:59:57 +00001003 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001004 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001005 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001006 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001007 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001008
John McCalldadc5752010-08-24 06:29:42 +00001009 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001010 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001011
Douglas Gregore60e41a2010-05-06 17:25:47 +00001012 if (Switch.isInvalid()) {
1013 // Skip the switch body.
1014 // FIXME: This is not optimal recovery, but parsing the body is more
1015 // dangerous due to the presence of case and default statements, which
1016 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001017 if (Tok.is(tok::l_brace)) {
1018 ConsumeBrace();
1019 SkipUntil(tok::r_brace, false, false);
1020 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001021 SkipUntil(tok::semi);
1022 return move(Switch);
1023 }
1024
Chris Lattner8fb26252007-08-22 05:28:50 +00001025 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001026 // there is no compound stmt. C90 does not have this clause. We only do this
1027 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001028 //
1029 // C++ 6.4p1:
1030 // The substatement in a selection-statement (each substatement, in the else
1031 // form of the if statement) implicitly defines a local scope.
1032 //
1033 // See comments in ParseIfStatement for why we create a scope for the
1034 // condition and a new scope for substatement in C++.
1035 //
Mike Stump11289f42009-09-09 15:08:12 +00001036 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001037 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001038
Chris Lattner9075bd72006-08-10 04:59:57 +00001039 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001040 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001041
Chris Lattner8fd2d012010-01-24 01:50:29 +00001042 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001043 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001044 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001045
Chris Lattner8fd2d012010-01-24 01:50:29 +00001046 if (Body.isInvalid())
1047 // FIXME: Remove the case statement list from the Switch statement.
1048 Body = Actions.ActOnNullStmt(Tok.getLocation());
1049
John McCallb268a282010-08-23 23:25:46 +00001050 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001051}
1052
1053/// ParseWhileStatement
1054/// while-statement: [C99 6.8.5.1]
1055/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001056/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +00001057StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001058 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001059
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001060 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001061 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001062 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001063
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001064 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001065 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001066 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001067 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001068 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001069
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001070 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1071
Chris Lattner2dd1b722007-08-26 23:08:06 +00001072 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1073 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001074 //
1075 // C++ 6.4p3:
1076 // A name introduced by a declaration in a condition is in scope from its
1077 // point of declaration until the end of the substatements controlled by the
1078 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001079 // C++ 3.3.2p4:
1080 // Names declared in the for-init-statement, and in the condition of if,
1081 // while, for, and switch statements are local to the if, while, for, or
1082 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001083 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001084 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001085 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001086 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1087 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001088 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001089 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1090 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001091
Chris Lattner9075bd72006-08-10 04:59:57 +00001092 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001093 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001094 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001095 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001096 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001097
John McCallb268a282010-08-23 23:25:46 +00001098 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001099
Chris Lattner8fb26252007-08-22 05:28:50 +00001100 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001101 // there is no compound stmt. C90 does not have this clause. We only do this
1102 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001103 //
1104 // C++ 6.5p2:
1105 // The substatement in an iteration-statement implicitly defines a local scope
1106 // which is entered and exited each time through the loop.
1107 //
1108 // See comments in ParseIfStatement for why we create a scope for the
1109 // condition and a new scope for substatement in C++.
1110 //
Mike Stump11289f42009-09-09 15:08:12 +00001111 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001112 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001113
Chris Lattner9075bd72006-08-10 04:59:57 +00001114 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001115 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001116
Chris Lattner8fb26252007-08-22 05:28:50 +00001117 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001118 InnerScope.Exit();
1119 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001120
John McCall48871652010-08-21 09:40:31 +00001121 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001122 return StmtError();
1123
John McCallb268a282010-08-23 23:25:46 +00001124 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001125}
1126
1127/// ParseDoStatement
1128/// do-statement: [C99 6.8.5.2]
1129/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001130/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001131StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001132 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001133
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001134 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001135 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001136
Chris Lattner2dd1b722007-08-26 23:08:06 +00001137 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1138 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001139 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001140 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001141 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001142 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001143 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001144
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001145 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001146
Chris Lattner8fb26252007-08-22 05:28:50 +00001147 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001148 // there is no compound stmt. C90 does not have this clause. We only do this
1149 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001150 //
1151 // C++ 6.5p2:
1152 // The substatement in an iteration-statement implicitly defines a local scope
1153 // which is entered and exited each time through the loop.
1154 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001155 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001156 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001157 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001158
Chris Lattner9075bd72006-08-10 04:59:57 +00001159 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001160 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001161
Chris Lattner8fb26252007-08-22 05:28:50 +00001162 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001163 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001164
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001165 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001166 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001167 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001168 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001169 SkipUntil(tok::semi, false, true);
1170 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001171 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001172 }
Chris Lattneraf635312006-10-16 06:06:51 +00001173 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001174
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001175 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001176 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001177 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001178 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001179 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001180
Chris Lattner10da53c2008-12-12 06:35:28 +00001181 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +00001182 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001183 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +00001184 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001185 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001186
Sebastian Redlb62406f2008-12-11 19:48:14 +00001187 if (Cond.isInvalid() || Body.isInvalid())
1188 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001189
John McCallb268a282010-08-23 23:25:46 +00001190 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1191 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +00001192}
1193
1194/// ParseForStatement
1195/// for-statement: [C99 6.8.5.3]
1196/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1197/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001198/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1199/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001200/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001201/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1202/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001203///
1204/// [C++] for-init-statement:
1205/// [C++] expression-statement
1206/// [C++] simple-declaration
1207///
Richard Smith02e85f32011-04-14 22:09:26 +00001208/// [C++0x] for-range-declaration:
1209/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1210/// [C++0x] for-range-initializer:
1211/// [C++0x] expression
1212/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001213StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001214 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001215
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001216 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001217 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001218
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001219 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001220 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001221 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001222 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001223 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001224
Chris Lattner934074c2009-04-22 00:54:41 +00001225 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001226
Chris Lattner2dd1b722007-08-26 23:08:06 +00001227 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1228 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001229 //
1230 // C++ 6.4p3:
1231 // A name introduced by a declaration in a condition is in scope from its
1232 // point of declaration until the end of the substatements controlled by the
1233 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001234 // C++ 3.3.2p4:
1235 // Names declared in the for-init-statement, and in the condition of if,
1236 // while, for, and switch statements are local to the if, while, for, or
1237 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001238 // C++ 6.5.3p1:
1239 // Names declared in the for-init-statement are in the same declarative-region
1240 // as those declared in the condition.
1241 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001242 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001243 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001244 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1245 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001246 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001247 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1248
1249 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001250
Chris Lattner04132372006-10-16 06:12:55 +00001251 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001252 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001253
Richard Smith02e85f32011-04-14 22:09:26 +00001254 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001255 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001256 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001257 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001258 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001259 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001260 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001261 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001262
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001263 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001264 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001265 C99orCXXorObjC? Sema::PCC_ForInit
1266 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001267 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001268 }
1269
Chris Lattner9075bd72006-08-10 04:59:57 +00001270 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001271 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001272 // no first part, eat the ';'.
1273 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001274 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001275 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001276 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001277 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001278
John McCall084e83d2011-03-24 11:26:52 +00001279 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001280 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001281
Richard Smith02e85f32011-04-14 22:09:26 +00001282 // In C++0x, "for (T NS:a" might not be a typo for ::
1283 bool MightBeForRangeStmt = getLang().CPlusPlus;
1284 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1285
Chris Lattner49836b42009-04-02 04:16:50 +00001286 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001287 StmtVector Stmts(Actions);
1288 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001289 DeclEnd, attrs, false,
1290 MightBeForRangeStmt ?
1291 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001292 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001293
Richard Smith02e85f32011-04-14 22:09:26 +00001294 if (ForRangeInit.ParsedForRangeDecl()) {
1295 ForRange = true;
1296 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001297 ConsumeToken();
1298 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001299 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001300 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001301 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001302
1303 if (Tok.is(tok::code_completion)) {
1304 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1305 ConsumeCodeCompletionToken();
1306 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001307 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001308 } else {
1309 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001310 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001311 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001312 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001313
John McCall34376a62010-12-04 03:47:34 +00001314 ForEach = isTokIdentifier_in();
1315
Chris Lattnercd68f642007-06-27 01:06:29 +00001316 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001317 if (!Value.isInvalid()) {
1318 if (ForEach)
1319 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1320 else
1321 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1322 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001323
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001324 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001325 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001326 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001327 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001328
1329 if (Tok.is(tok::code_completion)) {
1330 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1331 ConsumeCodeCompletionToken();
1332 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001333 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001334 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001335 if (!Value.isInvalid()) {
1336 Diag(Tok, diag::err_expected_semi_for);
1337 } else {
1338 // Skip until semicolon or rparen, don't consume it.
1339 SkipUntil(tok::r_paren, true, true);
1340 if (Tok.is(tok::semi))
1341 ConsumeToken();
1342 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001343 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001344 }
Richard Smith02e85f32011-04-14 22:09:26 +00001345 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001346 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001347 // Parse the second part of the for specifier.
1348 if (Tok.is(tok::semi)) { // for (...;;
1349 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001350 } else if (Tok.is(tok::r_paren)) {
1351 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001352 } else {
John McCalldadc5752010-08-24 06:29:42 +00001353 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001354 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001355 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1356 else {
1357 Second = ParseExpression();
1358 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001359 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001360 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001361 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001362 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001363 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001364 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001365
Douglas Gregor230a7e62011-02-17 03:38:46 +00001366 if (Tok.isNot(tok::semi)) {
1367 if (!SecondPartIsInvalid || SecondVar)
1368 Diag(Tok, diag::err_expected_semi_for);
1369 else
1370 // Skip until semicolon or rparen, don't consume it.
1371 SkipUntil(tok::r_paren, true, true);
1372 }
1373
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001374 if (Tok.is(tok::semi)) {
1375 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001376 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001377
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001378 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001379 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001380 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001381 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001382 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001383 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001384 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001385 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001386
Richard Smith02e85f32011-04-14 22:09:26 +00001387 // We need to perform most of the semantic analysis for a C++0x for-range
1388 // statememt before parsing the body, in order to be able to deduce the type
1389 // of an auto-typed loop variable.
1390 StmtResult ForRangeStmt;
1391 if (ForRange)
1392 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1393 FirstPart.take(),
1394 ForRangeInit.ColonLoc,
1395 ForRangeInit.RangeExpr.get(),
1396 RParenLoc);
1397
Chris Lattner8fb26252007-08-22 05:28:50 +00001398 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001399 // there is no compound stmt. C90 does not have this clause. We only do this
1400 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001401 //
1402 // C++ 6.5p2:
1403 // The substatement in an iteration-statement implicitly defines a local scope
1404 // which is entered and exited each time through the loop.
1405 //
1406 // See comments in ParseIfStatement for why we create a scope for
1407 // for-init-statement/condition and a new scope for substatement in C++.
1408 //
Mike Stump11289f42009-09-09 15:08:12 +00001409 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001410 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001411
Chris Lattner9075bd72006-08-10 04:59:57 +00001412 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001413 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001414
Chris Lattner8fb26252007-08-22 05:28:50 +00001415 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001416 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001417
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001418 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001419 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001420
1421 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001422 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001423
Richard Smith02e85f32011-04-14 22:09:26 +00001424 if (ForEach)
1425 // FIXME: It isn't clear how to communicate the late destruction of
1426 // C++ temporaries used to create the collection.
1427 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1428 FirstPart.take(),
1429 Collection.take(), RParenLoc,
1430 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001431
Richard Smith02e85f32011-04-14 22:09:26 +00001432 if (ForRange)
1433 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1434
1435 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1436 SecondVar, ThirdPart, RParenLoc, Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001437}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001438
Chris Lattner503fadc2006-08-10 05:45:44 +00001439/// ParseGotoStatement
1440/// jump-statement:
1441/// 'goto' identifier ';'
1442/// [GNU] 'goto' '*' expression ';'
1443///
1444/// Note: this lets the caller parse the end ';'.
1445///
John McCall53fa7142010-12-24 02:08:15 +00001446StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001447 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001448
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001449 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001450 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001451
John McCalldadc5752010-08-24 06:29:42 +00001452 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001453 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001454 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1455 Tok.getLocation());
1456 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001457 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001458 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001459 // GNU indirect goto extension.
1460 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001461 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001462 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001463 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001464 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001465 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001466 }
John McCallb268a282010-08-23 23:25:46 +00001467 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001468 } else {
1469 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001470 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001471 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001472
Sebastian Redlb62406f2008-12-11 19:48:14 +00001473 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001474}
1475
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001476/// ParseContinueStatement
1477/// jump-statement:
1478/// 'continue' ';'
1479///
1480/// Note: this lets the caller parse the end ';'.
1481///
John McCall53fa7142010-12-24 02:08:15 +00001482StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001483 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001484
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001485 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001486 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001487}
1488
1489/// ParseBreakStatement
1490/// jump-statement:
1491/// 'break' ';'
1492///
1493/// Note: this lets the caller parse the end ';'.
1494///
John McCall53fa7142010-12-24 02:08:15 +00001495StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001496 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001497
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001498 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001499 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001500}
1501
Chris Lattner503fadc2006-08-10 05:45:44 +00001502/// ParseReturnStatement
1503/// jump-statement:
1504/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001505StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001506 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001507
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001508 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001509 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001510
John McCalldadc5752010-08-24 06:29:42 +00001511 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001512 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001513 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001514 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001515 ConsumeCodeCompletionToken();
1516 SkipUntil(tok::semi, false, true);
1517 return StmtError();
1518 }
1519
Douglas Gregore9e27d92011-03-11 23:10:44 +00001520 // FIXME: This is a hack to allow something like C++0x's generalized
1521 // initializer lists, but only enough of this feature to allow Clang to
1522 // parse libstdc++ 4.5's headers.
1523 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1524 R = ParseInitializer();
1525 if (R.isUsable() && !getLang().CPlusPlus0x)
1526 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1527 << R.get()->getSourceRange();
1528 } else
1529 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001530 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001531 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001532 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001533 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001534 }
John McCallb268a282010-08-23 23:25:46 +00001535 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001536}
Chris Lattner0116c472006-08-15 06:03:28 +00001537
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001538/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1539/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001540StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1541 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001542 if (Tok.is(tok::l_brace)) {
1543 unsigned short savedBraceCount = BraceCount;
1544 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001545 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001546 ConsumeAnyToken();
1547 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001548 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001549 // From the MS website: If used without braces, the __asm keyword means
1550 // that the rest of the line is an assembly-language statement.
1551 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001552 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001553 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001554 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001555 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001556 ConsumeAnyToken();
1557 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001558 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1559 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001560 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001561 }
Mike Stump6da5d752009-12-11 00:04:56 +00001562 Token t;
1563 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001564 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001565 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001566 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001567 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001568 ExprVector Constraints(Actions);
1569 ExprVector Exprs(Actions);
1570 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001571 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001572 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001573 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001574 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001575}
1576
Chris Lattner0116c472006-08-15 06:03:28 +00001577/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001578/// asm-statement:
1579/// gnu-asm-statement
1580/// ms-asm-statement
1581///
1582/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001583/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1584///
1585/// [GNU] asm-argument:
1586/// asm-string-literal
1587/// asm-string-literal ':' asm-operands[opt]
1588/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1589/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1590/// ':' asm-clobbers
1591///
1592/// [GNU] asm-clobbers:
1593/// asm-string-literal
1594/// asm-clobbers ',' asm-string-literal
1595///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001596/// [MS] ms-asm-statement:
1597/// '__asm' assembly-instruction ';'[opt]
1598/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1599///
1600/// [MS] assembly-instruction-list:
1601/// assembly-instruction ';'[opt]
1602/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1603///
John McCalldadc5752010-08-24 06:29:42 +00001604StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001605 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001606 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001607
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001608 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001609 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001610 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001611 }
John McCall084e83d2011-03-24 11:26:52 +00001612 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001613 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001614 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001615
Chris Lattner0116c472006-08-15 06:03:28 +00001616 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001617 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001618 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001619 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001620 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001621
Chris Lattner0116c472006-08-15 06:03:28 +00001622 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001623 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001624 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001625 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001626 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001627 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001628 }
Chris Lattner04132372006-10-16 06:12:55 +00001629 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001630
John McCalldadc5752010-08-24 06:29:42 +00001631 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001632 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001633 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001634
Anders Carlsson9a020f92010-01-30 22:25:16 +00001635 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001636 ExprVector Constraints(Actions);
1637 ExprVector Exprs(Actions);
1638 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001639
Anders Carlsson19fe1162008-02-05 23:03:50 +00001640 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001641 // We have a simple asm expression like 'asm("foo")'.
1642 SourceLocation RParenLoc = ConsumeParen();
1643 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1644 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1645 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001646 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001647 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001648 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001649
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001650 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001651 bool AteExtraColon = false;
1652 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1653 // In C++ mode, parse "::" like ": :".
1654 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001655 ConsumeToken();
1656
Chris Lattner15768502009-12-20 23:08:04 +00001657 if (!AteExtraColon &&
1658 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001659 return StmtError();
1660 }
Chris Lattner15768502009-12-20 23:08:04 +00001661
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001662 unsigned NumOutputs = Names.size();
1663
1664 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001665 if (AteExtraColon ||
1666 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1667 // In C++ mode, parse "::" like ": :".
1668 if (AteExtraColon)
1669 AteExtraColon = false;
1670 else {
1671 AteExtraColon = Tok.is(tok::coloncolon);
1672 ConsumeToken();
1673 }
1674
1675 if (!AteExtraColon &&
1676 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001677 return StmtError();
1678 }
1679
1680 assert(Names.size() == Constraints.size() &&
1681 Constraints.size() == Exprs.size() &&
1682 "Input operand size mismatch!");
1683
1684 unsigned NumInputs = Names.size() - NumOutputs;
1685
1686 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001687 if (AteExtraColon || Tok.is(tok::colon)) {
1688 if (!AteExtraColon)
1689 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001690
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001691 // Parse the asm-string list for clobbers if present.
1692 if (Tok.isNot(tok::r_paren)) {
1693 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001694 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001695
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001696 if (Clobber.isInvalid())
1697 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001698
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001699 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001700
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001701 if (Tok.isNot(tok::comma)) break;
1702 ConsumeToken();
1703 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001704 }
1705 }
1706
1707 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1708 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001709 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001710 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001711 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001712 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001713}
1714
1715/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001716/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001717///
1718/// [GNU] asm-operands:
1719/// asm-operand
1720/// asm-operands ',' asm-operand
1721///
1722/// [GNU] asm-operand:
1723/// asm-string-literal '(' expression ')'
1724/// '[' identifier ']' asm-string-literal '(' expression ')'
1725///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001726//
1727// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001728bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1729 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1730 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001731 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001732 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001733 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001734
1735 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001736 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001737 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001738 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001739
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001740 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001741 Diag(Tok, diag::err_expected_ident);
1742 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001743 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001746 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001747 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001748
Anders Carlsson9a020f92010-01-30 22:25:16 +00001749 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001750 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001751 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001752 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001753
John McCalldadc5752010-08-24 06:29:42 +00001754 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001755 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001756 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001757 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001758 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001759 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001760
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001761 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001762 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001763 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001764 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001765 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001766
Chris Lattner0116c472006-08-15 06:03:28 +00001767 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001768 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001769 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001770 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001771 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001772 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001773 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001774 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001775 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001776 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001777 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001778 ConsumeToken();
1779 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001780
1781 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001782}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001783
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001784Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001785 assert(Tok.is(tok::l_brace));
1786 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001787
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001788 if (PP.isCodeCompletionEnabled()) {
1789 if (trySkippingFunctionBodyForCodeCompletion()) {
1790 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001791 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001792 }
1793 }
1794
John McCallfaf5fb42010-08-26 23:41:50 +00001795 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1796 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001797
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001798 // Do not enter a scope for the brace, as the arguments are in the same scope
1799 // (the function body) as the body itself. Instead, just read the statement
1800 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001801 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001802
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001803 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001804 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001805 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001806 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001807
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001808 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001809 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001810}
Sebastian Redlb219c902008-12-21 16:41:36 +00001811
Sebastian Redla7b98a72009-04-26 20:35:05 +00001812/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1813///
1814/// function-try-block:
1815/// 'try' ctor-initializer[opt] compound-statement handler-seq
1816///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001817Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001818 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1819 SourceLocation TryLoc = ConsumeToken();
1820
John McCallfaf5fb42010-08-26 23:41:50 +00001821 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1822 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001823
1824 // Constructor initializer list?
1825 if (Tok.is(tok::colon))
1826 ParseConstructorInitializer(Decl);
1827
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001828 if (PP.isCodeCompletionEnabled()) {
1829 if (trySkippingFunctionBodyForCodeCompletion()) {
1830 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001831 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001832 }
1833 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001834
Sebastian Redld98ecd62009-04-26 21:08:36 +00001835 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001836 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001837 // If we failed to parse the try-catch, we just give the function an empty
1838 // compound statement as the body.
1839 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001840 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001841 MultiStmtArg(Actions), false);
1842
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001843 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001844 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001845}
1846
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001847bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001848 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001849 assert(PP.isCodeCompletionEnabled() &&
1850 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001851
1852 // We're in code-completion mode. Skip parsing for all function bodies unless
1853 // the body contains the code-completion point.
1854 TentativeParsingAction PA(*this);
1855 ConsumeBrace();
1856 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1857 /*StopAtCodeCompletion=*/true)) {
1858 PA.Commit();
1859 return true;
1860 }
1861
1862 PA.Revert();
1863 return false;
1864}
1865
Sebastian Redlb219c902008-12-21 16:41:36 +00001866/// ParseCXXTryBlock - Parse a C++ try-block.
1867///
1868/// try-block:
1869/// 'try' compound-statement handler-seq
1870///
John McCall53fa7142010-12-24 02:08:15 +00001871StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001872 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001873
Sebastian Redlb219c902008-12-21 16:41:36 +00001874 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1875
1876 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001877 return ParseCXXTryBlockCommon(TryLoc);
1878}
1879
1880/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1881/// function-try-block.
1882///
1883/// try-block:
1884/// 'try' compound-statement handler-seq
1885///
1886/// function-try-block:
1887/// 'try' ctor-initializer[opt] compound-statement handler-seq
1888///
1889/// handler-seq:
1890/// handler handler-seq[opt]
1891///
John Wiegley1c0675e2011-04-28 01:08:34 +00001892/// [Borland] try-block:
1893/// 'try' compound-statement seh-except-block
1894/// 'try' compound-statment seh-finally-block
1895///
John McCalldadc5752010-08-24 06:29:42 +00001896StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001897 if (Tok.isNot(tok::l_brace))
1898 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001899 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001900 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001901 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001902 if (TryBlock.isInvalid())
1903 return move(TryBlock);
1904
John Wiegley1c0675e2011-04-28 01:08:34 +00001905 // Borland allows SEH-handlers with 'try'
1906 if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
1907 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1908 StmtResult Handler;
1909 if(Tok.is(tok::kw___except)) {
1910 SourceLocation Loc = ConsumeToken();
1911 Handler = ParseSEHExceptBlock(Loc);
1912 }
1913 else {
1914 SourceLocation Loc = ConsumeToken();
1915 Handler = ParseSEHFinallyBlock(Loc);
1916 }
1917 if(Handler.isInvalid())
1918 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00001919
John Wiegley1c0675e2011-04-28 01:08:34 +00001920 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1921 TryLoc,
1922 TryBlock.take(),
1923 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001924 }
John Wiegley1c0675e2011-04-28 01:08:34 +00001925 else {
1926 StmtVector Handlers(Actions);
1927 MaybeParseCXX0XAttributes(attrs);
1928 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00001929
John Wiegley1c0675e2011-04-28 01:08:34 +00001930 if (Tok.isNot(tok::kw_catch))
1931 return StmtError(Diag(Tok, diag::err_expected_catch));
1932 while (Tok.is(tok::kw_catch)) {
1933 StmtResult Handler(ParseCXXCatchBlock());
1934 if (!Handler.isInvalid())
1935 Handlers.push_back(Handler.release());
1936 }
1937 // Don't bother creating the full statement if we don't have any usable
1938 // handlers.
1939 if (Handlers.empty())
1940 return StmtError();
1941
1942 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
1943 }
Sebastian Redlb219c902008-12-21 16:41:36 +00001944}
1945
1946/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1947///
1948/// handler:
1949/// 'catch' '(' exception-declaration ')' compound-statement
1950///
1951/// exception-declaration:
1952/// type-specifier-seq declarator
1953/// type-specifier-seq abstract-declarator
1954/// type-specifier-seq
1955/// '...'
1956///
John McCalldadc5752010-08-24 06:29:42 +00001957StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001958 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1959
1960 SourceLocation CatchLoc = ConsumeToken();
1961
1962 SourceLocation LParenLoc = Tok.getLocation();
1963 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1964 return StmtError();
1965
1966 // C++ 3.3.2p3:
1967 // The name in a catch exception-declaration is local to the handler and
1968 // shall not be redeclared in the outermost block of the handler.
1969 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1970
1971 // exception-declaration is equivalent to '...' or a parameter-declaration
1972 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001973 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001974 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001975 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00001976 if (ParseCXXTypeSpecifierSeq(DS))
1977 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001978 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1979 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001980 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001981 } else
1982 ConsumeToken();
1983
1984 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1985 return StmtError();
1986
1987 if (Tok.isNot(tok::l_brace))
1988 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1989
Alexis Hunt96d5c762009-11-21 08:43:09 +00001990 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001991 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001992 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001993 if (Block.isInvalid())
1994 return move(Block);
1995
John McCallb268a282010-08-23 23:25:46 +00001996 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001997}