blob: 9f49ab59ce8172a9cf4a1fc8879325b23fa7aab3 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chad Rosier32503022012-06-11 20:47:18 +000023#include "llvm/ADT/SmallString.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.8: Statements and Blocks.
28//===----------------------------------------------------------------------===//
29
30/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
31/// StatementOrDeclaration:
32/// statement
33/// declaration
34///
35/// statement:
36/// labeled-statement
37/// compound-statement
38/// expression-statement
39/// selection-statement
40/// iteration-statement
41/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000042/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000043/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000044/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000045/// [OBC] objc-throw-statement
46/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000047/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000048/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000049/// [OMP] openmp-construct [TODO]
50///
51/// labeled-statement:
52/// identifier ':' statement
53/// 'case' constant-expression ':' statement
54/// 'default' ':' statement
55///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000056/// selection-statement:
57/// if-statement
58/// switch-statement
59///
60/// iteration-statement:
61/// while-statement
62/// do-statement
63/// for-statement
64///
Chris Lattner9075bd72006-08-10 04:59:57 +000065/// expression-statement:
66/// expression[opt] ';'
67///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000068/// jump-statement:
69/// 'goto' identifier ';'
70/// 'continue' ';'
71/// 'break' ';'
72/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000073/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000074///
Fariborz Jahanian90814572007-10-04 20:19:06 +000075/// [OBC] objc-throw-statement:
76/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000077/// [OBC] '@' 'throw' ';'
78///
John McCalldadc5752010-08-24 06:29:42 +000079StmtResult
Nico Weber3cef1082011-12-22 23:26:17 +000080Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
81 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +000082
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000083 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000084
Richard Smithc202b282012-04-14 00:33:13 +000085 ParsedAttributesWithRange Attrs(AttrFactory);
86 MaybeParseCXX0XAttributes(Attrs, 0, /*MightBeObjCMessageSend*/ true);
87
88 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
89 OnlyStatement, TrailingElseLoc, Attrs);
90
91 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
92 "attributes on empty statement");
93
94 if (Attrs.empty() || Res.isInvalid())
95 return Res;
96
97 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
98}
99
100StmtResult
101Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
102 bool OnlyStatement, SourceLocation *TrailingElseLoc,
103 ParsedAttributesWithRange &Attrs) {
104 const char *SemiError = 0;
105 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000106
Chris Lattner503fadc2006-08-10 05:45:44 +0000107 // Cases in this switch statement should fall through if the parser expects
108 // the token to end in a semicolon (in which case SemiError should be set),
109 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000110Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000111 tok::TokenKind Kind = Tok.getKind();
112 SourceLocation AtLoc;
113 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000114 case tok::at: // May be a @try or @throw statement
115 {
Richard Smithc202b282012-04-14 00:33:13 +0000116 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000117 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000118 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000119 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000120
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000121 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000122 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000123 cutOffParsing();
124 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000125
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000126 case tok::identifier: {
127 Token Next = NextToken();
128 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000129 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000130 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000131 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000132
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000133 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000134 CXXScopeSpec SS;
135 IdentifierInfo *Name = Tok.getIdentifierInfo();
136 SourceLocation NameLoc = Tok.getLocation();
Richard Trieu01fc0012011-09-19 19:01:00 +0000137
David Blaikiebbafb8a2012-03-11 07:00:24 +0000138 if (getLangOpts().CPlusPlus)
Richard Trieu01fc0012011-09-19 19:01:00 +0000139 CheckForTemplateAndDigraph(Next, ParsedType(),
140 /*EnteringContext=*/false, *Name, SS);
141
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000142 Sema::NameClassification Classification
143 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
144 switch (Classification.getKind()) {
145 case Sema::NC_Keyword:
146 // The identifier was corrected to a keyword. Update the token
147 // to this keyword, and try again.
148 if (Name->getTokenID() != tok::identifier) {
149 Tok.setIdentifierInfo(Name);
150 Tok.setKind(Name->getTokenID());
151 goto Retry;
152 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000153
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000154 // Fall through via the normal error path.
155 // FIXME: This seems like it could only happen for context-sensitive
156 // keywords.
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000157
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000158 case Sema::NC_Error:
159 // Handle errors here by skipping up to the next semicolon or '}', and
160 // eat the semicolon if that's what stopped us.
161 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
162 if (Tok.is(tok::semi))
163 ConsumeToken();
164 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000165
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000166 case Sema::NC_Unknown:
167 // Either we don't know anything about this identifier, or we know that
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000168 // we're in a syntactic context we haven't handled yet.
169 break;
170
Douglas Gregor19b7acf2011-04-27 05:41:15 +0000171 case Sema::NC_Type:
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000172 Tok.setKind(tok::annot_typename);
173 setTypeAnnotation(Tok, Classification.getType());
174 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000175 PP.AnnotateCachedTokens(Tok);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000176 break;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000177
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000178 case Sema::NC_Expression:
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000179 Tok.setKind(tok::annot_primary_expr);
180 setExprAnnotation(Tok, Classification.getExpression());
181 Tok.setAnnotationEndLoc(NameLoc);
182 PP.AnnotateCachedTokens(Tok);
183 break;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000184
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000185 case Sema::NC_TypeTemplate:
186 case Sema::NC_FunctionTemplate: {
187 ConsumeToken(); // the identifier
188 UnqualifiedId Id;
189 Id.setIdentifier(Name, NameLoc);
190 if (AnnotateTemplateIdToken(
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000191 TemplateTy::make(Classification.getTemplateName()),
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000192 Classification.getTemplateNameKind(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000193 SS, SourceLocation(), Id,
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000194 /*AllowTypeAnnotation=*/false)) {
195 // Handle errors here by skipping up to the next semicolon or '}', and
196 // eat the semicolon if that's what stopped us.
197 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
198 if (Tok.is(tok::semi))
199 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000200 return StmtError();
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000201 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000202
203 // If the next token is '::', jump right into parsing a
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000204 // nested-name-specifier. We don't want to leave the template-id
205 // hanging.
206 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000207 // Handle errors here by skipping up to the next semicolon or '}', and
208 // eat the semicolon if that's what stopped us.
209 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
210 if (Tok.is(tok::semi))
211 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000212 return StmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000213 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000214
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000215 // We've annotated a template-id, so try again now.
216 goto Retry;
217 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000218
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000219 case Sema::NC_NestedNameSpecifier:
220 // FIXME: Implement this!
221 break;
222 }
223 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000224
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000225 // Fall through
226 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000227
Chris Lattner803802d2009-03-24 17:04:48 +0000228 default: {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000229 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000230 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000231 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000232 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000233 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000234 }
235
236 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000237 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000238 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000242 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000243
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000244 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000245 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000246 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000247 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000248
Chris Lattner9075bd72006-08-10 04:59:57 +0000249 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000250 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000251 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000252 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
253 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000254 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000255
Chris Lattner9075bd72006-08-10 04:59:57 +0000256 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000257 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000258 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000259 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000260
Chris Lattner9075bd72006-08-10 04:59:57 +0000261 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000262 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000263 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000264 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000265 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000266 break;
267 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000268 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000269
270 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000271 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000272 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000273 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000274 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000275 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000276 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000277 break;
278 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000279 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000280 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000281 break;
282 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000283 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000284 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000285 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000286
Sebastian Redlb219c902008-12-21 16:41:36 +0000287 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000288 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000289 bool msAsm = false;
290 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000291 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000292 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000293 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000294 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000295 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000296
Sebastian Redlb219c902008-12-21 16:41:36 +0000297 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000298 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000299
300 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000301 ProhibitAttributes(Attrs); // TODO: is it correct?
302 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000303
304 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000305 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000306 HandlePragmaVisibility();
307 return StmtEmpty();
308
309 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000310 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000311 HandlePragmaPack();
312 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000313 }
314
Chris Lattner503fadc2006-08-10 05:45:44 +0000315 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000316 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000317 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000318 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000319 // If the result was valid, then we do want to diagnose this. Use
320 // ExpectAndConsume to emit the diagnostic, even though we know it won't
321 // succeed.
322 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000323 // Skip until we see a } or ;, but don't eat it.
324 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Sebastian Redl042ad952008-12-11 19:30:53 +0000327 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000328}
329
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000330/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000331StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000332 // If a case keyword is missing, this is where it should be inserted.
333 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000334
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000335 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000336 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000337 if (Expr.isInvalid()) {
338 // If the expression is invalid, skip ahead to the next semicolon or '}'.
339 // Not doing this opens us up to the possibility of infinite loops if
340 // ParseExpression does not consume any tokens.
341 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
342 if (Tok.is(tok::semi))
343 ConsumeToken();
344 return StmtError();
345 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000346
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000347 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
348 Actions.CheckCaseExpression(Expr.get())) {
349 // If a constant expression is followed by a colon inside a switch block,
350 // suggest a missing case keyword.
351 Diag(OldToken, diag::err_expected_case_before_expression)
352 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000353
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000354 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000355 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000356 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000357
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000358 // Otherwise, eat the semicolon.
359 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
360 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley1c0675e2011-04-28 01:08:34 +0000361}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000362
Richard Smithc202b282012-04-14 00:33:13 +0000363StmtResult Parser::ParseSEHTryBlock() {
John Wiegley1c0675e2011-04-28 01:08:34 +0000364 assert(Tok.is(tok::kw___try) && "Expected '__try'");
365 SourceLocation Loc = ConsumeToken();
366 return ParseSEHTryBlockCommon(Loc);
367}
368
369/// ParseSEHTryBlockCommon
370///
371/// seh-try-block:
372/// '__try' compound-statement seh-handler
373///
374/// seh-handler:
375/// seh-except-block
376/// seh-finally-block
377///
378StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
379 if(Tok.isNot(tok::l_brace))
380 return StmtError(Diag(Tok,diag::err_expected_lbrace));
381
Richard Smithc202b282012-04-14 00:33:13 +0000382 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000383 if(TryBlock.isInvalid())
384 return move(TryBlock);
385
386 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000387 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000388 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000389 SourceLocation Loc = ConsumeToken();
390 Handler = ParseSEHExceptBlock(Loc);
391 } else if (Tok.is(tok::kw___finally)) {
392 SourceLocation Loc = ConsumeToken();
393 Handler = ParseSEHFinallyBlock(Loc);
394 } else {
395 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
396 }
397
398 if(Handler.isInvalid())
399 return move(Handler);
400
401 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
402 TryLoc,
403 TryBlock.take(),
404 Handler.take());
405}
406
407/// ParseSEHExceptBlock - Handle __except
408///
409/// seh-except-block:
410/// '__except' '(' seh-filter-expression ')' compound-statement
411///
412StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
413 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
414 raii2(Ident___exception_code, false),
415 raii3(Ident_GetExceptionCode, false);
416
417 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
418 return StmtError();
419
420 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
421
David Blaikiebbafb8a2012-03-11 07:00:24 +0000422 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000423 Ident__exception_info->setIsPoisoned(false);
424 Ident___exception_info->setIsPoisoned(false);
425 Ident_GetExceptionInfo->setIsPoisoned(false);
426 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000427 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000428
David Blaikiebbafb8a2012-03-11 07:00:24 +0000429 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000430 Ident__exception_info->setIsPoisoned(true);
431 Ident___exception_info->setIsPoisoned(true);
432 Ident_GetExceptionInfo->setIsPoisoned(true);
433 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000434
435 if(FilterExpr.isInvalid())
436 return StmtError();
437
438 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
439 return StmtError();
440
Richard Smithc202b282012-04-14 00:33:13 +0000441 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000442
443 if(Block.isInvalid())
444 return move(Block);
445
446 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
447}
448
449/// ParseSEHFinallyBlock - Handle __finally
450///
451/// seh-finally-block:
452/// '__finally' compound-statement
453///
454StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
455 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
456 raii2(Ident___abnormal_termination, false),
457 raii3(Ident_AbnormalTermination, false);
458
Richard Smithc202b282012-04-14 00:33:13 +0000459 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000460 if(Block.isInvalid())
461 return move(Block);
462
463 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000464}
465
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000466/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000467///
468/// labeled-statement:
469/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000470/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000471///
Richard Smithc202b282012-04-14 00:33:13 +0000472StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000473 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
474 "Not an identifier!");
475
476 Token IdentTok = Tok; // Save the whole token.
477 ConsumeToken(); // eat the identifier.
478
479 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000480
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000481 // identifier ':' statement
482 SourceLocation ColonLoc = ConsumeToken();
483
Richard Smithc202b282012-04-14 00:33:13 +0000484 // Read label attributes, if present. attrs will contain both C++11 and GNU
485 // attributes (if present) after this point.
John McCall53fa7142010-12-24 02:08:15 +0000486 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000487
John McCalldadc5752010-08-24 06:29:42 +0000488 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000489
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000490 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000491 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000492 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000493
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000494 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
495 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000496 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000497 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000498 attrs.clear();
499 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000500
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000501 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
502 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000503}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000504
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000505/// ParseCaseStatement
506/// labeled-statement:
507/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000508/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000509///
Richard Smithc202b282012-04-14 00:33:13 +0000510StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000511 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chris Lattner34a22092009-03-04 04:23:07 +0000513 // It is very very common for code to contain many case statements recursively
514 // nested, as in (but usually without indentation):
515 // case 1:
516 // case 2:
517 // case 3:
518 // case 4:
519 // case 5: etc.
520 //
521 // Parsing this naively works, but is both inefficient and can cause us to run
522 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000523 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000524 // but all the grossness is constrained to ParseCaseStatement (and some
525 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner34a22092009-03-04 04:23:07 +0000527 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
528 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000529 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000530
Chris Lattner34a22092009-03-04 04:23:07 +0000531 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
532 // gets updated each time a new case is parsed, and whose body is unset so
533 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieu3481fcd2011-09-09 02:16:15 +0000534 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattner34a22092009-03-04 04:23:07 +0000536 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000537 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000538 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000539 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
540 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000541
Douglas Gregord328d572009-09-21 18:10:23 +0000542 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000543 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000544 cutOffParsing();
545 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000546 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000547
Chris Lattner125c0ee2009-12-10 00:38:54 +0000548 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
549 /// Disable this form of error recovery while we're parsing the case
550 /// expression.
551 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000552
Richard Trieu2c850c02011-04-21 21:44:26 +0000553 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
554 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000555 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000556 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000557 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000558 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000559
Chris Lattner34a22092009-03-04 04:23:07 +0000560 // GNU case range extension.
561 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000562 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000563 if (Tok.is(tok::ellipsis)) {
564 Diag(Tok, diag::ext_gnu_case_range);
565 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000566
Chris Lattner34a22092009-03-04 04:23:07 +0000567 RHS = ParseConstantExpression();
568 if (RHS.isInvalid()) {
569 SkipUntil(tok::colon);
570 return StmtError();
571 }
572 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000573
Chris Lattner125c0ee2009-12-10 00:38:54 +0000574 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000575
John McCall0140bfe2011-01-22 09:28:32 +0000576 if (Tok.is(tok::colon)) {
577 ColonLoc = ConsumeToken();
578
579 // Treat "case blah;" as a typo for "case blah:".
580 } else if (Tok.is(tok::semi)) {
581 ColonLoc = ConsumeToken();
582 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
583 << FixItHint::CreateReplacement(ColonLoc, ":");
584 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000585 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
586 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
587 << FixItHint::CreateInsertion(ExpectedLoc, ":");
588 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000589 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000590
John McCalldadc5752010-08-24 06:29:42 +0000591 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000592 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
593 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner34a22092009-03-04 04:23:07 +0000595 // If we had a sema error parsing this case, then just ignore it and
596 // continue parsing the sub-stmt.
597 if (Case.isInvalid()) {
598 if (TopLevelCase.isInvalid()) // No parsed case stmts.
599 return ParseStatement();
600 // Otherwise, just don't add it as a nested case.
601 } else {
602 // If this is the first case statement we parsed, it becomes TopLevelCase.
603 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000604 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000605 if (TopLevelCase.isInvalid())
606 TopLevelCase = move(Case);
607 else
John McCallb268a282010-08-23 23:25:46 +0000608 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000609 DeepestParsedCaseStmt = NextDeepest;
610 }
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattner34a22092009-03-04 04:23:07 +0000612 // Handle all case statements.
613 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattner34a22092009-03-04 04:23:07 +0000615 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000616
Chris Lattner34a22092009-03-04 04:23:07 +0000617 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000618 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattner34a22092009-03-04 04:23:07 +0000620 if (Tok.isNot(tok::r_brace)) {
621 SubStmt = ParseStatement();
622 } else {
623 // Nicely diagnose the common error "switch (X) { case 4: }", which is
624 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000625 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000626 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
627 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner34a22092009-03-04 04:23:07 +0000628 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000629 }
Mike Stump11289f42009-09-09 15:08:12 +0000630
Chris Lattner34a22092009-03-04 04:23:07 +0000631 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000632 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000633 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner34a22092009-03-04 04:23:07 +0000635 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000636 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000637
Chris Lattner34a22092009-03-04 04:23:07 +0000638 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000639 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000640}
641
642/// ParseDefaultStatement
643/// labeled-statement:
644/// 'default' ':' statement
645/// Note that this does not parse the 'statement' at the end.
646///
Richard Smithc202b282012-04-14 00:33:13 +0000647StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000648 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000649 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000650
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000651 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000652 if (Tok.is(tok::colon)) {
653 ColonLoc = ConsumeToken();
654
655 // Treat "default;" as a typo for "default:".
656 } else if (Tok.is(tok::semi)) {
657 ColonLoc = ConsumeToken();
658 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
659 << FixItHint::CreateReplacement(ColonLoc, ":");
660 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000661 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
662 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
663 << FixItHint::CreateInsertion(ExpectedLoc, ":");
664 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000665 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000666
Richard Smith1002d102012-02-17 01:35:32 +0000667 StmtResult SubStmt;
668
669 if (Tok.isNot(tok::r_brace)) {
670 SubStmt = ParseStatement();
671 } else {
672 // Diagnose the common error "switch (X) {... default: }", which is
673 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000674 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000675 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
676 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
677 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000678 }
679
Richard Smith1002d102012-02-17 01:35:32 +0000680 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000681 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000682 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000683
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000684 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000685 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000686}
687
Richard Smithc202b282012-04-14 00:33:13 +0000688StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
689 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000690}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000691
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000692/// ParseCompoundStatement - Parse a "{}" block.
693///
694/// compound-statement: [C99 6.8.2]
695/// { block-item-list[opt] }
696/// [GNU] { label-declarations block-item-list } [TODO]
697///
698/// block-item-list:
699/// block-item
700/// block-item-list block-item
701///
702/// block-item:
703/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000704/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000705/// statement
706/// [OMP] openmp-directive [TODO]
707///
708/// [GNU] label-declarations:
709/// [GNU] label-declaration
710/// [GNU] label-declarations label-declaration
711///
712/// [GNU] label-declaration:
713/// [GNU] '__label__' identifier-list ';'
714///
715/// [OMP] openmp-directive: [TODO]
716/// [OMP] barrier-directive
717/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000718///
Richard Smithc202b282012-04-14 00:33:13 +0000719StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000720 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000721 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000722
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000723 // Enter a scope to hold everything within the compound stmt. Compound
724 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000725 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000726
727 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000728 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000729}
730
Chris Lattnerf2978802007-01-21 06:52:16 +0000731/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000732/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000733/// consume the '}' at the end of the block. It does not manipulate the scope
734/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000735StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000736 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000737 Tok.getLocation(),
738 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000739 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000740 BalancedDelimiterTracker T(*this, tok::l_brace);
741 if (T.consumeOpen())
742 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000743
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000744 Sema::CompoundScopeRAII CompoundScope(Actions);
745
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000746 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000747
Chris Lattner43e7f312011-02-18 02:08:43 +0000748 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
749 // only allowed at the start of a compound stmt regardless of the language.
750 while (Tok.is(tok::kw___label__)) {
751 SourceLocation LabelLoc = ConsumeToken();
752 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000753
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000754 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000755 while (1) {
756 if (Tok.isNot(tok::identifier)) {
757 Diag(Tok, diag::err_expected_ident);
758 break;
759 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000760
Chris Lattner43e7f312011-02-18 02:08:43 +0000761 IdentifierInfo *II = Tok.getIdentifierInfo();
762 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000763 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000764
Chris Lattner43e7f312011-02-18 02:08:43 +0000765 if (!Tok.is(tok::comma))
766 break;
767 ConsumeToken();
768 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000769
John McCall084e83d2011-03-24 11:26:52 +0000770 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000771 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
772 DeclsInGroup.data(), DeclsInGroup.size());
773 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000774
Chris Lattner02f1b612012-04-28 16:12:17 +0000775 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000776 if (R.isUsable())
777 Stmts.push_back(R.release());
778 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000779
Chris Lattner43e7f312011-02-18 02:08:43 +0000780 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000781 if (Tok.is(tok::annot_pragma_unused)) {
782 HandlePragmaUnused();
783 continue;
784 }
785
David Blaikiebbafb8a2012-03-11 07:00:24 +0000786 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000787 Tok.is(tok::kw___if_not_exists))) {
788 ParseMicrosoftIfExistsStatement(Stmts);
789 continue;
790 }
791
John McCalldadc5752010-08-24 06:29:42 +0000792 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000793 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000794 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000795 } else {
796 // __extension__ can start declarations and it can also be a unary
797 // operator for expressions. Consume multiple __extension__ markers here
798 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000799 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000800 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000801 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000802 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000803
John McCall084e83d2011-03-24 11:26:52 +0000804 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith7bdcc4a2012-04-10 01:32:12 +0000805 MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000806
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000807 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000808 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000809 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000810 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000811 ExtensionRAIIObject O(Diags);
812
Chris Lattner49836b42009-04-02 04:16:50 +0000813 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000814 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
815 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000816 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000817 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000818 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000819 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000820 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000821
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000822 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000823 SkipUntil(tok::semi);
824 continue;
825 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000826
Alexis Hunt96d5c762009-11-21 08:43:09 +0000827 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000828 // Eat the semicolon at the end of stmt and convert the expr into a
829 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000830 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000831 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000832 }
833 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000834
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000835 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000836 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000837 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000838
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000839 SourceLocation CloseLoc = Tok.getLocation();
840
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000841 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000842 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000843 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000844 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000845 // Recover by creating a compound statement with what we parsed so far,
846 // instead of dropping everything and returning StmtError();
847 } else {
848 if (!T.consumeClose())
849 CloseLoc = T.getCloseLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000850 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000851
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000852 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000853 move_arg(Stmts), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000854}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000855
Chris Lattnerc0081db2008-12-12 06:31:07 +0000856/// ParseParenExprOrCondition:
857/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000858/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000859///
860/// This function parses and performs error recovery on the specified condition
861/// or expression (depending on whether we're in C++ or C mode). This function
862/// goes out of its way to recover well. It returns true if there was a parser
863/// error (the right paren couldn't be found), which indicates that the caller
864/// should try to recover harder. It returns false if the condition is
865/// successfully parsed. Note that a successful parse can still have semantic
866/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000867bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000868 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000869 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000870 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000871 BalancedDelimiterTracker T(*this, tok::l_paren);
872 T.consumeOpen();
873
David Blaikiebbafb8a2012-03-11 07:00:24 +0000874 if (getLangOpts().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000875 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000876 else {
877 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000878 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000879
Douglas Gregore60e41a2010-05-06 17:25:47 +0000880 // If required, convert to a boolean value.
881 if (!ExprResult.isInvalid() && ConvertToBoolean)
882 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000883 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000884 }
Mike Stump11289f42009-09-09 15:08:12 +0000885
Chris Lattnerc0081db2008-12-12 06:31:07 +0000886 // If the parser was confused by the condition and we don't have a ')', try to
887 // recover by skipping ahead to a semi and bailing out. If condexp is
888 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000889 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000890 SkipUntil(tok::semi);
891 // Skipping may have stopped if it found the containing ')'. If so, we can
892 // continue parsing the if statement.
893 if (Tok.isNot(tok::r_paren))
894 return true;
895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
Chris Lattnerc0081db2008-12-12 06:31:07 +0000897 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000898 T.consumeClose();
Chris Lattner70d44982012-04-28 16:24:20 +0000899
900 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
901 // that all callers are looking for a statement after the condition, so ")"
902 // isn't valid.
903 while (Tok.is(tok::r_paren)) {
904 Diag(Tok, diag::err_extraneous_rparen_in_condition)
905 << FixItHint::CreateRemoval(Tok.getLocation());
906 ConsumeParen();
907 }
908
Chris Lattnerc0081db2008-12-12 06:31:07 +0000909 return false;
910}
911
912
Chris Lattnerc951dae2006-08-10 04:23:57 +0000913/// ParseIfStatement
914/// if-statement: [C99 6.8.4.1]
915/// 'if' '(' expression ')' statement
916/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000917/// [C++] 'if' '(' condition ')' statement
918/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000919///
Richard Smithc202b282012-04-14 00:33:13 +0000920StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000921 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000922 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000923
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000924 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000925 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000926 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000927 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000928 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000929
David Blaikiebbafb8a2012-03-11 07:00:24 +0000930 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000931
Chris Lattner2dd1b722007-08-26 23:08:06 +0000932 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
933 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000934 //
935 // C++ 6.4p3:
936 // A name introduced by a declaration in a condition is in scope from its
937 // point of declaration until the end of the substatements controlled by the
938 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000939 // C++ 3.3.2p4:
940 // Names declared in the for-init-statement, and in the condition of if,
941 // while, for, and switch statements are local to the if, while, for, or
942 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000943 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000944 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000945
Chris Lattnerc951dae2006-08-10 04:23:57 +0000946 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000947 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000948 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000949 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000950 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000951
David Blaikiea5696df2012-05-16 04:20:04 +0000952 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000953
Chris Lattner8fb26252007-08-22 05:28:50 +0000954 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000955 // there is no compound stmt. C90 does not have this clause. We only do this
956 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000957 //
958 // C++ 6.4p1:
959 // The substatement in a selection-statement (each substatement, in the else
960 // form of the if statement) implicitly defines a local scope.
961 //
962 // For C++ we create a scope for the condition and a new scope for
963 // substatements because:
964 // -When the 'then' scope exits, we want the condition declaration to still be
965 // active for the 'else' scope too.
966 // -Sema will detect name clashes by considering declarations of a
967 // 'ControlScope' as part of its direct subscope.
968 // -If we wanted the condition and substatement to be in the same scope, we
969 // would have to notify ParseStatement not to create a new scope. It's
970 // simpler to let it create a new scope.
971 //
Mike Stump11289f42009-09-09 15:08:12 +0000972 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000973 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000974
Chris Lattner5c5808a2007-10-29 05:08:52 +0000975 // Read the 'then' stmt.
976 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +0000977
978 SourceLocation InnerStatementTrailingElseLoc;
979 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +0000980
Chris Lattner37e54f42007-08-22 05:16:28 +0000981 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000982 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000983
Chris Lattnerc951dae2006-08-10 04:23:57 +0000984 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000985 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000986 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000987 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000988
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000989 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +0000990 if (TrailingElseLoc)
991 *TrailingElseLoc = Tok.getLocation();
992
Chris Lattneraf635312006-10-16 06:06:51 +0000993 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000994 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000995
Chris Lattner8fb26252007-08-22 05:28:50 +0000996 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000997 // there is no compound stmt. C90 does not have this clause. We only do
998 // this if the body isn't a compound statement to avoid push/pop in common
999 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001000 //
1001 // C++ 6.4p1:
1002 // The substatement in a selection-statement (each substatement, in the else
1003 // form of the if statement) implicitly defines a local scope.
1004 //
Sebastian Redl042ad952008-12-11 19:30:53 +00001005 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001006 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001007
Chris Lattner30f910e2006-10-16 05:52:41 +00001008 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001009
Chris Lattner37e54f42007-08-22 05:16:28 +00001010 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001011 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001012 } else if (Tok.is(tok::code_completion)) {
1013 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001014 cutOffParsing();
1015 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001016 } else if (InnerStatementTrailingElseLoc.isValid()) {
1017 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001018 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001019
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001020 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001021
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001022 // If the condition was invalid, discard the if statement. We could recover
1023 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +00001024 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001025 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +00001026
Chris Lattner5c5808a2007-10-29 05:08:52 +00001027 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001028 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001029 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001030 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1031 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1032 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001033 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001034 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001035 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001036
Chris Lattner5c5808a2007-10-29 05:08:52 +00001037 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001038 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001039 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001040 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001041 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001042
John McCallb268a282010-08-23 23:25:46 +00001043 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001044 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001045}
1046
Chris Lattner9075bd72006-08-10 04:59:57 +00001047/// ParseSwitchStatement
1048/// switch-statement:
1049/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001050/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001051StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001052 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001053 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001054
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001055 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001056 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001057 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001058 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001059 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001060
David Blaikiebbafb8a2012-03-11 07:00:24 +00001061 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001062
Chris Lattner2dd1b722007-08-26 23:08:06 +00001063 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1064 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001065 //
1066 // C++ 6.4p3:
1067 // A name introduced by a declaration in a condition is in scope from its
1068 // point of declaration until the end of the substatements controlled by the
1069 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001070 // C++ 3.3.2p4:
1071 // Names declared in the for-init-statement, and in the condition of if,
1072 // while, for, and switch statements are local to the if, while, for, or
1073 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001074 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001075 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001076 if (C99orCXX)
1077 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001078 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001079
Chris Lattner9075bd72006-08-10 04:59:57 +00001080 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001081 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001082 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001083 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001084 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001085
John McCalldadc5752010-08-24 06:29:42 +00001086 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001087 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001088
Douglas Gregore60e41a2010-05-06 17:25:47 +00001089 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001090 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001091 // FIXME: This is not optimal recovery, but parsing the body is more
1092 // dangerous due to the presence of case and default statements, which
1093 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001094 if (Tok.is(tok::l_brace)) {
1095 ConsumeBrace();
1096 SkipUntil(tok::r_brace, false, false);
1097 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001098 SkipUntil(tok::semi);
1099 return move(Switch);
1100 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001101
Chris Lattner8fb26252007-08-22 05:28:50 +00001102 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001103 // there is no compound stmt. C90 does not have this clause. We only do this
1104 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001105 //
1106 // C++ 6.4p1:
1107 // The substatement in a selection-statement (each substatement, in the else
1108 // form of the if statement) implicitly defines a local scope.
1109 //
1110 // See comments in ParseIfStatement for why we create a scope for the
1111 // condition and a new scope for substatement in C++.
1112 //
Mike Stump11289f42009-09-09 15:08:12 +00001113 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001114 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001115
Chris Lattner9075bd72006-08-10 04:59:57 +00001116 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001117 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001118
Chris Lattner8fd2d012010-01-24 01:50:29 +00001119 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001120 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001121 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001122
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001123 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001124 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001125
1126 // Put the synthesized null statement on the same line as the end of switch
1127 // condition.
1128 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1129 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1130 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001131
John McCallb268a282010-08-23 23:25:46 +00001132 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001133}
1134
1135/// ParseWhileStatement
1136/// while-statement: [C99 6.8.5.1]
1137/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001138/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001139StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001140 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001141 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001142 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001143
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001144 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001145 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001146 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001147 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001148 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001149
David Blaikiebbafb8a2012-03-11 07:00:24 +00001150 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001151
Chris Lattner2dd1b722007-08-26 23:08:06 +00001152 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1153 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001154 //
1155 // C++ 6.4p3:
1156 // A name introduced by a declaration in a condition is in scope from its
1157 // point of declaration until the end of the substatements controlled by the
1158 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001159 // C++ 3.3.2p4:
1160 // Names declared in the for-init-statement, and in the condition of if,
1161 // while, for, and switch statements are local to the if, while, for, or
1162 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001163 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001164 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001165 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001166 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1167 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001168 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001169 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1170 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001171
Chris Lattner9075bd72006-08-10 04:59:57 +00001172 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001173 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001174 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001175 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001176 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001177
David Blaikiea5696df2012-05-16 04:20:04 +00001178 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001179
Chris Lattner8fb26252007-08-22 05:28:50 +00001180 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001181 // there is no compound stmt. C90 does not have this clause. We only do this
1182 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001183 //
1184 // C++ 6.5p2:
1185 // The substatement in an iteration-statement implicitly defines a local scope
1186 // which is entered and exited each time through the loop.
1187 //
1188 // See comments in ParseIfStatement for why we create a scope for the
1189 // condition and a new scope for substatement in C++.
1190 //
Mike Stump11289f42009-09-09 15:08:12 +00001191 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001192 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001193
Chris Lattner9075bd72006-08-10 04:59:57 +00001194 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001195 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001196
Chris Lattner8fb26252007-08-22 05:28:50 +00001197 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001198 InnerScope.Exit();
1199 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001200
John McCall48871652010-08-21 09:40:31 +00001201 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001202 return StmtError();
1203
John McCallb268a282010-08-23 23:25:46 +00001204 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001205}
1206
1207/// ParseDoStatement
1208/// do-statement: [C99 6.8.5.2]
1209/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001210/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001211StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001212 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001213 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001214
Chris Lattner2dd1b722007-08-26 23:08:06 +00001215 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1216 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001217 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001218 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001219 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001220 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001221 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001222
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001223 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001224
Chris Lattner8fb26252007-08-22 05:28:50 +00001225 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001226 // there is no compound stmt. C90 does not have this clause. We only do this
1227 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001228 //
1229 // C++ 6.5p2:
1230 // The substatement in an iteration-statement implicitly defines a local scope
1231 // which is entered and exited each time through the loop.
1232 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001233 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001234 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001235 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001236
Chris Lattner9075bd72006-08-10 04:59:57 +00001237 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001238 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001239
Chris Lattner8fb26252007-08-22 05:28:50 +00001240 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001241 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001242
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001243 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001244 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001245 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001246 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001247 SkipUntil(tok::semi, false, true);
1248 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001249 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001250 }
Chris Lattneraf635312006-10-16 06:06:51 +00001251 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001252
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001253 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001254 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001255 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001256 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001257 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001258
Chris Lattner10da53c2008-12-12 06:35:28 +00001259 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001260 BalancedDelimiterTracker T(*this, tok::l_paren);
1261 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001262 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001263 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001264 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001265
Sebastian Redlb62406f2008-12-11 19:48:14 +00001266 if (Cond.isInvalid() || Body.isInvalid())
1267 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001268
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001269 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1270 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001271}
1272
1273/// ParseForStatement
1274/// for-statement: [C99 6.8.5.3]
1275/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1276/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001277/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1278/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001279/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001280/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1281/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001282///
1283/// [C++] for-init-statement:
1284/// [C++] expression-statement
1285/// [C++] simple-declaration
1286///
Richard Smith02e85f32011-04-14 22:09:26 +00001287/// [C++0x] for-range-declaration:
1288/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1289/// [C++0x] for-range-initializer:
1290/// [C++0x] expression
1291/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001292StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001293 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001294 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001295
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001296 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001297 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001298 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001299 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001300 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001301
David Blaikiebbafb8a2012-03-11 07:00:24 +00001302 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001303
Chris Lattner2dd1b722007-08-26 23:08:06 +00001304 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1305 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001306 //
1307 // C++ 6.4p3:
1308 // A name introduced by a declaration in a condition is in scope from its
1309 // point of declaration until the end of the substatements controlled by the
1310 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001311 // C++ 3.3.2p4:
1312 // Names declared in the for-init-statement, and in the condition of if,
1313 // while, for, and switch statements are local to the if, while, for, or
1314 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001315 // C++ 6.5.3p1:
1316 // Names declared in the for-init-statement are in the same declarative-region
1317 // as those declared in the condition.
1318 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001319 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001320 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001321 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1322 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001323 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001324 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1325
1326 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001327
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001328 BalancedDelimiterTracker T(*this, tok::l_paren);
1329 T.consumeOpen();
1330
John McCalldadc5752010-08-24 06:29:42 +00001331 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001332
Richard Smith02e85f32011-04-14 22:09:26 +00001333 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001334 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001335 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001336 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001337 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001338 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001339 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001340 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001341
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001342 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001343 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001344 C99orCXXorObjC? Sema::PCC_ForInit
1345 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001346 cutOffParsing();
1347 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001348 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001349
Chris Lattner9075bd72006-08-10 04:59:57 +00001350 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001351 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001352 // no first part, eat the ';'.
1353 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001354 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001355 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001356 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001357 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001358
John McCall084e83d2011-03-24 11:26:52 +00001359 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001360 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001361
Richard Smith02e85f32011-04-14 22:09:26 +00001362 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001363 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001364 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1365
Chris Lattner49836b42009-04-02 04:16:50 +00001366 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001367 StmtVector Stmts(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001368 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001369 DeclEnd, attrs, false,
1370 MightBeForRangeStmt ?
1371 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001372 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001373
Richard Smith02e85f32011-04-14 22:09:26 +00001374 if (ForRangeInit.ParsedForRangeDecl()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001375 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001376 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001377
Richard Smith02e85f32011-04-14 22:09:26 +00001378 ForRange = true;
1379 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001380 ConsumeToken();
1381 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001382 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001383 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001384 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001385
Douglas Gregor68762e72010-08-23 21:17:50 +00001386 if (Tok.is(tok::code_completion)) {
1387 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001388 cutOffParsing();
1389 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001390 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001391 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001392 } else {
1393 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001394 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001395 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001396 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001397
John McCall34376a62010-12-04 03:47:34 +00001398 ForEach = isTokIdentifier_in();
1399
Chris Lattnercd68f642007-06-27 01:06:29 +00001400 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001401 if (!Value.isInvalid()) {
1402 if (ForEach)
1403 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1404 else
1405 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1406 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001407
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001408 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001409 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001410 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001411 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001412
Douglas Gregor68762e72010-08-23 21:17:50 +00001413 if (Tok.is(tok::code_completion)) {
1414 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001415 cutOffParsing();
1416 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001417 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001418 Collection = ParseExpression();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001419 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001420 // User tried to write the reasonable, but ill-formed, for-range-statement
1421 // for (expr : expr) { ... }
1422 Diag(Tok, diag::err_for_range_expected_decl)
1423 << FirstPart.get()->getSourceRange();
1424 SkipUntil(tok::r_paren, false, true);
1425 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001426 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001427 if (!Value.isInvalid()) {
1428 Diag(Tok, diag::err_expected_semi_for);
1429 } else {
1430 // Skip until semicolon or rparen, don't consume it.
1431 SkipUntil(tok::r_paren, true, true);
1432 if (Tok.is(tok::semi))
1433 ConsumeToken();
1434 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001435 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001436 }
Richard Smith02e85f32011-04-14 22:09:26 +00001437 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001438 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001439 // Parse the second part of the for specifier.
1440 if (Tok.is(tok::semi)) { // for (...;;
1441 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001442 } else if (Tok.is(tok::r_paren)) {
1443 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001444 } else {
John McCalldadc5752010-08-24 06:29:42 +00001445 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001446 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001447 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1448 else {
1449 Second = ParseExpression();
1450 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001451 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001452 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001453 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001454 SecondPartIsInvalid = Second.isInvalid();
David Blaikiea5696df2012-05-16 04:20:04 +00001455 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001456 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001457
Douglas Gregor230a7e62011-02-17 03:38:46 +00001458 if (Tok.isNot(tok::semi)) {
1459 if (!SecondPartIsInvalid || SecondVar)
1460 Diag(Tok, diag::err_expected_semi_for);
1461 else
1462 // Skip until semicolon or rparen, don't consume it.
1463 SkipUntil(tok::r_paren, true, true);
1464 }
1465
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001466 if (Tok.is(tok::semi)) {
1467 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001468 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001469
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001470 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001471 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001472 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001473 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001474 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001475 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001476 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001477 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001478
Richard Smith02e85f32011-04-14 22:09:26 +00001479 // We need to perform most of the semantic analysis for a C++0x for-range
1480 // statememt before parsing the body, in order to be able to deduce the type
1481 // of an auto-typed loop variable.
1482 StmtResult ForRangeStmt;
John McCall53848232011-07-27 01:07:15 +00001483 if (ForRange) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001484 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smith02e85f32011-04-14 22:09:26 +00001485 FirstPart.take(),
1486 ForRangeInit.ColonLoc,
1487 ForRangeInit.RangeExpr.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001488 T.getCloseLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001489
John McCall53848232011-07-27 01:07:15 +00001490
1491 // Similarly, we need to do the semantic analysis for a for-range
1492 // statement immediately in order to close over temporaries correctly.
1493 } else if (ForEach) {
1494 if (!Collection.isInvalid())
1495 Collection =
1496 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1497 }
1498
Chris Lattner8fb26252007-08-22 05:28:50 +00001499 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001500 // there is no compound stmt. C90 does not have this clause. We only do this
1501 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001502 //
1503 // C++ 6.5p2:
1504 // The substatement in an iteration-statement implicitly defines a local scope
1505 // which is entered and exited each time through the loop.
1506 //
1507 // See comments in ParseIfStatement for why we create a scope for
1508 // for-init-statement/condition and a new scope for substatement in C++.
1509 //
Mike Stump11289f42009-09-09 15:08:12 +00001510 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001511 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001512
Chris Lattner9075bd72006-08-10 04:59:57 +00001513 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001514 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001515
Chris Lattner8fb26252007-08-22 05:28:50 +00001516 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001517 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001518
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001519 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001520 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001521
1522 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001523 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001524
Richard Smith02e85f32011-04-14 22:09:26 +00001525 if (ForEach)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001526 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1527 FirstPart.take(),
1528 Collection.take(),
1529 T.getCloseLocation(),
1530 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001531
Richard Smith02e85f32011-04-14 22:09:26 +00001532 if (ForRange)
1533 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1534
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001535 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1536 SecondPart, SecondVar, ThirdPart,
1537 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001538}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001539
Chris Lattner503fadc2006-08-10 05:45:44 +00001540/// ParseGotoStatement
1541/// jump-statement:
1542/// 'goto' identifier ';'
1543/// [GNU] 'goto' '*' expression ';'
1544///
1545/// Note: this lets the caller parse the end ';'.
1546///
Richard Smithc202b282012-04-14 00:33:13 +00001547StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001548 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001549 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001550
John McCalldadc5752010-08-24 06:29:42 +00001551 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001552 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001553 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1554 Tok.getLocation());
1555 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001556 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001557 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001558 // GNU indirect goto extension.
1559 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001560 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001561 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001562 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001563 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001564 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001565 }
John McCallb268a282010-08-23 23:25:46 +00001566 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001567 } else {
1568 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001569 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001570 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001571
Sebastian Redlb62406f2008-12-11 19:48:14 +00001572 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001573}
1574
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001575/// ParseContinueStatement
1576/// jump-statement:
1577/// 'continue' ';'
1578///
1579/// Note: this lets the caller parse the end ';'.
1580///
Richard Smithc202b282012-04-14 00:33:13 +00001581StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001582 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001583 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001584}
1585
1586/// ParseBreakStatement
1587/// jump-statement:
1588/// 'break' ';'
1589///
1590/// Note: this lets the caller parse the end ';'.
1591///
Richard Smithc202b282012-04-14 00:33:13 +00001592StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001593 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001594 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001595}
1596
Chris Lattner503fadc2006-08-10 05:45:44 +00001597/// ParseReturnStatement
1598/// jump-statement:
1599/// 'return' expression[opt] ';'
Richard Smithc202b282012-04-14 00:33:13 +00001600StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001601 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001602 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001603
John McCalldadc5752010-08-24 06:29:42 +00001604 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001605 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001606 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001607 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001608 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001609 return StmtError();
1610 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001611
David Blaikiebbafb8a2012-03-11 07:00:24 +00001612 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001613 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001614 if (R.isUsable())
David Blaikiebbafb8a2012-03-11 07:00:24 +00001615 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001616 diag::warn_cxx98_compat_generalized_initializer_lists :
1617 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001618 << R.get()->getSourceRange();
1619 } else
1620 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001621 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001622 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001623 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001624 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001625 }
John McCallb268a282010-08-23 23:25:46 +00001626 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001627}
Chris Lattner0116c472006-08-15 06:03:28 +00001628
Eli Friedmana4b02c32011-09-30 01:13:51 +00001629/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1630/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier32503022012-06-11 20:47:18 +00001631///
1632/// [MS] ms-asm-statement:
1633/// ms-asm-block
1634/// ms-asm-block ms-asm-statement
1635///
1636/// [MS] ms-asm-block:
1637/// '__asm' ms-asm-line '\n'
1638/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1639///
1640/// [MS] ms-asm-instruction-block
1641/// ms-asm-line
1642/// ms-asm-line '\n' ms-asm-instruction-block
1643///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001644StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1645 SourceManager &SrcMgr = PP.getSourceManager();
1646 SourceLocation EndLoc = AsmLoc;
Chad Rosier32503022012-06-11 20:47:18 +00001647 SmallVector<Token, 4> AsmToks;
1648 SmallVector<unsigned, 4> LineEnds;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001649 do {
1650 bool InBraces = false;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001651 unsigned short savedBraceCount = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001652 bool InAsmComment = false;
1653 FileID FID;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001654 unsigned LineNo = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001655 unsigned NumTokensRead = 0;
1656 SourceLocation LBraceLoc;
1657
1658 if (Tok.is(tok::l_brace)) {
1659 // Braced inline asm: consume the opening brace.
1660 InBraces = true;
1661 savedBraceCount = BraceCount;
1662 EndLoc = LBraceLoc = ConsumeBrace();
1663 ++NumTokensRead;
1664 } else {
1665 // Single-line inline asm; compute which line it is on.
1666 std::pair<FileID, unsigned> ExpAsmLoc =
1667 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1668 FID = ExpAsmLoc.first;
1669 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1670 }
1671
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001672 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001673 do {
Eli Friedmana4b02c32011-09-30 01:13:51 +00001674 // If we hit EOF, we're done, period.
1675 if (Tok.is(tok::eof))
1676 break;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001677
1678 if (!InAsmComment && Tok.is(tok::semi)) {
1679 // A semicolon in an asm is the start of a comment.
1680 InAsmComment = true;
1681 if (InBraces) {
1682 // Compute which line the comment is on.
1683 std::pair<FileID, unsigned> ExpSemiLoc =
1684 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1685 FID = ExpSemiLoc.first;
1686 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1687 }
1688 } else if (!InBraces || InAsmComment) {
1689 // If end-of-line is significant, check whether this token is on a
1690 // new line.
1691 std::pair<FileID, unsigned> ExpLoc =
1692 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1693 if (ExpLoc.first != FID ||
1694 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1695 // If this is a single-line __asm, we're done.
1696 if (!InBraces)
1697 break;
1698 // We're no longer in a comment.
1699 InAsmComment = false;
1700 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1701 // Single-line asm always ends when a closing brace is seen.
1702 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1703 // does MSVC do here?
1704 break;
1705 }
Chad Rosier32503022012-06-11 20:47:18 +00001706 } else if (InBraces && Tok.is(tok::r_brace) &&
1707 BraceCount == savedBraceCount + 1) {
1708 // Consume the closing brace, and finish
1709 EndLoc = ConsumeBrace();
1710 break;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001711 }
1712
Chad Rosier32503022012-06-11 20:47:18 +00001713 AsmToks.push_back(Tok);
1714
Eli Friedmana4b02c32011-09-30 01:13:51 +00001715 // Consume the next token; make sure we don't modify the brace count etc.
1716 // if we are in a comment.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001717 EndLoc = TokLoc;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001718 if (InAsmComment)
1719 PP.Lex(Tok);
1720 else
1721 ConsumeAnyToken();
Steve Naroff8c099c32008-02-08 18:01:27 +00001722 TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001723 ++NumTokensRead;
1724 } while (1);
1725
Chad Rosier32503022012-06-11 20:47:18 +00001726 LineEnds.push_back(AsmToks.size());
1727
Eli Friedmana4b02c32011-09-30 01:13:51 +00001728 if (InBraces && BraceCount != savedBraceCount) {
1729 // __asm without closing brace (this can happen at EOF).
1730 Diag(Tok, diag::err_expected_rbrace);
1731 Diag(LBraceLoc, diag::note_matching) << "{";
1732 return StmtError();
1733 } else if (NumTokensRead == 0) {
1734 // Empty __asm.
1735 Diag(Tok, diag::err_expected_lbrace);
1736 return StmtError();
1737 }
1738 // Multiple adjacent asm's form together into a single asm statement
1739 // in the AST.
1740 if (!Tok.is(tok::kw_asm))
1741 break;
1742 EndLoc = ConsumeToken();
1743 } while (1);
Chad Rosier32503022012-06-11 20:47:18 +00001744
1745 // Collect the tokens into a string
1746 SmallString<512> Asm;
1747 SmallString<512> TokenBuf;
1748 TokenBuf.resize(512);
1749 unsigned AsmLineNum = 0;
1750 for (unsigned i = 0, e = AsmToks.size(); i < e; i++) {
1751 const char *ThisTokBuf = &TokenBuf[0];
1752 bool StringInvalid = false;
1753 unsigned ThisTokLen =
1754 Lexer::getSpelling(AsmToks[i], ThisTokBuf, PP.getSourceManager(),
1755 PP.getLangOpts(), &StringInvalid);
1756 if (i && (!AsmLineNum || i != LineEnds[AsmLineNum-1]))
1757 Asm += ' '; // FIXME: Too much whitespace around punctuation
1758 Asm += StringRef(ThisTokBuf, ThisTokLen);
1759 if (i + 1 == LineEnds[AsmLineNum] && i + 1 != AsmToks.size()) {
1760 Asm += '\n';
1761 ++AsmLineNum;
1762 }
1763 }
1764
1765 // FIXME: We should be passing the tokens and source locations, rather than
1766 // (or possibly in addition to the) AsmString. Sema is going to interact with
1767 // MC to determine Constraints, Clobbers, etc., which would be simplest to
1768 // do with the tokens.
1769 std::string AsmString = Asm.data();
1770 return Actions.ActOnMSAsmStmt(AsmLoc, AsmString, EndLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001771}
1772
Chris Lattner0116c472006-08-15 06:03:28 +00001773/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001774/// asm-statement:
1775/// gnu-asm-statement
1776/// ms-asm-statement
1777///
1778/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001779/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1780///
1781/// [GNU] asm-argument:
1782/// asm-string-literal
1783/// asm-string-literal ':' asm-operands[opt]
1784/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1785/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1786/// ':' asm-clobbers
1787///
1788/// [GNU] asm-clobbers:
1789/// asm-string-literal
1790/// asm-clobbers ',' asm-string-literal
1791///
John McCalldadc5752010-08-24 06:29:42 +00001792StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001793 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001794 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001795
David Blaikiebbafb8a2012-03-11 07:00:24 +00001796 if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001797 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001798 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001799 }
John McCall084e83d2011-03-24 11:26:52 +00001800 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001801 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001802 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001803
Chris Lattner0116c472006-08-15 06:03:28 +00001804 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001805 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001806 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001807 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001808 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001809
Chris Lattner0116c472006-08-15 06:03:28 +00001810 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001811 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001812 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001813 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001814 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001815 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001816 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001817 BalancedDelimiterTracker T(*this, tok::l_paren);
1818 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001819
John McCalldadc5752010-08-24 06:29:42 +00001820 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001821 if (AsmString.isInvalid()) {
Richard Smithd67aea22012-03-06 03:21:47 +00001822 // Consume up to and including the closing paren.
1823 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001824 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001825 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001826
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001827 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001828 ExprVector Constraints(Actions);
1829 ExprVector Exprs(Actions);
1830 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001831
Anders Carlsson19fe1162008-02-05 23:03:50 +00001832 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001833 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001834 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001835 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001836 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001837 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001838 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001839 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001840 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001841
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001842 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001843 bool AteExtraColon = false;
1844 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1845 // In C++ mode, parse "::" like ": :".
1846 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001847 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001848
Chris Lattner15768502009-12-20 23:08:04 +00001849 if (!AteExtraColon &&
1850 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001851 return StmtError();
1852 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001853
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001854 unsigned NumOutputs = Names.size();
1855
1856 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001857 if (AteExtraColon ||
1858 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1859 // In C++ mode, parse "::" like ": :".
1860 if (AteExtraColon)
1861 AteExtraColon = false;
1862 else {
1863 AteExtraColon = Tok.is(tok::coloncolon);
1864 ConsumeToken();
1865 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001866
Chris Lattner15768502009-12-20 23:08:04 +00001867 if (!AteExtraColon &&
1868 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001869 return StmtError();
1870 }
1871
1872 assert(Names.size() == Constraints.size() &&
1873 Constraints.size() == Exprs.size() &&
1874 "Input operand size mismatch!");
1875
1876 unsigned NumInputs = Names.size() - NumOutputs;
1877
1878 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001879 if (AteExtraColon || Tok.is(tok::colon)) {
1880 if (!AteExtraColon)
1881 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001882
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001883 // Parse the asm-string list for clobbers if present.
1884 if (Tok.isNot(tok::r_paren)) {
1885 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001886 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001887
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001888 if (Clobber.isInvalid())
1889 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001890
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001891 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001892
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001893 if (Tok.isNot(tok::comma)) break;
1894 ConsumeToken();
1895 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001896 }
1897 }
1898
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001899 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001900 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001901 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001902 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001903 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001904 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001905}
1906
1907/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001908/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001909///
1910/// [GNU] asm-operands:
1911/// asm-operand
1912/// asm-operands ',' asm-operand
1913///
1914/// [GNU] asm-operand:
1915/// asm-string-literal '(' expression ')'
1916/// '[' identifier ']' asm-string-literal '(' expression ')'
1917///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001918//
1919// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001920bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001921 SmallVectorImpl<Expr *> &Constraints,
1922 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001923 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001924 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001925 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001926
1927 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001928 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001929 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001930 BalancedDelimiterTracker T(*this, tok::l_square);
1931 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001932
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001933 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001934 Diag(Tok, diag::err_expected_ident);
1935 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001936 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001937 }
Mike Stump11289f42009-09-09 15:08:12 +00001938
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001939 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001940 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001941
Anders Carlsson9a020f92010-01-30 22:25:16 +00001942 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001943 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001944 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001945 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001946
John McCalldadc5752010-08-24 06:29:42 +00001947 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001948 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001949 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001950 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001951 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001952 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001953
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001954 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001955 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001956 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001957 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001958 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001959
Chris Lattner0116c472006-08-15 06:03:28 +00001960 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001961 BalancedDelimiterTracker T(*this, tok::l_paren);
1962 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001963 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001964 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001965 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001966 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001967 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001968 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001969 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001970 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001971 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001972 ConsumeToken();
1973 }
1974}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001975
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001976Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001977 assert(Tok.is(tok::l_brace));
1978 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001979
Erik Verbruggen6e922512012-04-12 10:11:59 +00001980 if (SkipFunctionBodies && trySkippingFunctionBody()) {
1981 BodyScope.Exit();
1982 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001983 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001984
John McCallfaf5fb42010-08-26 23:41:50 +00001985 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1986 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001987
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001988 // Do not enter a scope for the brace, as the arguments are in the same scope
1989 // (the function body) as the body itself. Instead, just read the statement
1990 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001991 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001992
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001993 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001994 if (FnBody.isInvalid()) {
1995 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001996 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001997 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001998 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001999
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002000 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002001 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00002002}
Sebastian Redlb219c902008-12-21 16:41:36 +00002003
Sebastian Redla7b98a72009-04-26 20:35:05 +00002004/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2005///
2006/// function-try-block:
2007/// 'try' ctor-initializer[opt] compound-statement handler-seq
2008///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002009Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00002010 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2011 SourceLocation TryLoc = ConsumeToken();
2012
John McCallfaf5fb42010-08-26 23:41:50 +00002013 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
2014 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00002015
2016 // Constructor initializer list?
2017 if (Tok.is(tok::colon))
2018 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002019 else
2020 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002021
Erik Verbruggen6e922512012-04-12 10:11:59 +00002022 if (SkipFunctionBodies && trySkippingFunctionBody()) {
2023 BodyScope.Exit();
2024 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002025 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002026
Sebastian Redld98ecd62009-04-26 21:08:36 +00002027 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00002028 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002029 // If we failed to parse the try-catch, we just give the function an empty
2030 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002031 if (FnBody.isInvalid()) {
2032 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002033 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00002034 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002035 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002036
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002037 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002038 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002039}
2040
Erik Verbruggen6e922512012-04-12 10:11:59 +00002041bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002042 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00002043 assert(SkipFunctionBodies &&
2044 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002045
2046 // We're in code-completion mode. Skip parsing for all function bodies unless
2047 // the body contains the code-completion point.
2048 TentativeParsingAction PA(*this);
2049 ConsumeBrace();
2050 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Erik Verbruggen6e922512012-04-12 10:11:59 +00002051 /*StopAtCodeCompletion=*/PP.isCodeCompletionEnabled())) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002052 PA.Commit();
2053 return true;
2054 }
2055
2056 PA.Revert();
2057 return false;
2058}
2059
Sebastian Redlb219c902008-12-21 16:41:36 +00002060/// ParseCXXTryBlock - Parse a C++ try-block.
2061///
2062/// try-block:
2063/// 'try' compound-statement handler-seq
2064///
Richard Smithc202b282012-04-14 00:33:13 +00002065StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002066 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2067
2068 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002069 return ParseCXXTryBlockCommon(TryLoc);
2070}
2071
2072/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2073/// function-try-block.
2074///
2075/// try-block:
2076/// 'try' compound-statement handler-seq
2077///
2078/// function-try-block:
2079/// 'try' ctor-initializer[opt] compound-statement handler-seq
2080///
2081/// handler-seq:
2082/// handler handler-seq[opt]
2083///
John Wiegley1c0675e2011-04-28 01:08:34 +00002084/// [Borland] try-block:
2085/// 'try' compound-statement seh-except-block
2086/// 'try' compound-statment seh-finally-block
2087///
John McCalldadc5752010-08-24 06:29:42 +00002088StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002089 if (Tok.isNot(tok::l_brace))
2090 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002091 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002092
2093 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002094 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002095 if (TryBlock.isInvalid())
2096 return move(TryBlock);
2097
John Wiegley1c0675e2011-04-28 01:08:34 +00002098 // Borland allows SEH-handlers with 'try'
Douglas Gregor60060d62011-10-21 03:57:52 +00002099
Richard Smithc202b282012-04-14 00:33:13 +00002100 if ((Tok.is(tok::identifier) &&
2101 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2102 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002103 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2104 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002105 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002106 SourceLocation Loc = ConsumeToken();
2107 Handler = ParseSEHExceptBlock(Loc);
2108 }
2109 else {
2110 SourceLocation Loc = ConsumeToken();
2111 Handler = ParseSEHFinallyBlock(Loc);
2112 }
2113 if(Handler.isInvalid())
2114 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00002115
John Wiegley1c0675e2011-04-28 01:08:34 +00002116 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2117 TryLoc,
2118 TryBlock.take(),
2119 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002120 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002121 else {
2122 StmtVector Handlers(Actions);
Richard Smithc202b282012-04-14 00:33:13 +00002123 ParsedAttributesWithRange attrs(AttrFactory);
John Wiegley1c0675e2011-04-28 01:08:34 +00002124 MaybeParseCXX0XAttributes(attrs);
2125 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002126
John Wiegley1c0675e2011-04-28 01:08:34 +00002127 if (Tok.isNot(tok::kw_catch))
2128 return StmtError(Diag(Tok, diag::err_expected_catch));
2129 while (Tok.is(tok::kw_catch)) {
2130 StmtResult Handler(ParseCXXCatchBlock());
2131 if (!Handler.isInvalid())
2132 Handlers.push_back(Handler.release());
2133 }
2134 // Don't bother creating the full statement if we don't have any usable
2135 // handlers.
2136 if (Handlers.empty())
2137 return StmtError();
2138
2139 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2140 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002141}
2142
2143/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2144///
2145/// handler:
2146/// 'catch' '(' exception-declaration ')' compound-statement
2147///
2148/// exception-declaration:
2149/// type-specifier-seq declarator
2150/// type-specifier-seq abstract-declarator
2151/// type-specifier-seq
2152/// '...'
2153///
John McCalldadc5752010-08-24 06:29:42 +00002154StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002155 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2156
2157 SourceLocation CatchLoc = ConsumeToken();
2158
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002159 BalancedDelimiterTracker T(*this, tok::l_paren);
2160 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002161 return StmtError();
2162
2163 // C++ 3.3.2p3:
2164 // The name in a catch exception-declaration is local to the handler and
2165 // shall not be redeclared in the outermost block of the handler.
2166 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2167
2168 // exception-declaration is equivalent to '...' or a parameter-declaration
2169 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002170 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002171 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002172 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002173 if (ParseCXXTypeSpecifierSeq(DS))
2174 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002175 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2176 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002177 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002178 } else
2179 ConsumeToken();
2180
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002181 T.consumeClose();
2182 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002183 return StmtError();
2184
2185 if (Tok.isNot(tok::l_brace))
2186 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2187
Alexis Hunt96d5c762009-11-21 08:43:09 +00002188 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002189 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002190 if (Block.isInvalid())
2191 return move(Block);
2192
John McCallb268a282010-08-23 23:25:46 +00002193 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002194}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002195
2196void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002197 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002198 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002199 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002200
Douglas Gregor43edb322011-10-24 22:31:10 +00002201 // Handle dependent statements by parsing the braces as a compound statement.
2202 // This is not the same behavior as Visual C++, which don't treat this as a
2203 // compound statement, but for Clang's type checking we can't have anything
2204 // inside these braces escaping to the surrounding code.
2205 if (Result.Behavior == IEB_Dependent) {
2206 if (!Tok.is(tok::l_brace)) {
2207 Diag(Tok, diag::err_expected_lbrace);
Richard Smithc202b282012-04-14 00:33:13 +00002208 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002209 }
Richard Smithc202b282012-04-14 00:33:13 +00002210
2211 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002212 if (Compound.isInvalid())
2213 return;
Richard Smithc202b282012-04-14 00:33:13 +00002214
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002215 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2216 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002217 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002218 Result.Name,
2219 Compound.get());
2220 if (DepResult.isUsable())
2221 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002222 return;
2223 }
Richard Smithc202b282012-04-14 00:33:13 +00002224
Douglas Gregor43edb322011-10-24 22:31:10 +00002225 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2226 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002227 Diag(Tok, diag::err_expected_lbrace);
2228 return;
2229 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002230
Douglas Gregor43edb322011-10-24 22:31:10 +00002231 switch (Result.Behavior) {
2232 case IEB_Parse:
2233 // Parse the statements below.
2234 break;
2235
2236 case IEB_Dependent:
2237 llvm_unreachable("Dependent case handled above");
Douglas Gregor43edb322011-10-24 22:31:10 +00002238
2239 case IEB_Skip:
2240 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002241 return;
2242 }
2243
2244 // Condition is true, parse the statements.
2245 while (Tok.isNot(tok::r_brace)) {
2246 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2247 if (R.isUsable())
2248 Stmts.push_back(R.release());
2249 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002250 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002251}