blob: 39c46234f610610ac4bb9ccfecf8111caf787d04 [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();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001262
1263 // FIXME: Do not just parse the attribute contents and throw them away
1264 ParsedAttributesWithRange attrs(AttrFactory);
1265 MaybeParseCXX0XAttributes(attrs);
1266 ProhibitAttributes(attrs);
1267
John McCalldadc5752010-08-24 06:29:42 +00001268 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001269 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001270 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001271
Sebastian Redlb62406f2008-12-11 19:48:14 +00001272 if (Cond.isInvalid() || Body.isInvalid())
1273 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001274
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001275 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1276 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001277}
1278
1279/// ParseForStatement
1280/// for-statement: [C99 6.8.5.3]
1281/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1282/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001283/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1284/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001285/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001286/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1287/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001288///
1289/// [C++] for-init-statement:
1290/// [C++] expression-statement
1291/// [C++] simple-declaration
1292///
Richard Smith02e85f32011-04-14 22:09:26 +00001293/// [C++0x] for-range-declaration:
1294/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1295/// [C++0x] for-range-initializer:
1296/// [C++0x] expression
1297/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001298StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001299 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001300 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001301
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001302 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001303 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001304 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001305 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001306 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001307
David Blaikiebbafb8a2012-03-11 07:00:24 +00001308 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001309
Chris Lattner2dd1b722007-08-26 23:08:06 +00001310 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1311 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001312 //
1313 // C++ 6.4p3:
1314 // A name introduced by a declaration in a condition is in scope from its
1315 // point of declaration until the end of the substatements controlled by the
1316 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001317 // C++ 3.3.2p4:
1318 // Names declared in the for-init-statement, and in the condition of if,
1319 // while, for, and switch statements are local to the if, while, for, or
1320 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001321 // C++ 6.5.3p1:
1322 // Names declared in the for-init-statement are in the same declarative-region
1323 // as those declared in the condition.
1324 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001325 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001326 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001327 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1328 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001329 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001330 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1331
1332 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001333
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001334 BalancedDelimiterTracker T(*this, tok::l_paren);
1335 T.consumeOpen();
1336
John McCalldadc5752010-08-24 06:29:42 +00001337 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001338
Richard Smith02e85f32011-04-14 22:09:26 +00001339 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001340 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001341 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001342 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001343 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001344 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001345 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001346 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001347
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001348 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001349 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001350 C99orCXXorObjC? Sema::PCC_ForInit
1351 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001352 cutOffParsing();
1353 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001354 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001355
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001356 ParsedAttributesWithRange attrs(AttrFactory);
1357 MaybeParseCXX0XAttributes(attrs);
1358
Chris Lattner9075bd72006-08-10 04:59:57 +00001359 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001360 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001361 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001362 // no first part, eat the ';'.
1363 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001364 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001365 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001366 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001367 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001368
John McCall084e83d2011-03-24 11:26:52 +00001369 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001370 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001371
Richard Smith02e85f32011-04-14 22:09:26 +00001372 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001373 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001374 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1375
Chris Lattner49836b42009-04-02 04:16:50 +00001376 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001377 StmtVector Stmts(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001378 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001379 DeclEnd, attrs, false,
1380 MightBeForRangeStmt ?
1381 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001382 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001383
Richard Smith02e85f32011-04-14 22:09:26 +00001384 if (ForRangeInit.ParsedForRangeDecl()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001385 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001386 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001387
Richard Smith02e85f32011-04-14 22:09:26 +00001388 ForRange = true;
1389 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001390 ConsumeToken();
1391 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001392 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001393 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001394 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001395
Douglas Gregor68762e72010-08-23 21:17:50 +00001396 if (Tok.is(tok::code_completion)) {
1397 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001398 cutOffParsing();
1399 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001400 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001401 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001402 } else {
1403 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001404 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001405 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001406 ProhibitAttributes(attrs);
Chris Lattner89c50c62006-08-11 06:41:18 +00001407 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001408
John McCall34376a62010-12-04 03:47:34 +00001409 ForEach = isTokIdentifier_in();
1410
Chris Lattnercd68f642007-06-27 01:06:29 +00001411 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001412 if (!Value.isInvalid()) {
1413 if (ForEach)
1414 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1415 else
1416 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1417 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001418
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001419 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001420 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001421 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001422 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001423
Douglas Gregor68762e72010-08-23 21:17:50 +00001424 if (Tok.is(tok::code_completion)) {
1425 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001426 cutOffParsing();
1427 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001428 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001429 Collection = ParseExpression();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001430 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001431 // User tried to write the reasonable, but ill-formed, for-range-statement
1432 // for (expr : expr) { ... }
1433 Diag(Tok, diag::err_for_range_expected_decl)
1434 << FirstPart.get()->getSourceRange();
1435 SkipUntil(tok::r_paren, false, true);
1436 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001437 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001438 if (!Value.isInvalid()) {
1439 Diag(Tok, diag::err_expected_semi_for);
1440 } else {
1441 // Skip until semicolon or rparen, don't consume it.
1442 SkipUntil(tok::r_paren, true, true);
1443 if (Tok.is(tok::semi))
1444 ConsumeToken();
1445 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001446 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001447 }
Richard Smith02e85f32011-04-14 22:09:26 +00001448 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001449 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001450 // Parse the second part of the for specifier.
1451 if (Tok.is(tok::semi)) { // for (...;;
1452 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001453 } else if (Tok.is(tok::r_paren)) {
1454 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001455 } else {
John McCalldadc5752010-08-24 06:29:42 +00001456 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001457 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001458 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1459 else {
1460 Second = ParseExpression();
1461 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001462 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001463 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001464 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001465 SecondPartIsInvalid = Second.isInvalid();
David Blaikiea5696df2012-05-16 04:20:04 +00001466 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001467 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001468
Douglas Gregor230a7e62011-02-17 03:38:46 +00001469 if (Tok.isNot(tok::semi)) {
1470 if (!SecondPartIsInvalid || SecondVar)
1471 Diag(Tok, diag::err_expected_semi_for);
1472 else
1473 // Skip until semicolon or rparen, don't consume it.
1474 SkipUntil(tok::r_paren, true, true);
1475 }
1476
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001477 if (Tok.is(tok::semi)) {
1478 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001479 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001480
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001481 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001482 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001483 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001484 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001485 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001486 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001487 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001488 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001489
Richard Smith02e85f32011-04-14 22:09:26 +00001490 // We need to perform most of the semantic analysis for a C++0x for-range
1491 // statememt before parsing the body, in order to be able to deduce the type
1492 // of an auto-typed loop variable.
1493 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001494 StmtResult ForEachStmt;
1495
John McCall53848232011-07-27 01:07:15 +00001496 if (ForRange) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001497 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smith02e85f32011-04-14 22:09:26 +00001498 FirstPart.take(),
1499 ForRangeInit.ColonLoc,
1500 ForRangeInit.RangeExpr.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001501 T.getCloseLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001502
John McCall53848232011-07-27 01:07:15 +00001503
1504 // Similarly, we need to do the semantic analysis for a for-range
1505 // statement immediately in order to close over temporaries correctly.
1506 } else if (ForEach) {
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001507 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1508 FirstPart.take(),
1509 Collection.take(),
1510 T.getCloseLocation());
John McCall53848232011-07-27 01:07:15 +00001511 }
1512
Chris Lattner8fb26252007-08-22 05:28:50 +00001513 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001514 // there is no compound stmt. C90 does not have this clause. We only do this
1515 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001516 //
1517 // C++ 6.5p2:
1518 // The substatement in an iteration-statement implicitly defines a local scope
1519 // which is entered and exited each time through the loop.
1520 //
1521 // See comments in ParseIfStatement for why we create a scope for
1522 // for-init-statement/condition and a new scope for substatement in C++.
1523 //
Mike Stump11289f42009-09-09 15:08:12 +00001524 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001525 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001526
Chris Lattner9075bd72006-08-10 04:59:57 +00001527 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001528 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001529
Chris Lattner8fb26252007-08-22 05:28:50 +00001530 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001531 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001532
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001533 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001534 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001535
1536 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001537 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001538
Richard Smith02e85f32011-04-14 22:09:26 +00001539 if (ForEach)
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001540 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1541 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001542
Richard Smith02e85f32011-04-14 22:09:26 +00001543 if (ForRange)
1544 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1545
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001546 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1547 SecondPart, SecondVar, ThirdPart,
1548 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001549}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001550
Chris Lattner503fadc2006-08-10 05:45:44 +00001551/// ParseGotoStatement
1552/// jump-statement:
1553/// 'goto' identifier ';'
1554/// [GNU] 'goto' '*' expression ';'
1555///
1556/// Note: this lets the caller parse the end ';'.
1557///
Richard Smithc202b282012-04-14 00:33:13 +00001558StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001559 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001560 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001561
John McCalldadc5752010-08-24 06:29:42 +00001562 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001563 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001564 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1565 Tok.getLocation());
1566 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001567 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001568 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001569 // GNU indirect goto extension.
1570 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001571 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001572 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001573 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001574 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001575 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001576 }
John McCallb268a282010-08-23 23:25:46 +00001577 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001578 } else {
1579 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001580 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001581 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001582
Sebastian Redlb62406f2008-12-11 19:48:14 +00001583 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001584}
1585
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001586/// ParseContinueStatement
1587/// jump-statement:
1588/// 'continue' ';'
1589///
1590/// Note: this lets the caller parse the end ';'.
1591///
Richard Smithc202b282012-04-14 00:33:13 +00001592StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001593 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001594 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001595}
1596
1597/// ParseBreakStatement
1598/// jump-statement:
1599/// 'break' ';'
1600///
1601/// Note: this lets the caller parse the end ';'.
1602///
Richard Smithc202b282012-04-14 00:33:13 +00001603StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001604 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001605 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001606}
1607
Chris Lattner503fadc2006-08-10 05:45:44 +00001608/// ParseReturnStatement
1609/// jump-statement:
1610/// 'return' expression[opt] ';'
Richard Smithc202b282012-04-14 00:33:13 +00001611StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001612 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001613 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001614
John McCalldadc5752010-08-24 06:29:42 +00001615 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001616 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001617 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001618 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001619 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001620 return StmtError();
1621 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001622
David Blaikiebbafb8a2012-03-11 07:00:24 +00001623 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001624 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001625 if (R.isUsable())
David Blaikiebbafb8a2012-03-11 07:00:24 +00001626 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001627 diag::warn_cxx98_compat_generalized_initializer_lists :
1628 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001629 << R.get()->getSourceRange();
1630 } else
1631 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001632 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001633 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001634 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001635 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001636 }
John McCallb268a282010-08-23 23:25:46 +00001637 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001638}
Chris Lattner0116c472006-08-15 06:03:28 +00001639
Chad Rosier0764e0b2012-06-12 19:03:42 +00001640// needSpaceAsmToken - This function handles whitespace around asm punctuation.
1641// Returns true if a space should be emitted.
1642static inline bool needSpaceAsmToken(Token currTok) {
1643 static Token prevTok;
1644
1645 // No need for space after prevToken.
1646 switch(prevTok.getKind()) {
1647 default:
1648 break;
1649 case tok::l_square:
1650 case tok::r_square:
1651 case tok::l_brace:
1652 case tok::r_brace:
1653 case tok::colon:
1654 prevTok = currTok;
1655 return false;
1656 }
1657
1658 // No need for a space before currToken.
1659 switch(currTok.getKind()) {
1660 default:
1661 break;
1662 case tok::l_square:
1663 case tok::r_square:
1664 case tok::l_brace:
1665 case tok::r_brace:
1666 case tok::comma:
1667 case tok::colon:
1668 prevTok = currTok;
1669 return false;
1670 }
1671 prevTok = currTok;
1672 return true;
1673}
1674
Eli Friedmana4b02c32011-09-30 01:13:51 +00001675/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1676/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier32503022012-06-11 20:47:18 +00001677///
1678/// [MS] ms-asm-statement:
1679/// ms-asm-block
1680/// ms-asm-block ms-asm-statement
1681///
1682/// [MS] ms-asm-block:
1683/// '__asm' ms-asm-line '\n'
1684/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1685///
1686/// [MS] ms-asm-instruction-block
1687/// ms-asm-line
1688/// ms-asm-line '\n' ms-asm-instruction-block
1689///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001690StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1691 SourceManager &SrcMgr = PP.getSourceManager();
1692 SourceLocation EndLoc = AsmLoc;
Chad Rosier32503022012-06-11 20:47:18 +00001693 SmallVector<Token, 4> AsmToks;
1694 SmallVector<unsigned, 4> LineEnds;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001695 do {
1696 bool InBraces = false;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001697 unsigned short savedBraceCount = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001698 bool InAsmComment = false;
1699 FileID FID;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001700 unsigned LineNo = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001701 unsigned NumTokensRead = 0;
1702 SourceLocation LBraceLoc;
1703
1704 if (Tok.is(tok::l_brace)) {
1705 // Braced inline asm: consume the opening brace.
1706 InBraces = true;
1707 savedBraceCount = BraceCount;
1708 EndLoc = LBraceLoc = ConsumeBrace();
1709 ++NumTokensRead;
1710 } else {
1711 // Single-line inline asm; compute which line it is on.
1712 std::pair<FileID, unsigned> ExpAsmLoc =
1713 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1714 FID = ExpAsmLoc.first;
1715 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1716 }
1717
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001718 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001719 do {
Eli Friedmana4b02c32011-09-30 01:13:51 +00001720 // If we hit EOF, we're done, period.
1721 if (Tok.is(tok::eof))
1722 break;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001723
Chad Rosier075608e2012-06-12 20:30:26 +00001724 // The asm keyword is a statement separator, so multiple asm statements
1725 // are allowed.
1726 if (!InAsmComment && Tok.is(tok::kw_asm))
1727 break;
1728
Eli Friedmana4b02c32011-09-30 01:13:51 +00001729 if (!InAsmComment && Tok.is(tok::semi)) {
1730 // A semicolon in an asm is the start of a comment.
1731 InAsmComment = true;
1732 if (InBraces) {
1733 // Compute which line the comment is on.
1734 std::pair<FileID, unsigned> ExpSemiLoc =
1735 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1736 FID = ExpSemiLoc.first;
1737 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1738 }
1739 } else if (!InBraces || InAsmComment) {
1740 // If end-of-line is significant, check whether this token is on a
1741 // new line.
1742 std::pair<FileID, unsigned> ExpLoc =
1743 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1744 if (ExpLoc.first != FID ||
1745 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1746 // If this is a single-line __asm, we're done.
1747 if (!InBraces)
1748 break;
1749 // We're no longer in a comment.
1750 InAsmComment = false;
1751 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1752 // Single-line asm always ends when a closing brace is seen.
1753 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1754 // does MSVC do here?
1755 break;
1756 }
Chad Rosier0764e0b2012-06-12 19:03:42 +00001757 }
1758 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
1759 BraceCount == (savedBraceCount + 1)) {
Chad Rosier32503022012-06-11 20:47:18 +00001760 // Consume the closing brace, and finish
1761 EndLoc = ConsumeBrace();
1762 break;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001763 }
1764
1765 // Consume the next token; make sure we don't modify the brace count etc.
1766 // if we are in a comment.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001767 EndLoc = TokLoc;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001768 if (InAsmComment)
1769 PP.Lex(Tok);
Chad Rosier0764e0b2012-06-12 19:03:42 +00001770 else {
1771 AsmToks.push_back(Tok);
Eli Friedmana4b02c32011-09-30 01:13:51 +00001772 ConsumeAnyToken();
Chad Rosier0764e0b2012-06-12 19:03:42 +00001773 }
Steve Naroff8c099c32008-02-08 18:01:27 +00001774 TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001775 ++NumTokensRead;
1776 } while (1);
1777
Chad Rosier32503022012-06-11 20:47:18 +00001778 LineEnds.push_back(AsmToks.size());
1779
Eli Friedmana4b02c32011-09-30 01:13:51 +00001780 if (InBraces && BraceCount != savedBraceCount) {
1781 // __asm without closing brace (this can happen at EOF).
1782 Diag(Tok, diag::err_expected_rbrace);
1783 Diag(LBraceLoc, diag::note_matching) << "{";
1784 return StmtError();
1785 } else if (NumTokensRead == 0) {
1786 // Empty __asm.
1787 Diag(Tok, diag::err_expected_lbrace);
1788 return StmtError();
1789 }
1790 // Multiple adjacent asm's form together into a single asm statement
1791 // in the AST.
1792 if (!Tok.is(tok::kw_asm))
1793 break;
1794 EndLoc = ConsumeToken();
1795 } while (1);
Chad Rosier32503022012-06-11 20:47:18 +00001796
1797 // Collect the tokens into a string
1798 SmallString<512> Asm;
1799 SmallString<512> TokenBuf;
1800 TokenBuf.resize(512);
1801 unsigned AsmLineNum = 0;
Chad Rosier0764e0b2012-06-12 19:03:42 +00001802 for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
Chad Rosier32503022012-06-11 20:47:18 +00001803 const char *ThisTokBuf = &TokenBuf[0];
1804 bool StringInvalid = false;
1805 unsigned ThisTokLen =
1806 Lexer::getSpelling(AsmToks[i], ThisTokBuf, PP.getSourceManager(),
1807 PP.getLangOpts(), &StringInvalid);
Chad Rosier0764e0b2012-06-12 19:03:42 +00001808 if (i && (!AsmLineNum || i != LineEnds[AsmLineNum-1]) &&
1809 needSpaceAsmToken(AsmToks[i]))
1810 Asm += ' ';
Chad Rosier32503022012-06-11 20:47:18 +00001811 Asm += StringRef(ThisTokBuf, ThisTokLen);
1812 if (i + 1 == LineEnds[AsmLineNum] && i + 1 != AsmToks.size()) {
1813 Asm += '\n';
1814 ++AsmLineNum;
1815 }
1816 }
1817
1818 // FIXME: We should be passing the tokens and source locations, rather than
1819 // (or possibly in addition to the) AsmString. Sema is going to interact with
1820 // MC to determine Constraints, Clobbers, etc., which would be simplest to
1821 // do with the tokens.
Chad Rosier0764e0b2012-06-12 19:03:42 +00001822 std::string AsmString = Asm.c_str();
Chad Rosier32503022012-06-11 20:47:18 +00001823 return Actions.ActOnMSAsmStmt(AsmLoc, AsmString, EndLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001824}
1825
Chris Lattner0116c472006-08-15 06:03:28 +00001826/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001827/// asm-statement:
1828/// gnu-asm-statement
1829/// ms-asm-statement
1830///
1831/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001832/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1833///
1834/// [GNU] asm-argument:
1835/// asm-string-literal
1836/// asm-string-literal ':' asm-operands[opt]
1837/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1838/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1839/// ':' asm-clobbers
1840///
1841/// [GNU] asm-clobbers:
1842/// asm-string-literal
1843/// asm-clobbers ',' asm-string-literal
1844///
John McCalldadc5752010-08-24 06:29:42 +00001845StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001846 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001847 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001848
David Blaikiebbafb8a2012-03-11 07:00:24 +00001849 if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001850 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001851 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001852 }
John McCall084e83d2011-03-24 11:26:52 +00001853 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001854 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001855 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001856
Chris Lattner0116c472006-08-15 06:03:28 +00001857 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001858 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001859 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001860 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001861 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001862
Chris Lattner0116c472006-08-15 06:03:28 +00001863 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001864 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001865 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001866 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001867 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001868 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001869 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001870 BalancedDelimiterTracker T(*this, tok::l_paren);
1871 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001872
John McCalldadc5752010-08-24 06:29:42 +00001873 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001874 if (AsmString.isInvalid()) {
Richard Smithd67aea22012-03-06 03:21:47 +00001875 // Consume up to and including the closing paren.
1876 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001877 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001878 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001879
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001880 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001881 ExprVector Constraints(Actions);
1882 ExprVector Exprs(Actions);
1883 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001884
Anders Carlsson19fe1162008-02-05 23:03:50 +00001885 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001886 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001887 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001888 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001889 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001890 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001891 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001892 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001893 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001894
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001895 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001896 bool AteExtraColon = false;
1897 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1898 // In C++ mode, parse "::" like ": :".
1899 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001900 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001901
Chris Lattner15768502009-12-20 23:08:04 +00001902 if (!AteExtraColon &&
1903 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001904 return StmtError();
1905 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001906
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001907 unsigned NumOutputs = Names.size();
1908
1909 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001910 if (AteExtraColon ||
1911 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1912 // In C++ mode, parse "::" like ": :".
1913 if (AteExtraColon)
1914 AteExtraColon = false;
1915 else {
1916 AteExtraColon = Tok.is(tok::coloncolon);
1917 ConsumeToken();
1918 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001919
Chris Lattner15768502009-12-20 23:08:04 +00001920 if (!AteExtraColon &&
1921 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001922 return StmtError();
1923 }
1924
1925 assert(Names.size() == Constraints.size() &&
1926 Constraints.size() == Exprs.size() &&
1927 "Input operand size mismatch!");
1928
1929 unsigned NumInputs = Names.size() - NumOutputs;
1930
1931 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001932 if (AteExtraColon || Tok.is(tok::colon)) {
1933 if (!AteExtraColon)
1934 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001935
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001936 // Parse the asm-string list for clobbers if present.
1937 if (Tok.isNot(tok::r_paren)) {
1938 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001939 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001940
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001941 if (Clobber.isInvalid())
1942 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001943
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001944 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001945
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001946 if (Tok.isNot(tok::comma)) break;
1947 ConsumeToken();
1948 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001949 }
1950 }
1951
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001952 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001953 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001954 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001955 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001956 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001957 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001958}
1959
1960/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001961/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001962///
1963/// [GNU] asm-operands:
1964/// asm-operand
1965/// asm-operands ',' asm-operand
1966///
1967/// [GNU] asm-operand:
1968/// asm-string-literal '(' expression ')'
1969/// '[' identifier ']' asm-string-literal '(' expression ')'
1970///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001971//
1972// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001973bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001974 SmallVectorImpl<Expr *> &Constraints,
1975 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001976 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001977 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001978 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001979
1980 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001981 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001982 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001983 BalancedDelimiterTracker T(*this, tok::l_square);
1984 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001985
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001986 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001987 Diag(Tok, diag::err_expected_ident);
1988 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001989 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001990 }
Mike Stump11289f42009-09-09 15:08:12 +00001991
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001992 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001993 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001994
Anders Carlsson9a020f92010-01-30 22:25:16 +00001995 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001996 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001997 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001998 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001999
John McCalldadc5752010-08-24 06:29:42 +00002000 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002001 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00002002 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00002003 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00002004 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002005 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00002006
Chris Lattnerfeb00b62007-10-09 17:41:39 +00002007 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002008 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00002009 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00002010 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00002011 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002012
Chris Lattner0116c472006-08-15 06:03:28 +00002013 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002014 BalancedDelimiterTracker T(*this, tok::l_paren);
2015 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00002016 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002017 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002018 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00002019 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00002020 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00002021 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002022 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00002023 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00002024 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00002025 ConsumeToken();
2026 }
2027}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00002028
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002029Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00002030 assert(Tok.is(tok::l_brace));
2031 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002032
Erik Verbruggen6e922512012-04-12 10:11:59 +00002033 if (SkipFunctionBodies && trySkippingFunctionBody()) {
2034 BodyScope.Exit();
2035 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002036 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002037
John McCallfaf5fb42010-08-26 23:41:50 +00002038 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
2039 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00002040
Fariborz Jahanian8e632942007-11-08 19:01:26 +00002041 // Do not enter a scope for the brace, as the arguments are in the same scope
2042 // (the function body) as the body itself. Instead, just read the statement
2043 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00002044 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00002045
Fariborz Jahanian8e632942007-11-08 19:01:26 +00002046 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002047 if (FnBody.isInvalid()) {
2048 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002049 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00002050 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002051 }
Sebastian Redl042ad952008-12-11 19:30:53 +00002052
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002053 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002054 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00002055}
Sebastian Redlb219c902008-12-21 16:41:36 +00002056
Sebastian Redla7b98a72009-04-26 20:35:05 +00002057/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2058///
2059/// function-try-block:
2060/// 'try' ctor-initializer[opt] compound-statement handler-seq
2061///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002062Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00002063 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2064 SourceLocation TryLoc = ConsumeToken();
2065
John McCallfaf5fb42010-08-26 23:41:50 +00002066 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
2067 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00002068
2069 // Constructor initializer list?
2070 if (Tok.is(tok::colon))
2071 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002072 else
2073 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002074
Erik Verbruggen6e922512012-04-12 10:11:59 +00002075 if (SkipFunctionBodies && trySkippingFunctionBody()) {
2076 BodyScope.Exit();
2077 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002078 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002079
Sebastian Redld98ecd62009-04-26 21:08:36 +00002080 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00002081 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002082 // If we failed to parse the try-catch, we just give the function an empty
2083 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002084 if (FnBody.isInvalid()) {
2085 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002086 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00002087 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002088 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002089
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002090 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002091 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002092}
2093
Erik Verbruggen6e922512012-04-12 10:11:59 +00002094bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002095 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00002096 assert(SkipFunctionBodies &&
2097 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002098
2099 // We're in code-completion mode. Skip parsing for all function bodies unless
2100 // the body contains the code-completion point.
2101 TentativeParsingAction PA(*this);
2102 ConsumeBrace();
2103 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Erik Verbruggen6e922512012-04-12 10:11:59 +00002104 /*StopAtCodeCompletion=*/PP.isCodeCompletionEnabled())) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002105 PA.Commit();
2106 return true;
2107 }
2108
2109 PA.Revert();
2110 return false;
2111}
2112
Sebastian Redlb219c902008-12-21 16:41:36 +00002113/// ParseCXXTryBlock - Parse a C++ try-block.
2114///
2115/// try-block:
2116/// 'try' compound-statement handler-seq
2117///
Richard Smithc202b282012-04-14 00:33:13 +00002118StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002119 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2120
2121 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002122 return ParseCXXTryBlockCommon(TryLoc);
2123}
2124
2125/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2126/// function-try-block.
2127///
2128/// try-block:
2129/// 'try' compound-statement handler-seq
2130///
2131/// function-try-block:
2132/// 'try' ctor-initializer[opt] compound-statement handler-seq
2133///
2134/// handler-seq:
2135/// handler handler-seq[opt]
2136///
John Wiegley1c0675e2011-04-28 01:08:34 +00002137/// [Borland] try-block:
2138/// 'try' compound-statement seh-except-block
2139/// 'try' compound-statment seh-finally-block
2140///
John McCalldadc5752010-08-24 06:29:42 +00002141StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002142 if (Tok.isNot(tok::l_brace))
2143 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002144 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002145
2146 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002147 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002148 if (TryBlock.isInvalid())
2149 return move(TryBlock);
2150
John Wiegley1c0675e2011-04-28 01:08:34 +00002151 // Borland allows SEH-handlers with 'try'
Douglas Gregor60060d62011-10-21 03:57:52 +00002152
Richard Smithc202b282012-04-14 00:33:13 +00002153 if ((Tok.is(tok::identifier) &&
2154 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2155 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002156 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2157 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002158 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002159 SourceLocation Loc = ConsumeToken();
2160 Handler = ParseSEHExceptBlock(Loc);
2161 }
2162 else {
2163 SourceLocation Loc = ConsumeToken();
2164 Handler = ParseSEHFinallyBlock(Loc);
2165 }
2166 if(Handler.isInvalid())
2167 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00002168
John Wiegley1c0675e2011-04-28 01:08:34 +00002169 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2170 TryLoc,
2171 TryBlock.take(),
2172 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002173 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002174 else {
2175 StmtVector Handlers(Actions);
Richard Smithc202b282012-04-14 00:33:13 +00002176 ParsedAttributesWithRange attrs(AttrFactory);
John Wiegley1c0675e2011-04-28 01:08:34 +00002177 MaybeParseCXX0XAttributes(attrs);
2178 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002179
John Wiegley1c0675e2011-04-28 01:08:34 +00002180 if (Tok.isNot(tok::kw_catch))
2181 return StmtError(Diag(Tok, diag::err_expected_catch));
2182 while (Tok.is(tok::kw_catch)) {
2183 StmtResult Handler(ParseCXXCatchBlock());
2184 if (!Handler.isInvalid())
2185 Handlers.push_back(Handler.release());
2186 }
2187 // Don't bother creating the full statement if we don't have any usable
2188 // handlers.
2189 if (Handlers.empty())
2190 return StmtError();
2191
2192 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2193 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002194}
2195
2196/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2197///
2198/// handler:
2199/// 'catch' '(' exception-declaration ')' compound-statement
2200///
2201/// exception-declaration:
2202/// type-specifier-seq declarator
2203/// type-specifier-seq abstract-declarator
2204/// type-specifier-seq
2205/// '...'
2206///
John McCalldadc5752010-08-24 06:29:42 +00002207StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002208 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2209
2210 SourceLocation CatchLoc = ConsumeToken();
2211
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002212 BalancedDelimiterTracker T(*this, tok::l_paren);
2213 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002214 return StmtError();
2215
2216 // C++ 3.3.2p3:
2217 // The name in a catch exception-declaration is local to the handler and
2218 // shall not be redeclared in the outermost block of the handler.
2219 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2220
2221 // exception-declaration is equivalent to '...' or a parameter-declaration
2222 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002223 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002224 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002225 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002226 if (ParseCXXTypeSpecifierSeq(DS))
2227 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002228 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2229 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002230 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002231 } else
2232 ConsumeToken();
2233
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002234 T.consumeClose();
2235 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002236 return StmtError();
2237
2238 if (Tok.isNot(tok::l_brace))
2239 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2240
Alexis Hunt96d5c762009-11-21 08:43:09 +00002241 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002242 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002243 if (Block.isInvalid())
2244 return move(Block);
2245
John McCallb268a282010-08-23 23:25:46 +00002246 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002247}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002248
2249void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002250 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002251 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002252 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002253
Douglas Gregor43edb322011-10-24 22:31:10 +00002254 // Handle dependent statements by parsing the braces as a compound statement.
2255 // This is not the same behavior as Visual C++, which don't treat this as a
2256 // compound statement, but for Clang's type checking we can't have anything
2257 // inside these braces escaping to the surrounding code.
2258 if (Result.Behavior == IEB_Dependent) {
2259 if (!Tok.is(tok::l_brace)) {
2260 Diag(Tok, diag::err_expected_lbrace);
Richard Smithc202b282012-04-14 00:33:13 +00002261 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002262 }
Richard Smithc202b282012-04-14 00:33:13 +00002263
2264 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002265 if (Compound.isInvalid())
2266 return;
Richard Smithc202b282012-04-14 00:33:13 +00002267
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002268 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2269 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002270 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002271 Result.Name,
2272 Compound.get());
2273 if (DepResult.isUsable())
2274 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002275 return;
2276 }
Richard Smithc202b282012-04-14 00:33:13 +00002277
Douglas Gregor43edb322011-10-24 22:31:10 +00002278 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2279 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002280 Diag(Tok, diag::err_expected_lbrace);
2281 return;
2282 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002283
Douglas Gregor43edb322011-10-24 22:31:10 +00002284 switch (Result.Behavior) {
2285 case IEB_Parse:
2286 // Parse the statements below.
2287 break;
2288
2289 case IEB_Dependent:
2290 llvm_unreachable("Dependent case handled above");
Douglas Gregor43edb322011-10-24 22:31:10 +00002291
2292 case IEB_Skip:
2293 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002294 return;
2295 }
2296
2297 // Condition is true, parse the statements.
2298 while (Tok.isNot(tok::r_brace)) {
2299 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2300 if (R.isUsable())
2301 Stmts.push_back(R.release());
2302 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002303 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002304}