blob: fdb9788667622e5bd5431e44f87fc24f0d7b4ba6 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000042/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000043/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000044/// [OBC] objc-throw-statement
45/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000046/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000047/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// [OMP] openmp-construct [TODO]
49///
50/// labeled-statement:
51/// identifier ':' statement
52/// 'case' constant-expression ':' statement
53/// 'default' ':' statement
54///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000055/// selection-statement:
56/// if-statement
57/// switch-statement
58///
59/// iteration-statement:
60/// while-statement
61/// do-statement
62/// for-statement
63///
Chris Lattner9075bd72006-08-10 04:59:57 +000064/// expression-statement:
65/// expression[opt] ';'
66///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000067/// jump-statement:
68/// 'goto' identifier ';'
69/// 'continue' ';'
70/// 'break' ';'
71/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000072/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000073///
Fariborz Jahanian90814572007-10-04 20:19:06 +000074/// [OBC] objc-throw-statement:
75/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000076/// [OBC] '@' 'throw' ';'
77///
John McCalldadc5752010-08-24 06:29:42 +000078StmtResult
Nico Weber3cef1082011-12-22 23:26:17 +000079Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
80 SourceLocation *TrailingElseLoc) {
Chris Lattner503fadc2006-08-10 05:45:44 +000081 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000082 StmtResult Res;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +000083
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000085
John McCall084e83d2011-03-24 11:26:52 +000086 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith7bdcc4a2012-04-10 01:32:12 +000087 MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +000088
Chris Lattner503fadc2006-08-10 05:45:44 +000089 // Cases in this switch statement should fall through if the parser expects
90 // the token to end in a semicolon (in which case SemiError should be set),
91 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +000092Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000093 tok::TokenKind Kind = Tok.getKind();
94 SourceLocation AtLoc;
95 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000096 case tok::at: // May be a @try or @throw statement
97 {
98 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000099 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000100 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000101
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000102 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000103 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000104 cutOffParsing();
105 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000106
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000107 case tok::identifier: {
108 Token Next = NextToken();
109 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000110 // identifier ':' statement
John McCall53fa7142010-12-24 02:08:15 +0000111 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000112 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000113
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000114 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000115 CXXScopeSpec SS;
116 IdentifierInfo *Name = Tok.getIdentifierInfo();
117 SourceLocation NameLoc = Tok.getLocation();
Richard Trieu01fc0012011-09-19 19:01:00 +0000118
David Blaikiebbafb8a2012-03-11 07:00:24 +0000119 if (getLangOpts().CPlusPlus)
Richard Trieu01fc0012011-09-19 19:01:00 +0000120 CheckForTemplateAndDigraph(Next, ParsedType(),
121 /*EnteringContext=*/false, *Name, SS);
122
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000123 Sema::NameClassification Classification
124 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
125 switch (Classification.getKind()) {
126 case Sema::NC_Keyword:
127 // The identifier was corrected to a keyword. Update the token
128 // to this keyword, and try again.
129 if (Name->getTokenID() != tok::identifier) {
130 Tok.setIdentifierInfo(Name);
131 Tok.setKind(Name->getTokenID());
132 goto Retry;
133 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000134
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000135 // Fall through via the normal error path.
136 // FIXME: This seems like it could only happen for context-sensitive
137 // keywords.
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000138
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000139 case Sema::NC_Error:
140 // Handle errors here by skipping up to the next semicolon or '}', and
141 // eat the semicolon if that's what stopped us.
142 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
143 if (Tok.is(tok::semi))
144 ConsumeToken();
145 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000146
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000147 case Sema::NC_Unknown:
148 // Either we don't know anything about this identifier, or we know that
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000149 // we're in a syntactic context we haven't handled yet.
150 break;
151
Douglas Gregor19b7acf2011-04-27 05:41:15 +0000152 case Sema::NC_Type:
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000153 Tok.setKind(tok::annot_typename);
154 setTypeAnnotation(Tok, Classification.getType());
155 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000156 PP.AnnotateCachedTokens(Tok);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000157 break;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000158
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000159 case Sema::NC_Expression:
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000160 Tok.setKind(tok::annot_primary_expr);
161 setExprAnnotation(Tok, Classification.getExpression());
162 Tok.setAnnotationEndLoc(NameLoc);
163 PP.AnnotateCachedTokens(Tok);
164 break;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000165
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000166 case Sema::NC_TypeTemplate:
167 case Sema::NC_FunctionTemplate: {
168 ConsumeToken(); // the identifier
169 UnqualifiedId Id;
170 Id.setIdentifier(Name, NameLoc);
171 if (AnnotateTemplateIdToken(
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000172 TemplateTy::make(Classification.getTemplateName()),
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000173 Classification.getTemplateNameKind(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000174 SS, SourceLocation(), Id,
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000175 /*AllowTypeAnnotation=*/false)) {
176 // Handle errors here by skipping up to the next semicolon or '}', and
177 // eat the semicolon if that's what stopped us.
178 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
179 if (Tok.is(tok::semi))
180 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000181 return StmtError();
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000182 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000183
184 // If the next token is '::', jump right into parsing a
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000185 // nested-name-specifier. We don't want to leave the template-id
186 // hanging.
187 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000188 // Handle errors here by skipping up to the next semicolon or '}', and
189 // eat the semicolon if that's what stopped us.
190 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
191 if (Tok.is(tok::semi))
192 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000193 return StmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000194 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000195
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000196 // We've annotated a template-id, so try again now.
197 goto Retry;
198 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000199
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000200 case Sema::NC_NestedNameSpecifier:
201 // FIXME: Implement this!
202 break;
203 }
204 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000205
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000206 // Fall through
207 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000208
Chris Lattner803802d2009-03-24 17:04:48 +0000209 default: {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000210 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000211 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000212 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000213 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000214 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000215 }
216
217 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000218 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000219 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000222 return ParseExprStatement(attrs);
Chris Lattner803802d2009-03-24 17:04:48 +0000223 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000224
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000225 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000226 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000227 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000228 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000229
Chris Lattner9075bd72006-08-10 04:59:57 +0000230 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000231 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000232 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000233 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
234 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000235 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000236
Chris Lattner9075bd72006-08-10 04:59:57 +0000237 case tok::kw_if: // C99 6.8.4.1: if-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000238 return ParseIfStatement(attrs, TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000239 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000240 return ParseSwitchStatement(attrs, TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000241
Chris Lattner9075bd72006-08-10 04:59:57 +0000242 case tok::kw_while: // C99 6.8.5.1: while-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000243 return ParseWhileStatement(attrs, TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000244 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000245 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000246 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000247 break;
248 case tok::kw_for: // C99 6.8.5.3: for-statement
Nico Weber3cef1082011-12-22 23:26:17 +0000249 return ParseForStatement(attrs, TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000250
251 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000252 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000253 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000254 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000255 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000256 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000257 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000258 break;
259 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000260 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000261 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000262 break;
263 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000264 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000265 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000266 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000267
Sebastian Redlb219c902008-12-21 16:41:36 +0000268 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000269 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000270 bool msAsm = false;
271 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000272 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000273 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000274 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000275 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000276 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000277
Sebastian Redlb219c902008-12-21 16:41:36 +0000278 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000279 return ParseCXXTryBlock(attrs);
John Wiegley1c0675e2011-04-28 01:08:34 +0000280
281 case tok::kw___try:
282 return ParseSEHTryBlock(attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000283
284 case tok::annot_pragma_vis:
285 HandlePragmaVisibility();
286 return StmtEmpty();
287
288 case tok::annot_pragma_pack:
289 HandlePragmaPack();
290 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000291 }
292
Chris Lattner503fadc2006-08-10 05:45:44 +0000293 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000294 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000295 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000296 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000297 // If the result was valid, then we do want to diagnose this. Use
298 // ExpectAndConsume to emit the diagnostic, even though we know it won't
299 // succeed.
300 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000301 // Skip until we see a } or ;, but don't eat it.
302 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000303 }
Mike Stump11289f42009-09-09 15:08:12 +0000304
Sebastian Redl042ad952008-12-11 19:30:53 +0000305 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000306}
307
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000308/// \brief Parse an expression statement.
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000309StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000310 // If a case keyword is missing, this is where it should be inserted.
311 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000312
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000313 // FIXME: Use the attributes
314 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000315 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000316 if (Expr.isInvalid()) {
317 // If the expression is invalid, skip ahead to the next semicolon or '}'.
318 // Not doing this opens us up to the possibility of infinite loops if
319 // ParseExpression does not consume any tokens.
320 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
321 if (Tok.is(tok::semi))
322 ConsumeToken();
323 return StmtError();
324 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000325
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000326 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
327 Actions.CheckCaseExpression(Expr.get())) {
328 // If a constant expression is followed by a colon inside a switch block,
329 // suggest a missing case keyword.
330 Diag(OldToken, diag::err_expected_case_before_expression)
331 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000332
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000333 // Recover parsing as a case statement.
334 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
335 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000336
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000337 // Otherwise, eat the semicolon.
338 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
339 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley1c0675e2011-04-28 01:08:34 +0000340}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000341
John Wiegley1c0675e2011-04-28 01:08:34 +0000342StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
343 assert(Tok.is(tok::kw___try) && "Expected '__try'");
344 SourceLocation Loc = ConsumeToken();
345 return ParseSEHTryBlockCommon(Loc);
346}
347
348/// ParseSEHTryBlockCommon
349///
350/// seh-try-block:
351/// '__try' compound-statement seh-handler
352///
353/// seh-handler:
354/// seh-except-block
355/// seh-finally-block
356///
357StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
358 if(Tok.isNot(tok::l_brace))
359 return StmtError(Diag(Tok,diag::err_expected_lbrace));
360
361 ParsedAttributesWithRange attrs(AttrFactory);
362 StmtResult TryBlock(ParseCompoundStatement(attrs));
363 if(TryBlock.isInvalid())
364 return move(TryBlock);
365
366 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +0000367 if (Tok.is(tok::identifier) &&
368 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000369 SourceLocation Loc = ConsumeToken();
370 Handler = ParseSEHExceptBlock(Loc);
371 } else if (Tok.is(tok::kw___finally)) {
372 SourceLocation Loc = ConsumeToken();
373 Handler = ParseSEHFinallyBlock(Loc);
374 } else {
375 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
376 }
377
378 if(Handler.isInvalid())
379 return move(Handler);
380
381 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
382 TryLoc,
383 TryBlock.take(),
384 Handler.take());
385}
386
387/// ParseSEHExceptBlock - Handle __except
388///
389/// seh-except-block:
390/// '__except' '(' seh-filter-expression ')' compound-statement
391///
392StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
393 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
394 raii2(Ident___exception_code, false),
395 raii3(Ident_GetExceptionCode, false);
396
397 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
398 return StmtError();
399
400 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
401
David Blaikiebbafb8a2012-03-11 07:00:24 +0000402 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000403 Ident__exception_info->setIsPoisoned(false);
404 Ident___exception_info->setIsPoisoned(false);
405 Ident_GetExceptionInfo->setIsPoisoned(false);
406 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000407 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000408
David Blaikiebbafb8a2012-03-11 07:00:24 +0000409 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000410 Ident__exception_info->setIsPoisoned(true);
411 Ident___exception_info->setIsPoisoned(true);
412 Ident_GetExceptionInfo->setIsPoisoned(true);
413 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000414
415 if(FilterExpr.isInvalid())
416 return StmtError();
417
418 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
419 return StmtError();
420
421 ParsedAttributesWithRange attrs(AttrFactory);
422 StmtResult Block(ParseCompoundStatement(attrs));
423
424 if(Block.isInvalid())
425 return move(Block);
426
427 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
428}
429
430/// ParseSEHFinallyBlock - Handle __finally
431///
432/// seh-finally-block:
433/// '__finally' compound-statement
434///
435StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
436 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
437 raii2(Ident___abnormal_termination, false),
438 raii3(Ident_AbnormalTermination, false);
439
440 ParsedAttributesWithRange attrs(AttrFactory);
441 StmtResult Block(ParseCompoundStatement(attrs));
442 if(Block.isInvalid())
443 return move(Block);
444
445 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000446}
447
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000448/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000449///
450/// labeled-statement:
451/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000452/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000453///
John McCall53fa7142010-12-24 02:08:15 +0000454StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000455 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
456 "Not an identifier!");
457
458 Token IdentTok = Tok; // Save the whole token.
459 ConsumeToken(); // eat the identifier.
460
461 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000462
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000463 // identifier ':' statement
464 SourceLocation ColonLoc = ConsumeToken();
465
466 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000467 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000468
John McCalldadc5752010-08-24 06:29:42 +0000469 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000470
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000471 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000472 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000473 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000474
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000475 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
476 IdentTok.getLocation());
477 if (AttributeList *Attrs = attrs.getList())
478 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000479
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000480 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
481 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000482}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000483
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000484/// ParseCaseStatement
485/// labeled-statement:
486/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000487/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000488///
Richard Trieu2c850c02011-04-21 21:44:26 +0000489StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
490 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000491 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000492 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner34a22092009-03-04 04:23:07 +0000494 // It is very very common for code to contain many case statements recursively
495 // nested, as in (but usually without indentation):
496 // case 1:
497 // case 2:
498 // case 3:
499 // case 4:
500 // case 5: etc.
501 //
502 // Parsing this naively works, but is both inefficient and can cause us to run
503 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000504 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000505 // but all the grossness is constrained to ParseCaseStatement (and some
506 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner34a22092009-03-04 04:23:07 +0000508 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
509 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000510 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattner34a22092009-03-04 04:23:07 +0000512 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
513 // gets updated each time a new case is parsed, and whose body is unset so
514 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieu3481fcd2011-09-09 02:16:15 +0000515 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattner34a22092009-03-04 04:23:07 +0000517 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000518 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000519 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000520 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
521 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000522
Douglas Gregord328d572009-09-21 18:10:23 +0000523 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000524 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000525 cutOffParsing();
526 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000527 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000528
Chris Lattner125c0ee2009-12-10 00:38:54 +0000529 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
530 /// Disable this form of error recovery while we're parsing the case
531 /// expression.
532 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000533
Richard Trieu2c850c02011-04-21 21:44:26 +0000534 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
535 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000536 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000537 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000538 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000539 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000540
Chris Lattner34a22092009-03-04 04:23:07 +0000541 // GNU case range extension.
542 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000543 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000544 if (Tok.is(tok::ellipsis)) {
545 Diag(Tok, diag::ext_gnu_case_range);
546 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000547
Chris Lattner34a22092009-03-04 04:23:07 +0000548 RHS = ParseConstantExpression();
549 if (RHS.isInvalid()) {
550 SkipUntil(tok::colon);
551 return StmtError();
552 }
553 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000554
Chris Lattner125c0ee2009-12-10 00:38:54 +0000555 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000556
John McCall0140bfe2011-01-22 09:28:32 +0000557 if (Tok.is(tok::colon)) {
558 ColonLoc = ConsumeToken();
559
560 // Treat "case blah;" as a typo for "case blah:".
561 } else if (Tok.is(tok::semi)) {
562 ColonLoc = ConsumeToken();
563 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
564 << FixItHint::CreateReplacement(ColonLoc, ":");
565 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000566 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
567 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
568 << FixItHint::CreateInsertion(ExpectedLoc, ":");
569 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000570 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000571
John McCalldadc5752010-08-24 06:29:42 +0000572 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000573 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
574 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000575
Chris Lattner34a22092009-03-04 04:23:07 +0000576 // If we had a sema error parsing this case, then just ignore it and
577 // continue parsing the sub-stmt.
578 if (Case.isInvalid()) {
579 if (TopLevelCase.isInvalid()) // No parsed case stmts.
580 return ParseStatement();
581 // Otherwise, just don't add it as a nested case.
582 } else {
583 // If this is the first case statement we parsed, it becomes TopLevelCase.
584 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000585 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000586 if (TopLevelCase.isInvalid())
587 TopLevelCase = move(Case);
588 else
John McCallb268a282010-08-23 23:25:46 +0000589 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000590 DeepestParsedCaseStmt = NextDeepest;
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner34a22092009-03-04 04:23:07 +0000593 // Handle all case statements.
594 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000595
Chris Lattner34a22092009-03-04 04:23:07 +0000596 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000597
Chris Lattner34a22092009-03-04 04:23:07 +0000598 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000599 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000600
Chris Lattner34a22092009-03-04 04:23:07 +0000601 if (Tok.isNot(tok::r_brace)) {
602 SubStmt = ParseStatement();
603 } else {
604 // Nicely diagnose the common error "switch (X) { case 4: }", which is
605 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000606 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000607 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
608 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner34a22092009-03-04 04:23:07 +0000609 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000610 }
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattner34a22092009-03-04 04:23:07 +0000612 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000613 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000614 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000615
Chris Lattner34a22092009-03-04 04:23:07 +0000616 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000617 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000618
Chris Lattner34a22092009-03-04 04:23:07 +0000619 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000620 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000621}
622
623/// ParseDefaultStatement
624/// labeled-statement:
625/// 'default' ':' statement
626/// Note that this does not parse the 'statement' at the end.
627///
John McCall53fa7142010-12-24 02:08:15 +0000628StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000629 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000630
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000631 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000632 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000633
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000634 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000635 if (Tok.is(tok::colon)) {
636 ColonLoc = ConsumeToken();
637
638 // Treat "default;" as a typo for "default:".
639 } else if (Tok.is(tok::semi)) {
640 ColonLoc = ConsumeToken();
641 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
642 << FixItHint::CreateReplacement(ColonLoc, ":");
643 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000644 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
645 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
646 << FixItHint::CreateInsertion(ExpectedLoc, ":");
647 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000648 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000649
Richard Smith1002d102012-02-17 01:35:32 +0000650 StmtResult SubStmt;
651
652 if (Tok.isNot(tok::r_brace)) {
653 SubStmt = ParseStatement();
654 } else {
655 // Diagnose the common error "switch (X) {... default: }", which is
656 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000657 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000658 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
659 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
660 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000661 }
662
Richard Smith1002d102012-02-17 01:35:32 +0000663 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000664 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000665 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000666
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000667 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000668 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000669}
670
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000671StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
672 bool isStmtExpr) {
673 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
674}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000675
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000676/// ParseCompoundStatement - Parse a "{}" block.
677///
678/// compound-statement: [C99 6.8.2]
679/// { block-item-list[opt] }
680/// [GNU] { label-declarations block-item-list } [TODO]
681///
682/// block-item-list:
683/// block-item
684/// block-item-list block-item
685///
686/// block-item:
687/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000688/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000689/// statement
690/// [OMP] openmp-directive [TODO]
691///
692/// [GNU] label-declarations:
693/// [GNU] label-declaration
694/// [GNU] label-declarations label-declaration
695///
696/// [GNU] label-declaration:
697/// [GNU] '__label__' identifier-list ';'
698///
699/// [OMP] openmp-directive: [TODO]
700/// [OMP] barrier-directive
701/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000702///
John McCall53fa7142010-12-24 02:08:15 +0000703StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000704 bool isStmtExpr,
705 unsigned ScopeFlags) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000706 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000707
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000708 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000709
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000710 // Enter a scope to hold everything within the compound stmt. Compound
711 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000712 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000713
714 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000715 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000716}
717
Chris Lattnerf2978802007-01-21 06:52:16 +0000718/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000719/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000720/// consume the '}' at the end of the block. It does not manipulate the scope
721/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000722StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000723 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000724 Tok.getLocation(),
725 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000726 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000727 BalancedDelimiterTracker T(*this, tok::l_brace);
728 if (T.consumeOpen())
729 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000730
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000731 Sema::CompoundScopeRAII CompoundScope(Actions);
732
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000733 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000734
Chris Lattner43e7f312011-02-18 02:08:43 +0000735 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
736 // only allowed at the start of a compound stmt regardless of the language.
737 while (Tok.is(tok::kw___label__)) {
738 SourceLocation LabelLoc = ConsumeToken();
739 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000740
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000741 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000742 while (1) {
743 if (Tok.isNot(tok::identifier)) {
744 Diag(Tok, diag::err_expected_ident);
745 break;
746 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000747
Chris Lattner43e7f312011-02-18 02:08:43 +0000748 IdentifierInfo *II = Tok.getIdentifierInfo();
749 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000750 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000751
Chris Lattner43e7f312011-02-18 02:08:43 +0000752 if (!Tok.is(tok::comma))
753 break;
754 ConsumeToken();
755 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000756
John McCall084e83d2011-03-24 11:26:52 +0000757 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000758 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
759 DeclsInGroup.data(), DeclsInGroup.size());
760 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000761
Chris Lattner43e7f312011-02-18 02:08:43 +0000762 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
763 if (R.isUsable())
764 Stmts.push_back(R.release());
765 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000766
Chris Lattner43e7f312011-02-18 02:08:43 +0000767 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000768 if (Tok.is(tok::annot_pragma_unused)) {
769 HandlePragmaUnused();
770 continue;
771 }
772
David Blaikiebbafb8a2012-03-11 07:00:24 +0000773 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000774 Tok.is(tok::kw___if_not_exists))) {
775 ParseMicrosoftIfExistsStatement(Stmts);
776 continue;
777 }
778
John McCalldadc5752010-08-24 06:29:42 +0000779 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000780 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000781 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000782 } else {
783 // __extension__ can start declarations and it can also be a unary
784 // operator for expressions. Consume multiple __extension__ markers here
785 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000786 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000787 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000788 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000789 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000790
John McCall084e83d2011-03-24 11:26:52 +0000791 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith7bdcc4a2012-04-10 01:32:12 +0000792 MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000793
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000794 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000795 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000796 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000797 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000798 ExtensionRAIIObject O(Diags);
799
Chris Lattner49836b42009-04-02 04:16:50 +0000800 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000801 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
802 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000803 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000804 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000805 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000806 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000807 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000808
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000809 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000810 SkipUntil(tok::semi);
811 continue;
812 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000813
Alexis Hunt96d5c762009-11-21 08:43:09 +0000814 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000815 // Eat the semicolon at the end of stmt and convert the expr into a
816 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000817 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000818 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000819 }
820 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000821
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000822 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000823 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000824 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000825
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000826 SourceLocation CloseLoc = Tok.getLocation();
827
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000828 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000829 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000830 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000831 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000832 // Recover by creating a compound statement with what we parsed so far,
833 // instead of dropping everything and returning StmtError();
834 } else {
835 if (!T.consumeClose())
836 CloseLoc = T.getCloseLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000837 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000838
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000839 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000840 move_arg(Stmts), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000841}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000842
Chris Lattnerc0081db2008-12-12 06:31:07 +0000843/// ParseParenExprOrCondition:
844/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000845/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000846///
847/// This function parses and performs error recovery on the specified condition
848/// or expression (depending on whether we're in C++ or C mode). This function
849/// goes out of its way to recover well. It returns true if there was a parser
850/// error (the right paren couldn't be found), which indicates that the caller
851/// should try to recover harder. It returns false if the condition is
852/// successfully parsed. Note that a successful parse can still have semantic
853/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000854bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000855 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000856 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000857 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000858 BalancedDelimiterTracker T(*this, tok::l_paren);
859 T.consumeOpen();
860
David Blaikiebbafb8a2012-03-11 07:00:24 +0000861 if (getLangOpts().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000862 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000863 else {
864 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000865 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000866
Douglas Gregore60e41a2010-05-06 17:25:47 +0000867 // If required, convert to a boolean value.
868 if (!ExprResult.isInvalid() && ConvertToBoolean)
869 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000870 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattnerc0081db2008-12-12 06:31:07 +0000873 // If the parser was confused by the condition and we don't have a ')', try to
874 // recover by skipping ahead to a semi and bailing out. If condexp is
875 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000876 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000877 SkipUntil(tok::semi);
878 // Skipping may have stopped if it found the containing ')'. If so, we can
879 // continue parsing the if statement.
880 if (Tok.isNot(tok::r_paren))
881 return true;
882 }
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattnerc0081db2008-12-12 06:31:07 +0000884 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000885 T.consumeClose();
Chris Lattnerc0081db2008-12-12 06:31:07 +0000886 return false;
887}
888
889
Chris Lattnerc951dae2006-08-10 04:23:57 +0000890/// ParseIfStatement
891/// if-statement: [C99 6.8.4.1]
892/// 'if' '(' expression ')' statement
893/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000894/// [C++] 'if' '(' condition ')' statement
895/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000896///
Nico Weber3cef1082011-12-22 23:26:17 +0000897StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs,
898 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000899 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000900
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000901 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000902 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000903
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000904 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000905 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000906 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000907 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000908 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000909
David Blaikiebbafb8a2012-03-11 07:00:24 +0000910 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000911
Chris Lattner2dd1b722007-08-26 23:08:06 +0000912 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
913 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000914 //
915 // C++ 6.4p3:
916 // A name introduced by a declaration in a condition is in scope from its
917 // point of declaration until the end of the substatements controlled by the
918 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000919 // C++ 3.3.2p4:
920 // Names declared in the for-init-statement, and in the condition of if,
921 // while, for, and switch statements are local to the if, while, for, or
922 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000923 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000924 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000925
Chris Lattnerc951dae2006-08-10 04:23:57 +0000926 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000927 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000928 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000929 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000930 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000931
John McCallb268a282010-08-23 23:25:46 +0000932 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000933
Chris Lattner8fb26252007-08-22 05:28:50 +0000934 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000935 // there is no compound stmt. C90 does not have this clause. We only do this
936 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000937 //
938 // C++ 6.4p1:
939 // The substatement in a selection-statement (each substatement, in the else
940 // form of the if statement) implicitly defines a local scope.
941 //
942 // For C++ we create a scope for the condition and a new scope for
943 // substatements because:
944 // -When the 'then' scope exits, we want the condition declaration to still be
945 // active for the 'else' scope too.
946 // -Sema will detect name clashes by considering declarations of a
947 // 'ControlScope' as part of its direct subscope.
948 // -If we wanted the condition and substatement to be in the same scope, we
949 // would have to notify ParseStatement not to create a new scope. It's
950 // simpler to let it create a new scope.
951 //
Mike Stump11289f42009-09-09 15:08:12 +0000952 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000953 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000954
Chris Lattner5c5808a2007-10-29 05:08:52 +0000955 // Read the 'then' stmt.
956 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +0000957
958 SourceLocation InnerStatementTrailingElseLoc;
959 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +0000960
Chris Lattner37e54f42007-08-22 05:16:28 +0000961 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000962 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000963
Chris Lattnerc951dae2006-08-10 04:23:57 +0000964 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000965 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000966 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000967 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000968
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000969 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +0000970 if (TrailingElseLoc)
971 *TrailingElseLoc = Tok.getLocation();
972
Chris Lattneraf635312006-10-16 06:06:51 +0000973 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000974 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000975
Chris Lattner8fb26252007-08-22 05:28:50 +0000976 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000977 // there is no compound stmt. C90 does not have this clause. We only do
978 // this if the body isn't a compound statement to avoid push/pop in common
979 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000980 //
981 // C++ 6.4p1:
982 // The substatement in a selection-statement (each substatement, in the else
983 // form of the if statement) implicitly defines a local scope.
984 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000985 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000986 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000987
Chris Lattner30f910e2006-10-16 05:52:41 +0000988 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000989
Chris Lattner37e54f42007-08-22 05:16:28 +0000990 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000991 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +0000992 } else if (Tok.is(tok::code_completion)) {
993 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000994 cutOffParsing();
995 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +0000996 } else if (InnerStatementTrailingElseLoc.isValid()) {
997 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000998 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000999
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001000 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001001
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001002 // If the condition was invalid, discard the if statement. We could recover
1003 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +00001004 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001005 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +00001006
Chris Lattner5c5808a2007-10-29 05:08:52 +00001007 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001008 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001009 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001010 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1011 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1012 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001013 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001014 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001015 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001016
Chris Lattner5c5808a2007-10-29 05:08:52 +00001017 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001018 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001019 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001020 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001021 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001022
John McCallb268a282010-08-23 23:25:46 +00001023 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001024 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001025}
1026
Chris Lattner9075bd72006-08-10 04:59:57 +00001027/// ParseSwitchStatement
1028/// switch-statement:
1029/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001030/// [C++] 'switch' '(' condition ')' statement
Nico Weber3cef1082011-12-22 23:26:17 +00001031StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs,
1032 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001033 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001034
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001035 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001036 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001037
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001038 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001039 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001040 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001041 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001042 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001043
David Blaikiebbafb8a2012-03-11 07:00:24 +00001044 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001045
Chris Lattner2dd1b722007-08-26 23:08:06 +00001046 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1047 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001048 //
1049 // C++ 6.4p3:
1050 // A name introduced by a declaration in a condition is in scope from its
1051 // point of declaration until the end of the substatements controlled by the
1052 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001053 // C++ 3.3.2p4:
1054 // Names declared in the for-init-statement, and in the condition of if,
1055 // while, for, and switch statements are local to the if, while, for, or
1056 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001057 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001058 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001059 if (C99orCXX)
1060 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001061 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001062
Chris Lattner9075bd72006-08-10 04:59:57 +00001063 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001064 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001065 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001066 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001067 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001068
John McCalldadc5752010-08-24 06:29:42 +00001069 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001070 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001071
Douglas Gregore60e41a2010-05-06 17:25:47 +00001072 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001073 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001074 // FIXME: This is not optimal recovery, but parsing the body is more
1075 // dangerous due to the presence of case and default statements, which
1076 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001077 if (Tok.is(tok::l_brace)) {
1078 ConsumeBrace();
1079 SkipUntil(tok::r_brace, false, false);
1080 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001081 SkipUntil(tok::semi);
1082 return move(Switch);
1083 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001084
Chris Lattner8fb26252007-08-22 05:28:50 +00001085 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001086 // there is no compound stmt. C90 does not have this clause. We only do this
1087 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001088 //
1089 // C++ 6.4p1:
1090 // The substatement in a selection-statement (each substatement, in the else
1091 // form of the if statement) implicitly defines a local scope.
1092 //
1093 // See comments in ParseIfStatement for why we create a scope for the
1094 // condition and a new scope for substatement in C++.
1095 //
Mike Stump11289f42009-09-09 15:08:12 +00001096 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001097 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001098
Chris Lattner9075bd72006-08-10 04:59:57 +00001099 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001100 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001101
Chris Lattner8fd2d012010-01-24 01:50:29 +00001102 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001103 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001104 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001105
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001106 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001107 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001108
1109 // Put the synthesized null statement on the same line as the end of switch
1110 // condition.
1111 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1112 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1113 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001114
John McCallb268a282010-08-23 23:25:46 +00001115 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001116}
1117
1118/// ParseWhileStatement
1119/// while-statement: [C99 6.8.5.1]
1120/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001121/// [C++] 'while' '(' condition ')' statement
Nico Weber3cef1082011-12-22 23:26:17 +00001122StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs,
1123 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001124 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001125
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001126 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001127 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001128 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001129
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001130 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001131 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001132 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001133 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001134 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001135
David Blaikiebbafb8a2012-03-11 07:00:24 +00001136 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001137
Chris Lattner2dd1b722007-08-26 23:08:06 +00001138 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1139 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001140 //
1141 // C++ 6.4p3:
1142 // A name introduced by a declaration in a condition is in scope from its
1143 // point of declaration until the end of the substatements controlled by the
1144 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001145 // C++ 3.3.2p4:
1146 // Names declared in the for-init-statement, and in the condition of if,
1147 // while, for, and switch statements are local to the if, while, for, or
1148 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001149 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001150 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001151 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001152 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1153 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001154 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001155 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1156 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001157
Chris Lattner9075bd72006-08-10 04:59:57 +00001158 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001159 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001160 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001161 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001162 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001163
John McCallb268a282010-08-23 23:25:46 +00001164 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001165
Chris Lattner8fb26252007-08-22 05:28:50 +00001166 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001167 // there is no compound stmt. C90 does not have this clause. We only do this
1168 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001169 //
1170 // C++ 6.5p2:
1171 // The substatement in an iteration-statement implicitly defines a local scope
1172 // which is entered and exited each time through the loop.
1173 //
1174 // See comments in ParseIfStatement for why we create a scope for the
1175 // condition and a new scope for substatement in C++.
1176 //
Mike Stump11289f42009-09-09 15:08:12 +00001177 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001178 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001179
Chris Lattner9075bd72006-08-10 04:59:57 +00001180 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001181 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001182
Chris Lattner8fb26252007-08-22 05:28:50 +00001183 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001184 InnerScope.Exit();
1185 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001186
John McCall48871652010-08-21 09:40:31 +00001187 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001188 return StmtError();
1189
John McCallb268a282010-08-23 23:25:46 +00001190 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001191}
1192
1193/// ParseDoStatement
1194/// do-statement: [C99 6.8.5.2]
1195/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001196/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001197StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001198 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001199
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001200 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001201 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001202
Chris Lattner2dd1b722007-08-26 23:08:06 +00001203 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1204 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001205 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001206 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001207 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001208 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001209 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001210
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001211 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001212
Chris Lattner8fb26252007-08-22 05:28:50 +00001213 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001214 // there is no compound stmt. C90 does not have this clause. We only do this
1215 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001216 //
1217 // C++ 6.5p2:
1218 // The substatement in an iteration-statement implicitly defines a local scope
1219 // which is entered and exited each time through the loop.
1220 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001221 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001222 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001223 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001224
Chris Lattner9075bd72006-08-10 04:59:57 +00001225 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001226 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001227
Chris Lattner8fb26252007-08-22 05:28:50 +00001228 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001229 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001230
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001231 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001232 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001233 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001234 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001235 SkipUntil(tok::semi, false, true);
1236 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001237 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001238 }
Chris Lattneraf635312006-10-16 06:06:51 +00001239 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001240
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001241 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001242 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001243 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001244 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001245 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001246
Chris Lattner10da53c2008-12-12 06:35:28 +00001247 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001248 BalancedDelimiterTracker T(*this, tok::l_paren);
1249 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001250 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001251 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001252 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001253
Sebastian Redlb62406f2008-12-11 19:48:14 +00001254 if (Cond.isInvalid() || Body.isInvalid())
1255 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001256
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001257 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1258 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001259}
1260
1261/// ParseForStatement
1262/// for-statement: [C99 6.8.5.3]
1263/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1264/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001265/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1266/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001267/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001268/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1269/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001270///
1271/// [C++] for-init-statement:
1272/// [C++] expression-statement
1273/// [C++] simple-declaration
1274///
Richard Smith02e85f32011-04-14 22:09:26 +00001275/// [C++0x] for-range-declaration:
1276/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1277/// [C++0x] for-range-initializer:
1278/// [C++0x] expression
1279/// [C++0x] braced-init-list [TODO]
Nico Weber3cef1082011-12-22 23:26:17 +00001280StmtResult Parser::ParseForStatement(ParsedAttributes &attrs,
1281 SourceLocation *TrailingElseLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001282 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001283
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001284 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001285 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001286
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001287 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001288 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001289 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001290 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001291 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001292
David Blaikiebbafb8a2012-03-11 07:00:24 +00001293 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001294
Chris Lattner2dd1b722007-08-26 23:08:06 +00001295 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1296 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001297 //
1298 // C++ 6.4p3:
1299 // A name introduced by a declaration in a condition is in scope from its
1300 // point of declaration until the end of the substatements controlled by the
1301 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001302 // C++ 3.3.2p4:
1303 // Names declared in the for-init-statement, and in the condition of if,
1304 // while, for, and switch statements are local to the if, while, for, or
1305 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001306 // C++ 6.5.3p1:
1307 // Names declared in the for-init-statement are in the same declarative-region
1308 // as those declared in the condition.
1309 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001310 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001311 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001312 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1313 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001314 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001315 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1316
1317 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001318
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001319 BalancedDelimiterTracker T(*this, tok::l_paren);
1320 T.consumeOpen();
1321
John McCalldadc5752010-08-24 06:29:42 +00001322 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001323
Richard Smith02e85f32011-04-14 22:09:26 +00001324 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001325 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001326 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001327 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001328 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001329 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001330 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001331 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001332
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001333 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001334 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001335 C99orCXXorObjC? Sema::PCC_ForInit
1336 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001337 cutOffParsing();
1338 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001339 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001340
Chris Lattner9075bd72006-08-10 04:59:57 +00001341 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001342 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001343 // no first part, eat the ';'.
1344 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001345 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001346 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001347 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001348 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001349
John McCall084e83d2011-03-24 11:26:52 +00001350 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001351 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001352
Richard Smith02e85f32011-04-14 22:09:26 +00001353 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001354 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001355 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1356
Chris Lattner49836b42009-04-02 04:16:50 +00001357 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001358 StmtVector Stmts(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001359 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001360 DeclEnd, attrs, false,
1361 MightBeForRangeStmt ?
1362 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001363 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001364
Richard Smith02e85f32011-04-14 22:09:26 +00001365 if (ForRangeInit.ParsedForRangeDecl()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001366 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001367 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001368
Richard Smith02e85f32011-04-14 22:09:26 +00001369 ForRange = true;
1370 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001371 ConsumeToken();
1372 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001373 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001374 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001375 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001376
Douglas Gregor68762e72010-08-23 21:17:50 +00001377 if (Tok.is(tok::code_completion)) {
1378 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001379 cutOffParsing();
1380 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001381 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001382 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001383 } else {
1384 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001385 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001386 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001387 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001388
John McCall34376a62010-12-04 03:47:34 +00001389 ForEach = isTokIdentifier_in();
1390
Chris Lattnercd68f642007-06-27 01:06:29 +00001391 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001392 if (!Value.isInvalid()) {
1393 if (ForEach)
1394 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1395 else
1396 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1397 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001398
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001399 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001400 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001401 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001402 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001403
Douglas Gregor68762e72010-08-23 21:17:50 +00001404 if (Tok.is(tok::code_completion)) {
1405 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001406 cutOffParsing();
1407 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001408 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001409 Collection = ParseExpression();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001410 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001411 // User tried to write the reasonable, but ill-formed, for-range-statement
1412 // for (expr : expr) { ... }
1413 Diag(Tok, diag::err_for_range_expected_decl)
1414 << FirstPart.get()->getSourceRange();
1415 SkipUntil(tok::r_paren, false, true);
1416 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001417 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001418 if (!Value.isInvalid()) {
1419 Diag(Tok, diag::err_expected_semi_for);
1420 } else {
1421 // Skip until semicolon or rparen, don't consume it.
1422 SkipUntil(tok::r_paren, true, true);
1423 if (Tok.is(tok::semi))
1424 ConsumeToken();
1425 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001426 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001427 }
Richard Smith02e85f32011-04-14 22:09:26 +00001428 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001429 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001430 // Parse the second part of the for specifier.
1431 if (Tok.is(tok::semi)) { // for (...;;
1432 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001433 } else if (Tok.is(tok::r_paren)) {
1434 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001435 } else {
John McCalldadc5752010-08-24 06:29:42 +00001436 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001437 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001438 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1439 else {
1440 Second = ParseExpression();
1441 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001442 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001443 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001444 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001445 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001446 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001447 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001448
Douglas Gregor230a7e62011-02-17 03:38:46 +00001449 if (Tok.isNot(tok::semi)) {
1450 if (!SecondPartIsInvalid || SecondVar)
1451 Diag(Tok, diag::err_expected_semi_for);
1452 else
1453 // Skip until semicolon or rparen, don't consume it.
1454 SkipUntil(tok::r_paren, true, true);
1455 }
1456
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001457 if (Tok.is(tok::semi)) {
1458 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001459 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001460
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001461 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001462 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001463 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001464 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001465 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001466 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001467 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001468 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001469
Richard Smith02e85f32011-04-14 22:09:26 +00001470 // We need to perform most of the semantic analysis for a C++0x for-range
1471 // statememt before parsing the body, in order to be able to deduce the type
1472 // of an auto-typed loop variable.
1473 StmtResult ForRangeStmt;
John McCall53848232011-07-27 01:07:15 +00001474 if (ForRange) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001475 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smith02e85f32011-04-14 22:09:26 +00001476 FirstPart.take(),
1477 ForRangeInit.ColonLoc,
1478 ForRangeInit.RangeExpr.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001479 T.getCloseLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001480
John McCall53848232011-07-27 01:07:15 +00001481
1482 // Similarly, we need to do the semantic analysis for a for-range
1483 // statement immediately in order to close over temporaries correctly.
1484 } else if (ForEach) {
1485 if (!Collection.isInvalid())
1486 Collection =
1487 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1488 }
1489
Chris Lattner8fb26252007-08-22 05:28:50 +00001490 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001491 // there is no compound stmt. C90 does not have this clause. We only do this
1492 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001493 //
1494 // C++ 6.5p2:
1495 // The substatement in an iteration-statement implicitly defines a local scope
1496 // which is entered and exited each time through the loop.
1497 //
1498 // See comments in ParseIfStatement for why we create a scope for
1499 // for-init-statement/condition and a new scope for substatement in C++.
1500 //
Mike Stump11289f42009-09-09 15:08:12 +00001501 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001502 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001503
Chris Lattner9075bd72006-08-10 04:59:57 +00001504 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001505 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001506
Chris Lattner8fb26252007-08-22 05:28:50 +00001507 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001508 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001509
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001510 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001511 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001512
1513 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001514 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001515
Richard Smith02e85f32011-04-14 22:09:26 +00001516 if (ForEach)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001517 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1518 FirstPart.take(),
1519 Collection.take(),
1520 T.getCloseLocation(),
1521 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001522
Richard Smith02e85f32011-04-14 22:09:26 +00001523 if (ForRange)
1524 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1525
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001526 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1527 SecondPart, SecondVar, ThirdPart,
1528 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001529}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001530
Chris Lattner503fadc2006-08-10 05:45:44 +00001531/// ParseGotoStatement
1532/// jump-statement:
1533/// 'goto' identifier ';'
1534/// [GNU] 'goto' '*' expression ';'
1535///
1536/// Note: this lets the caller parse the end ';'.
1537///
John McCall53fa7142010-12-24 02:08:15 +00001538StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001539 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001540
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001541 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001542 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001543
John McCalldadc5752010-08-24 06:29:42 +00001544 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001545 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001546 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1547 Tok.getLocation());
1548 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001549 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001550 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001551 // GNU indirect goto extension.
1552 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001553 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001554 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001555 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001556 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001557 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001558 }
John McCallb268a282010-08-23 23:25:46 +00001559 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001560 } else {
1561 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001562 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001563 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001564
Sebastian Redlb62406f2008-12-11 19:48:14 +00001565 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001566}
1567
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001568/// ParseContinueStatement
1569/// jump-statement:
1570/// 'continue' ';'
1571///
1572/// Note: this lets the caller parse the end ';'.
1573///
John McCall53fa7142010-12-24 02:08:15 +00001574StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001575 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001576
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001577 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001578 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001579}
1580
1581/// ParseBreakStatement
1582/// jump-statement:
1583/// 'break' ';'
1584///
1585/// Note: this lets the caller parse the end ';'.
1586///
John McCall53fa7142010-12-24 02:08:15 +00001587StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001588 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001589
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001590 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001591 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001592}
1593
Chris Lattner503fadc2006-08-10 05:45:44 +00001594/// ParseReturnStatement
1595/// jump-statement:
1596/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001597StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001598 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001599
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001600 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001601 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001602
John McCalldadc5752010-08-24 06:29:42 +00001603 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001604 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001605 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001606 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001607 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001608 return StmtError();
1609 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001610
David Blaikiebbafb8a2012-03-11 07:00:24 +00001611 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001612 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001613 if (R.isUsable())
David Blaikiebbafb8a2012-03-11 07:00:24 +00001614 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001615 diag::warn_cxx98_compat_generalized_initializer_lists :
1616 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001617 << R.get()->getSourceRange();
1618 } else
1619 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001620 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001621 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001622 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001623 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001624 }
John McCallb268a282010-08-23 23:25:46 +00001625 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001626}
Chris Lattner0116c472006-08-15 06:03:28 +00001627
Eli Friedmana4b02c32011-09-30 01:13:51 +00001628/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1629/// this routine is called to collect the tokens for an MS asm statement.
1630StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1631 SourceManager &SrcMgr = PP.getSourceManager();
1632 SourceLocation EndLoc = AsmLoc;
1633 do {
1634 bool InBraces = false;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001635 unsigned short savedBraceCount = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001636 bool InAsmComment = false;
1637 FileID FID;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001638 unsigned LineNo = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001639 unsigned NumTokensRead = 0;
1640 SourceLocation LBraceLoc;
1641
1642 if (Tok.is(tok::l_brace)) {
1643 // Braced inline asm: consume the opening brace.
1644 InBraces = true;
1645 savedBraceCount = BraceCount;
1646 EndLoc = LBraceLoc = ConsumeBrace();
1647 ++NumTokensRead;
1648 } else {
1649 // Single-line inline asm; compute which line it is on.
1650 std::pair<FileID, unsigned> ExpAsmLoc =
1651 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1652 FID = ExpAsmLoc.first;
1653 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1654 }
1655
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001656 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001657 do {
Eli Friedmana4b02c32011-09-30 01:13:51 +00001658 // If we hit EOF, we're done, period.
1659 if (Tok.is(tok::eof))
1660 break;
1661 // When we consume the closing brace, we're done.
1662 if (InBraces && BraceCount == savedBraceCount)
1663 break;
1664
1665 if (!InAsmComment && Tok.is(tok::semi)) {
1666 // A semicolon in an asm is the start of a comment.
1667 InAsmComment = true;
1668 if (InBraces) {
1669 // Compute which line the comment is on.
1670 std::pair<FileID, unsigned> ExpSemiLoc =
1671 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1672 FID = ExpSemiLoc.first;
1673 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1674 }
1675 } else if (!InBraces || InAsmComment) {
1676 // If end-of-line is significant, check whether this token is on a
1677 // new line.
1678 std::pair<FileID, unsigned> ExpLoc =
1679 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1680 if (ExpLoc.first != FID ||
1681 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1682 // If this is a single-line __asm, we're done.
1683 if (!InBraces)
1684 break;
1685 // We're no longer in a comment.
1686 InAsmComment = false;
1687 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1688 // Single-line asm always ends when a closing brace is seen.
1689 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1690 // does MSVC do here?
1691 break;
1692 }
1693 }
1694
1695 // Consume the next token; make sure we don't modify the brace count etc.
1696 // if we are in a comment.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001697 EndLoc = TokLoc;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001698 if (InAsmComment)
1699 PP.Lex(Tok);
1700 else
1701 ConsumeAnyToken();
Steve Naroff8c099c32008-02-08 18:01:27 +00001702 TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001703 ++NumTokensRead;
1704 } while (1);
1705
1706 if (InBraces && BraceCount != savedBraceCount) {
1707 // __asm without closing brace (this can happen at EOF).
1708 Diag(Tok, diag::err_expected_rbrace);
1709 Diag(LBraceLoc, diag::note_matching) << "{";
1710 return StmtError();
1711 } else if (NumTokensRead == 0) {
1712 // Empty __asm.
1713 Diag(Tok, diag::err_expected_lbrace);
1714 return StmtError();
1715 }
1716 // Multiple adjacent asm's form together into a single asm statement
1717 // in the AST.
1718 if (!Tok.is(tok::kw_asm))
1719 break;
1720 EndLoc = ConsumeToken();
1721 } while (1);
1722 // FIXME: Need to actually grab the data and pass it on to Sema. Ideally,
1723 // what Sema wants is a string of the entire inline asm, with one instruction
1724 // per line and all the __asm keywords stripped out, and a way of mapping
1725 // from any character of that string to its location in the original source
1726 // code. I'm not entirely sure how to go about that, though.
Mike Stump6da5d752009-12-11 00:04:56 +00001727 Token t;
1728 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001729 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001730 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001731 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001732 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001733 ExprVector Constraints(Actions);
1734 ExprVector Exprs(Actions);
1735 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001736 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001737 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001738 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001739 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001740}
1741
Chris Lattner0116c472006-08-15 06:03:28 +00001742/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001743/// asm-statement:
1744/// gnu-asm-statement
1745/// ms-asm-statement
1746///
1747/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001748/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1749///
1750/// [GNU] asm-argument:
1751/// asm-string-literal
1752/// asm-string-literal ':' asm-operands[opt]
1753/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1754/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1755/// ':' asm-clobbers
1756///
1757/// [GNU] asm-clobbers:
1758/// asm-string-literal
1759/// asm-clobbers ',' asm-string-literal
1760///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001761/// [MS] ms-asm-statement:
Eli Friedmana4b02c32011-09-30 01:13:51 +00001762/// ms-asm-block
1763/// ms-asm-block ms-asm-statement
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001764///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001765/// [MS] ms-asm-block:
1766/// '__asm' ms-asm-line '\n'
1767/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1768///
1769/// [MS] ms-asm-instruction-block
1770/// ms-asm-line
1771/// ms-asm-line '\n' ms-asm-instruction-block
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001772///
John McCalldadc5752010-08-24 06:29:42 +00001773StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001774 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001775 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001776
David Blaikiebbafb8a2012-03-11 07:00:24 +00001777 if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001778 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001779 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001780 }
John McCall084e83d2011-03-24 11:26:52 +00001781 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001782 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001783 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001784
Chris Lattner0116c472006-08-15 06:03:28 +00001785 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001786 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001787 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001788 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001789 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001790
Chris Lattner0116c472006-08-15 06:03:28 +00001791 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001792 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001793 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001794 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001795 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001796 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001797 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001798 BalancedDelimiterTracker T(*this, tok::l_paren);
1799 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001800
John McCalldadc5752010-08-24 06:29:42 +00001801 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001802 if (AsmString.isInvalid()) {
Richard Smithd67aea22012-03-06 03:21:47 +00001803 // Consume up to and including the closing paren.
1804 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001805 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001806 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001807
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001808 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001809 ExprVector Constraints(Actions);
1810 ExprVector Exprs(Actions);
1811 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001812
Anders Carlsson19fe1162008-02-05 23:03:50 +00001813 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001814 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001815 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001816 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001817 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001818 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001819 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001820 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001821 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001822
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001823 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001824 bool AteExtraColon = false;
1825 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1826 // In C++ mode, parse "::" like ": :".
1827 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001828 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001829
Chris Lattner15768502009-12-20 23:08:04 +00001830 if (!AteExtraColon &&
1831 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001832 return StmtError();
1833 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001834
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001835 unsigned NumOutputs = Names.size();
1836
1837 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001838 if (AteExtraColon ||
1839 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1840 // In C++ mode, parse "::" like ": :".
1841 if (AteExtraColon)
1842 AteExtraColon = false;
1843 else {
1844 AteExtraColon = Tok.is(tok::coloncolon);
1845 ConsumeToken();
1846 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001847
Chris Lattner15768502009-12-20 23:08:04 +00001848 if (!AteExtraColon &&
1849 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001850 return StmtError();
1851 }
1852
1853 assert(Names.size() == Constraints.size() &&
1854 Constraints.size() == Exprs.size() &&
1855 "Input operand size mismatch!");
1856
1857 unsigned NumInputs = Names.size() - NumOutputs;
1858
1859 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001860 if (AteExtraColon || Tok.is(tok::colon)) {
1861 if (!AteExtraColon)
1862 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001863
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001864 // Parse the asm-string list for clobbers if present.
1865 if (Tok.isNot(tok::r_paren)) {
1866 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001867 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001868
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001869 if (Clobber.isInvalid())
1870 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001871
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001872 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001873
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001874 if (Tok.isNot(tok::comma)) break;
1875 ConsumeToken();
1876 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001877 }
1878 }
1879
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001880 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001881 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001882 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001883 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001884 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001885 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001886}
1887
1888/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001889/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001890///
1891/// [GNU] asm-operands:
1892/// asm-operand
1893/// asm-operands ',' asm-operand
1894///
1895/// [GNU] asm-operand:
1896/// asm-string-literal '(' expression ')'
1897/// '[' identifier ']' asm-string-literal '(' expression ')'
1898///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001899//
1900// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001901bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001902 SmallVectorImpl<Expr *> &Constraints,
1903 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001904 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001905 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001906 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001907
1908 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001909 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001910 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001911 BalancedDelimiterTracker T(*this, tok::l_square);
1912 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001913
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001914 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001915 Diag(Tok, diag::err_expected_ident);
1916 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001917 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001918 }
Mike Stump11289f42009-09-09 15:08:12 +00001919
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001920 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001921 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001922
Anders Carlsson9a020f92010-01-30 22:25:16 +00001923 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001924 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001925 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001926 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001927
John McCalldadc5752010-08-24 06:29:42 +00001928 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001929 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001930 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001931 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001932 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001933 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001934
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001935 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001936 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001937 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001938 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001939 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001940
Chris Lattner0116c472006-08-15 06:03:28 +00001941 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001942 BalancedDelimiterTracker T(*this, tok::l_paren);
1943 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001944 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001945 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001946 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001947 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001948 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001949 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001950 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001951 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001952 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001953 ConsumeToken();
1954 }
1955}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001956
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001957Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001958 assert(Tok.is(tok::l_brace));
1959 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001960
Erik Verbruggen6e922512012-04-12 10:11:59 +00001961 if (SkipFunctionBodies && trySkippingFunctionBody()) {
1962 BodyScope.Exit();
1963 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001964 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001965
John McCallfaf5fb42010-08-26 23:41:50 +00001966 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1967 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001968
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001969 // Do not enter a scope for the brace, as the arguments are in the same scope
1970 // (the function body) as the body itself. Instead, just read the statement
1971 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001972 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001973
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001974 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001975 if (FnBody.isInvalid()) {
1976 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001977 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001978 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001979 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001980
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001981 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001982 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001983}
Sebastian Redlb219c902008-12-21 16:41:36 +00001984
Sebastian Redla7b98a72009-04-26 20:35:05 +00001985/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1986///
1987/// function-try-block:
1988/// 'try' ctor-initializer[opt] compound-statement handler-seq
1989///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001990Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001991 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1992 SourceLocation TryLoc = ConsumeToken();
1993
John McCallfaf5fb42010-08-26 23:41:50 +00001994 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1995 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001996
1997 // Constructor initializer list?
1998 if (Tok.is(tok::colon))
1999 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002000 else
2001 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002002
Erik Verbruggen6e922512012-04-12 10:11:59 +00002003 if (SkipFunctionBodies && trySkippingFunctionBody()) {
2004 BodyScope.Exit();
2005 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002006 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002007
Sebastian Redld98ecd62009-04-26 21:08:36 +00002008 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00002009 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002010 // If we failed to parse the try-catch, we just give the function an empty
2011 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002012 if (FnBody.isInvalid()) {
2013 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002014 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00002015 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002016 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002017
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002018 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002019 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002020}
2021
Erik Verbruggen6e922512012-04-12 10:11:59 +00002022bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002023 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00002024 assert(SkipFunctionBodies &&
2025 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002026
2027 // We're in code-completion mode. Skip parsing for all function bodies unless
2028 // the body contains the code-completion point.
2029 TentativeParsingAction PA(*this);
2030 ConsumeBrace();
2031 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Erik Verbruggen6e922512012-04-12 10:11:59 +00002032 /*StopAtCodeCompletion=*/PP.isCodeCompletionEnabled())) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002033 PA.Commit();
2034 return true;
2035 }
2036
2037 PA.Revert();
2038 return false;
2039}
2040
Sebastian Redlb219c902008-12-21 16:41:36 +00002041/// ParseCXXTryBlock - Parse a C++ try-block.
2042///
2043/// try-block:
2044/// 'try' compound-statement handler-seq
2045///
John McCall53fa7142010-12-24 02:08:15 +00002046StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002047 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002048
Sebastian Redlb219c902008-12-21 16:41:36 +00002049 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2050
2051 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002052 return ParseCXXTryBlockCommon(TryLoc);
2053}
2054
2055/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2056/// function-try-block.
2057///
2058/// try-block:
2059/// 'try' compound-statement handler-seq
2060///
2061/// function-try-block:
2062/// 'try' ctor-initializer[opt] compound-statement handler-seq
2063///
2064/// handler-seq:
2065/// handler handler-seq[opt]
2066///
John Wiegley1c0675e2011-04-28 01:08:34 +00002067/// [Borland] try-block:
2068/// 'try' compound-statement seh-except-block
2069/// 'try' compound-statment seh-finally-block
2070///
John McCalldadc5752010-08-24 06:29:42 +00002071StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002072 if (Tok.isNot(tok::l_brace))
2073 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002074 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002075 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002076 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
2077 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002078 if (TryBlock.isInvalid())
2079 return move(TryBlock);
2080
John Wiegley1c0675e2011-04-28 01:08:34 +00002081 // Borland allows SEH-handlers with 'try'
Douglas Gregor60060d62011-10-21 03:57:52 +00002082
2083 if((Tok.is(tok::identifier) &&
2084 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2085 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002086 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2087 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002088 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002089 SourceLocation Loc = ConsumeToken();
2090 Handler = ParseSEHExceptBlock(Loc);
2091 }
2092 else {
2093 SourceLocation Loc = ConsumeToken();
2094 Handler = ParseSEHFinallyBlock(Loc);
2095 }
2096 if(Handler.isInvalid())
2097 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00002098
John Wiegley1c0675e2011-04-28 01:08:34 +00002099 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2100 TryLoc,
2101 TryBlock.take(),
2102 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002103 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002104 else {
2105 StmtVector Handlers(Actions);
2106 MaybeParseCXX0XAttributes(attrs);
2107 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002108
John Wiegley1c0675e2011-04-28 01:08:34 +00002109 if (Tok.isNot(tok::kw_catch))
2110 return StmtError(Diag(Tok, diag::err_expected_catch));
2111 while (Tok.is(tok::kw_catch)) {
2112 StmtResult Handler(ParseCXXCatchBlock());
2113 if (!Handler.isInvalid())
2114 Handlers.push_back(Handler.release());
2115 }
2116 // Don't bother creating the full statement if we don't have any usable
2117 // handlers.
2118 if (Handlers.empty())
2119 return StmtError();
2120
2121 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2122 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002123}
2124
2125/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2126///
2127/// handler:
2128/// 'catch' '(' exception-declaration ')' compound-statement
2129///
2130/// exception-declaration:
2131/// type-specifier-seq declarator
2132/// type-specifier-seq abstract-declarator
2133/// type-specifier-seq
2134/// '...'
2135///
John McCalldadc5752010-08-24 06:29:42 +00002136StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002137 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2138
2139 SourceLocation CatchLoc = ConsumeToken();
2140
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002141 BalancedDelimiterTracker T(*this, tok::l_paren);
2142 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002143 return StmtError();
2144
2145 // C++ 3.3.2p3:
2146 // The name in a catch exception-declaration is local to the handler and
2147 // shall not be redeclared in the outermost block of the handler.
2148 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2149
2150 // exception-declaration is equivalent to '...' or a parameter-declaration
2151 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002152 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002153 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002154 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002155 if (ParseCXXTypeSpecifierSeq(DS))
2156 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002157 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2158 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002159 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002160 } else
2161 ConsumeToken();
2162
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002163 T.consumeClose();
2164 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002165 return StmtError();
2166
2167 if (Tok.isNot(tok::l_brace))
2168 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2169
Alexis Hunt96d5c762009-11-21 08:43:09 +00002170 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002171 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002172 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00002173 if (Block.isInvalid())
2174 return move(Block);
2175
John McCallb268a282010-08-23 23:25:46 +00002176 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002177}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002178
2179void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002180 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002181 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002182 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002183
Douglas Gregor43edb322011-10-24 22:31:10 +00002184 // Handle dependent statements by parsing the braces as a compound statement.
2185 // This is not the same behavior as Visual C++, which don't treat this as a
2186 // compound statement, but for Clang's type checking we can't have anything
2187 // inside these braces escaping to the surrounding code.
2188 if (Result.Behavior == IEB_Dependent) {
2189 if (!Tok.is(tok::l_brace)) {
2190 Diag(Tok, diag::err_expected_lbrace);
2191 return;
2192 }
2193
2194 ParsedAttributes Attrs(AttrFactory);
2195 StmtResult Compound = ParseCompoundStatement(Attrs);
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002196 if (Compound.isInvalid())
2197 return;
2198
2199 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2200 Result.IsIfExists,
2201 Result.SS,
2202 Result.Name,
2203 Compound.get());
2204 if (DepResult.isUsable())
2205 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002206 return;
2207 }
2208
2209 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2210 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002211 Diag(Tok, diag::err_expected_lbrace);
2212 return;
2213 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002214
Douglas Gregor43edb322011-10-24 22:31:10 +00002215 switch (Result.Behavior) {
2216 case IEB_Parse:
2217 // Parse the statements below.
2218 break;
2219
2220 case IEB_Dependent:
2221 llvm_unreachable("Dependent case handled above");
Douglas Gregor43edb322011-10-24 22:31:10 +00002222
2223 case IEB_Skip:
2224 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002225 return;
2226 }
2227
2228 // Condition is true, parse the statements.
2229 while (Tok.isNot(tok::r_brace)) {
2230 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2231 if (R.isUsable())
2232 Stmts.push_back(R.release());
2233 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002234 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002235}