blob: 7254eb3c11733cd08471fae34f3b2377e5db27ac [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCallaeeacf72013-05-03 00:10:13 +000017#include "clang/AST/ASTContext.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/PrettyStackTrace.h"
20#include "clang/Basic/SourceManager.h"
John McCallaeeacf72013-05-03 00:10:13 +000021#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/Scope.h"
Richard Smith05766812012-08-18 00:55:03 +000025#include "clang/Sema/TypoCorrection.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070026#include "llvm/ADT/SmallString.h"
John McCallaeeacf72013-05-03 00:10:13 +000027#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCObjectFileInfo.h"
30#include "llvm/MC/MCParser/MCAsmParser.h"
31#include "llvm/MC/MCRegisterInfo.h"
32#include "llvm/MC/MCStreamer.h"
33#include "llvm/MC/MCSubtargetInfo.h"
34#include "llvm/MC/MCTargetAsmParser.h"
35#include "llvm/Support/SourceMgr.h"
36#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/TargetSelect.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// C99 6.8: Statements and Blocks.
42//===----------------------------------------------------------------------===//
43
Richard Smith961d0572013-10-28 22:04:30 +000044/// \brief Parse a standalone statement (for instance, as the body of an 'if',
45/// 'while', or 'for').
46StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc) {
47 StmtResult Res;
48
49 // We may get back a null statement if we found a #pragma. Keep going until
50 // we get an actual statement.
51 do {
52 StmtVector Stmts;
53 Res = ParseStatementOrDeclaration(Stmts, true, TrailingElseLoc);
54 } while (!Res.isInvalid() && !Res.get());
55
56 return Res;
57}
58
Reid Spencer5f016e22007-07-11 17:01:13 +000059/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
60/// StatementOrDeclaration:
61/// statement
62/// declaration
63///
64/// statement:
65/// labeled-statement
66/// compound-statement
67/// expression-statement
68/// selection-statement
69/// iteration-statement
70/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000071/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000072/// [C++] try-block
John Wiegley28bbe4b2011-04-28 01:08:34 +000073/// [MS] seh-try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000074/// [OBC] objc-throw-statement
75/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000076/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// [GNU] asm-statement
78/// [OMP] openmp-construct [TODO]
79///
80/// labeled-statement:
81/// identifier ':' statement
82/// 'case' constant-expression ':' statement
83/// 'default' ':' statement
84///
85/// selection-statement:
86/// if-statement
87/// switch-statement
88///
89/// iteration-statement:
90/// while-statement
91/// do-statement
92/// for-statement
93///
94/// expression-statement:
95/// expression[opt] ';'
96///
97/// jump-statement:
98/// 'goto' identifier ';'
99/// 'continue' ';'
100/// 'break' ';'
101/// 'return' expression[opt] ';'
102/// [GNU] 'goto' '*' expression ';'
103///
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000104/// [OBC] objc-throw-statement:
105/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +0000106/// [OBC] '@' 'throw' ';'
107///
John McCall60d7b3a2010-08-24 06:29:42 +0000108StmtResult
Nico Weber5cb94a72011-12-22 23:26:17 +0000109Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
110 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000111
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000112 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000113
Richard Smith534986f2012-04-14 00:33:13 +0000114 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000115 MaybeParseCXX11Attributes(Attrs, 0, /*MightBeObjCMessageSend*/ true);
Richard Smith534986f2012-04-14 00:33:13 +0000116
117 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
118 OnlyStatement, TrailingElseLoc, Attrs);
119
120 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
121 "attributes on empty statement");
122
123 if (Attrs.empty() || Res.isInvalid())
124 return Res;
125
126 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
127}
128
Kaelyn Uhrain6243f622013-09-27 19:40:12 +0000129namespace {
130class StatementFilterCCC : public CorrectionCandidateCallback {
131public:
132 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
133 WantTypeSpecifiers = nextTok.is(tok::l_paren) || nextTok.is(tok::less) ||
134 nextTok.is(tok::identifier) || nextTok.is(tok::star) ||
135 nextTok.is(tok::amp) || nextTok.is(tok::l_square);
136 WantExpressionKeywords = nextTok.is(tok::l_paren) ||
137 nextTok.is(tok::identifier) ||
138 nextTok.is(tok::arrow) || nextTok.is(tok::period);
139 WantRemainingKeywords = nextTok.is(tok::l_paren) || nextTok.is(tok::semi) ||
140 nextTok.is(tok::identifier) ||
141 nextTok.is(tok::l_brace);
142 WantCXXNamedCasts = false;
143 }
144
Stephen Hines651f13c2014-04-23 16:59:28 -0700145 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain6243f622013-09-27 19:40:12 +0000146 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhraina89ee572013-10-01 22:00:28 +0000147 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain0f90ee02013-09-27 19:40:16 +0000148 if (NextToken.is(tok::equal))
149 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain2ceb67a2013-09-27 23:54:23 +0000150 if (NextToken.is(tok::period) &&
151 candidate.getCorrectionDeclAs<NamespaceDecl>())
152 return false;
Kaelyn Uhrain6243f622013-09-27 19:40:12 +0000153 return CorrectionCandidateCallback::ValidateCandidate(candidate);
154 }
155
156private:
157 Token NextToken;
158};
159}
160
Richard Smith534986f2012-04-14 00:33:13 +0000161StmtResult
162Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
163 bool OnlyStatement, SourceLocation *TrailingElseLoc,
164 ParsedAttributesWithRange &Attrs) {
165 const char *SemiError = 0;
166 StmtResult Res;
Sean Huntbbd37c62009-11-21 08:43:09 +0000167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 // Cases in this switch statement should fall through if the parser expects
169 // the token to end in a semicolon (in which case SemiError should be set),
170 // or they directly 'return;' if not.
Douglas Gregor312eadb2011-04-24 05:37:28 +0000171Retry:
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000172 tok::TokenKind Kind = Tok.getKind();
173 SourceLocation AtLoc;
174 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000175 case tok::at: // May be a @try or @throw statement
176 {
Richard Smith534986f2012-04-14 00:33:13 +0000177 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000178 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +0000179 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000180 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000181
Douglas Gregor791215b2009-09-21 20:51:25 +0000182 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000183 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000184 cutOffParsing();
185 return StmtError();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000186
Douglas Gregor312eadb2011-04-24 05:37:28 +0000187 case tok::identifier: {
188 Token Next = NextToken();
189 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000190 // identifier ':' statement
Richard Smith534986f2012-04-14 00:33:13 +0000191 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000192 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000193
Richard Smith05766812012-08-18 00:55:03 +0000194 // Look up the identifier, and typo-correct it to a keyword if it's not
195 // found.
Douglas Gregor3b887352011-04-27 04:48:22 +0000196 if (Next.isNot(tok::coloncolon)) {
Richard Smith05766812012-08-18 00:55:03 +0000197 // Try to limit which sets of keywords should be included in typo
198 // correction based on what the next token is.
Kaelyn Uhrain6243f622013-09-27 19:40:12 +0000199 StatementFilterCCC Validator(Next);
200 if (TryAnnotateName(/*IsAddressOfOperand*/false, &Validator)
Richard Smith05766812012-08-18 00:55:03 +0000201 == ANK_Error) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000202 // Handle errors here by skipping up to the next semicolon or '}', and
203 // eat the semicolon if that's what stopped us.
Alexey Bataev8fe24752013-11-18 08:17:37 +0000204 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000205 if (Tok.is(tok::semi))
206 ConsumeToken();
207 return StmtError();
Richard Smith05766812012-08-18 00:55:03 +0000208 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000209
Richard Smith05766812012-08-18 00:55:03 +0000210 // If the identifier was typo-corrected, try again.
211 if (Tok.isNot(tok::identifier))
Douglas Gregor312eadb2011-04-24 05:37:28 +0000212 goto Retry;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000213 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000214
Douglas Gregor312eadb2011-04-24 05:37:28 +0000215 // Fall through
216 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000217
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000218 default: {
David Blaikie4e4d0842012-03-11 07:00:24 +0000219 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000220 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000221 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
Richard Smith534986f2012-04-14 00:33:13 +0000222 DeclEnd, Attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000223 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000224 }
225
226 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000228 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 }
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Richard Smith534986f2012-04-14 00:33:13 +0000231 return ParseExprStatement();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000232 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smith534986f2012-04-14 00:33:13 +0000235 return ParseCaseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smith534986f2012-04-14 00:33:13 +0000237 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smith534986f2012-04-14 00:33:13 +0000240 return ParseCompoundStatement();
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000241 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidise2ca8282011-09-01 21:53:45 +0000242 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
243 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000244 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smith534986f2012-04-14 00:33:13 +0000247 return ParseIfStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smith534986f2012-04-14 00:33:13 +0000249 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smith534986f2012-04-14 00:33:13 +0000252 return ParseWhileStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smith534986f2012-04-14 00:33:13 +0000254 Res = ParseDoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000255 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 break;
257 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smith534986f2012-04-14 00:33:13 +0000258 return ParseForStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259
260 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smith534986f2012-04-14 00:33:13 +0000261 Res = ParseGotoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000262 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 break;
264 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smith534986f2012-04-14 00:33:13 +0000265 Res = ParseContinueStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000266 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 break;
268 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smith534986f2012-04-14 00:33:13 +0000269 Res = ParseBreakStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000270 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 break;
272 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smith534986f2012-04-14 00:33:13 +0000273 Res = ParseReturnStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000274 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000276
Sebastian Redla0fd8652008-12-21 16:41:36 +0000277 case tok::kw_asm: {
Richard Smith534986f2012-04-14 00:33:13 +0000278 ProhibitAttributes(Attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000279 bool msAsm = false;
280 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000281 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000282 if (msAsm) return Res;
Chris Lattner6869d8e2009-06-14 00:07:48 +0000283 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 break;
285 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000286
Sebastian Redla0fd8652008-12-21 16:41:36 +0000287 case tok::kw_try: // C++ 15: try-block
Richard Smith534986f2012-04-14 00:33:13 +0000288 return ParseCXXTryBlock();
John Wiegley28bbe4b2011-04-28 01:08:34 +0000289
290 case tok::kw___try:
Richard Smith534986f2012-04-14 00:33:13 +0000291 ProhibitAttributes(Attrs); // TODO: is it correct?
292 return ParseSEHTryBlock();
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000293
294 case tok::annot_pragma_vis:
Richard Smith534986f2012-04-14 00:33:13 +0000295 ProhibitAttributes(Attrs);
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000296 HandlePragmaVisibility();
297 return StmtEmpty();
298
299 case tok::annot_pragma_pack:
Richard Smith534986f2012-04-14 00:33:13 +0000300 ProhibitAttributes(Attrs);
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000301 HandlePragmaPack();
302 return StmtEmpty();
Eli Friedman9595c7e2012-10-04 02:36:51 +0000303
Eli Friedman8b2bfdd2012-10-09 22:46:54 +0000304 case tok::annot_pragma_msstruct:
305 ProhibitAttributes(Attrs);
306 HandlePragmaMSStruct();
307 return StmtEmpty();
308
Eli Friedman3ef38ee2012-10-08 23:52:38 +0000309 case tok::annot_pragma_align:
310 ProhibitAttributes(Attrs);
311 HandlePragmaAlign();
312 return StmtEmpty();
313
Eli Friedman8b2bfdd2012-10-09 22:46:54 +0000314 case tok::annot_pragma_weak:
315 ProhibitAttributes(Attrs);
316 HandlePragmaWeak();
317 return StmtEmpty();
318
319 case tok::annot_pragma_weakalias:
320 ProhibitAttributes(Attrs);
321 HandlePragmaWeakAlias();
322 return StmtEmpty();
323
324 case tok::annot_pragma_redefine_extname:
325 ProhibitAttributes(Attrs);
326 HandlePragmaRedefineExtname();
327 return StmtEmpty();
328
Eli Friedman9595c7e2012-10-04 02:36:51 +0000329 case tok::annot_pragma_fp_contract:
Richard Smithaed01162013-11-15 21:10:54 +0000330 ProhibitAttributes(Attrs);
Lang Hames860022c2012-10-21 01:10:01 +0000331 Diag(Tok, diag::err_pragma_fp_contract_scope);
332 ConsumeToken();
333 return StmtError();
334
Eli Friedman9595c7e2012-10-04 02:36:51 +0000335 case tok::annot_pragma_opencl_extension:
336 ProhibitAttributes(Attrs);
337 HandlePragmaOpenCLExtension();
338 return StmtEmpty();
Alexey Bataevc6400582013-03-22 06:34:35 +0000339
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000340 case tok::annot_pragma_captured:
Richard Smith175d4172013-09-16 21:17:44 +0000341 ProhibitAttributes(Attrs);
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000342 return HandlePragmaCaptured();
343
Alexey Bataevc6400582013-03-22 06:34:35 +0000344 case tok::annot_pragma_openmp:
Richard Smith175d4172013-09-16 21:17:44 +0000345 ProhibitAttributes(Attrs);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000346 return ParseOpenMPDeclarativeOrExecutableDirective();
347
Stephen Hines651f13c2014-04-23 16:59:28 -0700348 case tok::annot_pragma_ms_pointers_to_members:
349 ProhibitAttributes(Attrs);
350 HandlePragmaMSPointersToMembers();
351 return StmtEmpty();
352
Sebastian Redla0fd8652008-12-21 16:41:36 +0000353 }
354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // If we reached this code, the statement must end in a semicolon.
Stephen Hines651f13c2014-04-23 16:59:28 -0700356 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000357 // If the result was valid, then we do want to diagnose this. Use
358 // ExpectAndConsume to emit the diagnostic, even though we know it won't
359 // succeed.
360 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000361 // Skip until we see a } or ;, but don't eat it.
Alexey Bataev8fe24752013-11-18 08:17:37 +0000362 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000365 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000366}
367
Douglas Gregor312eadb2011-04-24 05:37:28 +0000368/// \brief Parse an expression statement.
Richard Smith534986f2012-04-14 00:33:13 +0000369StmtResult Parser::ParseExprStatement() {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000370 // If a case keyword is missing, this is where it should be inserted.
371 Token OldToken = Tok;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000372
Douglas Gregor312eadb2011-04-24 05:37:28 +0000373 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000374 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000375 if (Expr.isInvalid()) {
376 // If the expression is invalid, skip ahead to the next semicolon or '}'.
377 // Not doing this opens us up to the possibility of infinite loops if
378 // ParseExpression does not consume any tokens.
Alexey Bataev8fe24752013-11-18 08:17:37 +0000379 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000380 if (Tok.is(tok::semi))
381 ConsumeToken();
John McCallb760f112013-03-22 02:10:40 +0000382 return Actions.ActOnExprStmtError();
Douglas Gregor312eadb2011-04-24 05:37:28 +0000383 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000384
Douglas Gregor312eadb2011-04-24 05:37:28 +0000385 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
386 Actions.CheckCaseExpression(Expr.get())) {
387 // If a constant expression is followed by a colon inside a switch block,
388 // suggest a missing case keyword.
389 Diag(OldToken, diag::err_expected_case_before_expression)
390 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000391
Douglas Gregor312eadb2011-04-24 05:37:28 +0000392 // Recover parsing as a case statement.
Richard Smith534986f2012-04-14 00:33:13 +0000393 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000394 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000395
Douglas Gregor312eadb2011-04-24 05:37:28 +0000396 // Otherwise, eat the semicolon.
397 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +0000398 return Actions.ActOnExprStmt(Expr);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000399}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000400
Richard Smith534986f2012-04-14 00:33:13 +0000401StmtResult Parser::ParseSEHTryBlock() {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000402 assert(Tok.is(tok::kw___try) && "Expected '__try'");
403 SourceLocation Loc = ConsumeToken();
404 return ParseSEHTryBlockCommon(Loc);
405}
406
407/// ParseSEHTryBlockCommon
408///
409/// seh-try-block:
410/// '__try' compound-statement seh-handler
411///
412/// seh-handler:
413/// seh-except-block
414/// seh-finally-block
415///
416StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
417 if(Tok.isNot(tok::l_brace))
Stephen Hines651f13c2014-04-23 16:59:28 -0700418 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000419
Joao Matos568ba872012-09-04 17:49:35 +0000420 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000421 if(TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000422 return TryBlock;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000423
424 StmtResult Handler;
Richard Smith534986f2012-04-14 00:33:13 +0000425 if (Tok.is(tok::identifier) &&
Douglas Gregorb57791e2011-10-21 03:57:52 +0000426 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000427 SourceLocation Loc = ConsumeToken();
428 Handler = ParseSEHExceptBlock(Loc);
429 } else if (Tok.is(tok::kw___finally)) {
430 SourceLocation Loc = ConsumeToken();
431 Handler = ParseSEHFinallyBlock(Loc);
432 } else {
433 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
434 }
435
436 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000437 return Handler;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000438
439 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
440 TryLoc,
441 TryBlock.take(),
442 Handler.take());
443}
444
445/// ParseSEHExceptBlock - Handle __except
446///
447/// seh-except-block:
448/// '__except' '(' seh-filter-expression ')' compound-statement
449///
450StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
451 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
452 raii2(Ident___exception_code, false),
453 raii3(Ident_GetExceptionCode, false);
454
Stephen Hines651f13c2014-04-23 16:59:28 -0700455 if (ExpectAndConsume(tok::l_paren))
John Wiegley28bbe4b2011-04-28 01:08:34 +0000456 return StmtError();
457
458 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
459
David Blaikie4e4d0842012-03-11 07:00:24 +0000460 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000461 Ident__exception_info->setIsPoisoned(false);
462 Ident___exception_info->setIsPoisoned(false);
463 Ident_GetExceptionInfo->setIsPoisoned(false);
464 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000465 ExprResult FilterExpr(ParseExpression());
Francois Pichetd7f02df2011-04-28 03:14:31 +0000466
David Blaikie4e4d0842012-03-11 07:00:24 +0000467 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000468 Ident__exception_info->setIsPoisoned(true);
469 Ident___exception_info->setIsPoisoned(true);
470 Ident_GetExceptionInfo->setIsPoisoned(true);
471 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000472
473 if(FilterExpr.isInvalid())
474 return StmtError();
475
Stephen Hines651f13c2014-04-23 16:59:28 -0700476 if (ExpectAndConsume(tok::r_paren))
John Wiegley28bbe4b2011-04-28 01:08:34 +0000477 return StmtError();
478
Richard Smith534986f2012-04-14 00:33:13 +0000479 StmtResult Block(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000480
481 if(Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000482 return Block;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000483
484 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
485}
486
487/// ParseSEHFinallyBlock - Handle __finally
488///
489/// seh-finally-block:
490/// '__finally' compound-statement
491///
492StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
493 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
494 raii2(Ident___abnormal_termination, false),
495 raii3(Ident_AbnormalTermination, false);
496
Richard Smith534986f2012-04-14 00:33:13 +0000497 StmtResult Block(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000498 if(Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000499 return Block;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000500
501 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000502}
503
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000504/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000505///
506/// labeled-statement:
507/// identifier ':' statement
508/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000509///
Richard Smith534986f2012-04-14 00:33:13 +0000510StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000511 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
512 "Not an identifier!");
513
514 Token IdentTok = Tok; // Save the whole token.
515 ConsumeToken(); // eat the identifier.
516
517 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000518
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000519 // identifier ':' statement
520 SourceLocation ColonLoc = ConsumeToken();
521
Richard Smith93982a72013-11-15 22:45:29 +0000522 // Read label attributes, if present.
523 StmtResult SubStmt;
524 if (Tok.is(tok::kw___attribute)) {
525 ParsedAttributesWithRange TempAttrs(AttrFactory);
526 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000527
Richard Smith93982a72013-11-15 22:45:29 +0000528 // In C++, GNU attributes only apply to the label if they are followed by a
529 // semicolon, to disambiguate label attributes from attributes on a labeled
530 // declaration.
531 //
532 // This doesn't quite match what GCC does; if the attribute list is empty
533 // and followed by a semicolon, GCC will reject (it appears to parse the
534 // attributes as part of a statement in that case). That looks like a bug.
535 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
536 attrs.takeAllFrom(TempAttrs);
537 else if (isDeclarationStatement()) {
538 StmtVector Stmts;
539 // FIXME: We should do this whether or not we have a declaration
540 // statement, but that doesn't work correctly (because ProhibitAttributes
541 // can't handle GNU attributes), so only call it in the one case where
542 // GNU attributes are allowed.
543 SubStmt = ParseStatementOrDeclarationAfterAttributes(
544 Stmts, /*OnlyStmts*/ true, 0, TempAttrs);
545 if (!TempAttrs.empty() && !SubStmt.isInvalid())
546 SubStmt = Actions.ProcessStmtAttributes(
547 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
548 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -0700549 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smith93982a72013-11-15 22:45:29 +0000550 }
551 }
552
553 // If we've not parsed a statement yet, parse one now.
554 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
555 SubStmt = ParseStatement();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000556
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000557 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000558 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000559 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000560
Chris Lattner337e5502011-02-18 01:27:55 +0000561 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
562 IdentTok.getLocation());
Richard Smith534986f2012-04-14 00:33:13 +0000563 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattner337e5502011-02-18 01:27:55 +0000564 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smith534986f2012-04-14 00:33:13 +0000565 attrs.clear();
566 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000567
Chris Lattner337e5502011-02-18 01:27:55 +0000568 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
569 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000570}
Reid Spencer5f016e22007-07-11 17:01:13 +0000571
572/// ParseCaseStatement
573/// labeled-statement:
574/// 'case' constant-expression ':' statement
575/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
576///
Richard Smith534986f2012-04-14 00:33:13 +0000577StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000578 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattner24e1e702009-03-04 04:23:07 +0000580 // It is very very common for code to contain many case statements recursively
581 // nested, as in (but usually without indentation):
582 // case 1:
583 // case 2:
584 // case 3:
585 // case 4:
586 // case 5: etc.
587 //
588 // Parsing this naively works, but is both inefficient and can cause us to run
589 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000590 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000591 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smith93982a72013-11-15 22:45:29 +0000592 // weirdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Chris Lattner24e1e702009-03-04 04:23:07 +0000594 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
595 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000596 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Chris Lattner24e1e702009-03-04 04:23:07 +0000598 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
599 // gets updated each time a new case is parsed, and whose body is unset so
600 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieub2fc6902011-09-09 02:16:15 +0000601 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Chris Lattner24e1e702009-03-04 04:23:07 +0000603 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000604 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000605 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000606 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
607 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000609 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000610 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000611 cutOffParsing();
612 return StmtError();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000613 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000614
Chris Lattner6fb09c82009-12-10 00:38:54 +0000615 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
616 /// Disable this form of error recovery while we're parsing the case
617 /// expression.
618 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000619
Richard Trieubb9b80c2011-04-21 21:44:26 +0000620 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
621 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000622 if (LHS.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000623 SkipUntil(tok::colon, StopAtSemi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000624 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000626
Chris Lattner24e1e702009-03-04 04:23:07 +0000627 // GNU case range extension.
628 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000629 ExprResult RHS;
Stephen Hines651f13c2014-04-23 16:59:28 -0700630 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
631 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner24e1e702009-03-04 04:23:07 +0000632 RHS = ParseConstantExpression();
633 if (RHS.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000634 SkipUntil(tok::colon, StopAtSemi);
Chris Lattner24e1e702009-03-04 04:23:07 +0000635 return StmtError();
636 }
637 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000638
Chris Lattner6fb09c82009-12-10 00:38:54 +0000639 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000640
Stephen Hines651f13c2014-04-23 16:59:28 -0700641 if (TryConsumeToken(tok::colon, ColonLoc)) {
642 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
643 // Treat "case blah;" as a typo for "case blah:".
644 Diag(ColonLoc, diag::err_expected_after)
645 << "'case'" << tok::colon
646 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCallf6a3ab02011-01-22 09:28:32 +0000647 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000648 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Stephen Hines651f13c2014-04-23 16:59:28 -0700649 Diag(ExpectedLoc, diag::err_expected_after)
650 << "'case'" << tok::colon
651 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor662a4822010-12-23 22:56:40 +0000652 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000653 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000654
John McCall60d7b3a2010-08-24 06:29:42 +0000655 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000656 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
657 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattner24e1e702009-03-04 04:23:07 +0000659 // If we had a sema error parsing this case, then just ignore it and
660 // continue parsing the sub-stmt.
661 if (Case.isInvalid()) {
662 if (TopLevelCase.isInvalid()) // No parsed case stmts.
663 return ParseStatement();
664 // Otherwise, just don't add it as a nested case.
665 } else {
666 // If this is the first case statement we parsed, it becomes TopLevelCase.
667 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000668 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000669 if (TopLevelCase.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000670 TopLevelCase = Case;
Chris Lattner24e1e702009-03-04 04:23:07 +0000671 else
John McCall9ae2f072010-08-23 23:25:46 +0000672 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000673 DeepestParsedCaseStmt = NextDeepest;
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Chris Lattner24e1e702009-03-04 04:23:07 +0000676 // Handle all case statements.
677 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Chris Lattner24e1e702009-03-04 04:23:07 +0000679 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Chris Lattner24e1e702009-03-04 04:23:07 +0000681 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000682 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Chris Lattner24e1e702009-03-04 04:23:07 +0000684 if (Tok.isNot(tok::r_brace)) {
685 SubStmt = ParseStatement();
686 } else {
687 // Nicely diagnose the common error "switch (X) { case 4: }", which is
688 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000689 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000690 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
691 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner24e1e702009-03-04 04:23:07 +0000692 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattner24e1e702009-03-04 04:23:07 +0000695 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000696 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000697 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Chris Lattner24e1e702009-03-04 04:23:07 +0000699 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000700 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000701
Chris Lattner24e1e702009-03-04 04:23:07 +0000702 // Return the top level parsed statement tree.
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000703 return TopLevelCase;
Reid Spencer5f016e22007-07-11 17:01:13 +0000704}
705
706/// ParseDefaultStatement
707/// labeled-statement:
708/// 'default' ':' statement
709/// Note that this does not parse the 'statement' at the end.
710///
Richard Smith534986f2012-04-14 00:33:13 +0000711StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000712 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
714
Douglas Gregor662a4822010-12-23 22:56:40 +0000715 SourceLocation ColonLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -0700716 if (TryConsumeToken(tok::colon, ColonLoc)) {
717 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
718 // Treat "default;" as a typo for "default:".
719 Diag(ColonLoc, diag::err_expected_after)
720 << "'default'" << tok::colon
721 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCallf6a3ab02011-01-22 09:28:32 +0000722 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000723 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Stephen Hines651f13c2014-04-23 16:59:28 -0700724 Diag(ExpectedLoc, diag::err_expected_after)
725 << "'default'" << tok::colon
726 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor662a4822010-12-23 22:56:40 +0000727 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000729
Richard Smith85b29a42012-02-17 01:35:32 +0000730 StmtResult SubStmt;
731
732 if (Tok.isNot(tok::r_brace)) {
733 SubStmt = ParseStatement();
734 } else {
735 // Diagnose the common error "switch (X) {... default: }", which is
736 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000737 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000738 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
739 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
740 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 }
742
Richard Smith85b29a42012-02-17 01:35:32 +0000743 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000744 if (SubStmt.isInvalid())
Richard Smith85b29a42012-02-17 01:35:32 +0000745 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000746
Sebastian Redl117054a2008-12-28 16:13:43 +0000747 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000748 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000749}
750
Richard Smith534986f2012-04-14 00:33:13 +0000751StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
752 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregorbca01b42011-07-06 22:04:06 +0000753}
Reid Spencer5f016e22007-07-11 17:01:13 +0000754
755/// ParseCompoundStatement - Parse a "{}" block.
756///
757/// compound-statement: [C99 6.8.2]
758/// { block-item-list[opt] }
759/// [GNU] { label-declarations block-item-list } [TODO]
760///
761/// block-item-list:
762/// block-item
763/// block-item-list block-item
764///
765/// block-item:
766/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000767/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000768/// statement
769/// [OMP] openmp-directive [TODO]
770///
771/// [GNU] label-declarations:
772/// [GNU] label-declaration
773/// [GNU] label-declarations label-declaration
774///
775/// [GNU] label-declaration:
776/// [GNU] '__label__' identifier-list ';'
777///
778/// [OMP] openmp-directive: [TODO]
779/// [OMP] barrier-directive
780/// [OMP] flush-directive
781///
Richard Smith534986f2012-04-14 00:33:13 +0000782StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000783 unsigned ScopeFlags) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000784 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000785
Chris Lattner31e05722007-08-26 06:24:45 +0000786 // Enter a scope to hold everything within the compound stmt. Compound
787 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000788 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000789
790 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000791 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000792}
793
Lang Hamesa60d21d2012-11-03 22:29:05 +0000794/// Parse any pragmas at the start of the compound expression. We handle these
795/// separately since some pragmas (FP_CONTRACT) must appear before any C
796/// statement in the compound, but may be intermingled with other pragmas.
797void Parser::ParseCompoundStatementLeadingPragmas() {
798 bool checkForPragmas = true;
799 while (checkForPragmas) {
800 switch (Tok.getKind()) {
801 case tok::annot_pragma_vis:
802 HandlePragmaVisibility();
803 break;
804 case tok::annot_pragma_pack:
805 HandlePragmaPack();
806 break;
807 case tok::annot_pragma_msstruct:
808 HandlePragmaMSStruct();
809 break;
810 case tok::annot_pragma_align:
811 HandlePragmaAlign();
812 break;
813 case tok::annot_pragma_weak:
814 HandlePragmaWeak();
815 break;
816 case tok::annot_pragma_weakalias:
817 HandlePragmaWeakAlias();
818 break;
819 case tok::annot_pragma_redefine_extname:
820 HandlePragmaRedefineExtname();
821 break;
822 case tok::annot_pragma_opencl_extension:
823 HandlePragmaOpenCLExtension();
824 break;
825 case tok::annot_pragma_fp_contract:
826 HandlePragmaFPContract();
827 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700828 case tok::annot_pragma_ms_pointers_to_members:
829 HandlePragmaMSPointersToMembers();
830 break;
Lang Hamesa60d21d2012-11-03 22:29:05 +0000831 default:
832 checkForPragmas = false;
833 break;
834 }
835 }
836
837}
838
Reid Spencer5f016e22007-07-11 17:01:13 +0000839/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000840/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000841/// consume the '}' at the end of the block. It does not manipulate the scope
842/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000843StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000844 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000845 Tok.getLocation(),
846 "in compound statement ('{}')");
Lang Hamesbe9af122012-10-02 04:45:10 +0000847
848 // Record the state of the FP_CONTRACT pragma, restore on leaving the
849 // compound statement.
850 Sema::FPContractStateRAII SaveFPContractState(Actions);
851
Douglas Gregor0fbda682010-09-15 14:51:05 +0000852 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000853 BalancedDelimiterTracker T(*this, tok::l_brace);
854 if (T.consumeOpen())
855 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000856
Dmitri Gribenko625bb562012-02-14 22:14:32 +0000857 Sema::CompoundScopeRAII CompoundScope(Actions);
858
Lang Hamesa60d21d2012-11-03 22:29:05 +0000859 // Parse any pragmas at the beginning of the compound statement.
860 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000861
Lang Hamesa60d21d2012-11-03 22:29:05 +0000862 StmtVector Stmts;
Lang Hames860022c2012-10-21 01:10:01 +0000863
Chris Lattner4ae493c2011-02-18 02:08:43 +0000864 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
865 // only allowed at the start of a compound stmt regardless of the language.
866 while (Tok.is(tok::kw___label__)) {
867 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000868
Chris Lattner5f9e2722011-07-23 10:55:15 +0000869 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000870 while (1) {
871 if (Tok.isNot(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700872 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000873 break;
874 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000875
Chris Lattner4ae493c2011-02-18 02:08:43 +0000876 IdentifierInfo *II = Tok.getIdentifierInfo();
877 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000878 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000879
Stephen Hines651f13c2014-04-23 16:59:28 -0700880 if (!TryConsumeToken(tok::comma))
Chris Lattner4ae493c2011-02-18 02:08:43 +0000881 break;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000882 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000883
John McCall0b7e6782011-03-24 11:26:52 +0000884 DeclSpec DS(AttrFactory);
Rafael Espindola4549d7f2013-07-09 12:05:01 +0000885 DeclGroupPtrTy Res =
886 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000887 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000888
Chris Lattner8bb21d32012-04-28 16:12:17 +0000889 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000890 if (R.isUsable())
891 Stmts.push_back(R.release());
892 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000893
Stephen Hines651f13c2014-04-23 16:59:28 -0700894 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000895 if (Tok.is(tok::annot_pragma_unused)) {
896 HandlePragmaUnused();
897 continue;
898 }
899
David Blaikie4e4d0842012-03-11 07:00:24 +0000900 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet1e862692011-05-06 20:48:22 +0000901 Tok.is(tok::kw___if_not_exists))) {
902 ParseMicrosoftIfExistsStatement(Stmts);
903 continue;
904 }
905
John McCall60d7b3a2010-08-24 06:29:42 +0000906 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000907 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000908 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000909 } else {
910 // __extension__ can start declarations and it can also be a unary
911 // operator for expressions. Consume multiple __extension__ markers here
912 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000913 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000914 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000915 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000916 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000917
John McCall0b7e6782011-03-24 11:26:52 +0000918 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000919 MaybeParseCXX11Attributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Sean Huntbbd37c62009-11-21 08:43:09 +0000920
Chris Lattner45a566c2007-08-27 01:01:57 +0000921 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000922 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000923 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000924 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000925 ExtensionRAIIObject O(Diags);
926
Chris Lattner97144fc2009-04-02 04:16:50 +0000927 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000928 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
929 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000930 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000931 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000932 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000933 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000934 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000935
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000936 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000937 SkipUntil(tok::semi);
938 continue;
939 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000940
Sean Huntbbd37c62009-11-21 08:43:09 +0000941 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000942 // Eat the semicolon at the end of stmt and convert the expr into a
943 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000944 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +0000945 R = Actions.ActOnExprStmt(Res);
Chris Lattner45a566c2007-08-27 01:01:57 +0000946 }
947 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000948
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000949 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000950 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000952
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000953 SourceLocation CloseLoc = Tok.getLocation();
954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 // We broke out of the while loop because we found a '}' or EOF.
Nico Weberd11f4352012-12-30 23:36:56 +0000956 if (!T.consumeClose())
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000957 // Recover by creating a compound statement with what we parsed so far,
958 // instead of dropping everything and returning StmtError();
Nico Weberd11f4352012-12-30 23:36:56 +0000959 CloseLoc = T.getCloseLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000960
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000961 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000962 Stmts, isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000963}
964
Chris Lattner15ff1112008-12-12 06:31:07 +0000965/// ParseParenExprOrCondition:
966/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000967/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000968///
969/// This function parses and performs error recovery on the specified condition
970/// or expression (depending on whether we're in C++ or C mode). This function
971/// goes out of its way to recover well. It returns true if there was a parser
972/// error (the right paren couldn't be found), which indicates that the caller
973/// should try to recover harder. It returns false if the condition is
974/// successfully parsed. Note that a successful parse can still have semantic
975/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000976bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000977 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000978 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000979 bool ConvertToBoolean) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000980 BalancedDelimiterTracker T(*this, tok::l_paren);
981 T.consumeOpen();
982
David Blaikie4e4d0842012-03-11 07:00:24 +0000983 if (getLangOpts().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000984 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000985 else {
986 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000987 DeclResult = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000988
Douglas Gregor586596f2010-05-06 17:25:47 +0000989 // If required, convert to a boolean value.
990 if (!ExprResult.isInvalid() && ConvertToBoolean)
991 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000992 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattner15ff1112008-12-12 06:31:07 +0000995 // If the parser was confused by the condition and we don't have a ')', try to
996 // recover by skipping ahead to a semi and bailing out. If condexp is
997 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000998 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000999 SkipUntil(tok::semi);
1000 // Skipping may have stopped if it found the containing ')'. If so, we can
1001 // continue parsing the if statement.
1002 if (Tok.isNot(tok::r_paren))
1003 return true;
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner15ff1112008-12-12 06:31:07 +00001006 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001007 T.consumeClose();
Chad Rosierb6604462012-07-10 21:35:27 +00001008
Chris Lattnerbddc7e52012-04-28 16:24:20 +00001009 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1010 // that all callers are looking for a statement after the condition, so ")"
1011 // isn't valid.
1012 while (Tok.is(tok::r_paren)) {
1013 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1014 << FixItHint::CreateRemoval(Tok.getLocation());
1015 ConsumeParen();
1016 }
Chad Rosierb6604462012-07-10 21:35:27 +00001017
Chris Lattner15ff1112008-12-12 06:31:07 +00001018 return false;
1019}
1020
1021
Reid Spencer5f016e22007-07-11 17:01:13 +00001022/// ParseIfStatement
1023/// if-statement: [C99 6.8.4.1]
1024/// 'if' '(' expression ')' statement
1025/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001026/// [C++] 'if' '(' condition ')' statement
1027/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +00001028///
Richard Smith534986f2012-04-14 00:33:13 +00001029StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001030 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
1032
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001033 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001034 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001036 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001038
David Blaikie4e4d0842012-03-11 07:00:24 +00001039 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001040
Chris Lattner22153252007-08-26 23:08:06 +00001041 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1042 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001043 //
1044 // C++ 6.4p3:
1045 // A name introduced by a declaration in a condition is in scope from its
1046 // point of declaration until the end of the substatements controlled by the
1047 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001048 // C++ 3.3.2p4:
1049 // Names declared in the for-init-statement, and in the condition of if,
1050 // while, for, and switch statements are local to the if, while, for, or
1051 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001052 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001053 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +00001054
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001056 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +00001057 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001058 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001059 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +00001060
David Blaikiedef07622012-05-16 04:20:04 +00001061 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Chris Lattner0ecea032007-08-22 05:28:50 +00001063 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001064 // there is no compound stmt. C90 does not have this clause. We only do this
1065 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001066 //
1067 // C++ 6.4p1:
1068 // The substatement in a selection-statement (each substatement, in the else
1069 // form of the if statement) implicitly defines a local scope.
1070 //
1071 // For C++ we create a scope for the condition and a new scope for
1072 // substatements because:
1073 // -When the 'then' scope exits, we want the condition declaration to still be
1074 // active for the 'else' scope too.
1075 // -Sema will detect name clashes by considering declarations of a
1076 // 'ControlScope' as part of its direct subscope.
1077 // -If we wanted the condition and substatement to be in the same scope, we
1078 // would have to notify ParseStatement not to create a new scope. It's
1079 // simpler to let it create a new scope.
1080 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001081 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001082
Chris Lattnerb96728d2007-10-29 05:08:52 +00001083 // Read the 'then' stmt.
1084 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber5cb94a72011-12-22 23:26:17 +00001085
1086 SourceLocation InnerStatementTrailingElseLoc;
1087 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001088
Chris Lattnera36ce712007-08-22 05:16:28 +00001089 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001090 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001091
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 // If it has an else, parse it.
1093 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +00001094 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00001095 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001096
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001097 if (Tok.is(tok::kw_else)) {
Nico Weber5cb94a72011-12-22 23:26:17 +00001098 if (TrailingElseLoc)
1099 *TrailingElseLoc = Tok.getLocation();
1100
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +00001102 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001103
Chris Lattner0ecea032007-08-22 05:28:50 +00001104 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001105 // there is no compound stmt. C90 does not have this clause. We only do
1106 // this if the body isn't a compound statement to avoid push/pop in common
1107 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001108 //
1109 // C++ 6.4p1:
1110 // The substatement in a selection-statement (each substatement, in the else
1111 // form of the if statement) implicitly defines a local scope.
1112 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001113 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001114
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 ElseStmt = ParseStatement();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001116
Chris Lattnera36ce712007-08-22 05:16:28 +00001117 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001118 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +00001119 } else if (Tok.is(tok::code_completion)) {
1120 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001121 cutOffParsing();
1122 return StmtError();
Nico Weber5cb94a72011-12-22 23:26:17 +00001123 } else if (InnerStatementTrailingElseLoc.isValid()) {
1124 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001126
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001127 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Chris Lattnerb96728d2007-10-29 05:08:52 +00001129 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +00001130 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +00001131 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001132 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1133 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1134 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001135 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001136 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +00001137 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001138
Chris Lattnerb96728d2007-10-29 05:08:52 +00001139 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001140 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001141 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001142 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001143 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001144
John McCall9ae2f072010-08-23 23:25:46 +00001145 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001146 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001147}
1148
1149/// ParseSwitchStatement
1150/// switch-statement:
1151/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001152/// [C++] 'switch' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +00001153StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001154 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1156
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001157 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001158 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001160 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 }
Chris Lattner22153252007-08-26 23:08:06 +00001162
David Blaikie4e4d0842012-03-11 07:00:24 +00001163 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001164
Chris Lattner22153252007-08-26 23:08:06 +00001165 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1166 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001167 //
1168 // C++ 6.4p3:
1169 // A name introduced by a declaration in a condition is in scope from its
1170 // point of declaration until the end of the substatements controlled by the
1171 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001172 // C++ 3.3.2p4:
1173 // Names declared in the for-init-statement, and in the condition of if,
1174 // while, for, and switch statements are local to the if, while, for, or
1175 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001176 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001177 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001178 if (C99orCXX)
1179 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001180 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001181
1182 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001183 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001184 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001185 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001186 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001187
John McCall60d7b3a2010-08-24 06:29:42 +00001188 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001189 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001190
Douglas Gregor586596f2010-05-06 17:25:47 +00001191 if (Switch.isInvalid()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001192 // Skip the switch body.
Douglas Gregor586596f2010-05-06 17:25:47 +00001193 // FIXME: This is not optimal recovery, but parsing the body is more
1194 // dangerous due to the presence of case and default statements, which
1195 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001196 if (Tok.is(tok::l_brace)) {
1197 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001198 SkipUntil(tok::r_brace);
Douglas Gregor4186ff42010-05-20 23:20:59 +00001199 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001200 SkipUntil(tok::semi);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001201 return Switch;
Douglas Gregor586596f2010-05-06 17:25:47 +00001202 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001203
Chris Lattner0ecea032007-08-22 05:28:50 +00001204 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001205 // there is no compound stmt. C90 does not have this clause. We only do this
1206 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001207 //
1208 // C++ 6.4p1:
1209 // The substatement in a selection-statement (each substatement, in the else
1210 // form of the if statement) implicitly defines a local scope.
1211 //
1212 // See comments in ParseIfStatement for why we create a scope for the
1213 // condition and a new scope for substatement in C++.
1214 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001215 getCurScope()->AddFlags(Scope::BreakScope);
1216 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001217
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001219 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001220
Chris Lattner7e52de42010-01-24 01:50:29 +00001221 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001222 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001223 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001224
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001225 if (Body.isInvalid()) {
Chris Lattner7e52de42010-01-24 01:50:29 +00001226 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001227
1228 // Put the synthesized null statement on the same line as the end of switch
1229 // condition.
1230 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1231 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1232 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001233
John McCall9ae2f072010-08-23 23:25:46 +00001234 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001235}
1236
1237/// ParseWhileStatement
1238/// while-statement: [C99 6.8.5.1]
1239/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001240/// [C++] 'while' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +00001241StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001242 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 SourceLocation WhileLoc = Tok.getLocation();
1244 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001245
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001246 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001247 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001249 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001251
David Blaikie4e4d0842012-03-11 07:00:24 +00001252 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001253
Chris Lattner22153252007-08-26 23:08:06 +00001254 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1255 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001256 //
1257 // C++ 6.4p3:
1258 // A name introduced by a declaration in a condition is in scope from its
1259 // point of declaration until the end of the substatements controlled by the
1260 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001261 // C++ 3.3.2p4:
1262 // Names declared in the for-init-statement, and in the condition of if,
1263 // while, for, and switch statements are local to the if, while, for, or
1264 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001265 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001266 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001267 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001268 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1269 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001270 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001271 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1272 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273
1274 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001275 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001276 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001277 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001278 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001279
David Blaikiedef07622012-05-16 04:20:04 +00001280 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Stephen Hines651f13c2014-04-23 16:59:28 -07001282 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001283 // there is no compound stmt. C90 does not have this clause. We only do this
1284 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001285 //
1286 // C++ 6.5p2:
1287 // The substatement in an iteration-statement implicitly defines a local scope
1288 // which is entered and exited each time through the loop.
1289 //
1290 // See comments in ParseIfStatement for why we create a scope for the
1291 // condition and a new scope for substatement in C++.
1292 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001293 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001296 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001297
Chris Lattner0ecea032007-08-22 05:28:50 +00001298 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001299 InnerScope.Exit();
1300 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001301
John McCalld226f652010-08-21 09:40:31 +00001302 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001303 return StmtError();
1304
John McCall9ae2f072010-08-23 23:25:46 +00001305 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001306}
1307
1308/// ParseDoStatement
1309/// do-statement: [C99 6.8.5.2]
1310/// 'do' statement 'while' '(' expression ')' ';'
1311/// Note: this lets the caller parse the end ';'.
Richard Smith534986f2012-04-14 00:33:13 +00001312StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001313 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001315
Chris Lattner22153252007-08-26 23:08:06 +00001316 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1317 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001318 unsigned ScopeFlags;
David Blaikie4e4d0842012-03-11 07:00:24 +00001319 if (getLangOpts().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001320 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001321 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001322 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001323
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001324 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001325
Stephen Hines651f13c2014-04-23 16:59:28 -07001326 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001327 // there is no compound stmt. C90 does not have this clause. We only do this
1328 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001329 //
1330 // C++ 6.5p2:
1331 // The substatement in an iteration-statement implicitly defines a local scope
1332 // which is entered and exited each time through the loop.
1333 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001334 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1335 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001338 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001339
Chris Lattner0ecea032007-08-22 05:28:50 +00001340 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001341 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001342
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001343 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001344 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001345 Diag(Tok, diag::err_expected_while);
Stephen Hines651f13c2014-04-23 16:59:28 -07001346 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataev8fe24752013-11-18 08:17:37 +00001347 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner19504402008-11-13 18:52:53 +00001348 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001349 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 }
1351 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001352
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001353 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001354 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataev8fe24752013-11-18 08:17:37 +00001355 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redl9a920342008-12-11 19:48:14 +00001356 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001358
Richard Smith5eed7e02013-10-15 01:34:54 +00001359 // Parse the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001360 BalancedDelimiterTracker T(*this, tok::l_paren);
1361 T.consumeOpen();
Chad Rosierb6604462012-07-10 21:35:27 +00001362
Richard Smith5eed7e02013-10-15 01:34:54 +00001363 // A do-while expression is not a condition, so can't have attributes.
1364 DiagnoseAndSkipCXX11Attributes();
Sean Hunt2edf0a22012-06-23 05:07:58 +00001365
John McCall60d7b3a2010-08-24 06:29:42 +00001366 ExprResult Cond = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001367 T.consumeClose();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001368 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001369
Sebastian Redl9a920342008-12-11 19:48:14 +00001370 if (Cond.isInvalid() || Body.isInvalid())
1371 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001372
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001373 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1374 Cond.get(), T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001375}
1376
1377/// ParseForStatement
1378/// for-statement: [C99 6.8.5.3]
1379/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1380/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001381/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1382/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001383/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001384/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1385/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001386///
1387/// [C++] for-init-statement:
1388/// [C++] expression-statement
1389/// [C++] simple-declaration
1390///
Richard Smithad762fc2011-04-14 22:09:26 +00001391/// [C++0x] for-range-declaration:
1392/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1393/// [C++0x] for-range-initializer:
1394/// [C++0x] expression
1395/// [C++0x] braced-init-list [TODO]
Richard Smith534986f2012-04-14 00:33:13 +00001396StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001397 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001399
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001400 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001401 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001403 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001405
Chad Rosierb6604462012-07-10 21:35:27 +00001406 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1407 getLangOpts().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001408
Chris Lattner22153252007-08-26 23:08:06 +00001409 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1410 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001411 //
1412 // C++ 6.4p3:
1413 // A name introduced by a declaration in a condition is in scope from its
1414 // point of declaration until the end of the substatements controlled by the
1415 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001416 // C++ 3.3.2p4:
1417 // Names declared in the for-init-statement, and in the condition of if,
1418 // while, for, and switch statements are local to the if, while, for, or
1419 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001420 // C++ 6.5.3p1:
1421 // Names declared in the for-init-statement are in the same declarative-region
1422 // as those declared in the condition.
1423 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001424 unsigned ScopeFlags = 0;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001425 if (C99orCXXorObjC)
Stephen Hines651f13c2014-04-23 16:59:28 -07001426 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001427
1428 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001429
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001430 BalancedDelimiterTracker T(*this, tok::l_paren);
1431 T.consumeOpen();
1432
John McCall60d7b3a2010-08-24 06:29:42 +00001433 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001434
Richard Smithad762fc2011-04-14 22:09:26 +00001435 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001436 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001437 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001438 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001439 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001440 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001441 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001442 Decl *SecondVar = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001443
Douglas Gregor791215b2009-09-21 20:51:25 +00001444 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001445 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001446 C99orCXXorObjC? Sema::PCC_ForInit
1447 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001448 cutOffParsing();
1449 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001450 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001451
Sean Hunt2edf0a22012-06-23 05:07:58 +00001452 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001453 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001454
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001456 if (Tok.is(tok::semi)) { // for (;
Sean Hunt2edf0a22012-06-23 05:07:58 +00001457 ProhibitAttributes(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 // no first part, eat the ';'.
1459 ConsumeToken();
Eli Friedman9490ab42011-12-20 01:50:37 +00001460 } else if (isForInitDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001461 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001462 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001463 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001464
Richard Smithad762fc2011-04-14 22:09:26 +00001465 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikie4e4d0842012-03-11 07:00:24 +00001466 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smithad762fc2011-04-14 22:09:26 +00001467 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1468
Chris Lattner97144fc2009-04-02 04:16:50 +00001469 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001470 StmtVector Stmts;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001471 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001472 DeclEnd, attrs, false,
1473 MightBeForRangeStmt ?
1474 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001475 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Richard Smithad762fc2011-04-14 22:09:26 +00001477 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith80ad52f2013-01-02 11:42:31 +00001478 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00001479 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8f4fb192011-09-04 19:54:14 +00001480
Richard Smithad762fc2011-04-14 22:09:26 +00001481 ForRange = true;
1482 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001483 ConsumeToken();
1484 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001485 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001486 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001487 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001488
Douglas Gregorfb629412010-08-23 21:17:50 +00001489 if (Tok.is(tok::code_completion)) {
1490 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001491 cutOffParsing();
1492 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001493 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001494 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001495 } else {
1496 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001497 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001499 ProhibitAttributes(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 Value = ParseExpression();
1501
John McCallf6a16482010-12-04 03:47:34 +00001502 ForEach = isTokIdentifier_in();
1503
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001505 if (!Value.isInvalid()) {
1506 if (ForEach)
1507 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1508 else
Richard Smith41956372013-01-14 22:39:08 +00001509 FirstPart = Actions.ActOnExprStmt(Value);
John McCallf6a16482010-12-04 03:47:34 +00001510 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001511
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001512 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001514 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001515 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001516
Douglas Gregorfb629412010-08-23 21:17:50 +00001517 if (Tok.is(tok::code_completion)) {
1518 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001519 cutOffParsing();
1520 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001521 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001522 Collection = ParseExpression();
Richard Smith80ad52f2013-01-02 11:42:31 +00001523 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smitha44854a2011-12-20 22:56:20 +00001524 // User tried to write the reasonable, but ill-formed, for-range-statement
1525 // for (expr : expr) { ... }
1526 Diag(Tok, diag::err_for_range_expected_decl)
1527 << FirstPart.get()->getSourceRange();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001528 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smitha44854a2011-12-20 22:56:20 +00001529 SecondPartIsInvalid = true;
Chris Lattner682bf922009-03-29 16:50:03 +00001530 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001531 if (!Value.isInvalid()) {
1532 Diag(Tok, diag::err_expected_semi_for);
1533 } else {
1534 // Skip until semicolon or rparen, don't consume it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001535 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregorb72c7782011-02-17 03:38:46 +00001536 if (Tok.is(tok::semi))
1537 ConsumeToken();
1538 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 }
1540 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001541
1542 // Parse the second part of the for specifier.
1543 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smithad762fc2011-04-14 22:09:26 +00001544 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001545 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001546 // Parse the second part of the for specifier.
1547 if (Tok.is(tok::semi)) { // for (...;;
1548 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001549 } else if (Tok.is(tok::r_paren)) {
1550 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001551 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001552 ExprResult Second;
David Blaikie4e4d0842012-03-11 07:00:24 +00001553 if (getLangOpts().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001554 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1555 else {
1556 Second = ParseExpression();
1557 if (!Second.isInvalid())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001558 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001559 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001560 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001561 SecondPartIsInvalid = Second.isInvalid();
David Blaikiedef07622012-05-16 04:20:04 +00001562 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001563 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001564
Douglas Gregorb72c7782011-02-17 03:38:46 +00001565 if (Tok.isNot(tok::semi)) {
1566 if (!SecondPartIsInvalid || SecondVar)
1567 Diag(Tok, diag::err_expected_semi_for);
1568 else
1569 // Skip until semicolon or rparen, don't consume it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001570 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregorb72c7782011-02-17 03:38:46 +00001571 }
1572
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001573 if (Tok.is(tok::semi)) {
1574 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001575 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001576
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001577 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001578 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001579 ExprResult Third = ParseExpression();
Richard Smith41956372013-01-14 22:39:08 +00001580 // FIXME: The C++11 standard doesn't actually say that this is a
1581 // discarded-value expression, but it clearly should be.
1582 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001583 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001585 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001586 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001587
Richard Smithad762fc2011-04-14 22:09:26 +00001588 // We need to perform most of the semantic analysis for a C++0x for-range
1589 // statememt before parsing the body, in order to be able to deduce the type
1590 // of an auto-typed loop variable.
1591 StmtResult ForRangeStmt;
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001592 StmtResult ForEachStmt;
Chad Rosierb6604462012-07-10 21:35:27 +00001593
John McCall990567c2011-07-27 01:07:15 +00001594 if (ForRange) {
Sam Panzerbc20bbb2012-08-16 21:47:25 +00001595 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(),
Richard Smithad762fc2011-04-14 22:09:26 +00001596 ForRangeInit.ColonLoc,
1597 ForRangeInit.RangeExpr.get(),
Richard Smith8b533d92012-09-20 21:52:32 +00001598 T.getCloseLocation(),
1599 Sema::BFRK_Build);
Richard Smithad762fc2011-04-14 22:09:26 +00001600
John McCall990567c2011-07-27 01:07:15 +00001601
1602 // Similarly, we need to do the semantic analysis for a for-range
1603 // statement immediately in order to close over temporaries correctly.
1604 } else if (ForEach) {
Sam Panzerbc20bbb2012-08-16 21:47:25 +00001605 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001606 FirstPart.take(),
Chad Rosierb6604462012-07-10 21:35:27 +00001607 Collection.take(),
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001608 T.getCloseLocation());
John McCall990567c2011-07-27 01:07:15 +00001609 }
1610
Stephen Hines651f13c2014-04-23 16:59:28 -07001611 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001612 // there is no compound stmt. C90 does not have this clause. We only do this
1613 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001614 //
1615 // C++ 6.5p2:
1616 // The substatement in an iteration-statement implicitly defines a local scope
1617 // which is entered and exited each time through the loop.
1618 //
1619 // See comments in ParseIfStatement for why we create a scope for
1620 // for-init-statement/condition and a new scope for substatement in C++.
1621 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001622 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1623 Tok.is(tok::l_brace));
1624
1625 // The body of the for loop has the same local mangling number as the
1626 // for-init-statement.
1627 // It will only be incremented if the body contains other things that would
1628 // normally increment the mangling number (like a compound statement).
1629 if (C99orCXXorObjC)
1630 getCurScope()->decrementMSLocalManglingNumber();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001631
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001633 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001634
Chris Lattner0ecea032007-08-22 05:28:50 +00001635 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001636 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001637
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001639 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001640
1641 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001642 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001643
Richard Smithad762fc2011-04-14 22:09:26 +00001644 if (ForEach)
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001645 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1646 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Richard Smithad762fc2011-04-14 22:09:26 +00001648 if (ForRange)
1649 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1650
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001651 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1652 SecondPart, SecondVar, ThirdPart,
1653 T.getCloseLocation(), Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001654}
1655
1656/// ParseGotoStatement
1657/// jump-statement:
1658/// 'goto' identifier ';'
1659/// [GNU] 'goto' '*' expression ';'
1660///
1661/// Note: this lets the caller parse the end ';'.
1662///
Richard Smith534986f2012-04-14 00:33:13 +00001663StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001664 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001666
John McCall60d7b3a2010-08-24 06:29:42 +00001667 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001668 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001669 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1670 Tok.getLocation());
1671 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001673 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 // GNU indirect goto extension.
1675 Diag(Tok, diag::ext_gnu_indirect_goto);
1676 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001677 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001678 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001679 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redl9a920342008-12-11 19:48:14 +00001680 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 }
John McCall9ae2f072010-08-23 23:25:46 +00001682 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001683 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -07001684 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redl9a920342008-12-11 19:48:14 +00001685 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001687
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001688 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001689}
1690
1691/// ParseContinueStatement
1692/// jump-statement:
1693/// 'continue' ';'
1694///
1695/// Note: this lets the caller parse the end ';'.
1696///
Richard Smith534986f2012-04-14 00:33:13 +00001697StmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001699 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001700}
1701
1702/// ParseBreakStatement
1703/// jump-statement:
1704/// 'break' ';'
1705///
1706/// Note: this lets the caller parse the end ';'.
1707///
Richard Smith534986f2012-04-14 00:33:13 +00001708StmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001710 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001711}
1712
1713/// ParseReturnStatement
1714/// jump-statement:
1715/// 'return' expression[opt] ';'
Richard Smith534986f2012-04-14 00:33:13 +00001716StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001717 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001718 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001719
John McCall60d7b3a2010-08-24 06:29:42 +00001720 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001721 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001722 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001723 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001724 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001725 return StmtError();
1726 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001727
David Blaikie4e4d0842012-03-11 07:00:24 +00001728 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001729 R = ParseInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001730 if (R.isUsable())
Richard Smith80ad52f2013-01-02 11:42:31 +00001731 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00001732 diag::warn_cxx98_compat_generalized_initializer_lists :
1733 diag::ext_generalized_initializer_lists)
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001734 << R.get()->getSourceRange();
1735 } else
1736 R = ParseExpression();
Stephen Hines651f13c2014-04-23 16:59:28 -07001737 if (R.isInvalid()) {
1738 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redl9a920342008-12-11 19:48:14 +00001739 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 }
1741 }
John McCall9ae2f072010-08-23 23:25:46 +00001742 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001743}
1744
John McCallaeeacf72013-05-03 00:10:13 +00001745namespace {
1746 class ClangAsmParserCallback : public llvm::MCAsmParserSemaCallback {
1747 Parser &TheParser;
1748 SourceLocation AsmLoc;
1749 StringRef AsmString;
1750
1751 /// The tokens we streamed into AsmString and handed off to MC.
1752 ArrayRef<Token> AsmToks;
1753
1754 /// The offset of each token in AsmToks within AsmString.
1755 ArrayRef<unsigned> AsmTokOffsets;
1756
1757 public:
1758 ClangAsmParserCallback(Parser &P, SourceLocation Loc,
1759 StringRef AsmString,
1760 ArrayRef<Token> Toks,
1761 ArrayRef<unsigned> Offsets)
1762 : TheParser(P), AsmLoc(Loc), AsmString(AsmString),
1763 AsmToks(Toks), AsmTokOffsets(Offsets) {
1764 assert(AsmToks.size() == AsmTokOffsets.size());
1765 }
1766
1767 void *LookupInlineAsmIdentifier(StringRef &LineBuf,
1768 InlineAsmIdentifierInfo &Info,
Stephen Hines651f13c2014-04-23 16:59:28 -07001769 bool IsUnevaluatedContext) override {
John McCallaeeacf72013-05-03 00:10:13 +00001770 // Collect the desired tokens.
1771 SmallVector<Token, 16> LineToks;
1772 const Token *FirstOrigToken = 0;
1773 findTokensForString(LineBuf, LineToks, FirstOrigToken);
1774
1775 unsigned NumConsumedToks;
1776 ExprResult Result =
1777 TheParser.ParseMSAsmIdentifier(LineToks, NumConsumedToks, &Info,
1778 IsUnevaluatedContext);
1779
1780 // If we consumed the entire line, tell MC that.
1781 // Also do this if we consumed nothing as a way of reporting failure.
1782 if (NumConsumedToks == 0 || NumConsumedToks == LineToks.size()) {
1783 // By not modifying LineBuf, we're implicitly consuming it all.
1784
1785 // Otherwise, consume up to the original tokens.
1786 } else {
1787 assert(FirstOrigToken && "not using original tokens?");
1788
1789 // Since we're using original tokens, apply that offset.
1790 assert(FirstOrigToken[NumConsumedToks].getLocation()
1791 == LineToks[NumConsumedToks].getLocation());
1792 unsigned FirstIndex = FirstOrigToken - AsmToks.begin();
1793 unsigned LastIndex = FirstIndex + NumConsumedToks - 1;
1794
1795 // The total length we've consumed is the relative offset
1796 // of the last token we consumed plus its length.
1797 unsigned TotalOffset = (AsmTokOffsets[LastIndex]
1798 + AsmToks[LastIndex].getLength()
1799 - AsmTokOffsets[FirstIndex]);
1800 LineBuf = LineBuf.substr(0, TotalOffset);
1801 }
1802
1803 // Initialize the "decl" with the lookup result.
1804 Info.OpDecl = static_cast<void*>(Result.take());
1805 return Info.OpDecl;
1806 }
1807
1808 bool LookupInlineAsmField(StringRef Base, StringRef Member,
Stephen Hines651f13c2014-04-23 16:59:28 -07001809 unsigned &Offset) override {
John McCallaeeacf72013-05-03 00:10:13 +00001810 return TheParser.getActions().LookupInlineAsmField(Base, Member,
1811 Offset, AsmLoc);
1812 }
1813
1814 static void DiagHandlerCallback(const llvm::SMDiagnostic &D,
1815 void *Context) {
1816 ((ClangAsmParserCallback*) Context)->handleDiagnostic(D);
1817 }
1818
1819 private:
1820 /// Collect the appropriate tokens for the given string.
1821 void findTokensForString(StringRef Str, SmallVectorImpl<Token> &TempToks,
1822 const Token *&FirstOrigToken) const {
1823 // For now, assert that the string we're working with is a substring
1824 // of what we gave to MC. This lets us use the original tokens.
1825 assert(!std::less<const char*>()(Str.begin(), AsmString.begin()) &&
1826 !std::less<const char*>()(AsmString.end(), Str.end()));
1827
1828 // Try to find a token whose offset matches the first token.
1829 unsigned FirstCharOffset = Str.begin() - AsmString.begin();
1830 const unsigned *FirstTokOffset
1831 = std::lower_bound(AsmTokOffsets.begin(), AsmTokOffsets.end(),
1832 FirstCharOffset);
1833
1834 // For now, assert that the start of the string exactly
1835 // corresponds to the start of a token.
1836 assert(*FirstTokOffset == FirstCharOffset);
1837
1838 // Use all the original tokens for this line. (We assume the
1839 // end of the line corresponds cleanly to a token break.)
1840 unsigned FirstTokIndex = FirstTokOffset - AsmTokOffsets.begin();
1841 FirstOrigToken = &AsmToks[FirstTokIndex];
1842 unsigned LastCharOffset = Str.end() - AsmString.begin();
1843 for (unsigned i = FirstTokIndex, e = AsmTokOffsets.size(); i != e; ++i) {
1844 if (AsmTokOffsets[i] >= LastCharOffset) break;
1845 TempToks.push_back(AsmToks[i]);
1846 }
1847 }
1848
1849 void handleDiagnostic(const llvm::SMDiagnostic &D) {
1850 // Compute an offset into the inline asm buffer.
1851 // FIXME: This isn't right if .macro is involved (but hopefully, no
1852 // real-world code does that).
1853 const llvm::SourceMgr &LSM = *D.getSourceMgr();
1854 const llvm::MemoryBuffer *LBuf =
1855 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
1856 unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
1857
1858 // Figure out which token that offset points into.
1859 const unsigned *TokOffsetPtr =
1860 std::lower_bound(AsmTokOffsets.begin(), AsmTokOffsets.end(), Offset);
1861 unsigned TokIndex = TokOffsetPtr - AsmTokOffsets.begin();
1862 unsigned TokOffset = *TokOffsetPtr;
1863
1864 // If we come up with an answer which seems sane, use it; otherwise,
1865 // just point at the __asm keyword.
1866 // FIXME: Assert the answer is sane once we handle .macro correctly.
1867 SourceLocation Loc = AsmLoc;
1868 if (TokIndex < AsmToks.size()) {
1869 const Token &Tok = AsmToks[TokIndex];
1870 Loc = Tok.getLocation();
1871 Loc = Loc.getLocWithOffset(Offset - TokOffset);
1872 }
1873 TheParser.Diag(Loc, diag::err_inline_ms_asm_parsing)
1874 << D.getMessage();
1875 }
1876 };
1877}
1878
1879/// Parse an identifier in an MS-style inline assembly block.
1880///
1881/// \param CastInfo - a void* so that we don't have to teach Parser.h
1882/// about the actual type.
1883ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl<Token> &LineToks,
1884 unsigned &NumLineToksConsumed,
1885 void *CastInfo,
1886 bool IsUnevaluatedContext) {
1887 llvm::InlineAsmIdentifierInfo &Info =
1888 *(llvm::InlineAsmIdentifierInfo *) CastInfo;
1889
1890 // Push a fake token on the end so that we don't overrun the token
1891 // stream. We use ';' because it expression-parsing should never
1892 // overrun it.
1893 const tok::TokenKind EndOfStream = tok::semi;
1894 Token EndOfStreamTok;
1895 EndOfStreamTok.startToken();
1896 EndOfStreamTok.setKind(EndOfStream);
1897 LineToks.push_back(EndOfStreamTok);
1898
1899 // Also copy the current token over.
1900 LineToks.push_back(Tok);
1901
1902 PP.EnterTokenStream(LineToks.begin(),
1903 LineToks.size(),
1904 /*disable macros*/ true,
1905 /*owns tokens*/ false);
1906
1907 // Clear the current token and advance to the first token in LineToks.
1908 ConsumeAnyToken();
1909
1910 // Parse an optional scope-specifier if we're in C++.
1911 CXXScopeSpec SS;
1912 if (getLangOpts().CPlusPlus) {
1913 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
1914 }
1915
1916 // Require an identifier here.
1917 SourceLocation TemplateKWLoc;
1918 UnqualifiedId Id;
1919 bool Invalid = ParseUnqualifiedId(SS,
1920 /*EnteringContext=*/false,
1921 /*AllowDestructorName=*/false,
1922 /*AllowConstructorName=*/false,
1923 /*ObjectType=*/ ParsedType(),
1924 TemplateKWLoc,
1925 Id);
1926
Stephen Hines651f13c2014-04-23 16:59:28 -07001927 // Figure out how many tokens we are into LineToks.
1928 unsigned LineIndex = 0;
1929 if (Tok.is(EndOfStream)) {
1930 LineIndex = LineToks.size() - 2;
John McCallaeeacf72013-05-03 00:10:13 +00001931 } else {
John McCallaeeacf72013-05-03 00:10:13 +00001932 while (LineToks[LineIndex].getLocation() != Tok.getLocation()) {
1933 LineIndex++;
1934 assert(LineIndex < LineToks.size() - 2); // we added two extra tokens
1935 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001936 }
John McCallaeeacf72013-05-03 00:10:13 +00001937
Stephen Hines651f13c2014-04-23 16:59:28 -07001938 // If we've run into the poison token we inserted before, or there
1939 // was a parsing error, then claim the entire line.
1940 if (Invalid || Tok.is(EndOfStream)) {
1941 NumLineToksConsumed = LineToks.size() - 2;
1942 } else {
1943 // Otherwise, claim up to the start of the next token.
John McCallaeeacf72013-05-03 00:10:13 +00001944 NumLineToksConsumed = LineIndex;
1945 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001946
1947 // Finally, restore the old parsing state by consuming all the tokens we
1948 // staged before, implicitly killing off the token-lexer we pushed.
1949 for (unsigned i = 0, e = LineToks.size() - LineIndex - 2; i != e; ++i) {
John McCallaeeacf72013-05-03 00:10:13 +00001950 ConsumeAnyToken();
1951 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001952 assert(Tok.is(EndOfStream));
1953 ConsumeToken();
John McCallaeeacf72013-05-03 00:10:13 +00001954
1955 // Leave LineToks in its original state.
1956 LineToks.pop_back();
1957 LineToks.pop_back();
1958
1959 // Perform the lookup.
1960 return Actions.LookupInlineAsmIdentifier(SS, TemplateKWLoc, Id, Info,
1961 IsUnevaluatedContext);
1962}
1963
1964/// Turn a sequence of our tokens back into a string that we can hand
1965/// to the MC asm parser.
1966static bool buildMSAsmString(Preprocessor &PP,
1967 SourceLocation AsmLoc,
1968 ArrayRef<Token> AsmToks,
1969 SmallVectorImpl<unsigned> &TokOffsets,
1970 SmallString<512> &Asm) {
1971 assert (!AsmToks.empty() && "Didn't expect an empty AsmToks!");
1972
1973 // Is this the start of a new assembly statement?
1974 bool isNewStatement = true;
1975
1976 for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
1977 const Token &Tok = AsmToks[i];
1978
1979 // Start each new statement with a newline and a tab.
1980 if (!isNewStatement &&
1981 (Tok.is(tok::kw_asm) || Tok.isAtStartOfLine())) {
1982 Asm += "\n\t";
1983 isNewStatement = true;
1984 }
1985
1986 // Preserve the existence of leading whitespace except at the
1987 // start of a statement.
1988 if (!isNewStatement && Tok.hasLeadingSpace())
1989 Asm += ' ';
1990
1991 // Remember the offset of this token.
1992 TokOffsets.push_back(Asm.size());
1993
1994 // Don't actually write '__asm' into the assembly stream.
1995 if (Tok.is(tok::kw_asm)) {
1996 // Complain about __asm at the end of the stream.
1997 if (i + 1 == e) {
1998 PP.Diag(AsmLoc, diag::err_asm_empty);
1999 return true;
2000 }
2001
2002 continue;
2003 }
2004
2005 // Append the spelling of the token.
2006 SmallString<32> SpellingBuffer;
2007 bool SpellingInvalid = false;
2008 Asm += PP.getSpelling(Tok, SpellingBuffer, &SpellingInvalid);
2009 assert(!SpellingInvalid && "spelling was invalid after correct parse?");
2010
2011 // We are no longer at the start of a statement.
2012 isNewStatement = false;
2013 }
2014
2015 // Ensure that the buffer is null-terminated.
2016 Asm.push_back('\0');
2017 Asm.pop_back();
2018
2019 assert(TokOffsets.size() == AsmToks.size());
2020 return false;
2021}
2022
Eli Friedman3fedbe12011-09-30 01:13:51 +00002023/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
2024/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier8cd64b42012-06-11 20:47:18 +00002025///
2026/// [MS] ms-asm-statement:
2027/// ms-asm-block
2028/// ms-asm-block ms-asm-statement
2029///
2030/// [MS] ms-asm-block:
2031/// '__asm' ms-asm-line '\n'
2032/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
2033///
2034/// [MS] ms-asm-instruction-block
2035/// ms-asm-line
2036/// ms-asm-line '\n' ms-asm-instruction-block
2037///
Eli Friedman3fedbe12011-09-30 01:13:51 +00002038StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
2039 SourceManager &SrcMgr = PP.getSourceManager();
2040 SourceLocation EndLoc = AsmLoc;
Chad Rosier8cd64b42012-06-11 20:47:18 +00002041 SmallVector<Token, 4> AsmToks;
Chad Rosier21ef7112012-08-14 19:22:06 +00002042
2043 bool InBraces = false;
2044 unsigned short savedBraceCount = 0;
2045 bool InAsmComment = false;
2046 FileID FID;
2047 unsigned LineNo = 0;
2048 unsigned NumTokensRead = 0;
2049 SourceLocation LBraceLoc;
2050
2051 if (Tok.is(tok::l_brace)) {
2052 // Braced inline asm: consume the opening brace.
2053 InBraces = true;
2054 savedBraceCount = BraceCount;
2055 EndLoc = LBraceLoc = ConsumeBrace();
2056 ++NumTokensRead;
2057 } else {
2058 // Single-line inline asm; compute which line it is on.
2059 std::pair<FileID, unsigned> ExpAsmLoc =
2060 SrcMgr.getDecomposedExpansionLoc(EndLoc);
2061 FID = ExpAsmLoc.first;
2062 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
2063 }
2064
2065 SourceLocation TokLoc = Tok.getLocation();
Eli Friedman3fedbe12011-09-30 01:13:51 +00002066 do {
Chad Rosier21ef7112012-08-14 19:22:06 +00002067 // If we hit EOF, we're done, period.
Stephen Hines651f13c2014-04-23 16:59:28 -07002068 if (isEofOrEom())
Eli Friedman3fedbe12011-09-30 01:13:51 +00002069 break;
Chad Rosier21ef7112012-08-14 19:22:06 +00002070
Chad Rosier21ef7112012-08-14 19:22:06 +00002071 if (!InAsmComment && Tok.is(tok::semi)) {
2072 // A semicolon in an asm is the start of a comment.
2073 InAsmComment = true;
2074 if (InBraces) {
2075 // Compute which line the comment is on.
2076 std::pair<FileID, unsigned> ExpSemiLoc =
2077 SrcMgr.getDecomposedExpansionLoc(TokLoc);
2078 FID = ExpSemiLoc.first;
2079 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
2080 }
2081 } else if (!InBraces || InAsmComment) {
2082 // If end-of-line is significant, check whether this token is on a
2083 // new line.
2084 std::pair<FileID, unsigned> ExpLoc =
2085 SrcMgr.getDecomposedExpansionLoc(TokLoc);
2086 if (ExpLoc.first != FID ||
2087 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
2088 // If this is a single-line __asm, we're done.
2089 if (!InBraces)
2090 break;
2091 // We're no longer in a comment.
2092 InAsmComment = false;
2093 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
2094 // Single-line asm always ends when a closing brace is seen.
2095 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
2096 // does MSVC do here?
2097 break;
2098 }
2099 }
2100 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
2101 BraceCount == (savedBraceCount + 1)) {
2102 // Consume the closing brace, and finish
2103 EndLoc = ConsumeBrace();
2104 break;
2105 }
2106
2107 // Consume the next token; make sure we don't modify the brace count etc.
2108 // if we are in a comment.
2109 EndLoc = TokLoc;
2110 if (InAsmComment)
2111 PP.Lex(Tok);
2112 else {
2113 AsmToks.push_back(Tok);
2114 ConsumeAnyToken();
2115 }
2116 TokLoc = Tok.getLocation();
2117 ++NumTokensRead;
Eli Friedman3fedbe12011-09-30 01:13:51 +00002118 } while (1);
Chad Rosier8cd64b42012-06-11 20:47:18 +00002119
Chad Rosier21ef7112012-08-14 19:22:06 +00002120 if (InBraces && BraceCount != savedBraceCount) {
2121 // __asm without closing brace (this can happen at EOF).
Stephen Hines651f13c2014-04-23 16:59:28 -07002122 Diag(Tok, diag::err_expected) << tok::r_brace;
2123 Diag(LBraceLoc, diag::note_matching) << tok::l_brace;
Chad Rosier21ef7112012-08-14 19:22:06 +00002124 return StmtError();
2125 } else if (NumTokensRead == 0) {
2126 // Empty __asm.
Stephen Hines651f13c2014-04-23 16:59:28 -07002127 Diag(Tok, diag::err_expected) << tok::l_brace;
Chad Rosier21ef7112012-08-14 19:22:06 +00002128 return StmtError();
2129 }
2130
John McCallaeeacf72013-05-03 00:10:13 +00002131 // Okay, prepare to use MC to parse the assembly.
2132 SmallVector<StringRef, 4> ConstraintRefs;
2133 SmallVector<Expr*, 4> Exprs;
2134 SmallVector<StringRef, 4> ClobberRefs;
2135
2136 // We need an actual supported target.
Stephen Hines651f13c2014-04-23 16:59:28 -07002137 const llvm::Triple &TheTriple = Actions.Context.getTargetInfo().getTriple();
John McCallaeeacf72013-05-03 00:10:13 +00002138 llvm::Triple::ArchType ArchTy = TheTriple.getArch();
Alp Tokerc94b5ae2013-10-30 15:07:10 +00002139 const std::string &TT = TheTriple.getTriple();
2140 const llvm::Target *TheTarget = 0;
John McCallaeeacf72013-05-03 00:10:13 +00002141 bool UnsupportedArch = (ArchTy != llvm::Triple::x86 &&
2142 ArchTy != llvm::Triple::x86_64);
Alp Tokerc94b5ae2013-10-30 15:07:10 +00002143 if (UnsupportedArch) {
John McCallaeeacf72013-05-03 00:10:13 +00002144 Diag(AsmLoc, diag::err_msasm_unsupported_arch) << TheTriple.getArchName();
Alp Tokerc94b5ae2013-10-30 15:07:10 +00002145 } else {
2146 std::string Error;
2147 TheTarget = llvm::TargetRegistry::lookupTarget(TT, Error);
2148 if (!TheTarget)
2149 Diag(AsmLoc, diag::err_msasm_unable_to_create_target) << Error;
2150 }
Alp Toker25973152013-10-30 14:29:28 +00002151
John McCallaeeacf72013-05-03 00:10:13 +00002152 // If we don't support assembly, or the assembly is empty, we don't
2153 // need to instantiate the AsmParser, etc.
Alp Tokerc94b5ae2013-10-30 15:07:10 +00002154 if (!TheTarget || AsmToks.empty()) {
John McCallaeeacf72013-05-03 00:10:13 +00002155 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, StringRef(),
2156 /*NumOutputs*/ 0, /*NumInputs*/ 0,
2157 ConstraintRefs, ClobberRefs, Exprs, EndLoc);
2158 }
2159
2160 // Expand the tokens into a string buffer.
2161 SmallString<512> AsmString;
2162 SmallVector<unsigned, 8> TokOffsets;
2163 if (buildMSAsmString(PP, AsmLoc, AsmToks, TokOffsets, AsmString))
2164 return StmtError();
2165
Stephen Hines651f13c2014-04-23 16:59:28 -07002166 std::unique_ptr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
2167 std::unique_ptr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TT));
Joey Gouly12981a72013-09-12 10:59:24 +00002168 // Get the instruction descriptor.
Stephen Hines651f13c2014-04-23 16:59:28 -07002169 const llvm::MCInstrInfo *MII = TheTarget->createMCInstrInfo();
2170 std::unique_ptr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo());
2171 std::unique_ptr<llvm::MCSubtargetInfo> STI(
2172 TheTarget->createMCSubtargetInfo(TT, "", ""));
John McCallaeeacf72013-05-03 00:10:13 +00002173
2174 llvm::SourceMgr TempSrcMgr;
Bill Wendling4b7bae32013-06-18 07:22:05 +00002175 llvm::MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &TempSrcMgr);
John McCallaeeacf72013-05-03 00:10:13 +00002176 llvm::MemoryBuffer *Buffer =
2177 llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>");
2178
2179 // Tell SrcMgr about this buffer, which is what the parser will pick up.
2180 TempSrcMgr.AddNewSourceBuffer(Buffer, llvm::SMLoc());
2181
Stephen Hines651f13c2014-04-23 16:59:28 -07002182 std::unique_ptr<llvm::MCStreamer> Str(createNullStreamer(Ctx));
2183 std::unique_ptr<llvm::MCAsmParser> Parser(
2184 createMCAsmParser(TempSrcMgr, Ctx, *Str.get(), *MAI));
2185 std::unique_ptr<llvm::MCTargetAsmParser> TargetParser(
2186 TheTarget->createMCAsmParser(*STI, *Parser, *MII));
John McCallaeeacf72013-05-03 00:10:13 +00002187
John McCallaeeacf72013-05-03 00:10:13 +00002188 llvm::MCInstPrinter *IP =
2189 TheTarget->createMCInstPrinter(1, *MAI, *MII, *MRI, *STI);
2190
2191 // Change to the Intel dialect.
2192 Parser->setAssemblerDialect(1);
2193 Parser->setTargetParser(*TargetParser.get());
2194 Parser->setParsingInlineAsm(true);
2195 TargetParser->setParsingInlineAsm(true);
2196
2197 ClangAsmParserCallback Callback(*this, AsmLoc, AsmString,
2198 AsmToks, TokOffsets);
2199 TargetParser->setSemaCallback(&Callback);
2200 TempSrcMgr.setDiagHandler(ClangAsmParserCallback::DiagHandlerCallback,
2201 &Callback);
2202
2203 unsigned NumOutputs;
2204 unsigned NumInputs;
2205 std::string AsmStringIR;
2206 SmallVector<std::pair<void *, bool>, 4> OpExprs;
2207 SmallVector<std::string, 4> Constraints;
2208 SmallVector<std::string, 4> Clobbers;
2209 if (Parser->parseMSInlineAsm(AsmLoc.getPtrEncoding(), AsmStringIR,
2210 NumOutputs, NumInputs, OpExprs, Constraints,
2211 Clobbers, MII, IP, Callback))
2212 return StmtError();
2213
Stephen Hines651f13c2014-04-23 16:59:28 -07002214 // Filter out "fpsw". Clang doesn't accept it, and it always lists flags and
2215 // fpsr as clobbers.
2216 auto End = std::remove(Clobbers.begin(), Clobbers.end(), "fpsw");
2217 Clobbers.erase(End, Clobbers.end());
2218
John McCallaeeacf72013-05-03 00:10:13 +00002219 // Build the vector of clobber StringRefs.
2220 unsigned NumClobbers = Clobbers.size();
2221 ClobberRefs.resize(NumClobbers);
2222 for (unsigned i = 0; i != NumClobbers; ++i)
2223 ClobberRefs[i] = StringRef(Clobbers[i]);
2224
2225 // Recast the void pointers and build the vector of constraint StringRefs.
2226 unsigned NumExprs = NumOutputs + NumInputs;
2227 ConstraintRefs.resize(NumExprs);
2228 Exprs.resize(NumExprs);
2229 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
2230 Expr *OpExpr = static_cast<Expr *>(OpExprs[i].first);
2231 if (!OpExpr)
2232 return StmtError();
2233
2234 // Need address of variable.
2235 if (OpExprs[i].second)
2236 OpExpr = Actions.BuildUnaryOp(getCurScope(), AsmLoc, UO_AddrOf, OpExpr)
2237 .take();
2238
2239 ConstraintRefs[i] = StringRef(Constraints[i]);
2240 Exprs[i] = OpExpr;
2241 }
2242
Chad Rosier8f726de2012-08-06 20:03:45 +00002243 // FIXME: We should be passing source locations for better diagnostics.
John McCallaeeacf72013-05-03 00:10:13 +00002244 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc, AsmToks, AsmStringIR,
2245 NumOutputs, NumInputs,
2246 ConstraintRefs, ClobberRefs, Exprs, EndLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00002247}
2248
Reid Spencer5f016e22007-07-11 17:01:13 +00002249/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00002250/// asm-statement:
2251/// gnu-asm-statement
2252/// ms-asm-statement
2253///
2254/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00002255/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
2256///
2257/// [GNU] asm-argument:
2258/// asm-string-literal
2259/// asm-string-literal ':' asm-operands[opt]
2260/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
2261/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
2262/// ':' asm-clobbers
2263///
2264/// [GNU] asm-clobbers:
2265/// asm-string-literal
2266/// asm-clobbers ',' asm-string-literal
2267///
John McCall60d7b3a2010-08-24 06:29:42 +00002268StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00002269 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00002270 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00002271
Chad Rosier15490fd2012-12-05 21:08:21 +00002272 if (getLangOpts().AsmBlocks && Tok.isNot(tok::l_paren) &&
Chad Rosierb6604462012-07-10 21:35:27 +00002273 !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00002274 msAsm = true;
Eli Friedman3fedbe12011-09-30 01:13:51 +00002275 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00002276 }
John McCall0b7e6782011-03-24 11:26:52 +00002277 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002278 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00002279 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00002280
Reid Spencer5f016e22007-07-11 17:01:13 +00002281 // GNU asms accept, but warn, about type-qualifiers other than volatile.
2282 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002283 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002284 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002285 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00002286 // FIXME: Once GCC supports _Atomic, check whether it permits it here.
2287 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
2288 Diag(Loc, diag::w_asm_qualifier_ignored) << "_Atomic";
Sebastian Redl9a920342008-12-11 19:48:14 +00002289
Reid Spencer5f016e22007-07-11 17:01:13 +00002290 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00002291 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00002292 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002293 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Alexey Bataev8fe24752013-11-18 08:17:37 +00002294 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl9a920342008-12-11 19:48:14 +00002295 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002296 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002297 BalancedDelimiterTracker T(*this, tok::l_paren);
2298 T.consumeOpen();
Sebastian Redl9a920342008-12-11 19:48:14 +00002299
John McCall60d7b3a2010-08-24 06:29:42 +00002300 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenek320fa4b2011-12-02 01:30:14 +00002301 if (AsmString.isInvalid()) {
Richard Smith99831e42012-03-06 03:21:47 +00002302 // Consume up to and including the closing paren.
2303 T.skipToEnd();
Sebastian Redl9a920342008-12-11 19:48:14 +00002304 return StmtError();
Ted Kremenek320fa4b2011-12-02 01:30:14 +00002305 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002306
Chris Lattner5f9e2722011-07-23 10:55:15 +00002307 SmallVector<IdentifierInfo *, 4> Names;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002308 ExprVector Constraints;
2309 ExprVector Exprs;
2310 ExprVector Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00002311
Anders Carlssondfab34a2008-02-05 23:03:50 +00002312 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00002313 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002314 T.consumeClose();
Chad Rosierdf5faf52012-08-25 00:11:56 +00002315 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
2316 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
2317 Constraints, Exprs, AsmString.take(),
2318 Clobbers, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00002320
Chris Lattner64cb4752009-12-20 23:00:41 +00002321 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00002322 bool AteExtraColon = false;
2323 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
2324 // In C++ mode, parse "::" like ": :".
2325 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00002326 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002327
Chris Lattner64056462009-12-20 23:08:04 +00002328 if (!AteExtraColon &&
2329 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00002330 return StmtError();
2331 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002332
Chris Lattner64cb4752009-12-20 23:00:41 +00002333 unsigned NumOutputs = Names.size();
2334
2335 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00002336 if (AteExtraColon ||
2337 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
2338 // In C++ mode, parse "::" like ": :".
2339 if (AteExtraColon)
2340 AteExtraColon = false;
2341 else {
2342 AteExtraColon = Tok.is(tok::coloncolon);
2343 ConsumeToken();
2344 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002345
Chris Lattner64056462009-12-20 23:08:04 +00002346 if (!AteExtraColon &&
2347 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00002348 return StmtError();
2349 }
2350
2351 assert(Names.size() == Constraints.size() &&
2352 Constraints.size() == Exprs.size() &&
2353 "Input operand size mismatch!");
2354
2355 unsigned NumInputs = Names.size() - NumOutputs;
2356
2357 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00002358 if (AteExtraColon || Tok.is(tok::colon)) {
2359 if (!AteExtraColon)
2360 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00002361
Chandler Carruth102e1b62010-07-22 07:11:21 +00002362 // Parse the asm-string list for clobbers if present.
2363 if (Tok.isNot(tok::r_paren)) {
2364 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00002365 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00002366
Chandler Carruth102e1b62010-07-22 07:11:21 +00002367 if (Clobber.isInvalid())
2368 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00002369
Chandler Carruth102e1b62010-07-22 07:11:21 +00002370 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00002371
Stephen Hines651f13c2014-04-23 16:59:28 -07002372 if (!TryConsumeToken(tok::comma))
2373 break;
Chandler Carruth102e1b62010-07-22 07:11:21 +00002374 }
Chris Lattner64cb4752009-12-20 23:00:41 +00002375 }
2376 }
2377
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002378 T.consumeClose();
Chad Rosierdf5faf52012-08-25 00:11:56 +00002379 return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs,
2380 NumInputs, Names.data(), Constraints, Exprs,
2381 AsmString.take(), Clobbers,
2382 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002383}
2384
2385/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00002386/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00002387///
2388/// [GNU] asm-operands:
2389/// asm-operand
2390/// asm-operands ',' asm-operand
2391///
2392/// [GNU] asm-operand:
2393/// asm-string-literal '(' expression ')'
2394/// '[' identifier ']' asm-string-literal '(' expression ')'
2395///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00002396//
2397// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002398bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieuf81e5a92011-09-09 02:00:50 +00002399 SmallVectorImpl<Expr *> &Constraints,
2400 SmallVectorImpl<Expr *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002401 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00002402 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00002403 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002404
2405 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002406 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00002407 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002408 BalancedDelimiterTracker T(*this, tok::l_square);
2409 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Chris Lattner4e1d99a2007-10-09 17:41:39 +00002411 if (Tok.isNot(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002412 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev8fe24752013-11-18 08:17:37 +00002413 SkipUntil(tok::r_paren, StopAtSemi);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00002414 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002415 }
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Anders Carlssonb235fc22007-11-22 01:36:19 +00002417 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00002418 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00002419
Anders Carlssonff93dbd2010-01-30 22:25:16 +00002420 Names.push_back(II);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002421 T.consumeClose();
Anders Carlssonb235fc22007-11-22 01:36:19 +00002422 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00002423 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002424
John McCall60d7b3a2010-08-24 06:29:42 +00002425 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002426 if (Constraint.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002427 SkipUntil(tok::r_paren, StopAtSemi);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00002428 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00002429 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00002430 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002431
Chris Lattner4e1d99a2007-10-09 17:41:39 +00002432 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002433 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Alexey Bataev8fe24752013-11-18 08:17:37 +00002434 SkipUntil(tok::r_paren, StopAtSemi);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00002435 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002436 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00002437
Reid Spencer5f016e22007-07-11 17:01:13 +00002438 // Read the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002439 BalancedDelimiterTracker T(*this, tok::l_paren);
2440 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00002441 ExprResult Res(ParseExpression());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002442 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002443 if (Res.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002444 SkipUntil(tok::r_paren, StopAtSemi);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00002445 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002446 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00002447 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002448 // Eat the comma and continue parsing if it exists.
Stephen Hines651f13c2014-04-23 16:59:28 -07002449 if (!TryConsumeToken(tok::comma))
2450 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002451 }
2452}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00002453
Douglas Gregorc9977d02011-03-16 17:05:57 +00002454Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00002455 assert(Tok.is(tok::l_brace));
2456 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00002457
Argyrios Kyrtzidis1f12c472013-02-22 04:11:06 +00002458 if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
Richard Smith1a5bd5d2012-11-19 21:13:18 +00002459 trySkippingFunctionBody()) {
Erik Verbruggen6a91d382012-04-12 10:11:59 +00002460 BodyScope.Exit();
Argyrios Kyrtzidis35f3f362012-12-06 18:59:10 +00002461 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregorc9977d02011-03-16 17:05:57 +00002462 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002463
John McCallf312b1e2010-08-26 23:41:50 +00002464 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
2465 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00002467 // Do not enter a scope for the brace, as the arguments are in the same scope
2468 // (the function body) as the body itself. Instead, just read the statement
2469 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00002470 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00002471
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00002472 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002473 if (FnBody.isInvalid()) {
2474 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelmc895f4d2013-08-19 20:51:20 +00002475 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002476 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00002477
Douglas Gregorc9977d02011-03-16 17:05:57 +00002478 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00002479 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00002480}
Sebastian Redla0fd8652008-12-21 16:41:36 +00002481
Sebastian Redld3a413d2009-04-26 20:35:05 +00002482/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2483///
2484/// function-try-block:
2485/// 'try' ctor-initializer[opt] compound-statement handler-seq
2486///
Douglas Gregorc9977d02011-03-16 17:05:57 +00002487Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00002488 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2489 SourceLocation TryLoc = ConsumeToken();
2490
John McCallf312b1e2010-08-26 23:41:50 +00002491 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
2492 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00002493
2494 // Constructor initializer list?
2495 if (Tok.is(tok::colon))
2496 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00002497 else
2498 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002499
Richard Smith1a5bd5d2012-11-19 21:13:18 +00002500 if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
2501 trySkippingFunctionBody()) {
Erik Verbruggen6a91d382012-04-12 10:11:59 +00002502 BodyScope.Exit();
Argyrios Kyrtzidis35f3f362012-12-06 18:59:10 +00002503 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregorc9977d02011-03-16 17:05:57 +00002504 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002505
Sebastian Redlde1b60a2009-04-26 21:08:36 +00002506 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikiec4027c82012-11-10 01:04:23 +00002507 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redld3a413d2009-04-26 20:35:05 +00002508 // If we failed to parse the try-catch, we just give the function an empty
2509 // compound statement as the body.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002510 if (FnBody.isInvalid()) {
2511 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelmc895f4d2013-08-19 20:51:20 +00002512 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002513 }
Sebastian Redld3a413d2009-04-26 20:35:05 +00002514
Douglas Gregorc9977d02011-03-16 17:05:57 +00002515 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00002516 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00002517}
2518
Erik Verbruggen6a91d382012-04-12 10:11:59 +00002519bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002520 assert(Tok.is(tok::l_brace));
Erik Verbruggen6a91d382012-04-12 10:11:59 +00002521 assert(SkipFunctionBodies &&
2522 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002523
Argyrios Kyrtzidis81939a72012-10-31 17:29:28 +00002524 if (!PP.isCodeCompletionEnabled()) {
2525 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00002526 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis81939a72012-10-31 17:29:28 +00002527 return true;
2528 }
2529
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002530 // We're in code-completion mode. Skip parsing for all function bodies unless
2531 // the body contains the code-completion point.
2532 TentativeParsingAction PA(*this);
2533 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00002534 if (SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002535 PA.Commit();
2536 return true;
2537 }
2538
2539 PA.Revert();
2540 return false;
2541}
2542
Sebastian Redla0fd8652008-12-21 16:41:36 +00002543/// ParseCXXTryBlock - Parse a C++ try-block.
2544///
2545/// try-block:
2546/// 'try' compound-statement handler-seq
2547///
Richard Smith534986f2012-04-14 00:33:13 +00002548StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002549 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2550
2551 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00002552 return ParseCXXTryBlockCommon(TryLoc);
2553}
2554
2555/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2556/// function-try-block.
2557///
2558/// try-block:
2559/// 'try' compound-statement handler-seq
2560///
2561/// function-try-block:
2562/// 'try' ctor-initializer[opt] compound-statement handler-seq
2563///
2564/// handler-seq:
2565/// handler handler-seq[opt]
2566///
John Wiegley28bbe4b2011-04-28 01:08:34 +00002567/// [Borland] try-block:
2568/// 'try' compound-statement seh-except-block
Stephen Hines651f13c2014-04-23 16:59:28 -07002569/// 'try' compound-statement seh-finally-block
John Wiegley28bbe4b2011-04-28 01:08:34 +00002570///
David Blaikiec4027c82012-11-10 01:04:23 +00002571StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002572 if (Tok.isNot(tok::l_brace))
Stephen Hines651f13c2014-04-23 16:59:28 -07002573 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sean Huntbbd37c62009-11-21 08:43:09 +00002574 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smith534986f2012-04-14 00:33:13 +00002575
2576 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
David Blaikiee5afdcf2012-11-13 18:51:45 +00002577 Scope::DeclScope | Scope::TryScope |
2578 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002579 if (TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002580 return TryBlock;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002581
John Wiegley28bbe4b2011-04-28 01:08:34 +00002582 // Borland allows SEH-handlers with 'try'
Chad Rosierb6604462012-07-10 21:35:27 +00002583
Richard Smith534986f2012-04-14 00:33:13 +00002584 if ((Tok.is(tok::identifier) &&
2585 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2586 Tok.is(tok::kw___finally)) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002587 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2588 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +00002589 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002590 SourceLocation Loc = ConsumeToken();
2591 Handler = ParseSEHExceptBlock(Loc);
2592 }
2593 else {
2594 SourceLocation Loc = ConsumeToken();
2595 Handler = ParseSEHFinallyBlock(Loc);
2596 }
2597 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002598 return Handler;
John McCall7f040a92010-12-24 02:08:15 +00002599
John Wiegley28bbe4b2011-04-28 01:08:34 +00002600 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2601 TryLoc,
2602 TryBlock.take(),
2603 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002604 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00002605 else {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002606 StmtVector Handlers;
Richard Smith5eed7e02013-10-15 01:34:54 +00002607
2608 // C++11 attributes can't appear here, despite this context seeming
2609 // statement-like.
2610 DiagnoseAndSkipCXX11Attributes();
Sebastian Redla0fd8652008-12-21 16:41:36 +00002611
John Wiegley28bbe4b2011-04-28 01:08:34 +00002612 if (Tok.isNot(tok::kw_catch))
2613 return StmtError(Diag(Tok, diag::err_expected_catch));
2614 while (Tok.is(tok::kw_catch)) {
David Blaikiec4027c82012-11-10 01:04:23 +00002615 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley28bbe4b2011-04-28 01:08:34 +00002616 if (!Handler.isInvalid())
2617 Handlers.push_back(Handler.release());
2618 }
2619 // Don't bother creating the full statement if we don't have any usable
2620 // handlers.
2621 if (Handlers.empty())
2622 return StmtError();
2623
Robert Wilhelm21adb0c2013-08-22 09:20:03 +00002624 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), Handlers);
John Wiegley28bbe4b2011-04-28 01:08:34 +00002625 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00002626}
2627
2628/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2629///
Richard Smith4cd81c52013-01-29 09:02:09 +00002630/// handler:
2631/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +00002632///
Richard Smith4cd81c52013-01-29 09:02:09 +00002633/// exception-declaration:
2634/// attribute-specifier-seq[opt] type-specifier-seq declarator
2635/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2636/// '...'
Sebastian Redla0fd8652008-12-21 16:41:36 +00002637///
David Blaikiec4027c82012-11-10 01:04:23 +00002638StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002639 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2640
2641 SourceLocation CatchLoc = ConsumeToken();
2642
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002643 BalancedDelimiterTracker T(*this, tok::l_paren);
Stephen Hines651f13c2014-04-23 16:59:28 -07002644 if (T.expectAndConsume())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002645 return StmtError();
2646
2647 // C++ 3.3.2p3:
2648 // The name in a catch exception-declaration is local to the handler and
2649 // shall not be redeclared in the outermost block of the handler.
David Blaikiec4027c82012-11-10 01:04:23 +00002650 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikiee5afdcf2012-11-13 18:51:45 +00002651 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002652
2653 // exception-declaration is equivalent to '...' or a parameter-declaration
2654 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00002655 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002656 if (Tok.isNot(tok::ellipsis)) {
Richard Smith4cd81c52013-01-29 09:02:09 +00002657 ParsedAttributesWithRange Attributes(AttrFactory);
2658 MaybeParseCXX11Attributes(Attributes);
2659
John McCall0b7e6782011-03-24 11:26:52 +00002660 DeclSpec DS(AttrFactory);
Richard Smith4cd81c52013-01-29 09:02:09 +00002661 DS.takeAttributesFrom(Attributes);
2662
Sebastian Redl4b07b292008-12-22 19:15:10 +00002663 if (ParseCXXTypeSpecifierSeq(DS))
2664 return StmtError();
Richard Smith4cd81c52013-01-29 09:02:09 +00002665
Sebastian Redla0fd8652008-12-21 16:41:36 +00002666 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2667 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002668 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002669 } else
2670 ConsumeToken();
2671
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002672 T.consumeClose();
2673 if (T.getCloseLocation().isInvalid())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002674 return StmtError();
2675
2676 if (Tok.isNot(tok::l_brace))
Stephen Hines651f13c2014-04-23 16:59:28 -07002677 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002678
Sean Huntbbd37c62009-11-21 08:43:09 +00002679 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smith534986f2012-04-14 00:33:13 +00002680 StmtResult Block(ParseCompoundStatement());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002681 if (Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002682 return Block;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002683
John McCall9ae2f072010-08-23 23:25:46 +00002684 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002685}
Francois Pichet1e862692011-05-06 20:48:22 +00002686
2687void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002688 IfExistsCondition Result;
Francois Pichetf9860382011-05-07 17:30:27 +00002689 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002690 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002691
Douglas Gregor3896fc52011-10-24 22:31:10 +00002692 // Handle dependent statements by parsing the braces as a compound statement.
2693 // This is not the same behavior as Visual C++, which don't treat this as a
2694 // compound statement, but for Clang's type checking we can't have anything
2695 // inside these braces escaping to the surrounding code.
2696 if (Result.Behavior == IEB_Dependent) {
2697 if (!Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002698 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smith534986f2012-04-14 00:33:13 +00002699 return;
Douglas Gregor3896fc52011-10-24 22:31:10 +00002700 }
Richard Smith534986f2012-04-14 00:33:13 +00002701
2702 StmtResult Compound = ParseCompoundStatement();
Douglas Gregorba0513d2011-10-25 01:33:02 +00002703 if (Compound.isInvalid())
2704 return;
Richard Smith534986f2012-04-14 00:33:13 +00002705
Douglas Gregorba0513d2011-10-25 01:33:02 +00002706 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2707 Result.IsIfExists,
Richard Smith534986f2012-04-14 00:33:13 +00002708 Result.SS,
Douglas Gregorba0513d2011-10-25 01:33:02 +00002709 Result.Name,
2710 Compound.get());
2711 if (DepResult.isUsable())
2712 Stmts.push_back(DepResult.get());
Douglas Gregor3896fc52011-10-24 22:31:10 +00002713 return;
2714 }
Richard Smith534986f2012-04-14 00:33:13 +00002715
Douglas Gregor3896fc52011-10-24 22:31:10 +00002716 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2717 if (Braces.consumeOpen()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002718 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet1e862692011-05-06 20:48:22 +00002719 return;
2720 }
Francois Pichet1e862692011-05-06 20:48:22 +00002721
Douglas Gregor3896fc52011-10-24 22:31:10 +00002722 switch (Result.Behavior) {
2723 case IEB_Parse:
2724 // Parse the statements below.
2725 break;
Chad Rosierb6604462012-07-10 21:35:27 +00002726
Douglas Gregor3896fc52011-10-24 22:31:10 +00002727 case IEB_Dependent:
2728 llvm_unreachable("Dependent case handled above");
Chad Rosierb6604462012-07-10 21:35:27 +00002729
Douglas Gregor3896fc52011-10-24 22:31:10 +00002730 case IEB_Skip:
2731 Braces.skipToEnd();
Francois Pichet1e862692011-05-06 20:48:22 +00002732 return;
2733 }
2734
2735 // Condition is true, parse the statements.
2736 while (Tok.isNot(tok::r_brace)) {
2737 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2738 if (R.isUsable())
2739 Stmts.push_back(R.release());
2740 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002741 Braces.consumeClose();
Francois Pichet1e862692011-05-06 20:48:22 +00002742}