blob: 07cef5597b8e7b1fbeb97985d70b0ca42dc43e15 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000046/// [GNU] asm-statement
47/// [OMP] openmp-construct [TODO]
48///
49/// labeled-statement:
50/// identifier ':' statement
51/// 'case' constant-expression ':' statement
52/// 'default' ':' statement
53///
54/// selection-statement:
55/// if-statement
56/// switch-statement
57///
58/// iteration-statement:
59/// while-statement
60/// do-statement
61/// for-statement
62///
63/// expression-statement:
64/// expression[opt] ';'
65///
66/// jump-statement:
67/// 'goto' identifier ';'
68/// 'continue' ';'
69/// 'break' ';'
70/// 'return' expression[opt] ';'
71/// [GNU] 'goto' '*' expression ';'
72///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000075/// [OBC] '@' 'throw' ';'
76///
John McCall60d7b3a2010-08-24 06:29:42 +000077StmtResult
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +000078Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000079 const char *SemiError = 0;
John McCall60d7b3a2010-08-24 06:29:42 +000080 StmtResult Res;
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000081
82 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000083
John McCall0b7e6782011-03-24 11:26:52 +000084 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000085 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 // Cases in this switch statement should fall through if the parser expects
88 // the token to end in a semicolon (in which case SemiError should be set),
89 // or they directly 'return;' if not.
Douglas Gregor312eadb2011-04-24 05:37:28 +000090Retry:
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 tok::TokenKind Kind = Tok.getKind();
92 SourceLocation AtLoc;
93 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000094 case tok::at: // May be a @try or @throw statement
95 {
96 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000097 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000098 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000099
Douglas Gregor791215b2009-09-21 20:51:25 +0000100 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000101 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorc8bddde2010-05-28 00:22:41 +0000102 ConsumeCodeCompletionToken();
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000103 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor791215b2009-09-21 20:51:25 +0000104
Douglas Gregor312eadb2011-04-24 05:37:28 +0000105 case tok::identifier: {
106 Token Next = NextToken();
107 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000108 // identifier ':' statement
John McCall7f040a92010-12-24 02:08:15 +0000109 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000110 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000111
Douglas Gregor3b887352011-04-27 04:48:22 +0000112 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000113 CXXScopeSpec SS;
114 IdentifierInfo *Name = Tok.getIdentifierInfo();
115 SourceLocation NameLoc = Tok.getLocation();
116 Sema::NameClassification Classification
117 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
118 switch (Classification.getKind()) {
119 case Sema::NC_Keyword:
120 // The identifier was corrected to a keyword. Update the token
121 // to this keyword, and try again.
122 if (Name->getTokenID() != tok::identifier) {
123 Tok.setIdentifierInfo(Name);
124 Tok.setKind(Name->getTokenID());
125 goto Retry;
126 }
127
128 // Fall through via the normal error path.
129 // FIXME: This seems like it could only happen for context-sensitive
130 // keywords.
131
132 case Sema::NC_Error:
133 // Handle errors here by skipping up to the next semicolon or '}', and
134 // eat the semicolon if that's what stopped us.
135 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
136 if (Tok.is(tok::semi))
137 ConsumeToken();
138 return StmtError();
139
140 case Sema::NC_Unknown:
141 // Either we don't know anything about this identifier, or we know that
142 // we're in a syntactic context we haven't handled yet.
143 break;
144
Douglas Gregord9d75e52011-04-27 05:41:15 +0000145 case Sema::NC_Type:
Douglas Gregor3b887352011-04-27 04:48:22 +0000146 Tok.setKind(tok::annot_typename);
147 setTypeAnnotation(Tok, Classification.getType());
148 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor3b887352011-04-27 04:48:22 +0000149 PP.AnnotateCachedTokens(Tok);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000150 break;
151
152 case Sema::NC_Expression:
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000153 Tok.setKind(tok::annot_primary_expr);
154 setExprAnnotation(Tok, Classification.getExpression());
155 Tok.setAnnotationEndLoc(NameLoc);
156 PP.AnnotateCachedTokens(Tok);
157 break;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000158
159 case Sema::NC_TypeTemplate:
160 case Sema::NC_FunctionTemplate: {
161 ConsumeToken(); // the identifier
162 UnqualifiedId Id;
163 Id.setIdentifier(Name, NameLoc);
164 if (AnnotateTemplateIdToken(
165 TemplateTy::make(Classification.getTemplateName()),
166 Classification.getTemplateNameKind(),
Douglas Gregor3b887352011-04-27 04:48:22 +0000167 SS, Id, SourceLocation(),
168 /*AllowTypeAnnotation=*/false)) {
169 // Handle errors here by skipping up to the next semicolon or '}', and
170 // eat the semicolon if that's what stopped us.
171 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
172 if (Tok.is(tok::semi))
173 ConsumeToken();
174 return StmtError();
175 }
176
177 // If the next token is '::', jump right into parsing a
178 // nested-name-specifier. We don't want to leave the template-id
179 // hanging.
180 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor312eadb2011-04-24 05:37:28 +0000181 // Handle errors here by skipping up to the next semicolon or '}', and
182 // eat the semicolon if that's what stopped us.
183 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
184 if (Tok.is(tok::semi))
185 ConsumeToken();
186 return StmtError();
187 }
188
189 // We've annotated a template-id, so try again now.
190 goto Retry;
191 }
192
193 case Sema::NC_NestedNameSpecifier:
194 // FIXME: Implement this!
195 break;
196 }
197 }
198
199 // Fall through
200 }
201
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000202 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000203 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000204 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000205 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall7f040a92010-12-24 02:08:15 +0000206 DeclEnd, attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000207 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000208 }
209
210 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000212 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000215 return ParseExprStatement(attrs);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000216 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000219 return ParseCaseStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000221 return ParseDefaultStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall7f040a92010-12-24 02:08:15 +0000224 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000225 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidisb7d98d32011-04-27 05:04:02 +0000226 SourceLocation LeadingEmptyMacroLoc;
227 if (Tok.hasLeadingEmptyMacro())
228 LeadingEmptyMacroLoc = PP.getLastEmptyMacroInstantiationLoc();
229 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacroLoc);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000230 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall7f040a92010-12-24 02:08:15 +0000233 return ParseIfStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall7f040a92010-12-24 02:08:15 +0000235 return ParseSwitchStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall7f040a92010-12-24 02:08:15 +0000238 return ParseWhileStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000240 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000241 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 break;
243 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall7f040a92010-12-24 02:08:15 +0000244 return ParseForStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000245
246 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000247 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000248 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 break;
250 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000251 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000252 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 break;
254 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000255 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000256 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 break;
258 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000259 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000260 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000262
Sebastian Redla0fd8652008-12-21 16:41:36 +0000263 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000264 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000265 bool msAsm = false;
266 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000267 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000268 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000269 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 break;
271 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000272
Sebastian Redla0fd8652008-12-21 16:41:36 +0000273 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000274 return ParseCXXTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000275 }
276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000278 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000280 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000281 // If the result was valid, then we do want to diagnose this. Use
282 // ExpectAndConsume to emit the diagnostic, even though we know it won't
283 // succeed.
284 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000285 // Skip until we see a } or ;, but don't eat it.
286 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Sebastian Redl61364dd2008-12-11 19:30:53 +0000289 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000290}
291
Douglas Gregor312eadb2011-04-24 05:37:28 +0000292/// \brief Parse an expression statement.
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000293StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000294 // If a case keyword is missing, this is where it should be inserted.
295 Token OldToken = Tok;
296
297 // FIXME: Use the attributes
298 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000299 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000300 if (Expr.isInvalid()) {
301 // If the expression is invalid, skip ahead to the next semicolon or '}'.
302 // Not doing this opens us up to the possibility of infinite loops if
303 // ParseExpression does not consume any tokens.
304 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
305 if (Tok.is(tok::semi))
306 ConsumeToken();
307 return StmtError();
308 }
309
310 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
311 Actions.CheckCaseExpression(Expr.get())) {
312 // If a constant expression is followed by a colon inside a switch block,
313 // suggest a missing case keyword.
314 Diag(OldToken, diag::err_expected_case_before_expression)
315 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
316
317 // Recover parsing as a case statement.
318 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
319 }
320
321 // Otherwise, eat the semicolon.
322 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
323 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
324
325}
326
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000327/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000328///
329/// labeled-statement:
330/// identifier ':' statement
331/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000332///
John McCall7f040a92010-12-24 02:08:15 +0000333StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000334 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
335 "Not an identifier!");
336
337 Token IdentTok = Tok; // Save the whole token.
338 ConsumeToken(); // eat the identifier.
339
340 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000341
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000342 // identifier ':' statement
343 SourceLocation ColonLoc = ConsumeToken();
344
345 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000346 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000347
John McCall60d7b3a2010-08-24 06:29:42 +0000348 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000349
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000350 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000351 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000352 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner337e5502011-02-18 01:27:55 +0000353
354 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
355 IdentTok.getLocation());
356 if (AttributeList *Attrs = attrs.getList())
357 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
358
359 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
360 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000361}
Reid Spencer5f016e22007-07-11 17:01:13 +0000362
363/// ParseCaseStatement
364/// labeled-statement:
365/// 'case' constant-expression ':' statement
366/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
367///
Richard Trieubb9b80c2011-04-21 21:44:26 +0000368StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
369 ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000370 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000371 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner24e1e702009-03-04 04:23:07 +0000373 // It is very very common for code to contain many case statements recursively
374 // nested, as in (but usually without indentation):
375 // case 1:
376 // case 2:
377 // case 3:
378 // case 4:
379 // case 5: etc.
380 //
381 // Parsing this naively works, but is both inefficient and can cause us to run
382 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000383 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000384 // but all the grossness is constrained to ParseCaseStatement (and some
385 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Chris Lattner24e1e702009-03-04 04:23:07 +0000387 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
388 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000389 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner24e1e702009-03-04 04:23:07 +0000391 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
392 // gets updated each time a new case is parsed, and whose body is unset so
393 // far. When parsing 'case 4', this is the 'case 3' node.
394 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattner24e1e702009-03-04 04:23:07 +0000396 // While we have case statements, eat and stack them.
397 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000398 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
399 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000401 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000402 Actions.CodeCompleteCase(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000403 ConsumeCodeCompletionToken();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000404 }
405
Chris Lattner6fb09c82009-12-10 00:38:54 +0000406 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
407 /// Disable this form of error recovery while we're parsing the case
408 /// expression.
409 ColonProtectionRAIIObject ColonProtection(*this);
410
Richard Trieubb9b80c2011-04-21 21:44:26 +0000411 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
412 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000413 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000415 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000417
Chris Lattner24e1e702009-03-04 04:23:07 +0000418 // GNU case range extension.
419 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000420 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000421 if (Tok.is(tok::ellipsis)) {
422 Diag(Tok, diag::ext_gnu_case_range);
423 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000424
Chris Lattner24e1e702009-03-04 04:23:07 +0000425 RHS = ParseConstantExpression();
426 if (RHS.isInvalid()) {
427 SkipUntil(tok::colon);
428 return StmtError();
429 }
430 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000431
432 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000433
Douglas Gregor662a4822010-12-23 22:56:40 +0000434 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000435 if (Tok.is(tok::colon)) {
436 ColonLoc = ConsumeToken();
437
438 // Treat "case blah;" as a typo for "case blah:".
439 } else if (Tok.is(tok::semi)) {
440 ColonLoc = ConsumeToken();
441 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
442 << FixItHint::CreateReplacement(ColonLoc, ":");
443 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000444 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
445 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
446 << FixItHint::CreateInsertion(ExpectedLoc, ":");
447 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000448 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000449
John McCall60d7b3a2010-08-24 06:29:42 +0000450 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000451 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
452 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner24e1e702009-03-04 04:23:07 +0000454 // If we had a sema error parsing this case, then just ignore it and
455 // continue parsing the sub-stmt.
456 if (Case.isInvalid()) {
457 if (TopLevelCase.isInvalid()) // No parsed case stmts.
458 return ParseStatement();
459 // Otherwise, just don't add it as a nested case.
460 } else {
461 // If this is the first case statement we parsed, it becomes TopLevelCase.
462 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000463 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000464 if (TopLevelCase.isInvalid())
465 TopLevelCase = move(Case);
466 else
John McCall9ae2f072010-08-23 23:25:46 +0000467 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000468 DeepestParsedCaseStmt = NextDeepest;
469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattner24e1e702009-03-04 04:23:07 +0000471 // Handle all case statements.
472 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Chris Lattner24e1e702009-03-04 04:23:07 +0000474 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattner24e1e702009-03-04 04:23:07 +0000476 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000477 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner24e1e702009-03-04 04:23:07 +0000479 if (Tok.isNot(tok::r_brace)) {
480 SubStmt = ParseStatement();
481 } else {
482 // Nicely diagnose the common error "switch (X) { case 4: }", which is
483 // not valid.
484 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000486 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner24e1e702009-03-04 04:23:07 +0000489 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000490 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000491 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner24e1e702009-03-04 04:23:07 +0000493 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000494 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000495
Chris Lattner24e1e702009-03-04 04:23:07 +0000496 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000497 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000498}
499
500/// ParseDefaultStatement
501/// labeled-statement:
502/// 'default' ':' statement
503/// Note that this does not parse the 'statement' at the end.
504///
John McCall7f040a92010-12-24 02:08:15 +0000505StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000506 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000507
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000508 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
510
Douglas Gregor662a4822010-12-23 22:56:40 +0000511 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000512 if (Tok.is(tok::colon)) {
513 ColonLoc = ConsumeToken();
514
515 // Treat "default;" as a typo for "default:".
516 } else if (Tok.is(tok::semi)) {
517 ColonLoc = ConsumeToken();
518 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
519 << FixItHint::CreateReplacement(ColonLoc, ":");
520 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000521 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
522 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
523 << FixItHint::CreateInsertion(ExpectedLoc, ":");
524 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000526
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000528 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000530 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 }
532
John McCall60d7b3a2010-08-24 06:29:42 +0000533 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000534 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000535 return StmtError();
536
Sebastian Redl117054a2008-12-28 16:13:43 +0000537 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000538 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000539}
540
541
542/// ParseCompoundStatement - Parse a "{}" block.
543///
544/// compound-statement: [C99 6.8.2]
545/// { block-item-list[opt] }
546/// [GNU] { label-declarations block-item-list } [TODO]
547///
548/// block-item-list:
549/// block-item
550/// block-item-list block-item
551///
552/// block-item:
553/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000554/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000555/// statement
556/// [OMP] openmp-directive [TODO]
557///
558/// [GNU] label-declarations:
559/// [GNU] label-declaration
560/// [GNU] label-declarations label-declaration
561///
562/// [GNU] label-declaration:
563/// [GNU] '__label__' identifier-list ';'
564///
565/// [OMP] openmp-directive: [TODO]
566/// [OMP] barrier-directive
567/// [OMP] flush-directive
568///
John McCall7f040a92010-12-24 02:08:15 +0000569StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Sean Huntbbd37c62009-11-21 08:43:09 +0000570 bool isStmtExpr) {
571 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000572
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000573 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000574
Chris Lattner31e05722007-08-26 06:24:45 +0000575 // Enter a scope to hold everything within the compound stmt. Compound
576 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000577 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
579 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000580 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000581}
582
583
584/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000585/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000586/// consume the '}' at the end of the block. It does not manipulate the scope
587/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000588StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000589 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000590 Tok.getLocation(),
591 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000592 InMessageExpressionRAIIObject InMessage(*this, false);
593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
595
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000596 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000597
Chris Lattner4ae493c2011-02-18 02:08:43 +0000598 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
599 // only allowed at the start of a compound stmt regardless of the language.
600 while (Tok.is(tok::kw___label__)) {
601 SourceLocation LabelLoc = ConsumeToken();
602 Diag(LabelLoc, diag::ext_gnu_local_label);
603
604 llvm::SmallVector<Decl *, 8> DeclsInGroup;
605 while (1) {
606 if (Tok.isNot(tok::identifier)) {
607 Diag(Tok, diag::err_expected_ident);
608 break;
609 }
610
611 IdentifierInfo *II = Tok.getIdentifierInfo();
612 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000613 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner4ae493c2011-02-18 02:08:43 +0000614
615 if (!Tok.is(tok::comma))
616 break;
617 ConsumeToken();
618 }
619
John McCall0b7e6782011-03-24 11:26:52 +0000620 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000621 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
622 DeclsInGroup.data(), DeclsInGroup.size());
623 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
624
625 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
626 if (R.isUsable())
627 Stmts.push_back(R.release());
628 }
629
630 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000631 if (Tok.is(tok::annot_pragma_unused)) {
632 HandlePragmaUnused();
633 continue;
634 }
635
John McCall60d7b3a2010-08-24 06:29:42 +0000636 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000637 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000638 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000639 } else {
640 // __extension__ can start declarations and it can also be a unary
641 // operator for expressions. Consume multiple __extension__ markers here
642 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000643 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000644 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000645 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000646 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000647
John McCall0b7e6782011-03-24 11:26:52 +0000648 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000649 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000650
Chris Lattner45a566c2007-08-27 01:01:57 +0000651 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000652 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000653 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000654 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000655 ExtensionRAIIObject O(Diags);
656
Chris Lattner97144fc2009-04-02 04:16:50 +0000657 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000658 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
659 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000660 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000661 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000662 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000663 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000664 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000665
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000666 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000667 SkipUntil(tok::semi);
668 continue;
669 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000670
Sean Huntbbd37c62009-11-21 08:43:09 +0000671 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000672 // Eat the semicolon at the end of stmt and convert the expr into a
673 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000674 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000675 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000676 }
677 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000678
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000679 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000680 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000684 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 Diag(Tok, diag::err_expected_rbrace);
Chris Lattnerf65086b2010-09-01 15:49:26 +0000686 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000687 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000691 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000692 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693}
694
Chris Lattner15ff1112008-12-12 06:31:07 +0000695/// ParseParenExprOrCondition:
696/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000697/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000698///
699/// This function parses and performs error recovery on the specified condition
700/// or expression (depending on whether we're in C++ or C mode). This function
701/// goes out of its way to recover well. It returns true if there was a parser
702/// error (the right paren couldn't be found), which indicates that the caller
703/// should try to recover harder. It returns false if the condition is
704/// successfully parsed. Note that a successful parse can still have semantic
705/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000706bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000707 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000708 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000709 bool ConvertToBoolean) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000710 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000711 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000712 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000713 else {
714 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000715 DeclResult = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000716
717 // If required, convert to a boolean value.
718 if (!ExprResult.isInvalid() && ConvertToBoolean)
719 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000720 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Chris Lattner15ff1112008-12-12 06:31:07 +0000723 // If the parser was confused by the condition and we don't have a ')', try to
724 // recover by skipping ahead to a semi and bailing out. If condexp is
725 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000726 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000727 SkipUntil(tok::semi);
728 // Skipping may have stopped if it found the containing ')'. If so, we can
729 // continue parsing the if statement.
730 if (Tok.isNot(tok::r_paren))
731 return true;
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattner15ff1112008-12-12 06:31:07 +0000734 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000735 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000736 return false;
737}
738
739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740/// ParseIfStatement
741/// if-statement: [C99 6.8.4.1]
742/// 'if' '(' expression ')' statement
743/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000744/// [C++] 'if' '(' condition ')' statement
745/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000746///
John McCall7f040a92010-12-24 02:08:15 +0000747StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000748 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000749
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000750 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
752
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000753 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000754 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000756 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000758
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000759 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
760
Chris Lattner22153252007-08-26 23:08:06 +0000761 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
762 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000763 //
764 // C++ 6.4p3:
765 // A name introduced by a declaration in a condition is in scope from its
766 // point of declaration until the end of the substatements controlled by the
767 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000768 // C++ 3.3.2p4:
769 // Names declared in the for-init-statement, and in the condition of if,
770 // while, for, and switch statements are local to the if, while, for, or
771 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000772 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000773 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000776 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000777 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000778 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000779 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000780
John McCall9ae2f072010-08-23 23:25:46 +0000781 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattner0ecea032007-08-22 05:28:50 +0000783 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000784 // there is no compound stmt. C90 does not have this clause. We only do this
785 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000786 //
787 // C++ 6.4p1:
788 // The substatement in a selection-statement (each substatement, in the else
789 // form of the if statement) implicitly defines a local scope.
790 //
791 // For C++ we create a scope for the condition and a new scope for
792 // substatements because:
793 // -When the 'then' scope exits, we want the condition declaration to still be
794 // active for the 'else' scope too.
795 // -Sema will detect name clashes by considering declarations of a
796 // 'ControlScope' as part of its direct subscope.
797 // -If we wanted the condition and substatement to be in the same scope, we
798 // would have to notify ParseStatement not to create a new scope. It's
799 // simpler to let it create a new scope.
800 //
Mike Stump1eb44332009-09-09 15:08:12 +0000801 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000802 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000803
Chris Lattnerb96728d2007-10-29 05:08:52 +0000804 // Read the 'then' stmt.
805 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000806 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000807
Chris Lattnera36ce712007-08-22 05:16:28 +0000808 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000809 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 // If it has an else, parse it.
812 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000813 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000814 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000815
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000816 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000818 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000819
Chris Lattner0ecea032007-08-22 05:28:50 +0000820 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000821 // there is no compound stmt. C90 does not have this clause. We only do
822 // this if the body isn't a compound statement to avoid push/pop in common
823 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000824 //
825 // C++ 6.4p1:
826 // The substatement in a selection-statement (each substatement, in the else
827 // form of the if statement) implicitly defines a local scope.
828 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000829 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000830 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000833
Chris Lattnera36ce712007-08-22 05:16:28 +0000834 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000835 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000837
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000838 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattner18914bc2008-12-12 06:19:11 +0000840 // If the condition was invalid, discard the if statement. We could recover
841 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000842 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000843 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000844
Chris Lattnerb96728d2007-10-29 05:08:52 +0000845 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000846 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000847 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000848 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
849 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
850 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000851 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000852 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000853 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000854
Chris Lattnerb96728d2007-10-29 05:08:52 +0000855 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000856 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000857 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000858 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000859 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000860
John McCall9ae2f072010-08-23 23:25:46 +0000861 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000862 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000863}
864
865/// ParseSwitchStatement
866/// switch-statement:
867/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000868/// [C++] 'switch' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000869StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000870 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000871
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000872 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
874
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000875 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000876 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000878 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 }
Chris Lattner22153252007-08-26 23:08:06 +0000880
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000881 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
882
Chris Lattner22153252007-08-26 23:08:06 +0000883 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
884 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000885 //
886 // C++ 6.4p3:
887 // A name introduced by a declaration in a condition is in scope from its
888 // point of declaration until the end of the substatements controlled by the
889 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000890 // C++ 3.3.2p4:
891 // Names declared in the for-init-statement, and in the condition of if,
892 // while, for, and switch statements are local to the if, while, for, or
893 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000894 //
Richard Trieubb9b80c2011-04-21 21:44:26 +0000895 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +0000896 if (C99orCXX)
897 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000898 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000899
900 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000901 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000902 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000903 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +0000904 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000905
John McCall60d7b3a2010-08-24 06:29:42 +0000906 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +0000907 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000908
Douglas Gregor586596f2010-05-06 17:25:47 +0000909 if (Switch.isInvalid()) {
910 // Skip the switch body.
911 // FIXME: This is not optimal recovery, but parsing the body is more
912 // dangerous due to the presence of case and default statements, which
913 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +0000914 if (Tok.is(tok::l_brace)) {
915 ConsumeBrace();
916 SkipUntil(tok::r_brace, false, false);
917 } else
Douglas Gregor586596f2010-05-06 17:25:47 +0000918 SkipUntil(tok::semi);
919 return move(Switch);
920 }
921
Chris Lattner0ecea032007-08-22 05:28:50 +0000922 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000923 // there is no compound stmt. C90 does not have this clause. We only do this
924 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000925 //
926 // C++ 6.4p1:
927 // The substatement in a selection-statement (each substatement, in the else
928 // form of the if statement) implicitly defines a local scope.
929 //
930 // See comments in ParseIfStatement for why we create a scope for the
931 // condition and a new scope for substatement in C++.
932 //
Mike Stump1eb44332009-09-09 15:08:12 +0000933 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000934 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000937 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000938
Chris Lattner7e52de42010-01-24 01:50:29 +0000939 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000940 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000941 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000942
Chris Lattner7e52de42010-01-24 01:50:29 +0000943 if (Body.isInvalid())
944 // FIXME: Remove the case statement list from the Switch statement.
945 Body = Actions.ActOnNullStmt(Tok.getLocation());
946
John McCall9ae2f072010-08-23 23:25:46 +0000947 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000948}
949
950/// ParseWhileStatement
951/// while-statement: [C99 6.8.5.1]
952/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000953/// [C++] 'while' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000954StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000955 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000956
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000957 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 SourceLocation WhileLoc = Tok.getLocation();
959 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000960
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000961 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000962 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000964 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000966
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000967 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
968
Chris Lattner22153252007-08-26 23:08:06 +0000969 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
970 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000971 //
972 // C++ 6.4p3:
973 // A name introduced by a declaration in a condition is in scope from its
974 // point of declaration until the end of the substatements controlled by the
975 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000976 // C++ 3.3.2p4:
977 // Names declared in the for-init-statement, and in the condition of if,
978 // while, for, and switch statements are local to the if, while, for, or
979 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000980 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000981 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000982 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000983 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
984 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000985 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000986 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
987 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000988
989 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000990 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000991 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000992 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000993 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000994
John McCall9ae2f072010-08-23 23:25:46 +0000995 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattner0ecea032007-08-22 05:28:50 +0000997 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000998 // there is no compound stmt. C90 does not have this clause. We only do this
999 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001000 //
1001 // C++ 6.5p2:
1002 // The substatement in an iteration-statement implicitly defines a local scope
1003 // which is entered and exited each time through the loop.
1004 //
1005 // See comments in ParseIfStatement for why we create a scope for the
1006 // condition and a new scope for substatement in C++.
1007 //
Mike Stump1eb44332009-09-09 15:08:12 +00001008 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001009 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001010
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001012 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001013
Chris Lattner0ecea032007-08-22 05:28:50 +00001014 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001015 InnerScope.Exit();
1016 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001017
John McCalld226f652010-08-21 09:40:31 +00001018 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001019 return StmtError();
1020
John McCall9ae2f072010-08-23 23:25:46 +00001021 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001022}
1023
1024/// ParseDoStatement
1025/// do-statement: [C99 6.8.5.2]
1026/// 'do' statement 'while' '(' expression ')' ';'
1027/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +00001028StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001029 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001030
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001031 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001033
Chris Lattner22153252007-08-26 23:08:06 +00001034 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1035 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001036 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +00001037 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001038 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001039 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001040 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001041
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001042 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001043
Chris Lattner0ecea032007-08-22 05:28:50 +00001044 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001045 // there is no compound stmt. C90 does not have this clause. We only do this
1046 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001047 //
1048 // C++ 6.5p2:
1049 // The substatement in an iteration-statement implicitly defines a local scope
1050 // which is entered and exited each time through the loop.
1051 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001052 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +00001053 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001054 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001055
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001057 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001058
Chris Lattner0ecea032007-08-22 05:28:50 +00001059 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001060 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001061
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001062 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001063 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001064 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001065 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001066 SkipUntil(tok::semi, false, true);
1067 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001068 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 }
1070 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001071
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001072 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001073 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001074 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001075 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001077
Chris Lattnerff871fb2008-12-12 06:35:28 +00001078 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +00001079 SourceLocation LPLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001080 ExprResult Cond = ParseExpression();
Douglas Gregor04895d32009-11-24 21:34:32 +00001081 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001082 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001083
Sebastian Redl9a920342008-12-11 19:48:14 +00001084 if (Cond.isInvalid() || Body.isInvalid())
1085 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001086
John McCall9ae2f072010-08-23 23:25:46 +00001087 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1088 Cond.get(), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001089}
1090
1091/// ParseForStatement
1092/// for-statement: [C99 6.8.5.3]
1093/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1094/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001095/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1096/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001097/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001098/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1099/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001100///
1101/// [C++] for-init-statement:
1102/// [C++] expression-statement
1103/// [C++] simple-declaration
1104///
Richard Smithad762fc2011-04-14 22:09:26 +00001105/// [C++0x] for-range-declaration:
1106/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1107/// [C++0x] for-range-initializer:
1108/// [C++0x] expression
1109/// [C++0x] braced-init-list [TODO]
John McCall7f040a92010-12-24 02:08:15 +00001110StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001111 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001112
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001113 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001115
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001116 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001117 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001119 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001121
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001122 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001123
Chris Lattner22153252007-08-26 23:08:06 +00001124 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1125 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001126 //
1127 // C++ 6.4p3:
1128 // A name introduced by a declaration in a condition is in scope from its
1129 // point of declaration until the end of the substatements controlled by the
1130 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001131 // C++ 3.3.2p4:
1132 // Names declared in the for-init-statement, and in the condition of if,
1133 // while, for, and switch statements are local to the if, while, for, or
1134 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001135 // C++ 6.5.3p1:
1136 // Names declared in the for-init-statement are in the same declarative-region
1137 // as those declared in the condition.
1138 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001139 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001140 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001141 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1142 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001143 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001144 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1145
1146 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001147
1148 SourceLocation LParenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001149 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001150
Richard Smithad762fc2011-04-14 22:09:26 +00001151 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001152 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001153 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001154 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001155 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001156 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001157 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001158 Decl *SecondVar = 0;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001159
Douglas Gregor791215b2009-09-21 20:51:25 +00001160 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001161 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001162 C99orCXXorObjC? Sema::PCC_ForInit
1163 : Sema::PCC_Expression);
Douglas Gregordc845342010-05-25 05:58:43 +00001164 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +00001165 }
1166
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001168 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 // no first part, eat the ';'.
1170 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001171 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001173 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001175
John McCall0b7e6782011-03-24 11:26:52 +00001176 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001177 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001178
Richard Smithad762fc2011-04-14 22:09:26 +00001179 // In C++0x, "for (T NS:a" might not be a typo for ::
1180 bool MightBeForRangeStmt = getLang().CPlusPlus;
1181 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1182
Chris Lattner97144fc2009-04-02 04:16:50 +00001183 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001184 StmtVector Stmts(Actions);
1185 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001186 DeclEnd, attrs, false,
1187 MightBeForRangeStmt ?
1188 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001189 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Richard Smithad762fc2011-04-14 22:09:26 +00001191 if (ForRangeInit.ParsedForRangeDecl()) {
1192 ForRange = true;
1193 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001194 ConsumeToken();
1195 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001196 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001197 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001198 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001199
1200 if (Tok.is(tok::code_completion)) {
1201 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1202 ConsumeCodeCompletionToken();
1203 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001204 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001205 } else {
1206 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001207 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 } else {
1209 Value = ParseExpression();
1210
John McCallf6a16482010-12-04 03:47:34 +00001211 ForEach = isTokIdentifier_in();
1212
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001214 if (!Value.isInvalid()) {
1215 if (ForEach)
1216 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1217 else
1218 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1219 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001220
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001221 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001223 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001224 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001225
1226 if (Tok.is(tok::code_completion)) {
1227 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1228 ConsumeCodeCompletionToken();
1229 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001230 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001231 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001232 if (!Value.isInvalid()) {
1233 Diag(Tok, diag::err_expected_semi_for);
1234 } else {
1235 // Skip until semicolon or rparen, don't consume it.
1236 SkipUntil(tok::r_paren, true, true);
1237 if (Tok.is(tok::semi))
1238 ConsumeToken();
1239 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 }
1241 }
Richard Smithad762fc2011-04-14 22:09:26 +00001242 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001243 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001244 // Parse the second part of the for specifier.
1245 if (Tok.is(tok::semi)) { // for (...;;
1246 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001247 } else if (Tok.is(tok::r_paren)) {
1248 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001249 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001250 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001251 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001252 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1253 else {
1254 Second = ParseExpression();
1255 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001256 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001257 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001258 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001259 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001260 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001261 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001262
Douglas Gregorb72c7782011-02-17 03:38:46 +00001263 if (Tok.isNot(tok::semi)) {
1264 if (!SecondPartIsInvalid || SecondVar)
1265 Diag(Tok, diag::err_expected_semi_for);
1266 else
1267 // Skip until semicolon or rparen, don't consume it.
1268 SkipUntil(tok::r_paren, true, true);
1269 }
1270
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001271 if (Tok.is(tok::semi)) {
1272 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001273 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001274
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001275 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001276 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001277 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001278 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001279 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 // Match the ')'.
1282 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001283
Richard Smithad762fc2011-04-14 22:09:26 +00001284 // We need to perform most of the semantic analysis for a C++0x for-range
1285 // statememt before parsing the body, in order to be able to deduce the type
1286 // of an auto-typed loop variable.
1287 StmtResult ForRangeStmt;
1288 if (ForRange)
1289 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1290 FirstPart.take(),
1291 ForRangeInit.ColonLoc,
1292 ForRangeInit.RangeExpr.get(),
1293 RParenLoc);
1294
Chris Lattner0ecea032007-08-22 05:28:50 +00001295 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001296 // there is no compound stmt. C90 does not have this clause. We only do this
1297 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001298 //
1299 // C++ 6.5p2:
1300 // The substatement in an iteration-statement implicitly defines a local scope
1301 // which is entered and exited each time through the loop.
1302 //
1303 // See comments in ParseIfStatement for why we create a scope for
1304 // for-init-statement/condition and a new scope for substatement in C++.
1305 //
Mike Stump1eb44332009-09-09 15:08:12 +00001306 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001307 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001308
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001310 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001311
Chris Lattner0ecea032007-08-22 05:28:50 +00001312 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001313 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001314
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001316 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001317
1318 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001319 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001320
Richard Smithad762fc2011-04-14 22:09:26 +00001321 if (ForEach)
1322 // FIXME: It isn't clear how to communicate the late destruction of
1323 // C++ temporaries used to create the collection.
1324 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1325 FirstPart.take(),
1326 Collection.take(), RParenLoc,
1327 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Richard Smithad762fc2011-04-14 22:09:26 +00001329 if (ForRange)
1330 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1331
1332 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1333 SecondVar, ThirdPart, RParenLoc, Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001334}
1335
1336/// ParseGotoStatement
1337/// jump-statement:
1338/// 'goto' identifier ';'
1339/// [GNU] 'goto' '*' expression ';'
1340///
1341/// Note: this lets the caller parse the end ';'.
1342///
John McCall7f040a92010-12-24 02:08:15 +00001343StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001344 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001345
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001346 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001348
John McCall60d7b3a2010-08-24 06:29:42 +00001349 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001350 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001351 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1352 Tok.getLocation());
1353 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001354 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001355 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001356 // GNU indirect goto extension.
1357 Diag(Tok, diag::ext_gnu_indirect_goto);
1358 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001359 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001360 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001362 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 }
John McCall9ae2f072010-08-23 23:25:46 +00001364 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001365 } else {
1366 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001367 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001368 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001369
Sebastian Redl9a920342008-12-11 19:48:14 +00001370 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001371}
1372
1373/// ParseContinueStatement
1374/// jump-statement:
1375/// 'continue' ';'
1376///
1377/// Note: this lets the caller parse the end ';'.
1378///
John McCall7f040a92010-12-24 02:08:15 +00001379StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001380 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001381
Reid Spencer5f016e22007-07-11 17:01:13 +00001382 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001383 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001384}
1385
1386/// ParseBreakStatement
1387/// jump-statement:
1388/// 'break' ';'
1389///
1390/// Note: this lets the caller parse the end ';'.
1391///
John McCall7f040a92010-12-24 02:08:15 +00001392StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001393 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001394
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001396 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001397}
1398
1399/// ParseReturnStatement
1400/// jump-statement:
1401/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001402StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001403 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001404
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001405 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001407
John McCall60d7b3a2010-08-24 06:29:42 +00001408 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001409 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001410 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001411 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001412 ConsumeCodeCompletionToken();
1413 SkipUntil(tok::semi, false, true);
1414 return StmtError();
1415 }
1416
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001417 // FIXME: This is a hack to allow something like C++0x's generalized
1418 // initializer lists, but only enough of this feature to allow Clang to
1419 // parse libstdc++ 4.5's headers.
1420 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1421 R = ParseInitializer();
1422 if (R.isUsable() && !getLang().CPlusPlus0x)
1423 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1424 << R.get()->getSourceRange();
1425 } else
1426 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001427 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001429 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 }
1431 }
John McCall9ae2f072010-08-23 23:25:46 +00001432 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001433}
1434
Steve Naroff5f8aa692008-02-11 23:15:56 +00001435/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1436/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001437StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1438 SourceLocation EndLoc;
Steve Naroffb746ce82008-02-07 23:24:32 +00001439 if (Tok.is(tok::l_brace)) {
1440 unsigned short savedBraceCount = BraceCount;
1441 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001442 EndLoc = Tok.getLocation();
Steve Naroffb746ce82008-02-07 23:24:32 +00001443 ConsumeAnyToken();
1444 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001445 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001446 // From the MS website: If used without braces, the __asm keyword means
1447 // that the rest of the line is an assembly-language statement.
1448 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001449 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001450 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001451 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001452 EndLoc = TokLoc;
Steve Naroff36280972008-02-08 18:01:27 +00001453 ConsumeAnyToken();
1454 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001455 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1456 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001457 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001458 }
Mike Stump95059b52009-12-11 00:04:56 +00001459 Token t;
1460 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001461 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001462 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001463 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001464 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001465 ExprVector Constraints(Actions);
1466 ExprVector Exprs(Actions);
1467 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001468 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001469 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001470 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001471 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001472}
1473
Reid Spencer5f016e22007-07-11 17:01:13 +00001474/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001475/// asm-statement:
1476/// gnu-asm-statement
1477/// ms-asm-statement
1478///
1479/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001480/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1481///
1482/// [GNU] asm-argument:
1483/// asm-string-literal
1484/// asm-string-literal ':' asm-operands[opt]
1485/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1486/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1487/// ':' asm-clobbers
1488///
1489/// [GNU] asm-clobbers:
1490/// asm-string-literal
1491/// asm-clobbers ',' asm-string-literal
1492///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001493/// [MS] ms-asm-statement:
1494/// '__asm' assembly-instruction ';'[opt]
1495/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1496///
1497/// [MS] assembly-instruction-list:
1498/// assembly-instruction ';'[opt]
1499/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1500///
John McCall60d7b3a2010-08-24 06:29:42 +00001501StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001502 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001503 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001504
Steve Naroff5f8aa692008-02-11 23:15:56 +00001505 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001506 msAsm = true;
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001507 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001508 }
John McCall0b7e6782011-03-24 11:26:52 +00001509 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001511 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001512
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1514 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001515 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001517 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001518
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001520 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001521 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001522 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001524 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 }
1526 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001527
John McCall60d7b3a2010-08-24 06:29:42 +00001528 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001529 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001530 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001531
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001532 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001533 ExprVector Constraints(Actions);
1534 ExprVector Exprs(Actions);
1535 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001536
Anders Carlssondfab34a2008-02-05 23:03:50 +00001537 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001538 // We have a simple asm expression like 'asm("foo")'.
1539 SourceLocation RParenLoc = ConsumeParen();
1540 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1541 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1542 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001543 AsmString.take(), move_arg(Clobbers),
Chris Lattner64cb4752009-12-20 23:00:41 +00001544 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001546
Chris Lattner64cb4752009-12-20 23:00:41 +00001547 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001548 bool AteExtraColon = false;
1549 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1550 // In C++ mode, parse "::" like ": :".
1551 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001552 ConsumeToken();
1553
Chris Lattner64056462009-12-20 23:08:04 +00001554 if (!AteExtraColon &&
1555 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001556 return StmtError();
1557 }
Chris Lattner64056462009-12-20 23:08:04 +00001558
Chris Lattner64cb4752009-12-20 23:00:41 +00001559 unsigned NumOutputs = Names.size();
1560
1561 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001562 if (AteExtraColon ||
1563 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1564 // In C++ mode, parse "::" like ": :".
1565 if (AteExtraColon)
1566 AteExtraColon = false;
1567 else {
1568 AteExtraColon = Tok.is(tok::coloncolon);
1569 ConsumeToken();
1570 }
1571
1572 if (!AteExtraColon &&
1573 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001574 return StmtError();
1575 }
1576
1577 assert(Names.size() == Constraints.size() &&
1578 Constraints.size() == Exprs.size() &&
1579 "Input operand size mismatch!");
1580
1581 unsigned NumInputs = Names.size() - NumOutputs;
1582
1583 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001584 if (AteExtraColon || Tok.is(tok::colon)) {
1585 if (!AteExtraColon)
1586 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001587
Chandler Carruth102e1b62010-07-22 07:11:21 +00001588 // Parse the asm-string list for clobbers if present.
1589 if (Tok.isNot(tok::r_paren)) {
1590 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001591 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001592
Chandler Carruth102e1b62010-07-22 07:11:21 +00001593 if (Clobber.isInvalid())
1594 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001595
Chandler Carruth102e1b62010-07-22 07:11:21 +00001596 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001597
Chandler Carruth102e1b62010-07-22 07:11:21 +00001598 if (Tok.isNot(tok::comma)) break;
1599 ConsumeToken();
1600 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001601 }
1602 }
1603
1604 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1605 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001606 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001607 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001608 AsmString.take(), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001609 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001610}
1611
1612/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001613/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001614///
1615/// [GNU] asm-operands:
1616/// asm-operand
1617/// asm-operands ',' asm-operand
1618///
1619/// [GNU] asm-operand:
1620/// asm-string-literal '(' expression ')'
1621/// '[' identifier ']' asm-string-literal '(' expression ')'
1622///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001623//
1624// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001625bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1626 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1627 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001629 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001630 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
1632 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001634 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001637 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 Diag(Tok, diag::err_expected_ident);
1639 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001640 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 }
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Anders Carlssonb235fc22007-11-22 01:36:19 +00001643 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001644 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001645
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001646 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001648 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001649 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001650
John McCall60d7b3a2010-08-24 06:29:42 +00001651 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001652 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001653 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001654 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001655 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001656 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001657
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001658 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001659 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001661 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001663
Reid Spencer5f016e22007-07-11 17:01:13 +00001664 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001665 SourceLocation OpenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001666 ExprResult Res(ParseExpression());
Eli Friedman72056a22009-05-03 07:49:42 +00001667 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001668 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001670 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001672 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001674 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 ConsumeToken();
1676 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001677
1678 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001679}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001680
Douglas Gregorc9977d02011-03-16 17:05:57 +00001681Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001682 assert(Tok.is(tok::l_brace));
1683 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001684
Douglas Gregorc9977d02011-03-16 17:05:57 +00001685 if (PP.isCodeCompletionEnabled()) {
1686 if (trySkippingFunctionBodyForCodeCompletion()) {
1687 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001688 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001689 }
1690 }
1691
John McCallf312b1e2010-08-26 23:41:50 +00001692 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1693 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001695 // Do not enter a scope for the brace, as the arguments are in the same scope
1696 // (the function body) as the body itself. Instead, just read the statement
1697 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001698 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001699
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001700 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001701 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001702 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001703 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001704
Douglas Gregorc9977d02011-03-16 17:05:57 +00001705 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001706 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001707}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001708
Sebastian Redld3a413d2009-04-26 20:35:05 +00001709/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1710///
1711/// function-try-block:
1712/// 'try' ctor-initializer[opt] compound-statement handler-seq
1713///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001714Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001715 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1716 SourceLocation TryLoc = ConsumeToken();
1717
John McCallf312b1e2010-08-26 23:41:50 +00001718 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1719 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001720
1721 // Constructor initializer list?
1722 if (Tok.is(tok::colon))
1723 ParseConstructorInitializer(Decl);
1724
Douglas Gregorc9977d02011-03-16 17:05:57 +00001725 if (PP.isCodeCompletionEnabled()) {
1726 if (trySkippingFunctionBodyForCodeCompletion()) {
1727 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001728 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001729 }
1730 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001731
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001732 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001733 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001734 // If we failed to parse the try-catch, we just give the function an empty
1735 // compound statement as the body.
1736 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001737 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001738 MultiStmtArg(Actions), false);
1739
Douglas Gregorc9977d02011-03-16 17:05:57 +00001740 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001741 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001742}
1743
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001744bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001745 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001746 assert(PP.isCodeCompletionEnabled() &&
1747 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001748
1749 // We're in code-completion mode. Skip parsing for all function bodies unless
1750 // the body contains the code-completion point.
1751 TentativeParsingAction PA(*this);
1752 ConsumeBrace();
1753 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1754 /*StopAtCodeCompletion=*/true)) {
1755 PA.Commit();
1756 return true;
1757 }
1758
1759 PA.Revert();
1760 return false;
1761}
1762
Sebastian Redla0fd8652008-12-21 16:41:36 +00001763/// ParseCXXTryBlock - Parse a C++ try-block.
1764///
1765/// try-block:
1766/// 'try' compound-statement handler-seq
1767///
John McCall7f040a92010-12-24 02:08:15 +00001768StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001769 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001770
Sebastian Redla0fd8652008-12-21 16:41:36 +00001771 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1772
1773 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001774 return ParseCXXTryBlockCommon(TryLoc);
1775}
1776
1777/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1778/// function-try-block.
1779///
1780/// try-block:
1781/// 'try' compound-statement handler-seq
1782///
1783/// function-try-block:
1784/// 'try' ctor-initializer[opt] compound-statement handler-seq
1785///
1786/// handler-seq:
1787/// handler handler-seq[opt]
1788///
John McCall60d7b3a2010-08-24 06:29:42 +00001789StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001790 if (Tok.isNot(tok::l_brace))
1791 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001792 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00001793 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001794 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001795 if (TryBlock.isInvalid())
1796 return move(TryBlock);
1797
1798 StmtVector Handlers(Actions);
John McCall7f040a92010-12-24 02:08:15 +00001799 MaybeParseCXX0XAttributes(attrs);
1800 ProhibitAttributes(attrs);
1801
Sebastian Redla0fd8652008-12-21 16:41:36 +00001802 if (Tok.isNot(tok::kw_catch))
1803 return StmtError(Diag(Tok, diag::err_expected_catch));
1804 while (Tok.is(tok::kw_catch)) {
John McCall60d7b3a2010-08-24 06:29:42 +00001805 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001806 if (!Handler.isInvalid())
1807 Handlers.push_back(Handler.release());
1808 }
1809 // Don't bother creating the full statement if we don't have any usable
1810 // handlers.
1811 if (Handlers.empty())
1812 return StmtError();
1813
John McCall9ae2f072010-08-23 23:25:46 +00001814 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001815}
1816
1817/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1818///
1819/// handler:
1820/// 'catch' '(' exception-declaration ')' compound-statement
1821///
1822/// exception-declaration:
1823/// type-specifier-seq declarator
1824/// type-specifier-seq abstract-declarator
1825/// type-specifier-seq
1826/// '...'
1827///
John McCall60d7b3a2010-08-24 06:29:42 +00001828StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001829 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1830
1831 SourceLocation CatchLoc = ConsumeToken();
1832
1833 SourceLocation LParenLoc = Tok.getLocation();
1834 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1835 return StmtError();
1836
1837 // C++ 3.3.2p3:
1838 // The name in a catch exception-declaration is local to the handler and
1839 // shall not be redeclared in the outermost block of the handler.
1840 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1841
1842 // exception-declaration is equivalent to '...' or a parameter-declaration
1843 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00001844 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001845 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001846 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00001847 if (ParseCXXTypeSpecifierSeq(DS))
1848 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001849 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1850 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001851 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001852 } else
1853 ConsumeToken();
1854
1855 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1856 return StmtError();
1857
1858 if (Tok.isNot(tok::l_brace))
1859 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1860
Sean Huntbbd37c62009-11-21 08:43:09 +00001861 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00001862 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001863 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001864 if (Block.isInvalid())
1865 return move(Block);
1866
John McCall9ae2f072010-08-23 23:25:46 +00001867 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001868}