blob: 5138cc159546a34225548cd302ade0d8501cdedb [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000023using namespace clang;
24
Douglas Gregor0e7dde52011-04-24 05:37:28 +000025static bool isColonOrRSquareBracket(const Token &Tok) {
26 return Tok.is(tok::colon) || Tok.is(tok::r_square);
27}
28
Chris Lattner0ccd51e2006-08-09 05:47:47 +000029//===----------------------------------------------------------------------===//
30// C99 6.8: Statements and Blocks.
31//===----------------------------------------------------------------------===//
32
33/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
34/// StatementOrDeclaration:
35/// statement
36/// declaration
37///
38/// statement:
39/// labeled-statement
40/// compound-statement
41/// expression-statement
42/// selection-statement
43/// iteration-statement
44/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000045/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000046/// [C++] try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000047/// [OBC] objc-throw-statement
48/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000049/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000050/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000051/// [OMP] openmp-construct [TODO]
52///
53/// labeled-statement:
54/// identifier ':' statement
55/// 'case' constant-expression ':' statement
56/// 'default' ':' statement
57///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000058/// selection-statement:
59/// if-statement
60/// switch-statement
61///
62/// iteration-statement:
63/// while-statement
64/// do-statement
65/// for-statement
66///
Chris Lattner9075bd72006-08-10 04:59:57 +000067/// expression-statement:
68/// expression[opt] ';'
69///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000070/// jump-statement:
71/// 'goto' identifier ';'
72/// 'continue' ';'
73/// 'break' ';'
74/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000075/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000076///
Fariborz Jahanian90814572007-10-04 20:19:06 +000077/// [OBC] objc-throw-statement:
78/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000079/// [OBC] '@' 'throw' ';'
80///
John McCalldadc5752010-08-24 06:29:42 +000081StmtResult
Fariborz Jahanian1db5c942010-09-28 20:42:35 +000082Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000083 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000084 StmtResult Res;
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000085
86 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000087
John McCall084e83d2011-03-24 11:26:52 +000088 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000089 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +000090
Chris Lattner503fadc2006-08-10 05:45:44 +000091 // Cases in this switch statement should fall through if the parser expects
92 // the token to end in a semicolon (in which case SemiError should be set),
93 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +000094Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000095 tok::TokenKind Kind = Tok.getKind();
96 SourceLocation AtLoc;
97 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000098 case tok::at: // May be a @try or @throw statement
99 {
100 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000101 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000102 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000103
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000104 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000105 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorf4c33342010-05-28 00:22:41 +0000106 ConsumeCodeCompletionToken();
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000107 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000108
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000109 case tok::identifier: {
110 Token Next = NextToken();
111 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000112 // identifier ':' statement
John McCall53fa7142010-12-24 02:08:15 +0000113 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000114 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000115
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000116 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000117 // FIXME: Temporarily enable this code only for C.
118 CXXScopeSpec SS;
119 IdentifierInfo *Name = Tok.getIdentifierInfo();
120 SourceLocation NameLoc = Tok.getLocation();
121 Sema::NameClassification Classification
122 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
123 switch (Classification.getKind()) {
124 case Sema::NC_Keyword:
125 // The identifier was corrected to a keyword. Update the token
126 // to this keyword, and try again.
127 if (Name->getTokenID() != tok::identifier) {
128 Tok.setIdentifierInfo(Name);
129 Tok.setKind(Name->getTokenID());
130 goto Retry;
131 }
132
133 // Fall through via the normal error path.
134 // FIXME: This seems like it could only happen for context-sensitive
135 // keywords.
136
137 case Sema::NC_Error:
138 // Handle errors here by skipping up to the next semicolon or '}', and
139 // eat the semicolon if that's what stopped us.
140 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
141 if (Tok.is(tok::semi))
142 ConsumeToken();
143 return StmtError();
144
145 case Sema::NC_Unknown:
146 // Either we don't know anything about this identifier, or we know that
147 // we're in a syntactic context we haven't handled yet.
148 break;
149
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000150 case Sema::NC_Type: {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000151 // We have a type. In C, this means that we have a declaration.
152 if (!getLang().CPlusPlus) {
153 ParsedType Type = Classification.getType();
154 const char *PrevSpec = 0;
155 unsigned DiagID;
156 ConsumeToken(); // the identifier
157 ParsingDeclSpec DS(*this);
158 DS.takeAttributesFrom(attrs);
159 DS.SetTypeSpecType(DeclSpec::TST_typename, NameLoc, PrevSpec, DiagID,
160 Type);
161 DS.SetRangeStart(NameLoc);
162 DS.SetRangeEnd(NameLoc);
163
164 // In Objective-C, check whether this is the start of a class message
165 // send that is missing an opening square bracket ('[').
166 if (getLang().ObjC1 && Tok.is(tok::identifier) &&
167 Type.get()->isObjCObjectOrInterfaceType() &&
168 isColonOrRSquareBracket(NextToken())) {
169 // Fake up a Declarator to use with ActOnTypeName.
170 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
171 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
172 if (Ty.isInvalid()) {
173 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
174 if (Tok.is(tok::semi))
175 ConsumeToken();
176 return StmtError();
177 }
178
179 ExprResult MsgExpr = ParseObjCMessageExpressionBody(SourceLocation(),
180 SourceLocation(),
181 Ty.get(), 0);
182 return ParseExprStatement(attrs, MsgExpr);
183 }
184
185 // Objective-C supports syntax of the form 'id<proto1,proto2>' where
186 // 'id' is a specific typedef and 'itf<proto1,proto2>' where 'itf' is
187 // an Objective-C interface.
188 if (Tok.is(tok::less) && getLang().ObjC1)
189 ParseObjCProtocolQualifiers(DS);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000190
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000191 SourceLocation DeclStart = NameLoc, DeclEnd;
192 DeclGroupPtrTy Decl = ParseSimpleDeclaration(DS, Stmts,
193 Declarator::BlockContext,
194 DeclEnd, true);
195 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
196 }
197
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000198 // In C++, we might also have a functional-style cast. Just annotate
199 // this as a type token.
200 Tok.setKind(tok::annot_typename);
201 setTypeAnnotation(Tok, Classification.getType());
202 Tok.setAnnotationEndLoc(NameLoc);
203 Tok.setLocation(NameLoc);
204 PP.AnnotateCachedTokens(Tok);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000205 break;
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000206 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000207
208 case Sema::NC_Expression:
209 ConsumeToken(); // the identifier
210 return ParseExprStatement(attrs, Classification.getExpression());
211
212 case Sema::NC_TypeTemplate:
213 case Sema::NC_FunctionTemplate: {
214 ConsumeToken(); // the identifier
215 UnqualifiedId Id;
216 Id.setIdentifier(Name, NameLoc);
217 if (AnnotateTemplateIdToken(
218 TemplateTy::make(Classification.getTemplateName()),
219 Classification.getTemplateNameKind(),
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000220 SS, Id, SourceLocation(),
221 /*AllowTypeAnnotation=*/false)) {
222 // Handle errors here by skipping up to the next semicolon or '}', and
223 // eat the semicolon if that's what stopped us.
224 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
225 if (Tok.is(tok::semi))
226 ConsumeToken();
227 return StmtError();
228 }
229
230 // If the next token is '::', jump right into parsing a
231 // nested-name-specifier. We don't want to leave the template-id
232 // hanging.
233 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000234 // Handle errors here by skipping up to the next semicolon or '}', and
235 // eat the semicolon if that's what stopped us.
236 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
237 if (Tok.is(tok::semi))
238 ConsumeToken();
239 return StmtError();
240 }
241
242 // We've annotated a template-id, so try again now.
243 goto Retry;
244 }
245
246 case Sema::NC_NestedNameSpecifier:
247 // FIXME: Implement this!
248 break;
249 }
250 }
251
252 // Fall through
253 }
254
Chris Lattner803802d2009-03-24 17:04:48 +0000255 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000256 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000257 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000258 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000259 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000260 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000261 }
262
263 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000264 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000265 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000266 }
Mike Stump11289f42009-09-09 15:08:12 +0000267
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000268 return ParseExprStatement(attrs, ExprResult());
Chris Lattner803802d2009-03-24 17:04:48 +0000269 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000270
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000271 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000272 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000273 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000274 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000275
Chris Lattner9075bd72006-08-10 04:59:57 +0000276 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000277 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000278 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidisf7620e42011-04-27 05:04:02 +0000279 SourceLocation LeadingEmptyMacroLoc;
280 if (Tok.hasLeadingEmptyMacro())
281 LeadingEmptyMacroLoc = PP.getLastEmptyMacroInstantiationLoc();
282 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacroLoc);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000283 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000284
Chris Lattner9075bd72006-08-10 04:59:57 +0000285 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall53fa7142010-12-24 02:08:15 +0000286 return ParseIfStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000287 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall53fa7142010-12-24 02:08:15 +0000288 return ParseSwitchStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000289
Chris Lattner9075bd72006-08-10 04:59:57 +0000290 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall53fa7142010-12-24 02:08:15 +0000291 return ParseWhileStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000292 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000293 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000294 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000295 break;
296 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall53fa7142010-12-24 02:08:15 +0000297 return ParseForStatement(attrs);
Chris Lattner503fadc2006-08-10 05:45:44 +0000298
299 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000300 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000301 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000302 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000303 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000304 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000305 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000306 break;
307 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000308 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000309 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000310 break;
311 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000312 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000313 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000314 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000315
Sebastian Redlb219c902008-12-21 16:41:36 +0000316 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000317 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000318 bool msAsm = false;
319 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000320 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000321 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000322 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000323 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000324 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000325
Sebastian Redlb219c902008-12-21 16:41:36 +0000326 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000327 return ParseCXXTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000328 }
329
Chris Lattner503fadc2006-08-10 05:45:44 +0000330 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000331 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000332 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000333 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000334 // If the result was valid, then we do want to diagnose this. Use
335 // ExpectAndConsume to emit the diagnostic, even though we know it won't
336 // succeed.
337 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000338 // Skip until we see a } or ;, but don't eat it.
339 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000340 }
Mike Stump11289f42009-09-09 15:08:12 +0000341
Sebastian Redl042ad952008-12-11 19:30:53 +0000342 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000343}
344
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000345/// \brief Parse an expression statement.
346StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs,
347 ExprResult Primary) {
348 // If a case keyword is missing, this is where it should be inserted.
349 Token OldToken = Tok;
350
351 // FIXME: Use the attributes
352 // expression[opt] ';'
353 ExprResult Expr(ParseExpression(Primary));
354 if (Expr.isInvalid()) {
355 // If the expression is invalid, skip ahead to the next semicolon or '}'.
356 // Not doing this opens us up to the possibility of infinite loops if
357 // ParseExpression does not consume any tokens.
358 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
359 if (Tok.is(tok::semi))
360 ConsumeToken();
361 return StmtError();
362 }
363
364 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
365 Actions.CheckCaseExpression(Expr.get())) {
366 // If a constant expression is followed by a colon inside a switch block,
367 // suggest a missing case keyword.
368 Diag(OldToken, diag::err_expected_case_before_expression)
369 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
370
371 // Recover parsing as a case statement.
372 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
373 }
374
375 // Otherwise, eat the semicolon.
376 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
377 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
378
379}
380
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000381/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000382///
383/// labeled-statement:
384/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000385/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000386///
John McCall53fa7142010-12-24 02:08:15 +0000387StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000388 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
389 "Not an identifier!");
390
391 Token IdentTok = Tok; // Save the whole token.
392 ConsumeToken(); // eat the identifier.
393
394 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000395
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000396 // identifier ':' statement
397 SourceLocation ColonLoc = ConsumeToken();
398
399 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000400 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000401
John McCalldadc5752010-08-24 06:29:42 +0000402 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000403
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000404 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000405 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000406 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000407
408 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
409 IdentTok.getLocation());
410 if (AttributeList *Attrs = attrs.getList())
411 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
412
413 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
414 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000415}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000416
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000417/// ParseCaseStatement
418/// labeled-statement:
419/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000420/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000421///
Richard Trieu2c850c02011-04-21 21:44:26 +0000422StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
423 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000424 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000425 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner34a22092009-03-04 04:23:07 +0000427 // It is very very common for code to contain many case statements recursively
428 // nested, as in (but usually without indentation):
429 // case 1:
430 // case 2:
431 // case 3:
432 // case 4:
433 // case 5: etc.
434 //
435 // Parsing this naively works, but is both inefficient and can cause us to run
436 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000437 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000438 // but all the grossness is constrained to ParseCaseStatement (and some
439 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattner34a22092009-03-04 04:23:07 +0000441 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
442 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000443 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattner34a22092009-03-04 04:23:07 +0000445 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
446 // gets updated each time a new case is parsed, and whose body is unset so
447 // far. When parsing 'case 4', this is the 'case 3' node.
448 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000449
Chris Lattner34a22092009-03-04 04:23:07 +0000450 // While we have case statements, eat and stack them.
451 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000452 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
453 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000454
Douglas Gregord328d572009-09-21 18:10:23 +0000455 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000456 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000457 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000458 }
459
Chris Lattner125c0ee2009-12-10 00:38:54 +0000460 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
461 /// Disable this form of error recovery while we're parsing the case
462 /// expression.
463 ColonProtectionRAIIObject ColonProtection(*this);
464
Richard Trieu2c850c02011-04-21 21:44:26 +0000465 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
466 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000467 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000468 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000469 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000470 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000471
Chris Lattner34a22092009-03-04 04:23:07 +0000472 // GNU case range extension.
473 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000474 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000475 if (Tok.is(tok::ellipsis)) {
476 Diag(Tok, diag::ext_gnu_case_range);
477 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000478
Chris Lattner34a22092009-03-04 04:23:07 +0000479 RHS = ParseConstantExpression();
480 if (RHS.isInvalid()) {
481 SkipUntil(tok::colon);
482 return StmtError();
483 }
484 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000485
486 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000487
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000488 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000489 if (Tok.is(tok::colon)) {
490 ColonLoc = ConsumeToken();
491
492 // Treat "case blah;" as a typo for "case blah:".
493 } else if (Tok.is(tok::semi)) {
494 ColonLoc = ConsumeToken();
495 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
496 << FixItHint::CreateReplacement(ColonLoc, ":");
497 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000498 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
499 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
500 << FixItHint::CreateInsertion(ExpectedLoc, ":");
501 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000502 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000503
John McCalldadc5752010-08-24 06:29:42 +0000504 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000505 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
506 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner34a22092009-03-04 04:23:07 +0000508 // If we had a sema error parsing this case, then just ignore it and
509 // continue parsing the sub-stmt.
510 if (Case.isInvalid()) {
511 if (TopLevelCase.isInvalid()) // No parsed case stmts.
512 return ParseStatement();
513 // Otherwise, just don't add it as a nested case.
514 } else {
515 // If this is the first case statement we parsed, it becomes TopLevelCase.
516 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000517 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000518 if (TopLevelCase.isInvalid())
519 TopLevelCase = move(Case);
520 else
John McCallb268a282010-08-23 23:25:46 +0000521 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000522 DeepestParsedCaseStmt = NextDeepest;
523 }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattner34a22092009-03-04 04:23:07 +0000525 // Handle all case statements.
526 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattner34a22092009-03-04 04:23:07 +0000528 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattner34a22092009-03-04 04:23:07 +0000530 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000531 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000532
Chris Lattner34a22092009-03-04 04:23:07 +0000533 if (Tok.isNot(tok::r_brace)) {
534 SubStmt = ParseStatement();
535 } else {
536 // Nicely diagnose the common error "switch (X) { case 4: }", which is
537 // not valid.
538 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000539 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000540 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000541 }
Mike Stump11289f42009-09-09 15:08:12 +0000542
Chris Lattner34a22092009-03-04 04:23:07 +0000543 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000544 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000545 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000546
Chris Lattner34a22092009-03-04 04:23:07 +0000547 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000548 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000549
Chris Lattner34a22092009-03-04 04:23:07 +0000550 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000551 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000552}
553
554/// ParseDefaultStatement
555/// labeled-statement:
556/// 'default' ':' statement
557/// Note that this does not parse the 'statement' at the end.
558///
John McCall53fa7142010-12-24 02:08:15 +0000559StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000560 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000561
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000562 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000563 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000564
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000565 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000566 if (Tok.is(tok::colon)) {
567 ColonLoc = ConsumeToken();
568
569 // Treat "default;" as a typo for "default:".
570 } else if (Tok.is(tok::semi)) {
571 ColonLoc = ConsumeToken();
572 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
573 << FixItHint::CreateReplacement(ColonLoc, ":");
574 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000575 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
576 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
577 << FixItHint::CreateInsertion(ExpectedLoc, ":");
578 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000579 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000580
Chris Lattner30f910e2006-10-16 05:52:41 +0000581 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000582 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000583 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000584 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000585 }
586
John McCalldadc5752010-08-24 06:29:42 +0000587 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000588 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000589 return StmtError();
590
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000591 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000592 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000593}
594
595
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000596/// ParseCompoundStatement - Parse a "{}" block.
597///
598/// compound-statement: [C99 6.8.2]
599/// { block-item-list[opt] }
600/// [GNU] { label-declarations block-item-list } [TODO]
601///
602/// block-item-list:
603/// block-item
604/// block-item-list block-item
605///
606/// block-item:
607/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000608/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000609/// statement
610/// [OMP] openmp-directive [TODO]
611///
612/// [GNU] label-declarations:
613/// [GNU] label-declaration
614/// [GNU] label-declarations label-declaration
615///
616/// [GNU] label-declaration:
617/// [GNU] '__label__' identifier-list ';'
618///
619/// [OMP] openmp-directive: [TODO]
620/// [OMP] barrier-directive
621/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000622///
John McCall53fa7142010-12-24 02:08:15 +0000623StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000624 bool isStmtExpr) {
625 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000626
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000627 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000628
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000629 // Enter a scope to hold everything within the compound stmt. Compound
630 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000631 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000632
633 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000634 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000635}
636
637
638/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000639/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000640/// consume the '}' at the end of the block. It does not manipulate the scope
641/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000642StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000643 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000644 Tok.getLocation(),
645 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000646 InMessageExpressionRAIIObject InMessage(*this, false);
647
Chris Lattnerf2978802007-01-21 06:52:16 +0000648 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
649
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000650 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000651
Chris Lattner43e7f312011-02-18 02:08:43 +0000652 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
653 // only allowed at the start of a compound stmt regardless of the language.
654 while (Tok.is(tok::kw___label__)) {
655 SourceLocation LabelLoc = ConsumeToken();
656 Diag(LabelLoc, diag::ext_gnu_local_label);
657
658 llvm::SmallVector<Decl *, 8> DeclsInGroup;
659 while (1) {
660 if (Tok.isNot(tok::identifier)) {
661 Diag(Tok, diag::err_expected_ident);
662 break;
663 }
664
665 IdentifierInfo *II = Tok.getIdentifierInfo();
666 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000667 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner43e7f312011-02-18 02:08:43 +0000668
669 if (!Tok.is(tok::comma))
670 break;
671 ConsumeToken();
672 }
673
John McCall084e83d2011-03-24 11:26:52 +0000674 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000675 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
676 DeclsInGroup.data(), DeclsInGroup.size());
677 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
678
679 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
680 if (R.isUsable())
681 Stmts.push_back(R.release());
682 }
683
684 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000685 if (Tok.is(tok::annot_pragma_unused)) {
686 HandlePragmaUnused();
687 continue;
688 }
689
John McCalldadc5752010-08-24 06:29:42 +0000690 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000691 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000692 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000693 } else {
694 // __extension__ can start declarations and it can also be a unary
695 // operator for expressions. Consume multiple __extension__ markers here
696 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000697 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000698 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000699 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000700 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000701
John McCall084e83d2011-03-24 11:26:52 +0000702 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000703 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000704
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000705 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000706 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000707 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000708 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000709 ExtensionRAIIObject O(Diags);
710
Chris Lattner49836b42009-04-02 04:16:50 +0000711 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000712 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
713 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000714 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000715 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000716 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000717 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000718 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000719
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000720 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000721 SkipUntil(tok::semi);
722 continue;
723 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000724
Alexis Hunt96d5c762009-11-21 08:43:09 +0000725 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000726 // Eat the semicolon at the end of stmt and convert the expr into a
727 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000728 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000729 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000730 }
731 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000732
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000733 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000734 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000735 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000736
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000737 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000738 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000739 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000740 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000741 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000742 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000743
Chris Lattner04132372006-10-16 06:12:55 +0000744 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000745 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000746 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000747}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000748
Chris Lattnerc0081db2008-12-12 06:31:07 +0000749/// ParseParenExprOrCondition:
750/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000751/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000752///
753/// This function parses and performs error recovery on the specified condition
754/// or expression (depending on whether we're in C++ or C mode). This function
755/// goes out of its way to recover well. It returns true if there was a parser
756/// error (the right paren couldn't be found), which indicates that the caller
757/// should try to recover harder. It returns false if the condition is
758/// successfully parsed. Note that a successful parse can still have semantic
759/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000760bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000761 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000762 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000763 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000764 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000765 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000766 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000767 else {
768 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000769 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000770
771 // If required, convert to a boolean value.
772 if (!ExprResult.isInvalid() && ConvertToBoolean)
773 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000774 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattnerc0081db2008-12-12 06:31:07 +0000777 // If the parser was confused by the condition and we don't have a ')', try to
778 // recover by skipping ahead to a semi and bailing out. If condexp is
779 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000780 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000781 SkipUntil(tok::semi);
782 // Skipping may have stopped if it found the containing ')'. If so, we can
783 // continue parsing the if statement.
784 if (Tok.isNot(tok::r_paren))
785 return true;
786 }
Mike Stump11289f42009-09-09 15:08:12 +0000787
Chris Lattnerc0081db2008-12-12 06:31:07 +0000788 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000789 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000790 return false;
791}
792
793
Chris Lattnerc951dae2006-08-10 04:23:57 +0000794/// ParseIfStatement
795/// if-statement: [C99 6.8.4.1]
796/// 'if' '(' expression ')' statement
797/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000798/// [C++] 'if' '(' condition ')' statement
799/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000800///
John McCall53fa7142010-12-24 02:08:15 +0000801StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000802 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000803
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000804 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000805 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000806
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000807 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000808 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000809 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000810 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000811 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000812
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000813 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
814
Chris Lattner2dd1b722007-08-26 23:08:06 +0000815 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
816 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000817 //
818 // C++ 6.4p3:
819 // A name introduced by a declaration in a condition is in scope from its
820 // point of declaration until the end of the substatements controlled by the
821 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000822 // C++ 3.3.2p4:
823 // Names declared in the for-init-statement, and in the condition of if,
824 // while, for, and switch statements are local to the if, while, for, or
825 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000826 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000827 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000828
Chris Lattnerc951dae2006-08-10 04:23:57 +0000829 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000830 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000831 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000832 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000833 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000834
John McCallb268a282010-08-23 23:25:46 +0000835 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000836
Chris Lattner8fb26252007-08-22 05:28:50 +0000837 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000838 // there is no compound stmt. C90 does not have this clause. We only do this
839 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000840 //
841 // C++ 6.4p1:
842 // The substatement in a selection-statement (each substatement, in the else
843 // form of the if statement) implicitly defines a local scope.
844 //
845 // For C++ we create a scope for the condition and a new scope for
846 // substatements because:
847 // -When the 'then' scope exits, we want the condition declaration to still be
848 // active for the 'else' scope too.
849 // -Sema will detect name clashes by considering declarations of a
850 // 'ControlScope' as part of its direct subscope.
851 // -If we wanted the condition and substatement to be in the same scope, we
852 // would have to notify ParseStatement not to create a new scope. It's
853 // simpler to let it create a new scope.
854 //
Mike Stump11289f42009-09-09 15:08:12 +0000855 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000856 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000857
Chris Lattner5c5808a2007-10-29 05:08:52 +0000858 // Read the 'then' stmt.
859 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000860 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000861
Chris Lattner37e54f42007-08-22 05:16:28 +0000862 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000863 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000864
Chris Lattnerc951dae2006-08-10 04:23:57 +0000865 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000866 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000867 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000868 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000869
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000870 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000871 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000872 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000873
Chris Lattner8fb26252007-08-22 05:28:50 +0000874 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000875 // there is no compound stmt. C90 does not have this clause. We only do
876 // this if the body isn't a compound statement to avoid push/pop in common
877 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000878 //
879 // C++ 6.4p1:
880 // The substatement in a selection-statement (each substatement, in the else
881 // form of the if statement) implicitly defines a local scope.
882 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000883 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000884 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000885
Chris Lattner30f910e2006-10-16 05:52:41 +0000886 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000887
Chris Lattner37e54f42007-08-22 05:16:28 +0000888 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000889 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000890 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000891
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000892 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000893
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000894 // If the condition was invalid, discard the if statement. We could recover
895 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000896 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000897 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000898
Chris Lattner5c5808a2007-10-29 05:08:52 +0000899 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000900 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000901 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000902 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
903 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
904 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000905 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000906 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000907 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000908
Chris Lattner5c5808a2007-10-29 05:08:52 +0000909 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000910 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000911 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000912 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000913 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000914
John McCallb268a282010-08-23 23:25:46 +0000915 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000916 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000917}
918
Chris Lattner9075bd72006-08-10 04:59:57 +0000919/// ParseSwitchStatement
920/// switch-statement:
921/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000922/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000923StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000924 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000925
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000926 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000927 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000928
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000929 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000930 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000931 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000932 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000933 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000934
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000935 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
936
Chris Lattner2dd1b722007-08-26 23:08:06 +0000937 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
938 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000939 //
940 // C++ 6.4p3:
941 // A name introduced by a declaration in a condition is in scope from its
942 // point of declaration until the end of the substatements controlled by the
943 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000944 // C++ 3.3.2p4:
945 // Names declared in the for-init-statement, and in the condition of if,
946 // while, for, and switch statements are local to the if, while, for, or
947 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000948 //
Richard Trieu2c850c02011-04-21 21:44:26 +0000949 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +0000950 if (C99orCXX)
951 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000952 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000953
Chris Lattner9075bd72006-08-10 04:59:57 +0000954 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000955 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000956 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000957 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000958 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000959
John McCalldadc5752010-08-24 06:29:42 +0000960 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000961 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000962
Douglas Gregore60e41a2010-05-06 17:25:47 +0000963 if (Switch.isInvalid()) {
964 // Skip the switch body.
965 // FIXME: This is not optimal recovery, but parsing the body is more
966 // dangerous due to the presence of case and default statements, which
967 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000968 if (Tok.is(tok::l_brace)) {
969 ConsumeBrace();
970 SkipUntil(tok::r_brace, false, false);
971 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000972 SkipUntil(tok::semi);
973 return move(Switch);
974 }
975
Chris Lattner8fb26252007-08-22 05:28:50 +0000976 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000977 // there is no compound stmt. C90 does not have this clause. We only do this
978 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000979 //
980 // C++ 6.4p1:
981 // The substatement in a selection-statement (each substatement, in the else
982 // form of the if statement) implicitly defines a local scope.
983 //
984 // See comments in ParseIfStatement for why we create a scope for the
985 // condition and a new scope for substatement in C++.
986 //
Mike Stump11289f42009-09-09 15:08:12 +0000987 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000988 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000989
Chris Lattner9075bd72006-08-10 04:59:57 +0000990 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000991 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000992
Chris Lattner8fd2d012010-01-24 01:50:29 +0000993 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000994 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000995 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000996
Chris Lattner8fd2d012010-01-24 01:50:29 +0000997 if (Body.isInvalid())
998 // FIXME: Remove the case statement list from the Switch statement.
999 Body = Actions.ActOnNullStmt(Tok.getLocation());
1000
John McCallb268a282010-08-23 23:25:46 +00001001 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001002}
1003
1004/// ParseWhileStatement
1005/// while-statement: [C99 6.8.5.1]
1006/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001007/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +00001008StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001009 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001010
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001011 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001012 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001013 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001014
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001015 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001016 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001017 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001018 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001019 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001020
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001021 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1022
Chris Lattner2dd1b722007-08-26 23:08:06 +00001023 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1024 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001025 //
1026 // C++ 6.4p3:
1027 // A name introduced by a declaration in a condition is in scope from its
1028 // point of declaration until the end of the substatements controlled by the
1029 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001030 // C++ 3.3.2p4:
1031 // Names declared in the for-init-statement, and in the condition of if,
1032 // while, for, and switch statements are local to the if, while, for, or
1033 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001034 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001035 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001036 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001037 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1038 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001039 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001040 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1041 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001042
Chris Lattner9075bd72006-08-10 04:59:57 +00001043 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001044 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001045 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001046 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001047 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001048
John McCallb268a282010-08-23 23:25:46 +00001049 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner8fb26252007-08-22 05:28:50 +00001051 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001052 // there is no compound stmt. C90 does not have this clause. We only do this
1053 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001054 //
1055 // C++ 6.5p2:
1056 // The substatement in an iteration-statement implicitly defines a local scope
1057 // which is entered and exited each time through the loop.
1058 //
1059 // See comments in ParseIfStatement for why we create a scope for the
1060 // condition and a new scope for substatement in C++.
1061 //
Mike Stump11289f42009-09-09 15:08:12 +00001062 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001063 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001064
Chris Lattner9075bd72006-08-10 04:59:57 +00001065 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001066 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001067
Chris Lattner8fb26252007-08-22 05:28:50 +00001068 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001069 InnerScope.Exit();
1070 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001071
John McCall48871652010-08-21 09:40:31 +00001072 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001073 return StmtError();
1074
John McCallb268a282010-08-23 23:25:46 +00001075 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001076}
1077
1078/// ParseDoStatement
1079/// do-statement: [C99 6.8.5.2]
1080/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001081/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001082StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001083 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001084
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001085 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001086 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001087
Chris Lattner2dd1b722007-08-26 23:08:06 +00001088 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1089 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001090 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001091 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001092 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001093 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001094 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001095
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001096 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001097
Chris Lattner8fb26252007-08-22 05:28:50 +00001098 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001099 // there is no compound stmt. C90 does not have this clause. We only do this
1100 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001101 //
1102 // C++ 6.5p2:
1103 // The substatement in an iteration-statement implicitly defines a local scope
1104 // which is entered and exited each time through the loop.
1105 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001106 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001107 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001108 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001109
Chris Lattner9075bd72006-08-10 04:59:57 +00001110 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001111 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001112
Chris Lattner8fb26252007-08-22 05:28:50 +00001113 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001114 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001115
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001116 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001117 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001118 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001119 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001120 SkipUntil(tok::semi, false, true);
1121 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001122 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001123 }
Chris Lattneraf635312006-10-16 06:06:51 +00001124 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001125
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001126 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001127 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001128 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001129 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001130 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001131
Chris Lattner10da53c2008-12-12 06:35:28 +00001132 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +00001133 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001134 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +00001135 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001136 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001137
Sebastian Redlb62406f2008-12-11 19:48:14 +00001138 if (Cond.isInvalid() || Body.isInvalid())
1139 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001140
John McCallb268a282010-08-23 23:25:46 +00001141 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1142 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +00001143}
1144
1145/// ParseForStatement
1146/// for-statement: [C99 6.8.5.3]
1147/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1148/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001149/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1150/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001151/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001152/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1153/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001154///
1155/// [C++] for-init-statement:
1156/// [C++] expression-statement
1157/// [C++] simple-declaration
1158///
Richard Smith02e85f32011-04-14 22:09:26 +00001159/// [C++0x] for-range-declaration:
1160/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1161/// [C++0x] for-range-initializer:
1162/// [C++0x] expression
1163/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001164StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001165 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001166
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001167 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001168 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001169
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001170 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001171 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001172 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001173 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001174 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001175
Chris Lattner934074c2009-04-22 00:54:41 +00001176 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001177
Chris Lattner2dd1b722007-08-26 23:08:06 +00001178 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1179 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001180 //
1181 // C++ 6.4p3:
1182 // A name introduced by a declaration in a condition is in scope from its
1183 // point of declaration until the end of the substatements controlled by the
1184 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001185 // C++ 3.3.2p4:
1186 // Names declared in the for-init-statement, and in the condition of if,
1187 // while, for, and switch statements are local to the if, while, for, or
1188 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001189 // C++ 6.5.3p1:
1190 // Names declared in the for-init-statement are in the same declarative-region
1191 // as those declared in the condition.
1192 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001193 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001194 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001195 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1196 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001197 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001198 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1199
1200 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001201
Chris Lattner04132372006-10-16 06:12:55 +00001202 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001203 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001204
Richard Smith02e85f32011-04-14 22:09:26 +00001205 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001206 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001207 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001208 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001209 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001210 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001211 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001212 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001213
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001214 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001215 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001216 C99orCXXorObjC? Sema::PCC_ForInit
1217 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001218 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001219 }
1220
Chris Lattner9075bd72006-08-10 04:59:57 +00001221 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001222 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001223 // no first part, eat the ';'.
1224 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001225 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001226 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001227 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001228 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001229
John McCall084e83d2011-03-24 11:26:52 +00001230 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001231 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001232
Richard Smith02e85f32011-04-14 22:09:26 +00001233 // In C++0x, "for (T NS:a" might not be a typo for ::
1234 bool MightBeForRangeStmt = getLang().CPlusPlus;
1235 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1236
Chris Lattner49836b42009-04-02 04:16:50 +00001237 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001238 StmtVector Stmts(Actions);
1239 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001240 DeclEnd, attrs, false,
1241 MightBeForRangeStmt ?
1242 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001243 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001244
Richard Smith02e85f32011-04-14 22:09:26 +00001245 if (ForRangeInit.ParsedForRangeDecl()) {
1246 ForRange = true;
1247 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001248 ConsumeToken();
1249 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001250 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001251 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001252 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001253
1254 if (Tok.is(tok::code_completion)) {
1255 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1256 ConsumeCodeCompletionToken();
1257 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001258 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001259 } else {
1260 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001261 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001262 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001263 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001264
John McCall34376a62010-12-04 03:47:34 +00001265 ForEach = isTokIdentifier_in();
1266
Chris Lattnercd68f642007-06-27 01:06:29 +00001267 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001268 if (!Value.isInvalid()) {
1269 if (ForEach)
1270 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1271 else
1272 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1273 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001274
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001275 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001276 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001277 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001278 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001279
1280 if (Tok.is(tok::code_completion)) {
1281 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1282 ConsumeCodeCompletionToken();
1283 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001284 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001285 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001286 if (!Value.isInvalid()) {
1287 Diag(Tok, diag::err_expected_semi_for);
1288 } else {
1289 // Skip until semicolon or rparen, don't consume it.
1290 SkipUntil(tok::r_paren, true, true);
1291 if (Tok.is(tok::semi))
1292 ConsumeToken();
1293 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001294 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001295 }
Richard Smith02e85f32011-04-14 22:09:26 +00001296 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001297 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001298 // Parse the second part of the for specifier.
1299 if (Tok.is(tok::semi)) { // for (...;;
1300 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001301 } else if (Tok.is(tok::r_paren)) {
1302 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001303 } else {
John McCalldadc5752010-08-24 06:29:42 +00001304 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001305 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001306 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1307 else {
1308 Second = ParseExpression();
1309 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001310 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001311 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001312 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001313 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001314 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001315 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001316
Douglas Gregor230a7e62011-02-17 03:38:46 +00001317 if (Tok.isNot(tok::semi)) {
1318 if (!SecondPartIsInvalid || SecondVar)
1319 Diag(Tok, diag::err_expected_semi_for);
1320 else
1321 // Skip until semicolon or rparen, don't consume it.
1322 SkipUntil(tok::r_paren, true, true);
1323 }
1324
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001325 if (Tok.is(tok::semi)) {
1326 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001327 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001328
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001329 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001330 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001331 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001332 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001333 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001334 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001335 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001336 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001337
Richard Smith02e85f32011-04-14 22:09:26 +00001338 // We need to perform most of the semantic analysis for a C++0x for-range
1339 // statememt before parsing the body, in order to be able to deduce the type
1340 // of an auto-typed loop variable.
1341 StmtResult ForRangeStmt;
1342 if (ForRange)
1343 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1344 FirstPart.take(),
1345 ForRangeInit.ColonLoc,
1346 ForRangeInit.RangeExpr.get(),
1347 RParenLoc);
1348
Chris Lattner8fb26252007-08-22 05:28:50 +00001349 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001350 // there is no compound stmt. C90 does not have this clause. We only do this
1351 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001352 //
1353 // C++ 6.5p2:
1354 // The substatement in an iteration-statement implicitly defines a local scope
1355 // which is entered and exited each time through the loop.
1356 //
1357 // See comments in ParseIfStatement for why we create a scope for
1358 // for-init-statement/condition and a new scope for substatement in C++.
1359 //
Mike Stump11289f42009-09-09 15:08:12 +00001360 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001361 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001362
Chris Lattner9075bd72006-08-10 04:59:57 +00001363 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001364 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001365
Chris Lattner8fb26252007-08-22 05:28:50 +00001366 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001367 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001368
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001369 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001370 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001371
1372 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001373 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001374
Richard Smith02e85f32011-04-14 22:09:26 +00001375 if (ForEach)
1376 // FIXME: It isn't clear how to communicate the late destruction of
1377 // C++ temporaries used to create the collection.
1378 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1379 FirstPart.take(),
1380 Collection.take(), RParenLoc,
1381 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001382
Richard Smith02e85f32011-04-14 22:09:26 +00001383 if (ForRange)
1384 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1385
1386 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1387 SecondVar, ThirdPart, RParenLoc, Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001388}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001389
Chris Lattner503fadc2006-08-10 05:45:44 +00001390/// ParseGotoStatement
1391/// jump-statement:
1392/// 'goto' identifier ';'
1393/// [GNU] 'goto' '*' expression ';'
1394///
1395/// Note: this lets the caller parse the end ';'.
1396///
John McCall53fa7142010-12-24 02:08:15 +00001397StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001398 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001399
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001400 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001401 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001402
John McCalldadc5752010-08-24 06:29:42 +00001403 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001404 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001405 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1406 Tok.getLocation());
1407 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001408 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001409 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001410 // GNU indirect goto extension.
1411 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001412 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001413 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001414 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001415 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001416 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001417 }
John McCallb268a282010-08-23 23:25:46 +00001418 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001419 } else {
1420 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001421 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001422 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001423
Sebastian Redlb62406f2008-12-11 19:48:14 +00001424 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001425}
1426
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001427/// ParseContinueStatement
1428/// jump-statement:
1429/// 'continue' ';'
1430///
1431/// Note: this lets the caller parse the end ';'.
1432///
John McCall53fa7142010-12-24 02:08:15 +00001433StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001434 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001435
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001436 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001437 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001438}
1439
1440/// ParseBreakStatement
1441/// jump-statement:
1442/// 'break' ';'
1443///
1444/// Note: this lets the caller parse the end ';'.
1445///
John McCall53fa7142010-12-24 02:08:15 +00001446StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001447 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001448
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001449 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001450 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001451}
1452
Chris Lattner503fadc2006-08-10 05:45:44 +00001453/// ParseReturnStatement
1454/// jump-statement:
1455/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001456StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001457 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001458
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001459 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001460 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001461
John McCalldadc5752010-08-24 06:29:42 +00001462 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001463 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001464 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001465 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001466 ConsumeCodeCompletionToken();
1467 SkipUntil(tok::semi, false, true);
1468 return StmtError();
1469 }
1470
Douglas Gregore9e27d92011-03-11 23:10:44 +00001471 // FIXME: This is a hack to allow something like C++0x's generalized
1472 // initializer lists, but only enough of this feature to allow Clang to
1473 // parse libstdc++ 4.5's headers.
1474 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1475 R = ParseInitializer();
1476 if (R.isUsable() && !getLang().CPlusPlus0x)
1477 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1478 << R.get()->getSourceRange();
1479 } else
1480 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001481 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001482 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001483 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001484 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001485 }
John McCallb268a282010-08-23 23:25:46 +00001486 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001487}
Chris Lattner0116c472006-08-15 06:03:28 +00001488
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001489/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1490/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001491StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1492 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001493 if (Tok.is(tok::l_brace)) {
1494 unsigned short savedBraceCount = BraceCount;
1495 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001496 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001497 ConsumeAnyToken();
1498 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001499 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001500 // From the MS website: If used without braces, the __asm keyword means
1501 // that the rest of the line is an assembly-language statement.
1502 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001503 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001504 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001505 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001506 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001507 ConsumeAnyToken();
1508 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001509 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1510 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001511 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001512 }
Mike Stump6da5d752009-12-11 00:04:56 +00001513 Token t;
1514 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001515 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001516 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001517 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001518 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001519 ExprVector Constraints(Actions);
1520 ExprVector Exprs(Actions);
1521 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001522 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001523 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001524 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001525 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001526}
1527
Chris Lattner0116c472006-08-15 06:03:28 +00001528/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001529/// asm-statement:
1530/// gnu-asm-statement
1531/// ms-asm-statement
1532///
1533/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001534/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1535///
1536/// [GNU] asm-argument:
1537/// asm-string-literal
1538/// asm-string-literal ':' asm-operands[opt]
1539/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1540/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1541/// ':' asm-clobbers
1542///
1543/// [GNU] asm-clobbers:
1544/// asm-string-literal
1545/// asm-clobbers ',' asm-string-literal
1546///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001547/// [MS] ms-asm-statement:
1548/// '__asm' assembly-instruction ';'[opt]
1549/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1550///
1551/// [MS] assembly-instruction-list:
1552/// assembly-instruction ';'[opt]
1553/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1554///
John McCalldadc5752010-08-24 06:29:42 +00001555StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001556 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001557 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001558
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001559 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001560 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001561 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001562 }
John McCall084e83d2011-03-24 11:26:52 +00001563 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001564 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001565 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001566
Chris Lattner0116c472006-08-15 06:03:28 +00001567 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001568 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001569 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001570 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001571 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001572
Chris Lattner0116c472006-08-15 06:03:28 +00001573 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001574 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001575 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001576 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001577 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001578 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001579 }
Chris Lattner04132372006-10-16 06:12:55 +00001580 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001581
John McCalldadc5752010-08-24 06:29:42 +00001582 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001583 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001584 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001585
Anders Carlsson9a020f92010-01-30 22:25:16 +00001586 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001587 ExprVector Constraints(Actions);
1588 ExprVector Exprs(Actions);
1589 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001590
Anders Carlsson19fe1162008-02-05 23:03:50 +00001591 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001592 // We have a simple asm expression like 'asm("foo")'.
1593 SourceLocation RParenLoc = ConsumeParen();
1594 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1595 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1596 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001597 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001598 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001599 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001600
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001601 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001602 bool AteExtraColon = false;
1603 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1604 // In C++ mode, parse "::" like ": :".
1605 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001606 ConsumeToken();
1607
Chris Lattner15768502009-12-20 23:08:04 +00001608 if (!AteExtraColon &&
1609 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001610 return StmtError();
1611 }
Chris Lattner15768502009-12-20 23:08:04 +00001612
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001613 unsigned NumOutputs = Names.size();
1614
1615 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001616 if (AteExtraColon ||
1617 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1618 // In C++ mode, parse "::" like ": :".
1619 if (AteExtraColon)
1620 AteExtraColon = false;
1621 else {
1622 AteExtraColon = Tok.is(tok::coloncolon);
1623 ConsumeToken();
1624 }
1625
1626 if (!AteExtraColon &&
1627 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001628 return StmtError();
1629 }
1630
1631 assert(Names.size() == Constraints.size() &&
1632 Constraints.size() == Exprs.size() &&
1633 "Input operand size mismatch!");
1634
1635 unsigned NumInputs = Names.size() - NumOutputs;
1636
1637 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001638 if (AteExtraColon || Tok.is(tok::colon)) {
1639 if (!AteExtraColon)
1640 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001641
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001642 // Parse the asm-string list for clobbers if present.
1643 if (Tok.isNot(tok::r_paren)) {
1644 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001645 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001646
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001647 if (Clobber.isInvalid())
1648 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001649
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001650 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001651
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001652 if (Tok.isNot(tok::comma)) break;
1653 ConsumeToken();
1654 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001655 }
1656 }
1657
1658 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1659 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001660 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001661 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001662 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001663 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001664}
1665
1666/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001667/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001668///
1669/// [GNU] asm-operands:
1670/// asm-operand
1671/// asm-operands ',' asm-operand
1672///
1673/// [GNU] asm-operand:
1674/// asm-string-literal '(' expression ')'
1675/// '[' identifier ']' asm-string-literal '(' expression ')'
1676///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001677//
1678// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001679bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1680 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1681 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001682 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001683 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001684 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001685
1686 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001687 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001688 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001689 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001690
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001691 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001692 Diag(Tok, diag::err_expected_ident);
1693 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001694 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001695 }
Mike Stump11289f42009-09-09 15:08:12 +00001696
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001697 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001698 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001699
Anders Carlsson9a020f92010-01-30 22:25:16 +00001700 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001701 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001702 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001703 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001704
John McCalldadc5752010-08-24 06:29:42 +00001705 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001706 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001707 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001708 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001709 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001710 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001711
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001712 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001713 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001714 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001715 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001716 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001717
Chris Lattner0116c472006-08-15 06:03:28 +00001718 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001719 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001720 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001721 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001722 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001723 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001724 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001725 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001726 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001727 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001728 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001729 ConsumeToken();
1730 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001731
1732 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001733}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001734
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001735Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001736 assert(Tok.is(tok::l_brace));
1737 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001738
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001739 if (PP.isCodeCompletionEnabled()) {
1740 if (trySkippingFunctionBodyForCodeCompletion()) {
1741 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001742 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001743 }
1744 }
1745
John McCallfaf5fb42010-08-26 23:41:50 +00001746 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1747 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001748
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001749 // Do not enter a scope for the brace, as the arguments are in the same scope
1750 // (the function body) as the body itself. Instead, just read the statement
1751 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001752 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001753
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001754 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001755 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001756 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001757 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001758
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001759 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001760 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001761}
Sebastian Redlb219c902008-12-21 16:41:36 +00001762
Sebastian Redla7b98a72009-04-26 20:35:05 +00001763/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1764///
1765/// function-try-block:
1766/// 'try' ctor-initializer[opt] compound-statement handler-seq
1767///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001768Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001769 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1770 SourceLocation TryLoc = ConsumeToken();
1771
John McCallfaf5fb42010-08-26 23:41:50 +00001772 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1773 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001774
1775 // Constructor initializer list?
1776 if (Tok.is(tok::colon))
1777 ParseConstructorInitializer(Decl);
1778
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001779 if (PP.isCodeCompletionEnabled()) {
1780 if (trySkippingFunctionBodyForCodeCompletion()) {
1781 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001782 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001783 }
1784 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001785
Sebastian Redld98ecd62009-04-26 21:08:36 +00001786 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001787 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001788 // If we failed to parse the try-catch, we just give the function an empty
1789 // compound statement as the body.
1790 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001791 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001792 MultiStmtArg(Actions), false);
1793
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001794 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001795 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001796}
1797
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001798bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001799 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001800 assert(PP.isCodeCompletionEnabled() &&
1801 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001802
1803 // We're in code-completion mode. Skip parsing for all function bodies unless
1804 // the body contains the code-completion point.
1805 TentativeParsingAction PA(*this);
1806 ConsumeBrace();
1807 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1808 /*StopAtCodeCompletion=*/true)) {
1809 PA.Commit();
1810 return true;
1811 }
1812
1813 PA.Revert();
1814 return false;
1815}
1816
Sebastian Redlb219c902008-12-21 16:41:36 +00001817/// ParseCXXTryBlock - Parse a C++ try-block.
1818///
1819/// try-block:
1820/// 'try' compound-statement handler-seq
1821///
John McCall53fa7142010-12-24 02:08:15 +00001822StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001823 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001824
Sebastian Redlb219c902008-12-21 16:41:36 +00001825 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1826
1827 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001828 return ParseCXXTryBlockCommon(TryLoc);
1829}
1830
1831/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1832/// function-try-block.
1833///
1834/// try-block:
1835/// 'try' compound-statement handler-seq
1836///
1837/// function-try-block:
1838/// 'try' ctor-initializer[opt] compound-statement handler-seq
1839///
1840/// handler-seq:
1841/// handler handler-seq[opt]
1842///
John McCalldadc5752010-08-24 06:29:42 +00001843StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001844 if (Tok.isNot(tok::l_brace))
1845 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001846 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001847 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001848 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001849 if (TryBlock.isInvalid())
1850 return move(TryBlock);
1851
1852 StmtVector Handlers(Actions);
John McCall53fa7142010-12-24 02:08:15 +00001853 MaybeParseCXX0XAttributes(attrs);
1854 ProhibitAttributes(attrs);
1855
Sebastian Redlb219c902008-12-21 16:41:36 +00001856 if (Tok.isNot(tok::kw_catch))
1857 return StmtError(Diag(Tok, diag::err_expected_catch));
1858 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001859 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001860 if (!Handler.isInvalid())
1861 Handlers.push_back(Handler.release());
1862 }
1863 // Don't bother creating the full statement if we don't have any usable
1864 // handlers.
1865 if (Handlers.empty())
1866 return StmtError();
1867
John McCallb268a282010-08-23 23:25:46 +00001868 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001869}
1870
1871/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1872///
1873/// handler:
1874/// 'catch' '(' exception-declaration ')' compound-statement
1875///
1876/// exception-declaration:
1877/// type-specifier-seq declarator
1878/// type-specifier-seq abstract-declarator
1879/// type-specifier-seq
1880/// '...'
1881///
John McCalldadc5752010-08-24 06:29:42 +00001882StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001883 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1884
1885 SourceLocation CatchLoc = ConsumeToken();
1886
1887 SourceLocation LParenLoc = Tok.getLocation();
1888 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1889 return StmtError();
1890
1891 // C++ 3.3.2p3:
1892 // The name in a catch exception-declaration is local to the handler and
1893 // shall not be redeclared in the outermost block of the handler.
1894 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1895
1896 // exception-declaration is equivalent to '...' or a parameter-declaration
1897 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001898 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001899 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001900 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00001901 if (ParseCXXTypeSpecifierSeq(DS))
1902 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001903 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1904 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001905 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001906 } else
1907 ConsumeToken();
1908
1909 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1910 return StmtError();
1911
1912 if (Tok.isNot(tok::l_brace))
1913 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1914
Alexis Hunt96d5c762009-11-21 08:43:09 +00001915 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001916 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001917 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001918 if (Block.isInvalid())
1919 return move(Block);
1920
John McCallb268a282010-08-23 23:25:46 +00001921 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001922}