blob: 28be864bdf6181520613d1b75cfaa35d40a1ad65 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000023using namespace clang;
24
Douglas Gregor0e7dde52011-04-24 05:37:28 +000025static bool isColonOrRSquareBracket(const Token &Tok) {
26 return Tok.is(tok::colon) || Tok.is(tok::r_square);
27}
28
Chris Lattner0ccd51e2006-08-09 05:47:47 +000029//===----------------------------------------------------------------------===//
30// C99 6.8: Statements and Blocks.
31//===----------------------------------------------------------------------===//
32
33/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
34/// StatementOrDeclaration:
35/// statement
36/// declaration
37///
38/// statement:
39/// labeled-statement
40/// compound-statement
41/// expression-statement
42/// selection-statement
43/// iteration-statement
44/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000045/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000046/// [C++] try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000047/// [OBC] objc-throw-statement
48/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000049/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000050/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000051/// [OMP] openmp-construct [TODO]
52///
53/// labeled-statement:
54/// identifier ':' statement
55/// 'case' constant-expression ':' statement
56/// 'default' ':' statement
57///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000058/// selection-statement:
59/// if-statement
60/// switch-statement
61///
62/// iteration-statement:
63/// while-statement
64/// do-statement
65/// for-statement
66///
Chris Lattner9075bd72006-08-10 04:59:57 +000067/// expression-statement:
68/// expression[opt] ';'
69///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000070/// jump-statement:
71/// 'goto' identifier ';'
72/// 'continue' ';'
73/// 'break' ';'
74/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000075/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000076///
Fariborz Jahanian90814572007-10-04 20:19:06 +000077/// [OBC] objc-throw-statement:
78/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000079/// [OBC] '@' 'throw' ';'
80///
John McCalldadc5752010-08-24 06:29:42 +000081StmtResult
Fariborz Jahanian1db5c942010-09-28 20:42:35 +000082Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000083 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000084 StmtResult Res;
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000085
86 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000087
John McCall084e83d2011-03-24 11:26:52 +000088 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000089 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +000090
Chris Lattner503fadc2006-08-10 05:45:44 +000091 // Cases in this switch statement should fall through if the parser expects
92 // the token to end in a semicolon (in which case SemiError should be set),
93 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +000094Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000095 tok::TokenKind Kind = Tok.getKind();
96 SourceLocation AtLoc;
97 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000098 case tok::at: // May be a @try or @throw statement
99 {
100 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000101 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000102 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000103
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000104 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000105 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorf4c33342010-05-28 00:22:41 +0000106 ConsumeCodeCompletionToken();
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000107 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000108
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000109 case tok::identifier: {
110 Token Next = NextToken();
111 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000112 // identifier ':' statement
John McCall53fa7142010-12-24 02:08:15 +0000113 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000114 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000115
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000116 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000117 CXXScopeSpec SS;
118 IdentifierInfo *Name = Tok.getIdentifierInfo();
119 SourceLocation NameLoc = Tok.getLocation();
120 Sema::NameClassification Classification
121 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
122 switch (Classification.getKind()) {
123 case Sema::NC_Keyword:
124 // The identifier was corrected to a keyword. Update the token
125 // to this keyword, and try again.
126 if (Name->getTokenID() != tok::identifier) {
127 Tok.setIdentifierInfo(Name);
128 Tok.setKind(Name->getTokenID());
129 goto Retry;
130 }
131
132 // Fall through via the normal error path.
133 // FIXME: This seems like it could only happen for context-sensitive
134 // keywords.
135
136 case Sema::NC_Error:
137 // Handle errors here by skipping up to the next semicolon or '}', and
138 // eat the semicolon if that's what stopped us.
139 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
140 if (Tok.is(tok::semi))
141 ConsumeToken();
142 return StmtError();
143
144 case Sema::NC_Unknown:
145 // Either we don't know anything about this identifier, or we know that
146 // we're in a syntactic context we haven't handled yet.
147 break;
148
Douglas Gregor19b7acf2011-04-27 05:41:15 +0000149 case Sema::NC_Type:
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000150 Tok.setKind(tok::annot_typename);
151 setTypeAnnotation(Tok, Classification.getType());
152 Tok.setAnnotationEndLoc(NameLoc);
153 Tok.setLocation(NameLoc);
154 PP.AnnotateCachedTokens(Tok);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000155 break;
156
157 case Sema::NC_Expression:
158 ConsumeToken(); // the identifier
159 return ParseExprStatement(attrs, Classification.getExpression());
160
161 case Sema::NC_TypeTemplate:
162 case Sema::NC_FunctionTemplate: {
163 ConsumeToken(); // the identifier
164 UnqualifiedId Id;
165 Id.setIdentifier(Name, NameLoc);
166 if (AnnotateTemplateIdToken(
167 TemplateTy::make(Classification.getTemplateName()),
168 Classification.getTemplateNameKind(),
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000169 SS, Id, SourceLocation(),
170 /*AllowTypeAnnotation=*/false)) {
171 // Handle errors here by skipping up to the next semicolon or '}', and
172 // eat the semicolon if that's what stopped us.
173 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
174 if (Tok.is(tok::semi))
175 ConsumeToken();
176 return StmtError();
177 }
178
179 // If the next token is '::', jump right into parsing a
180 // nested-name-specifier. We don't want to leave the template-id
181 // hanging.
182 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000183 // Handle errors here by skipping up to the next semicolon or '}', and
184 // eat the semicolon if that's what stopped us.
185 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
186 if (Tok.is(tok::semi))
187 ConsumeToken();
188 return StmtError();
189 }
190
191 // We've annotated a template-id, so try again now.
192 goto Retry;
193 }
194
195 case Sema::NC_NestedNameSpecifier:
196 // FIXME: Implement this!
197 break;
198 }
199 }
200
201 // Fall through
202 }
203
Chris Lattner803802d2009-03-24 17:04:48 +0000204 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000205 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000206 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000207 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000208 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000209 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000210 }
211
212 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000213 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000214 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000217 return ParseExprStatement(attrs, ExprResult());
Chris Lattner803802d2009-03-24 17:04:48 +0000218 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000219
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000220 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000221 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000222 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000223 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000224
Chris Lattner9075bd72006-08-10 04:59:57 +0000225 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000226 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000227 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidisf7620e42011-04-27 05:04:02 +0000228 SourceLocation LeadingEmptyMacroLoc;
229 if (Tok.hasLeadingEmptyMacro())
230 LeadingEmptyMacroLoc = PP.getLastEmptyMacroInstantiationLoc();
231 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacroLoc);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000232 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000233
Chris Lattner9075bd72006-08-10 04:59:57 +0000234 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall53fa7142010-12-24 02:08:15 +0000235 return ParseIfStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000236 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall53fa7142010-12-24 02:08:15 +0000237 return ParseSwitchStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000238
Chris Lattner9075bd72006-08-10 04:59:57 +0000239 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall53fa7142010-12-24 02:08:15 +0000240 return ParseWhileStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000241 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000242 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000243 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000244 break;
245 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall53fa7142010-12-24 02:08:15 +0000246 return ParseForStatement(attrs);
Chris Lattner503fadc2006-08-10 05:45:44 +0000247
248 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000249 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000250 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000251 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000252 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000253 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000254 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000255 break;
256 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000257 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000258 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000259 break;
260 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000261 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000262 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000263 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000264
Sebastian Redlb219c902008-12-21 16:41:36 +0000265 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000266 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000267 bool msAsm = false;
268 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000269 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000270 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000271 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000272 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000273 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000274
Sebastian Redlb219c902008-12-21 16:41:36 +0000275 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000276 return ParseCXXTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000277 }
278
Chris Lattner503fadc2006-08-10 05:45:44 +0000279 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000280 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000281 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000282 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000283 // If the result was valid, then we do want to diagnose this. Use
284 // ExpectAndConsume to emit the diagnostic, even though we know it won't
285 // succeed.
286 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000287 // Skip until we see a } or ;, but don't eat it.
288 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000289 }
Mike Stump11289f42009-09-09 15:08:12 +0000290
Sebastian Redl042ad952008-12-11 19:30:53 +0000291 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000292}
293
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000294/// \brief Parse an expression statement.
295StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs,
296 ExprResult Primary) {
297 // If a case keyword is missing, this is where it should be inserted.
298 Token OldToken = Tok;
299
300 // FIXME: Use the attributes
301 // expression[opt] ';'
302 ExprResult Expr(ParseExpression(Primary));
303 if (Expr.isInvalid()) {
304 // If the expression is invalid, skip ahead to the next semicolon or '}'.
305 // Not doing this opens us up to the possibility of infinite loops if
306 // ParseExpression does not consume any tokens.
307 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
308 if (Tok.is(tok::semi))
309 ConsumeToken();
310 return StmtError();
311 }
312
313 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
314 Actions.CheckCaseExpression(Expr.get())) {
315 // If a constant expression is followed by a colon inside a switch block,
316 // suggest a missing case keyword.
317 Diag(OldToken, diag::err_expected_case_before_expression)
318 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
319
320 // Recover parsing as a case statement.
321 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
322 }
323
324 // Otherwise, eat the semicolon.
325 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
326 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
327
328}
329
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000330/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000331///
332/// labeled-statement:
333/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000334/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000335///
John McCall53fa7142010-12-24 02:08:15 +0000336StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000337 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
338 "Not an identifier!");
339
340 Token IdentTok = Tok; // Save the whole token.
341 ConsumeToken(); // eat the identifier.
342
343 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000344
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000345 // identifier ':' statement
346 SourceLocation ColonLoc = ConsumeToken();
347
348 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000349 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000350
John McCalldadc5752010-08-24 06:29:42 +0000351 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000352
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000353 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000354 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000355 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000356
357 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
358 IdentTok.getLocation());
359 if (AttributeList *Attrs = attrs.getList())
360 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
361
362 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
363 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000364}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000365
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000366/// ParseCaseStatement
367/// labeled-statement:
368/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000369/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000370///
Richard Trieu2c850c02011-04-21 21:44:26 +0000371StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
372 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000373 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000374 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000375
Chris Lattner34a22092009-03-04 04:23:07 +0000376 // It is very very common for code to contain many case statements recursively
377 // nested, as in (but usually without indentation):
378 // case 1:
379 // case 2:
380 // case 3:
381 // case 4:
382 // case 5: etc.
383 //
384 // Parsing this naively works, but is both inefficient and can cause us to run
385 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000386 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000387 // but all the grossness is constrained to ParseCaseStatement (and some
388 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattner34a22092009-03-04 04:23:07 +0000390 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
391 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000392 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000393
Chris Lattner34a22092009-03-04 04:23:07 +0000394 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
395 // gets updated each time a new case is parsed, and whose body is unset so
396 // far. When parsing 'case 4', this is the 'case 3' node.
397 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattner34a22092009-03-04 04:23:07 +0000399 // While we have case statements, eat and stack them.
400 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000401 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
402 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000403
Douglas Gregord328d572009-09-21 18:10:23 +0000404 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000405 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000406 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000407 }
408
Chris Lattner125c0ee2009-12-10 00:38:54 +0000409 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
410 /// Disable this form of error recovery while we're parsing the case
411 /// expression.
412 ColonProtectionRAIIObject ColonProtection(*this);
413
Richard Trieu2c850c02011-04-21 21:44:26 +0000414 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
415 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000416 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000417 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000418 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000419 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000420
Chris Lattner34a22092009-03-04 04:23:07 +0000421 // GNU case range extension.
422 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000423 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000424 if (Tok.is(tok::ellipsis)) {
425 Diag(Tok, diag::ext_gnu_case_range);
426 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000427
Chris Lattner34a22092009-03-04 04:23:07 +0000428 RHS = ParseConstantExpression();
429 if (RHS.isInvalid()) {
430 SkipUntil(tok::colon);
431 return StmtError();
432 }
433 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000434
435 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000436
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000437 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000438 if (Tok.is(tok::colon)) {
439 ColonLoc = ConsumeToken();
440
441 // Treat "case blah;" as a typo for "case blah:".
442 } else if (Tok.is(tok::semi)) {
443 ColonLoc = ConsumeToken();
444 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
445 << FixItHint::CreateReplacement(ColonLoc, ":");
446 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000447 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
448 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
449 << FixItHint::CreateInsertion(ExpectedLoc, ":");
450 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000451 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000452
John McCalldadc5752010-08-24 06:29:42 +0000453 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000454 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
455 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattner34a22092009-03-04 04:23:07 +0000457 // If we had a sema error parsing this case, then just ignore it and
458 // continue parsing the sub-stmt.
459 if (Case.isInvalid()) {
460 if (TopLevelCase.isInvalid()) // No parsed case stmts.
461 return ParseStatement();
462 // Otherwise, just don't add it as a nested case.
463 } else {
464 // If this is the first case statement we parsed, it becomes TopLevelCase.
465 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000466 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000467 if (TopLevelCase.isInvalid())
468 TopLevelCase = move(Case);
469 else
John McCallb268a282010-08-23 23:25:46 +0000470 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000471 DeepestParsedCaseStmt = NextDeepest;
472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattner34a22092009-03-04 04:23:07 +0000474 // Handle all case statements.
475 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner34a22092009-03-04 04:23:07 +0000477 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner34a22092009-03-04 04:23:07 +0000479 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000480 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattner34a22092009-03-04 04:23:07 +0000482 if (Tok.isNot(tok::r_brace)) {
483 SubStmt = ParseStatement();
484 } else {
485 // Nicely diagnose the common error "switch (X) { case 4: }", which is
486 // not valid.
487 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000488 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000489 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000490 }
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattner34a22092009-03-04 04:23:07 +0000492 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000493 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000494 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000495
Chris Lattner34a22092009-03-04 04:23:07 +0000496 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000497 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000498
Chris Lattner34a22092009-03-04 04:23:07 +0000499 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000500 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000501}
502
503/// ParseDefaultStatement
504/// labeled-statement:
505/// 'default' ':' statement
506/// Note that this does not parse the 'statement' at the end.
507///
John McCall53fa7142010-12-24 02:08:15 +0000508StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000509 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000510
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000511 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000512 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000513
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000514 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000515 if (Tok.is(tok::colon)) {
516 ColonLoc = ConsumeToken();
517
518 // Treat "default;" as a typo for "default:".
519 } else if (Tok.is(tok::semi)) {
520 ColonLoc = ConsumeToken();
521 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
522 << FixItHint::CreateReplacement(ColonLoc, ":");
523 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000524 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
525 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
526 << FixItHint::CreateInsertion(ExpectedLoc, ":");
527 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000528 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000529
Chris Lattner30f910e2006-10-16 05:52:41 +0000530 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000531 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000532 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000533 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000534 }
535
John McCalldadc5752010-08-24 06:29:42 +0000536 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000537 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000538 return StmtError();
539
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000540 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000541 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000542}
543
544
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000545/// ParseCompoundStatement - Parse a "{}" block.
546///
547/// compound-statement: [C99 6.8.2]
548/// { block-item-list[opt] }
549/// [GNU] { label-declarations block-item-list } [TODO]
550///
551/// block-item-list:
552/// block-item
553/// block-item-list block-item
554///
555/// block-item:
556/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000557/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000558/// statement
559/// [OMP] openmp-directive [TODO]
560///
561/// [GNU] label-declarations:
562/// [GNU] label-declaration
563/// [GNU] label-declarations label-declaration
564///
565/// [GNU] label-declaration:
566/// [GNU] '__label__' identifier-list ';'
567///
568/// [OMP] openmp-directive: [TODO]
569/// [OMP] barrier-directive
570/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000571///
John McCall53fa7142010-12-24 02:08:15 +0000572StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000573 bool isStmtExpr) {
574 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000575
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000576 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000577
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000578 // Enter a scope to hold everything within the compound stmt. Compound
579 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000580 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000581
582 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000583 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000584}
585
586
587/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000588/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000589/// consume the '}' at the end of the block. It does not manipulate the scope
590/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000591StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000592 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000593 Tok.getLocation(),
594 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000595 InMessageExpressionRAIIObject InMessage(*this, false);
596
Chris Lattnerf2978802007-01-21 06:52:16 +0000597 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
598
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000599 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000600
Chris Lattner43e7f312011-02-18 02:08:43 +0000601 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
602 // only allowed at the start of a compound stmt regardless of the language.
603 while (Tok.is(tok::kw___label__)) {
604 SourceLocation LabelLoc = ConsumeToken();
605 Diag(LabelLoc, diag::ext_gnu_local_label);
606
607 llvm::SmallVector<Decl *, 8> DeclsInGroup;
608 while (1) {
609 if (Tok.isNot(tok::identifier)) {
610 Diag(Tok, diag::err_expected_ident);
611 break;
612 }
613
614 IdentifierInfo *II = Tok.getIdentifierInfo();
615 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000616 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner43e7f312011-02-18 02:08:43 +0000617
618 if (!Tok.is(tok::comma))
619 break;
620 ConsumeToken();
621 }
622
John McCall084e83d2011-03-24 11:26:52 +0000623 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000624 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
625 DeclsInGroup.data(), DeclsInGroup.size());
626 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
627
628 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
629 if (R.isUsable())
630 Stmts.push_back(R.release());
631 }
632
633 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000634 if (Tok.is(tok::annot_pragma_unused)) {
635 HandlePragmaUnused();
636 continue;
637 }
638
John McCalldadc5752010-08-24 06:29:42 +0000639 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000640 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000641 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000642 } else {
643 // __extension__ can start declarations and it can also be a unary
644 // operator for expressions. Consume multiple __extension__ markers here
645 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000646 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000647 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000648 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000649 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000650
John McCall084e83d2011-03-24 11:26:52 +0000651 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000652 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000653
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000654 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000655 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000656 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000657 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000658 ExtensionRAIIObject O(Diags);
659
Chris Lattner49836b42009-04-02 04:16:50 +0000660 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000661 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
662 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000663 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000664 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000665 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000666 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000667 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000668
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000669 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000670 SkipUntil(tok::semi);
671 continue;
672 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000673
Alexis Hunt96d5c762009-11-21 08:43:09 +0000674 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000675 // Eat the semicolon at the end of stmt and convert the expr into a
676 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000677 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000678 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000679 }
680 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000681
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000682 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000683 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000684 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000685
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000686 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000687 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000688 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000689 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000690 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000691 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000692
Chris Lattner04132372006-10-16 06:12:55 +0000693 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000694 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000695 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000696}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000697
Chris Lattnerc0081db2008-12-12 06:31:07 +0000698/// ParseParenExprOrCondition:
699/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000700/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000701///
702/// This function parses and performs error recovery on the specified condition
703/// or expression (depending on whether we're in C++ or C mode). This function
704/// goes out of its way to recover well. It returns true if there was a parser
705/// error (the right paren couldn't be found), which indicates that the caller
706/// should try to recover harder. It returns false if the condition is
707/// successfully parsed. Note that a successful parse can still have semantic
708/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000709bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000710 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000711 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000712 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000713 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000714 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000715 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000716 else {
717 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000718 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000719
720 // If required, convert to a boolean value.
721 if (!ExprResult.isInvalid() && ConvertToBoolean)
722 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000723 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000724 }
Mike Stump11289f42009-09-09 15:08:12 +0000725
Chris Lattnerc0081db2008-12-12 06:31:07 +0000726 // If the parser was confused by the condition and we don't have a ')', try to
727 // recover by skipping ahead to a semi and bailing out. If condexp is
728 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000729 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000730 SkipUntil(tok::semi);
731 // Skipping may have stopped if it found the containing ')'. If so, we can
732 // continue parsing the if statement.
733 if (Tok.isNot(tok::r_paren))
734 return true;
735 }
Mike Stump11289f42009-09-09 15:08:12 +0000736
Chris Lattnerc0081db2008-12-12 06:31:07 +0000737 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000738 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000739 return false;
740}
741
742
Chris Lattnerc951dae2006-08-10 04:23:57 +0000743/// ParseIfStatement
744/// if-statement: [C99 6.8.4.1]
745/// 'if' '(' expression ')' statement
746/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000747/// [C++] 'if' '(' condition ')' statement
748/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000749///
John McCall53fa7142010-12-24 02:08:15 +0000750StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000751 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000752
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000753 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000754 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000755
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000756 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000757 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000758 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000759 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000760 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000761
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000762 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
763
Chris Lattner2dd1b722007-08-26 23:08:06 +0000764 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
765 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000766 //
767 // C++ 6.4p3:
768 // A name introduced by a declaration in a condition is in scope from its
769 // point of declaration until the end of the substatements controlled by the
770 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000771 // C++ 3.3.2p4:
772 // Names declared in the for-init-statement, and in the condition of if,
773 // while, for, and switch statements are local to the if, while, for, or
774 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000775 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000776 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000777
Chris Lattnerc951dae2006-08-10 04:23:57 +0000778 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000779 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000780 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000781 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000782 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000783
John McCallb268a282010-08-23 23:25:46 +0000784 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000785
Chris Lattner8fb26252007-08-22 05:28:50 +0000786 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000787 // there is no compound stmt. C90 does not have this clause. We only do this
788 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000789 //
790 // C++ 6.4p1:
791 // The substatement in a selection-statement (each substatement, in the else
792 // form of the if statement) implicitly defines a local scope.
793 //
794 // For C++ we create a scope for the condition and a new scope for
795 // substatements because:
796 // -When the 'then' scope exits, we want the condition declaration to still be
797 // active for the 'else' scope too.
798 // -Sema will detect name clashes by considering declarations of a
799 // 'ControlScope' as part of its direct subscope.
800 // -If we wanted the condition and substatement to be in the same scope, we
801 // would have to notify ParseStatement not to create a new scope. It's
802 // simpler to let it create a new scope.
803 //
Mike Stump11289f42009-09-09 15:08:12 +0000804 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000805 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000806
Chris Lattner5c5808a2007-10-29 05:08:52 +0000807 // Read the 'then' stmt.
808 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000809 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000810
Chris Lattner37e54f42007-08-22 05:16:28 +0000811 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000812 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000813
Chris Lattnerc951dae2006-08-10 04:23:57 +0000814 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000815 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000816 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000817 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000818
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000819 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000820 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000821 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000822
Chris Lattner8fb26252007-08-22 05:28:50 +0000823 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000824 // there is no compound stmt. C90 does not have this clause. We only do
825 // this if the body isn't a compound statement to avoid push/pop in common
826 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000827 //
828 // C++ 6.4p1:
829 // The substatement in a selection-statement (each substatement, in the else
830 // form of the if statement) implicitly defines a local scope.
831 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000832 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000833 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000834
Chris Lattner30f910e2006-10-16 05:52:41 +0000835 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000836
Chris Lattner37e54f42007-08-22 05:16:28 +0000837 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000838 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000839 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000840
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000841 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000843 // If the condition was invalid, discard the if statement. We could recover
844 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000845 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000846 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000847
Chris Lattner5c5808a2007-10-29 05:08:52 +0000848 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000849 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000850 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000851 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
852 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
853 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000854 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000855 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000856 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000857
Chris Lattner5c5808a2007-10-29 05:08:52 +0000858 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000859 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000860 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000861 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000862 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000863
John McCallb268a282010-08-23 23:25:46 +0000864 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000865 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000866}
867
Chris Lattner9075bd72006-08-10 04:59:57 +0000868/// ParseSwitchStatement
869/// switch-statement:
870/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000871/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000872StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000873 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000874
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000875 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000876 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000877
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000878 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000879 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000880 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000881 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000882 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000883
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000884 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
885
Chris Lattner2dd1b722007-08-26 23:08:06 +0000886 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
887 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000888 //
889 // C++ 6.4p3:
890 // A name introduced by a declaration in a condition is in scope from its
891 // point of declaration until the end of the substatements controlled by the
892 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000893 // C++ 3.3.2p4:
894 // Names declared in the for-init-statement, and in the condition of if,
895 // while, for, and switch statements are local to the if, while, for, or
896 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000897 //
Richard Trieu2c850c02011-04-21 21:44:26 +0000898 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +0000899 if (C99orCXX)
900 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000901 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000902
Chris Lattner9075bd72006-08-10 04:59:57 +0000903 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000904 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000905 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000906 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000907 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000908
John McCalldadc5752010-08-24 06:29:42 +0000909 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000910 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000911
Douglas Gregore60e41a2010-05-06 17:25:47 +0000912 if (Switch.isInvalid()) {
913 // Skip the switch body.
914 // FIXME: This is not optimal recovery, but parsing the body is more
915 // dangerous due to the presence of case and default statements, which
916 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000917 if (Tok.is(tok::l_brace)) {
918 ConsumeBrace();
919 SkipUntil(tok::r_brace, false, false);
920 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000921 SkipUntil(tok::semi);
922 return move(Switch);
923 }
924
Chris Lattner8fb26252007-08-22 05:28:50 +0000925 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000926 // there is no compound stmt. C90 does not have this clause. We only do this
927 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000928 //
929 // C++ 6.4p1:
930 // The substatement in a selection-statement (each substatement, in the else
931 // form of the if statement) implicitly defines a local scope.
932 //
933 // See comments in ParseIfStatement for why we create a scope for the
934 // condition and a new scope for substatement in C++.
935 //
Mike Stump11289f42009-09-09 15:08:12 +0000936 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000937 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000938
Chris Lattner9075bd72006-08-10 04:59:57 +0000939 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000940 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000941
Chris Lattner8fd2d012010-01-24 01:50:29 +0000942 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000943 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000944 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000945
Chris Lattner8fd2d012010-01-24 01:50:29 +0000946 if (Body.isInvalid())
947 // FIXME: Remove the case statement list from the Switch statement.
948 Body = Actions.ActOnNullStmt(Tok.getLocation());
949
John McCallb268a282010-08-23 23:25:46 +0000950 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000951}
952
953/// ParseWhileStatement
954/// while-statement: [C99 6.8.5.1]
955/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000956/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000957StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000958 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000959
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000960 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000961 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000962 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000963
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000964 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000965 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000966 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000967 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000968 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000969
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000970 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
971
Chris Lattner2dd1b722007-08-26 23:08:06 +0000972 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
973 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000974 //
975 // C++ 6.4p3:
976 // A name introduced by a declaration in a condition is in scope from its
977 // point of declaration until the end of the substatements controlled by the
978 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000979 // C++ 3.3.2p4:
980 // Names declared in the for-init-statement, and in the condition of if,
981 // while, for, and switch statements are local to the if, while, for, or
982 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000983 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000984 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000985 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000986 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
987 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000988 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000989 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
990 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000991
Chris Lattner9075bd72006-08-10 04:59:57 +0000992 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000993 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000994 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000995 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000996 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000997
John McCallb268a282010-08-23 23:25:46 +0000998 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000999
Chris Lattner8fb26252007-08-22 05:28:50 +00001000 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001001 // there is no compound stmt. C90 does not have this clause. We only do this
1002 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001003 //
1004 // C++ 6.5p2:
1005 // The substatement in an iteration-statement implicitly defines a local scope
1006 // which is entered and exited each time through the loop.
1007 //
1008 // See comments in ParseIfStatement for why we create a scope for the
1009 // condition and a new scope for substatement in C++.
1010 //
Mike Stump11289f42009-09-09 15:08:12 +00001011 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001012 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001013
Chris Lattner9075bd72006-08-10 04:59:57 +00001014 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001015 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001016
Chris Lattner8fb26252007-08-22 05:28:50 +00001017 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001018 InnerScope.Exit();
1019 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001020
John McCall48871652010-08-21 09:40:31 +00001021 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001022 return StmtError();
1023
John McCallb268a282010-08-23 23:25:46 +00001024 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001025}
1026
1027/// ParseDoStatement
1028/// do-statement: [C99 6.8.5.2]
1029/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001030/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001031StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001032 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001033
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001034 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001035 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001036
Chris Lattner2dd1b722007-08-26 23:08:06 +00001037 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1038 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001039 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001040 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001041 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001042 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001043 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001044
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001045 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001046
Chris Lattner8fb26252007-08-22 05:28:50 +00001047 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001048 // there is no compound stmt. C90 does not have this clause. We only do this
1049 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001050 //
1051 // C++ 6.5p2:
1052 // The substatement in an iteration-statement implicitly defines a local scope
1053 // which is entered and exited each time through the loop.
1054 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001055 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001056 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001057 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001058
Chris Lattner9075bd72006-08-10 04:59:57 +00001059 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001060 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001061
Chris Lattner8fb26252007-08-22 05:28:50 +00001062 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001063 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001064
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001065 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001066 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001067 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001068 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001069 SkipUntil(tok::semi, false, true);
1070 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001071 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001072 }
Chris Lattneraf635312006-10-16 06:06:51 +00001073 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001074
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001075 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001076 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001077 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001078 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001079 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001080
Chris Lattner10da53c2008-12-12 06:35:28 +00001081 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +00001082 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001083 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +00001084 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001085 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001086
Sebastian Redlb62406f2008-12-11 19:48:14 +00001087 if (Cond.isInvalid() || Body.isInvalid())
1088 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001089
John McCallb268a282010-08-23 23:25:46 +00001090 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1091 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +00001092}
1093
1094/// ParseForStatement
1095/// for-statement: [C99 6.8.5.3]
1096/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1097/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001098/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1099/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001100/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001101/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1102/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001103///
1104/// [C++] for-init-statement:
1105/// [C++] expression-statement
1106/// [C++] simple-declaration
1107///
Richard Smith02e85f32011-04-14 22:09:26 +00001108/// [C++0x] for-range-declaration:
1109/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1110/// [C++0x] for-range-initializer:
1111/// [C++0x] expression
1112/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001113StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001114 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001115
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001116 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001117 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001118
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001119 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001120 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001121 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001122 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001123 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001124
Chris Lattner934074c2009-04-22 00:54:41 +00001125 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001126
Chris Lattner2dd1b722007-08-26 23:08:06 +00001127 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1128 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001129 //
1130 // C++ 6.4p3:
1131 // A name introduced by a declaration in a condition is in scope from its
1132 // point of declaration until the end of the substatements controlled by the
1133 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001134 // C++ 3.3.2p4:
1135 // Names declared in the for-init-statement, and in the condition of if,
1136 // while, for, and switch statements are local to the if, while, for, or
1137 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001138 // C++ 6.5.3p1:
1139 // Names declared in the for-init-statement are in the same declarative-region
1140 // as those declared in the condition.
1141 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001142 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001143 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001144 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1145 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001146 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001147 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1148
1149 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001150
Chris Lattner04132372006-10-16 06:12:55 +00001151 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001152 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001153
Richard Smith02e85f32011-04-14 22:09:26 +00001154 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001155 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001156 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001157 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001158 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001159 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001160 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001161 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001162
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001163 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001164 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001165 C99orCXXorObjC? Sema::PCC_ForInit
1166 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001167 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001168 }
1169
Chris Lattner9075bd72006-08-10 04:59:57 +00001170 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001171 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001172 // no first part, eat the ';'.
1173 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001174 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001175 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001176 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001177 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001178
John McCall084e83d2011-03-24 11:26:52 +00001179 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001180 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001181
Richard Smith02e85f32011-04-14 22:09:26 +00001182 // In C++0x, "for (T NS:a" might not be a typo for ::
1183 bool MightBeForRangeStmt = getLang().CPlusPlus;
1184 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1185
Chris Lattner49836b42009-04-02 04:16:50 +00001186 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001187 StmtVector Stmts(Actions);
1188 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001189 DeclEnd, attrs, false,
1190 MightBeForRangeStmt ?
1191 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001192 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001193
Richard Smith02e85f32011-04-14 22:09:26 +00001194 if (ForRangeInit.ParsedForRangeDecl()) {
1195 ForRange = true;
1196 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001197 ConsumeToken();
1198 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001199 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001200 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001201 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001202
1203 if (Tok.is(tok::code_completion)) {
1204 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1205 ConsumeCodeCompletionToken();
1206 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001207 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001208 } else {
1209 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001210 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001211 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001212 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001213
John McCall34376a62010-12-04 03:47:34 +00001214 ForEach = isTokIdentifier_in();
1215
Chris Lattnercd68f642007-06-27 01:06:29 +00001216 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001217 if (!Value.isInvalid()) {
1218 if (ForEach)
1219 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1220 else
1221 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1222 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001223
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001224 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001225 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001226 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001227 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001228
1229 if (Tok.is(tok::code_completion)) {
1230 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1231 ConsumeCodeCompletionToken();
1232 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001233 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001234 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001235 if (!Value.isInvalid()) {
1236 Diag(Tok, diag::err_expected_semi_for);
1237 } else {
1238 // Skip until semicolon or rparen, don't consume it.
1239 SkipUntil(tok::r_paren, true, true);
1240 if (Tok.is(tok::semi))
1241 ConsumeToken();
1242 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001243 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001244 }
Richard Smith02e85f32011-04-14 22:09:26 +00001245 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001246 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001247 // Parse the second part of the for specifier.
1248 if (Tok.is(tok::semi)) { // for (...;;
1249 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001250 } else if (Tok.is(tok::r_paren)) {
1251 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001252 } else {
John McCalldadc5752010-08-24 06:29:42 +00001253 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001254 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001255 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1256 else {
1257 Second = ParseExpression();
1258 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001259 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001260 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001261 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001262 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001263 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001264 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001265
Douglas Gregor230a7e62011-02-17 03:38:46 +00001266 if (Tok.isNot(tok::semi)) {
1267 if (!SecondPartIsInvalid || SecondVar)
1268 Diag(Tok, diag::err_expected_semi_for);
1269 else
1270 // Skip until semicolon or rparen, don't consume it.
1271 SkipUntil(tok::r_paren, true, true);
1272 }
1273
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001274 if (Tok.is(tok::semi)) {
1275 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001276 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001277
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001278 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001279 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001280 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001281 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001282 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001283 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001284 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001285 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001286
Richard Smith02e85f32011-04-14 22:09:26 +00001287 // We need to perform most of the semantic analysis for a C++0x for-range
1288 // statememt before parsing the body, in order to be able to deduce the type
1289 // of an auto-typed loop variable.
1290 StmtResult ForRangeStmt;
1291 if (ForRange)
1292 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1293 FirstPart.take(),
1294 ForRangeInit.ColonLoc,
1295 ForRangeInit.RangeExpr.get(),
1296 RParenLoc);
1297
Chris Lattner8fb26252007-08-22 05:28:50 +00001298 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001299 // there is no compound stmt. C90 does not have this clause. We only do this
1300 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001301 //
1302 // C++ 6.5p2:
1303 // The substatement in an iteration-statement implicitly defines a local scope
1304 // which is entered and exited each time through the loop.
1305 //
1306 // See comments in ParseIfStatement for why we create a scope for
1307 // for-init-statement/condition and a new scope for substatement in C++.
1308 //
Mike Stump11289f42009-09-09 15:08:12 +00001309 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001310 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001311
Chris Lattner9075bd72006-08-10 04:59:57 +00001312 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001313 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001314
Chris Lattner8fb26252007-08-22 05:28:50 +00001315 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001316 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001317
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001318 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001319 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001320
1321 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001322 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001323
Richard Smith02e85f32011-04-14 22:09:26 +00001324 if (ForEach)
1325 // FIXME: It isn't clear how to communicate the late destruction of
1326 // C++ temporaries used to create the collection.
1327 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1328 FirstPart.take(),
1329 Collection.take(), RParenLoc,
1330 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001331
Richard Smith02e85f32011-04-14 22:09:26 +00001332 if (ForRange)
1333 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1334
1335 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1336 SecondVar, ThirdPart, RParenLoc, Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001337}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001338
Chris Lattner503fadc2006-08-10 05:45:44 +00001339/// ParseGotoStatement
1340/// jump-statement:
1341/// 'goto' identifier ';'
1342/// [GNU] 'goto' '*' expression ';'
1343///
1344/// Note: this lets the caller parse the end ';'.
1345///
John McCall53fa7142010-12-24 02:08:15 +00001346StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001347 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001348
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001349 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001350 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001351
John McCalldadc5752010-08-24 06:29:42 +00001352 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001353 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001354 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1355 Tok.getLocation());
1356 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001357 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001358 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001359 // GNU indirect goto extension.
1360 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001361 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001362 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001363 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001364 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001365 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001366 }
John McCallb268a282010-08-23 23:25:46 +00001367 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001368 } else {
1369 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001370 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001371 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001372
Sebastian Redlb62406f2008-12-11 19:48:14 +00001373 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001374}
1375
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001376/// ParseContinueStatement
1377/// jump-statement:
1378/// 'continue' ';'
1379///
1380/// Note: this lets the caller parse the end ';'.
1381///
John McCall53fa7142010-12-24 02:08:15 +00001382StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001383 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001384
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001385 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001386 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001387}
1388
1389/// ParseBreakStatement
1390/// jump-statement:
1391/// 'break' ';'
1392///
1393/// Note: this lets the caller parse the end ';'.
1394///
John McCall53fa7142010-12-24 02:08:15 +00001395StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001396 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001397
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001398 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001399 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001400}
1401
Chris Lattner503fadc2006-08-10 05:45:44 +00001402/// ParseReturnStatement
1403/// jump-statement:
1404/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001405StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001406 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001407
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001408 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001409 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001410
John McCalldadc5752010-08-24 06:29:42 +00001411 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001412 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001413 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001414 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001415 ConsumeCodeCompletionToken();
1416 SkipUntil(tok::semi, false, true);
1417 return StmtError();
1418 }
1419
Douglas Gregore9e27d92011-03-11 23:10:44 +00001420 // FIXME: This is a hack to allow something like C++0x's generalized
1421 // initializer lists, but only enough of this feature to allow Clang to
1422 // parse libstdc++ 4.5's headers.
1423 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1424 R = ParseInitializer();
1425 if (R.isUsable() && !getLang().CPlusPlus0x)
1426 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1427 << R.get()->getSourceRange();
1428 } else
1429 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001430 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001431 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001432 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001433 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001434 }
John McCallb268a282010-08-23 23:25:46 +00001435 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001436}
Chris Lattner0116c472006-08-15 06:03:28 +00001437
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001438/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1439/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001440StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1441 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001442 if (Tok.is(tok::l_brace)) {
1443 unsigned short savedBraceCount = BraceCount;
1444 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001445 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001446 ConsumeAnyToken();
1447 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001448 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001449 // From the MS website: If used without braces, the __asm keyword means
1450 // that the rest of the line is an assembly-language statement.
1451 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001452 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001453 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001454 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001455 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001456 ConsumeAnyToken();
1457 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001458 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1459 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001460 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001461 }
Mike Stump6da5d752009-12-11 00:04:56 +00001462 Token t;
1463 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001464 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001465 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001466 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001467 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001468 ExprVector Constraints(Actions);
1469 ExprVector Exprs(Actions);
1470 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001471 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001472 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001473 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001474 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001475}
1476
Chris Lattner0116c472006-08-15 06:03:28 +00001477/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001478/// asm-statement:
1479/// gnu-asm-statement
1480/// ms-asm-statement
1481///
1482/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001483/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1484///
1485/// [GNU] asm-argument:
1486/// asm-string-literal
1487/// asm-string-literal ':' asm-operands[opt]
1488/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1489/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1490/// ':' asm-clobbers
1491///
1492/// [GNU] asm-clobbers:
1493/// asm-string-literal
1494/// asm-clobbers ',' asm-string-literal
1495///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001496/// [MS] ms-asm-statement:
1497/// '__asm' assembly-instruction ';'[opt]
1498/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1499///
1500/// [MS] assembly-instruction-list:
1501/// assembly-instruction ';'[opt]
1502/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1503///
John McCalldadc5752010-08-24 06:29:42 +00001504StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001505 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001506 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001507
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001508 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001509 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001510 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001511 }
John McCall084e83d2011-03-24 11:26:52 +00001512 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001513 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001514 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001515
Chris Lattner0116c472006-08-15 06:03:28 +00001516 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001517 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001518 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001519 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001520 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001521
Chris Lattner0116c472006-08-15 06:03:28 +00001522 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001523 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001524 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001525 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001526 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001527 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001528 }
Chris Lattner04132372006-10-16 06:12:55 +00001529 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001530
John McCalldadc5752010-08-24 06:29:42 +00001531 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001532 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001533 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001534
Anders Carlsson9a020f92010-01-30 22:25:16 +00001535 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001536 ExprVector Constraints(Actions);
1537 ExprVector Exprs(Actions);
1538 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001539
Anders Carlsson19fe1162008-02-05 23:03:50 +00001540 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001541 // We have a simple asm expression like 'asm("foo")'.
1542 SourceLocation RParenLoc = ConsumeParen();
1543 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1544 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1545 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001546 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001547 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001548 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001549
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001550 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001551 bool AteExtraColon = false;
1552 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1553 // In C++ mode, parse "::" like ": :".
1554 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001555 ConsumeToken();
1556
Chris Lattner15768502009-12-20 23:08:04 +00001557 if (!AteExtraColon &&
1558 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001559 return StmtError();
1560 }
Chris Lattner15768502009-12-20 23:08:04 +00001561
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001562 unsigned NumOutputs = Names.size();
1563
1564 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001565 if (AteExtraColon ||
1566 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1567 // In C++ mode, parse "::" like ": :".
1568 if (AteExtraColon)
1569 AteExtraColon = false;
1570 else {
1571 AteExtraColon = Tok.is(tok::coloncolon);
1572 ConsumeToken();
1573 }
1574
1575 if (!AteExtraColon &&
1576 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001577 return StmtError();
1578 }
1579
1580 assert(Names.size() == Constraints.size() &&
1581 Constraints.size() == Exprs.size() &&
1582 "Input operand size mismatch!");
1583
1584 unsigned NumInputs = Names.size() - NumOutputs;
1585
1586 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001587 if (AteExtraColon || Tok.is(tok::colon)) {
1588 if (!AteExtraColon)
1589 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001590
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001591 // Parse the asm-string list for clobbers if present.
1592 if (Tok.isNot(tok::r_paren)) {
1593 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001594 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001595
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001596 if (Clobber.isInvalid())
1597 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001598
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001599 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001600
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001601 if (Tok.isNot(tok::comma)) break;
1602 ConsumeToken();
1603 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001604 }
1605 }
1606
1607 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1608 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001609 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001610 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001611 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001612 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001613}
1614
1615/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001616/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001617///
1618/// [GNU] asm-operands:
1619/// asm-operand
1620/// asm-operands ',' asm-operand
1621///
1622/// [GNU] asm-operand:
1623/// asm-string-literal '(' expression ')'
1624/// '[' identifier ']' asm-string-literal '(' expression ')'
1625///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001626//
1627// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001628bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1629 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1630 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001631 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001632 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001633 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001634
1635 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001636 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001637 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001638 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001639
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001640 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001641 Diag(Tok, diag::err_expected_ident);
1642 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001643 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001644 }
Mike Stump11289f42009-09-09 15:08:12 +00001645
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001646 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001647 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001648
Anders Carlsson9a020f92010-01-30 22:25:16 +00001649 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001650 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001651 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001652 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001653
John McCalldadc5752010-08-24 06:29:42 +00001654 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001655 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001656 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001657 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001658 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001659 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001660
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001661 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001662 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001663 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001664 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001665 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001666
Chris Lattner0116c472006-08-15 06:03:28 +00001667 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001668 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001669 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001670 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001671 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001672 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001673 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001674 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001675 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001676 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001677 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001678 ConsumeToken();
1679 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001680
1681 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001682}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001683
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001684Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001685 assert(Tok.is(tok::l_brace));
1686 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001687
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001688 if (PP.isCodeCompletionEnabled()) {
1689 if (trySkippingFunctionBodyForCodeCompletion()) {
1690 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001691 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001692 }
1693 }
1694
John McCallfaf5fb42010-08-26 23:41:50 +00001695 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1696 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001697
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001698 // Do not enter a scope for the brace, as the arguments are in the same scope
1699 // (the function body) as the body itself. Instead, just read the statement
1700 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001701 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001702
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001703 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001704 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001705 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001706 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001707
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001708 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001709 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001710}
Sebastian Redlb219c902008-12-21 16:41:36 +00001711
Sebastian Redla7b98a72009-04-26 20:35:05 +00001712/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1713///
1714/// function-try-block:
1715/// 'try' ctor-initializer[opt] compound-statement handler-seq
1716///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001717Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001718 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1719 SourceLocation TryLoc = ConsumeToken();
1720
John McCallfaf5fb42010-08-26 23:41:50 +00001721 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1722 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001723
1724 // Constructor initializer list?
1725 if (Tok.is(tok::colon))
1726 ParseConstructorInitializer(Decl);
1727
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001728 if (PP.isCodeCompletionEnabled()) {
1729 if (trySkippingFunctionBodyForCodeCompletion()) {
1730 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001731 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001732 }
1733 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001734
Sebastian Redld98ecd62009-04-26 21:08:36 +00001735 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001736 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001737 // If we failed to parse the try-catch, we just give the function an empty
1738 // compound statement as the body.
1739 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001740 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001741 MultiStmtArg(Actions), false);
1742
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001743 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001744 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001745}
1746
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001747bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001748 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001749 assert(PP.isCodeCompletionEnabled() &&
1750 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001751
1752 // We're in code-completion mode. Skip parsing for all function bodies unless
1753 // the body contains the code-completion point.
1754 TentativeParsingAction PA(*this);
1755 ConsumeBrace();
1756 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1757 /*StopAtCodeCompletion=*/true)) {
1758 PA.Commit();
1759 return true;
1760 }
1761
1762 PA.Revert();
1763 return false;
1764}
1765
Sebastian Redlb219c902008-12-21 16:41:36 +00001766/// ParseCXXTryBlock - Parse a C++ try-block.
1767///
1768/// try-block:
1769/// 'try' compound-statement handler-seq
1770///
John McCall53fa7142010-12-24 02:08:15 +00001771StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001772 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001773
Sebastian Redlb219c902008-12-21 16:41:36 +00001774 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1775
1776 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001777 return ParseCXXTryBlockCommon(TryLoc);
1778}
1779
1780/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1781/// function-try-block.
1782///
1783/// try-block:
1784/// 'try' compound-statement handler-seq
1785///
1786/// function-try-block:
1787/// 'try' ctor-initializer[opt] compound-statement handler-seq
1788///
1789/// handler-seq:
1790/// handler handler-seq[opt]
1791///
John McCalldadc5752010-08-24 06:29:42 +00001792StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001793 if (Tok.isNot(tok::l_brace))
1794 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001795 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001796 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001797 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001798 if (TryBlock.isInvalid())
1799 return move(TryBlock);
1800
1801 StmtVector Handlers(Actions);
John McCall53fa7142010-12-24 02:08:15 +00001802 MaybeParseCXX0XAttributes(attrs);
1803 ProhibitAttributes(attrs);
1804
Sebastian Redlb219c902008-12-21 16:41:36 +00001805 if (Tok.isNot(tok::kw_catch))
1806 return StmtError(Diag(Tok, diag::err_expected_catch));
1807 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001808 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001809 if (!Handler.isInvalid())
1810 Handlers.push_back(Handler.release());
1811 }
1812 // Don't bother creating the full statement if we don't have any usable
1813 // handlers.
1814 if (Handlers.empty())
1815 return StmtError();
1816
John McCallb268a282010-08-23 23:25:46 +00001817 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001818}
1819
1820/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1821///
1822/// handler:
1823/// 'catch' '(' exception-declaration ')' compound-statement
1824///
1825/// exception-declaration:
1826/// type-specifier-seq declarator
1827/// type-specifier-seq abstract-declarator
1828/// type-specifier-seq
1829/// '...'
1830///
John McCalldadc5752010-08-24 06:29:42 +00001831StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001832 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1833
1834 SourceLocation CatchLoc = ConsumeToken();
1835
1836 SourceLocation LParenLoc = Tok.getLocation();
1837 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1838 return StmtError();
1839
1840 // C++ 3.3.2p3:
1841 // The name in a catch exception-declaration is local to the handler and
1842 // shall not be redeclared in the outermost block of the handler.
1843 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1844
1845 // exception-declaration is equivalent to '...' or a parameter-declaration
1846 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001847 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001848 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001849 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00001850 if (ParseCXXTypeSpecifierSeq(DS))
1851 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001852 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1853 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001854 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001855 } else
1856 ConsumeToken();
1857
1858 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1859 return StmtError();
1860
1861 if (Tok.isNot(tok::l_brace))
1862 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1863
Alexis Hunt96d5c762009-11-21 08:43:09 +00001864 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001865 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001866 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001867 if (Block.isInvalid())
1868 return move(Block);
1869
John McCallb268a282010-08-23 23:25:46 +00001870 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001871}