blob: a324bdc0455154038c01c224625ea435a8ddec31 [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
116 if (!getLang().CPlusPlus) {
117 // 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
150 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
198 // In C++, we might also have a functional-style cast.
199 // FIXME: Implement this!
200 break;
201
202 case Sema::NC_Expression:
203 ConsumeToken(); // the identifier
204 return ParseExprStatement(attrs, Classification.getExpression());
205
206 case Sema::NC_TypeTemplate:
207 case Sema::NC_FunctionTemplate: {
208 ConsumeToken(); // the identifier
209 UnqualifiedId Id;
210 Id.setIdentifier(Name, NameLoc);
211 if (AnnotateTemplateIdToken(
212 TemplateTy::make(Classification.getTemplateName()),
213 Classification.getTemplateNameKind(),
214 SS, Id)) {
215 // Handle errors here by skipping up to the next semicolon or '}', and
216 // eat the semicolon if that's what stopped us.
217 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
218 if (Tok.is(tok::semi))
219 ConsumeToken();
220 return StmtError();
221 }
222
223 // We've annotated a template-id, so try again now.
224 goto Retry;
225 }
226
227 case Sema::NC_NestedNameSpecifier:
228 // FIXME: Implement this!
229 break;
230 }
231 }
232
233 // Fall through
234 }
235
Chris Lattner803802d2009-03-24 17:04:48 +0000236 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000237 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000238 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000239 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000240 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000241 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000242 }
243
244 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000245 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000246 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000247 }
Mike Stump11289f42009-09-09 15:08:12 +0000248
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000249 return ParseExprStatement(attrs, ExprResult());
Chris Lattner803802d2009-03-24 17:04:48 +0000250 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000251
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000252 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000253 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000254 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000255 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000256
Chris Lattner9075bd72006-08-10 04:59:57 +0000257 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000258 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000259 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
260 bool LeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
261 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacro);
262 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000263
Chris Lattner9075bd72006-08-10 04:59:57 +0000264 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall53fa7142010-12-24 02:08:15 +0000265 return ParseIfStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000266 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall53fa7142010-12-24 02:08:15 +0000267 return ParseSwitchStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000268
Chris Lattner9075bd72006-08-10 04:59:57 +0000269 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall53fa7142010-12-24 02:08:15 +0000270 return ParseWhileStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000271 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000272 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000273 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000274 break;
275 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall53fa7142010-12-24 02:08:15 +0000276 return ParseForStatement(attrs);
Chris Lattner503fadc2006-08-10 05:45:44 +0000277
278 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000279 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000280 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000281 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000282 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000283 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000284 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000285 break;
286 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000287 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000288 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000289 break;
290 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000291 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000292 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000293 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000294
Sebastian Redlb219c902008-12-21 16:41:36 +0000295 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000296 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000297 bool msAsm = false;
298 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000299 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000300 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000301 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000302 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000303 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000304
Sebastian Redlb219c902008-12-21 16:41:36 +0000305 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000306 return ParseCXXTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000307 }
308
Chris Lattner503fadc2006-08-10 05:45:44 +0000309 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000310 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000311 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000312 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000313 // If the result was valid, then we do want to diagnose this. Use
314 // ExpectAndConsume to emit the diagnostic, even though we know it won't
315 // succeed.
316 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000317 // Skip until we see a } or ;, but don't eat it.
318 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000319 }
Mike Stump11289f42009-09-09 15:08:12 +0000320
Sebastian Redl042ad952008-12-11 19:30:53 +0000321 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000322}
323
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000324/// \brief Parse an expression statement.
325StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs,
326 ExprResult Primary) {
327 // If a case keyword is missing, this is where it should be inserted.
328 Token OldToken = Tok;
329
330 // FIXME: Use the attributes
331 // expression[opt] ';'
332 ExprResult Expr(ParseExpression(Primary));
333 if (Expr.isInvalid()) {
334 // If the expression is invalid, skip ahead to the next semicolon or '}'.
335 // Not doing this opens us up to the possibility of infinite loops if
336 // ParseExpression does not consume any tokens.
337 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
338 if (Tok.is(tok::semi))
339 ConsumeToken();
340 return StmtError();
341 }
342
343 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
344 Actions.CheckCaseExpression(Expr.get())) {
345 // If a constant expression is followed by a colon inside a switch block,
346 // suggest a missing case keyword.
347 Diag(OldToken, diag::err_expected_case_before_expression)
348 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
349
350 // Recover parsing as a case statement.
351 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
352 }
353
354 // Otherwise, eat the semicolon.
355 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
356 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
357
358}
359
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000360/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000361///
362/// labeled-statement:
363/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000364/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000365///
John McCall53fa7142010-12-24 02:08:15 +0000366StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000367 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
368 "Not an identifier!");
369
370 Token IdentTok = Tok; // Save the whole token.
371 ConsumeToken(); // eat the identifier.
372
373 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000374
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000375 // identifier ':' statement
376 SourceLocation ColonLoc = ConsumeToken();
377
378 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000379 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000380
John McCalldadc5752010-08-24 06:29:42 +0000381 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000382
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000383 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000384 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000385 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000386
387 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
388 IdentTok.getLocation());
389 if (AttributeList *Attrs = attrs.getList())
390 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
391
392 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
393 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000394}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000395
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000396/// ParseCaseStatement
397/// labeled-statement:
398/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000399/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000400///
Richard Trieu2c850c02011-04-21 21:44:26 +0000401StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
402 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000403 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000404 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner34a22092009-03-04 04:23:07 +0000406 // It is very very common for code to contain many case statements recursively
407 // nested, as in (but usually without indentation):
408 // case 1:
409 // case 2:
410 // case 3:
411 // case 4:
412 // case 5: etc.
413 //
414 // Parsing this naively works, but is both inefficient and can cause us to run
415 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000416 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000417 // but all the grossness is constrained to ParseCaseStatement (and some
418 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattner34a22092009-03-04 04:23:07 +0000420 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
421 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000422 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner34a22092009-03-04 04:23:07 +0000424 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
425 // gets updated each time a new case is parsed, and whose body is unset so
426 // far. When parsing 'case 4', this is the 'case 3' node.
427 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner34a22092009-03-04 04:23:07 +0000429 // While we have case statements, eat and stack them.
430 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000431 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
432 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000433
Douglas Gregord328d572009-09-21 18:10:23 +0000434 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000435 Actions.CodeCompleteCase(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000436 ConsumeCodeCompletionToken();
Douglas Gregord328d572009-09-21 18:10:23 +0000437 }
438
Chris Lattner125c0ee2009-12-10 00:38:54 +0000439 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
440 /// Disable this form of error recovery while we're parsing the case
441 /// expression.
442 ColonProtectionRAIIObject ColonProtection(*this);
443
Richard Trieu2c850c02011-04-21 21:44:26 +0000444 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
445 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000446 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000447 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000448 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000449 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000450
Chris Lattner34a22092009-03-04 04:23:07 +0000451 // GNU case range extension.
452 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000453 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000454 if (Tok.is(tok::ellipsis)) {
455 Diag(Tok, diag::ext_gnu_case_range);
456 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000457
Chris Lattner34a22092009-03-04 04:23:07 +0000458 RHS = ParseConstantExpression();
459 if (RHS.isInvalid()) {
460 SkipUntil(tok::colon);
461 return StmtError();
462 }
463 }
Chris Lattner125c0ee2009-12-10 00:38:54 +0000464
465 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000466
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000467 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000468 if (Tok.is(tok::colon)) {
469 ColonLoc = ConsumeToken();
470
471 // Treat "case blah;" as a typo for "case blah:".
472 } else if (Tok.is(tok::semi)) {
473 ColonLoc = ConsumeToken();
474 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
475 << FixItHint::CreateReplacement(ColonLoc, ":");
476 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000477 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
478 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
479 << FixItHint::CreateInsertion(ExpectedLoc, ":");
480 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000481 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000482
John McCalldadc5752010-08-24 06:29:42 +0000483 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000484 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
485 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000486
Chris Lattner34a22092009-03-04 04:23:07 +0000487 // If we had a sema error parsing this case, then just ignore it and
488 // continue parsing the sub-stmt.
489 if (Case.isInvalid()) {
490 if (TopLevelCase.isInvalid()) // No parsed case stmts.
491 return ParseStatement();
492 // Otherwise, just don't add it as a nested case.
493 } else {
494 // If this is the first case statement we parsed, it becomes TopLevelCase.
495 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000496 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000497 if (TopLevelCase.isInvalid())
498 TopLevelCase = move(Case);
499 else
John McCallb268a282010-08-23 23:25:46 +0000500 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000501 DeepestParsedCaseStmt = NextDeepest;
502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattner34a22092009-03-04 04:23:07 +0000504 // Handle all case statements.
505 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattner34a22092009-03-04 04:23:07 +0000507 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattner34a22092009-03-04 04:23:07 +0000509 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000510 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattner34a22092009-03-04 04:23:07 +0000512 if (Tok.isNot(tok::r_brace)) {
513 SubStmt = ParseStatement();
514 } else {
515 // Nicely diagnose the common error "switch (X) { case 4: }", which is
516 // not valid.
517 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000518 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000519 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chris Lattner34a22092009-03-04 04:23:07 +0000522 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000523 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000524 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000525
Chris Lattner34a22092009-03-04 04:23:07 +0000526 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000527 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000528
Chris Lattner34a22092009-03-04 04:23:07 +0000529 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000530 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000531}
532
533/// ParseDefaultStatement
534/// labeled-statement:
535/// 'default' ':' statement
536/// Note that this does not parse the 'statement' at the end.
537///
John McCall53fa7142010-12-24 02:08:15 +0000538StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000539 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000540
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000541 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000542 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000543
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000544 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000545 if (Tok.is(tok::colon)) {
546 ColonLoc = ConsumeToken();
547
548 // Treat "default;" as a typo for "default:".
549 } else if (Tok.is(tok::semi)) {
550 ColonLoc = ConsumeToken();
551 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
552 << FixItHint::CreateReplacement(ColonLoc, ":");
553 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000554 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
555 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
556 << FixItHint::CreateInsertion(ExpectedLoc, ":");
557 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000558 }
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000559
Chris Lattner30f910e2006-10-16 05:52:41 +0000560 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000561 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000562 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000563 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000564 }
565
John McCalldadc5752010-08-24 06:29:42 +0000566 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000567 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000568 return StmtError();
569
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000570 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000571 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000572}
573
574
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000575/// ParseCompoundStatement - Parse a "{}" block.
576///
577/// compound-statement: [C99 6.8.2]
578/// { block-item-list[opt] }
579/// [GNU] { label-declarations block-item-list } [TODO]
580///
581/// block-item-list:
582/// block-item
583/// block-item-list block-item
584///
585/// block-item:
586/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000587/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000588/// statement
589/// [OMP] openmp-directive [TODO]
590///
591/// [GNU] label-declarations:
592/// [GNU] label-declaration
593/// [GNU] label-declarations label-declaration
594///
595/// [GNU] label-declaration:
596/// [GNU] '__label__' identifier-list ';'
597///
598/// [OMP] openmp-directive: [TODO]
599/// [OMP] barrier-directive
600/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000601///
John McCall53fa7142010-12-24 02:08:15 +0000602StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000603 bool isStmtExpr) {
604 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000605
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000606 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000607
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000608 // Enter a scope to hold everything within the compound stmt. Compound
609 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000610 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000611
612 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000613 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000614}
615
616
617/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000618/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000619/// consume the '}' at the end of the block. It does not manipulate the scope
620/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000621StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000622 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000623 Tok.getLocation(),
624 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000625 InMessageExpressionRAIIObject InMessage(*this, false);
626
Chris Lattnerf2978802007-01-21 06:52:16 +0000627 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
628
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000629 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000630
Chris Lattner43e7f312011-02-18 02:08:43 +0000631 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
632 // only allowed at the start of a compound stmt regardless of the language.
633 while (Tok.is(tok::kw___label__)) {
634 SourceLocation LabelLoc = ConsumeToken();
635 Diag(LabelLoc, diag::ext_gnu_local_label);
636
637 llvm::SmallVector<Decl *, 8> DeclsInGroup;
638 while (1) {
639 if (Tok.isNot(tok::identifier)) {
640 Diag(Tok, diag::err_expected_ident);
641 break;
642 }
643
644 IdentifierInfo *II = Tok.getIdentifierInfo();
645 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000646 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner43e7f312011-02-18 02:08:43 +0000647
648 if (!Tok.is(tok::comma))
649 break;
650 ConsumeToken();
651 }
652
John McCall084e83d2011-03-24 11:26:52 +0000653 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000654 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
655 DeclsInGroup.data(), DeclsInGroup.size());
656 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
657
658 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
659 if (R.isUsable())
660 Stmts.push_back(R.release());
661 }
662
663 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000664 if (Tok.is(tok::annot_pragma_unused)) {
665 HandlePragmaUnused();
666 continue;
667 }
668
John McCalldadc5752010-08-24 06:29:42 +0000669 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000670 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000671 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000672 } else {
673 // __extension__ can start declarations and it can also be a unary
674 // operator for expressions. Consume multiple __extension__ markers here
675 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000676 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000677 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000678 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000679 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000680
John McCall084e83d2011-03-24 11:26:52 +0000681 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000682 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000683
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000684 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000685 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000686 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000687 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000688 ExtensionRAIIObject O(Diags);
689
Chris Lattner49836b42009-04-02 04:16:50 +0000690 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000691 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
692 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000693 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000694 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000695 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000696 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000697 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000698
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000699 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000700 SkipUntil(tok::semi);
701 continue;
702 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000703
Alexis Hunt96d5c762009-11-21 08:43:09 +0000704 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000705 // Eat the semicolon at the end of stmt and convert the expr into a
706 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000707 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000708 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000709 }
710 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000711
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000712 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000713 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000714 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000715
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000716 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000717 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000718 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner00739622010-09-01 15:49:26 +0000719 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000720 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000721 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000722
Chris Lattner04132372006-10-16 06:12:55 +0000723 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000724 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000725 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000726}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000727
Chris Lattnerc0081db2008-12-12 06:31:07 +0000728/// ParseParenExprOrCondition:
729/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000730/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000731///
732/// This function parses and performs error recovery on the specified condition
733/// or expression (depending on whether we're in C++ or C mode). This function
734/// goes out of its way to recover well. It returns true if there was a parser
735/// error (the right paren couldn't be found), which indicates that the caller
736/// should try to recover harder. It returns false if the condition is
737/// successfully parsed. Note that a successful parse can still have semantic
738/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000739bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000740 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000741 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000742 bool ConvertToBoolean) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000743 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000744 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000745 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000746 else {
747 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000748 DeclResult = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000749
750 // If required, convert to a boolean value.
751 if (!ExprResult.isInvalid() && ConvertToBoolean)
752 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000753 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattnerc0081db2008-12-12 06:31:07 +0000756 // If the parser was confused by the condition and we don't have a ')', try to
757 // recover by skipping ahead to a semi and bailing out. If condexp is
758 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000759 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000760 SkipUntil(tok::semi);
761 // Skipping may have stopped if it found the containing ')'. If so, we can
762 // continue parsing the if statement.
763 if (Tok.isNot(tok::r_paren))
764 return true;
765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattnerc0081db2008-12-12 06:31:07 +0000767 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000768 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000769 return false;
770}
771
772
Chris Lattnerc951dae2006-08-10 04:23:57 +0000773/// ParseIfStatement
774/// if-statement: [C99 6.8.4.1]
775/// 'if' '(' expression ')' statement
776/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000777/// [C++] 'if' '(' condition ')' statement
778/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000779///
John McCall53fa7142010-12-24 02:08:15 +0000780StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000781 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000782
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000783 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000784 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000785
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000786 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000787 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000788 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000789 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000790 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000791
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000792 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
793
Chris Lattner2dd1b722007-08-26 23:08:06 +0000794 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
795 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000796 //
797 // C++ 6.4p3:
798 // A name introduced by a declaration in a condition is in scope from its
799 // point of declaration until the end of the substatements controlled by the
800 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000801 // C++ 3.3.2p4:
802 // Names declared in the for-init-statement, and in the condition of if,
803 // while, for, and switch statements are local to the if, while, for, or
804 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000805 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000806 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000807
Chris Lattnerc951dae2006-08-10 04:23:57 +0000808 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000809 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000810 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000811 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000812 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000813
John McCallb268a282010-08-23 23:25:46 +0000814 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattner8fb26252007-08-22 05:28:50 +0000816 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000817 // there is no compound stmt. C90 does not have this clause. We only do this
818 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000819 //
820 // C++ 6.4p1:
821 // The substatement in a selection-statement (each substatement, in the else
822 // form of the if statement) implicitly defines a local scope.
823 //
824 // For C++ we create a scope for the condition and a new scope for
825 // substatements because:
826 // -When the 'then' scope exits, we want the condition declaration to still be
827 // active for the 'else' scope too.
828 // -Sema will detect name clashes by considering declarations of a
829 // 'ControlScope' as part of its direct subscope.
830 // -If we wanted the condition and substatement to be in the same scope, we
831 // would have to notify ParseStatement not to create a new scope. It's
832 // simpler to let it create a new scope.
833 //
Mike Stump11289f42009-09-09 15:08:12 +0000834 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000835 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000836
Chris Lattner5c5808a2007-10-29 05:08:52 +0000837 // Read the 'then' stmt.
838 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000839 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000840
Chris Lattner37e54f42007-08-22 05:16:28 +0000841 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000842 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000843
Chris Lattnerc951dae2006-08-10 04:23:57 +0000844 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000845 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000846 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000847 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000848
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000849 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000850 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000851 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000852
Chris Lattner8fb26252007-08-22 05:28:50 +0000853 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000854 // there is no compound stmt. C90 does not have this clause. We only do
855 // this if the body isn't a compound statement to avoid push/pop in common
856 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000857 //
858 // C++ 6.4p1:
859 // The substatement in a selection-statement (each substatement, in the else
860 // form of the if statement) implicitly defines a local scope.
861 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000862 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000863 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000864
Chris Lattner30f910e2006-10-16 05:52:41 +0000865 ElseStmt = ParseStatement();
Chris Lattnerdf742642010-04-12 06:12:50 +0000866
Chris Lattner37e54f42007-08-22 05:16:28 +0000867 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000868 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000869 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000870
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000871 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000873 // If the condition was invalid, discard the if statement. We could recover
874 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000875 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000876 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000877
Chris Lattner5c5808a2007-10-29 05:08:52 +0000878 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000879 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000880 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000881 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
882 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
883 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000884 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000885 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000886 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000887
Chris Lattner5c5808a2007-10-29 05:08:52 +0000888 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000889 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000890 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000891 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000892 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000893
John McCallb268a282010-08-23 23:25:46 +0000894 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000895 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000896}
897
Chris Lattner9075bd72006-08-10 04:59:57 +0000898/// ParseSwitchStatement
899/// switch-statement:
900/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000901/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000902StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000903 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000904
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000905 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000906 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000907
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000908 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000909 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000910 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000911 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000912 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000913
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000914 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
915
Chris Lattner2dd1b722007-08-26 23:08:06 +0000916 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
917 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000918 //
919 // C++ 6.4p3:
920 // A name introduced by a declaration in a condition is in scope from its
921 // point of declaration until the end of the substatements controlled by the
922 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000923 // C++ 3.3.2p4:
924 // Names declared in the for-init-statement, and in the condition of if,
925 // while, for, and switch statements are local to the if, while, for, or
926 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000927 //
Richard Trieu2c850c02011-04-21 21:44:26 +0000928 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +0000929 if (C99orCXX)
930 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000931 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000932
Chris Lattner9075bd72006-08-10 04:59:57 +0000933 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000934 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +0000935 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +0000936 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000937 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000938
John McCalldadc5752010-08-24 06:29:42 +0000939 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +0000940 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000941
Douglas Gregore60e41a2010-05-06 17:25:47 +0000942 if (Switch.isInvalid()) {
943 // Skip the switch body.
944 // FIXME: This is not optimal recovery, but parsing the body is more
945 // dangerous due to the presence of case and default statements, which
946 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +0000947 if (Tok.is(tok::l_brace)) {
948 ConsumeBrace();
949 SkipUntil(tok::r_brace, false, false);
950 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +0000951 SkipUntil(tok::semi);
952 return move(Switch);
953 }
954
Chris Lattner8fb26252007-08-22 05:28:50 +0000955 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000956 // there is no compound stmt. C90 does not have this clause. We only do this
957 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000958 //
959 // C++ 6.4p1:
960 // The substatement in a selection-statement (each substatement, in the else
961 // form of the if statement) implicitly defines a local scope.
962 //
963 // See comments in ParseIfStatement for why we create a scope for the
964 // condition and a new scope for substatement in C++.
965 //
Mike Stump11289f42009-09-09 15:08:12 +0000966 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000967 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000968
Chris Lattner9075bd72006-08-10 04:59:57 +0000969 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +0000970 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000971
Chris Lattner8fd2d012010-01-24 01:50:29 +0000972 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000973 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000974 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000975
Chris Lattner8fd2d012010-01-24 01:50:29 +0000976 if (Body.isInvalid())
977 // FIXME: Remove the case statement list from the Switch statement.
978 Body = Actions.ActOnNullStmt(Tok.getLocation());
979
John McCallb268a282010-08-23 23:25:46 +0000980 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +0000981}
982
983/// ParseWhileStatement
984/// while-statement: [C99 6.8.5.1]
985/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000986/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +0000987StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000988 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000989
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000990 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000991 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000992 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000993
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000994 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000995 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000996 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000997 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000998 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000999
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001000 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1001
Chris Lattner2dd1b722007-08-26 23:08:06 +00001002 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1003 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001004 //
1005 // C++ 6.4p3:
1006 // A name introduced by a declaration in a condition is in scope from its
1007 // point of declaration until the end of the substatements controlled by the
1008 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001009 // C++ 3.3.2p4:
1010 // Names declared in the for-init-statement, and in the condition of if,
1011 // while, for, and switch statements are local to the if, while, for, or
1012 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001013 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001014 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001015 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001016 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1017 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001018 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001019 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1020 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001021
Chris Lattner9075bd72006-08-10 04:59:57 +00001022 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001023 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001024 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001025 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001026 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001027
John McCallb268a282010-08-23 23:25:46 +00001028 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner8fb26252007-08-22 05:28:50 +00001030 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001031 // there is no compound stmt. C90 does not have this clause. We only do this
1032 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001033 //
1034 // C++ 6.5p2:
1035 // The substatement in an iteration-statement implicitly defines a local scope
1036 // which is entered and exited each time through the loop.
1037 //
1038 // See comments in ParseIfStatement for why we create a scope for the
1039 // condition and a new scope for substatement in C++.
1040 //
Mike Stump11289f42009-09-09 15:08:12 +00001041 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001042 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001043
Chris Lattner9075bd72006-08-10 04:59:57 +00001044 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001045 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001046
Chris Lattner8fb26252007-08-22 05:28:50 +00001047 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001048 InnerScope.Exit();
1049 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001050
John McCall48871652010-08-21 09:40:31 +00001051 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001052 return StmtError();
1053
John McCallb268a282010-08-23 23:25:46 +00001054 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001055}
1056
1057/// ParseDoStatement
1058/// do-statement: [C99 6.8.5.2]
1059/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001060/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001061StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001062 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001063
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001064 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001065 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001066
Chris Lattner2dd1b722007-08-26 23:08:06 +00001067 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1068 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001069 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001070 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001071 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001072 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001073 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001074
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001075 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001076
Chris Lattner8fb26252007-08-22 05:28:50 +00001077 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001078 // there is no compound stmt. C90 does not have this clause. We only do this
1079 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001080 //
1081 // C++ 6.5p2:
1082 // The substatement in an iteration-statement implicitly defines a local scope
1083 // which is entered and exited each time through the loop.
1084 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001085 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001086 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001087 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001088
Chris Lattner9075bd72006-08-10 04:59:57 +00001089 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001090 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001091
Chris Lattner8fb26252007-08-22 05:28:50 +00001092 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001093 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001094
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001095 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001096 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001097 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001098 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001099 SkipUntil(tok::semi, false, true);
1100 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001101 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001102 }
Chris Lattneraf635312006-10-16 06:06:51 +00001103 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001104
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001105 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001106 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001107 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001108 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001109 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001110
Chris Lattner10da53c2008-12-12 06:35:28 +00001111 // Parse the parenthesized condition.
Douglas Gregor7f800f92009-11-24 21:34:32 +00001112 SourceLocation LPLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001113 ExprResult Cond = ParseExpression();
Douglas Gregor7f800f92009-11-24 21:34:32 +00001114 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001115 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001116
Sebastian Redlb62406f2008-12-11 19:48:14 +00001117 if (Cond.isInvalid() || Body.isInvalid())
1118 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001119
John McCallb268a282010-08-23 23:25:46 +00001120 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1121 Cond.get(), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +00001122}
1123
1124/// ParseForStatement
1125/// for-statement: [C99 6.8.5.3]
1126/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1127/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001128/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1129/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001130/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001131/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1132/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001133///
1134/// [C++] for-init-statement:
1135/// [C++] expression-statement
1136/// [C++] simple-declaration
1137///
Richard Smith02e85f32011-04-14 22:09:26 +00001138/// [C++0x] for-range-declaration:
1139/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1140/// [C++0x] for-range-initializer:
1141/// [C++0x] expression
1142/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001143StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001144 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001145
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001146 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001147 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001148
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001149 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001150 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001151 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001152 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001153 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001154
Chris Lattner934074c2009-04-22 00:54:41 +00001155 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001156
Chris Lattner2dd1b722007-08-26 23:08:06 +00001157 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1158 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001159 //
1160 // C++ 6.4p3:
1161 // A name introduced by a declaration in a condition is in scope from its
1162 // point of declaration until the end of the substatements controlled by the
1163 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001164 // C++ 3.3.2p4:
1165 // Names declared in the for-init-statement, and in the condition of if,
1166 // while, for, and switch statements are local to the if, while, for, or
1167 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001168 // C++ 6.5.3p1:
1169 // Names declared in the for-init-statement are in the same declarative-region
1170 // as those declared in the condition.
1171 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001172 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001173 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001174 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1175 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001176 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001177 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1178
1179 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001180
Chris Lattner04132372006-10-16 06:12:55 +00001181 SourceLocation LParenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001182 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001183
Richard Smith02e85f32011-04-14 22:09:26 +00001184 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001185 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001186 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001187 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001188 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001189 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001190 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001191 Decl *SecondVar = 0;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001192
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001193 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001194 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001195 C99orCXXorObjC? Sema::PCC_ForInit
1196 : Sema::PCC_Expression);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001197 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001198 }
1199
Chris Lattner9075bd72006-08-10 04:59:57 +00001200 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001201 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001202 // no first part, eat the ';'.
1203 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001204 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001205 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001206 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001207 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001208
John McCall084e83d2011-03-24 11:26:52 +00001209 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001210 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001211
Richard Smith02e85f32011-04-14 22:09:26 +00001212 // In C++0x, "for (T NS:a" might not be a typo for ::
1213 bool MightBeForRangeStmt = getLang().CPlusPlus;
1214 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1215
Chris Lattner49836b42009-04-02 04:16:50 +00001216 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001217 StmtVector Stmts(Actions);
1218 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001219 DeclEnd, attrs, false,
1220 MightBeForRangeStmt ?
1221 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001222 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001223
Richard Smith02e85f32011-04-14 22:09:26 +00001224 if (ForRangeInit.ParsedForRangeDecl()) {
1225 ForRange = true;
1226 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001227 ConsumeToken();
1228 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001229 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001230 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001231 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001232
1233 if (Tok.is(tok::code_completion)) {
1234 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1235 ConsumeCodeCompletionToken();
1236 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001237 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001238 } else {
1239 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001240 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001241 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001242 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001243
John McCall34376a62010-12-04 03:47:34 +00001244 ForEach = isTokIdentifier_in();
1245
Chris Lattnercd68f642007-06-27 01:06:29 +00001246 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001247 if (!Value.isInvalid()) {
1248 if (ForEach)
1249 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1250 else
1251 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1252 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001253
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001254 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001255 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001256 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001257 ConsumeToken(); // consume 'in'
Douglas Gregor68762e72010-08-23 21:17:50 +00001258
1259 if (Tok.is(tok::code_completion)) {
1260 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1261 ConsumeCodeCompletionToken();
1262 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001263 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001264 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001265 if (!Value.isInvalid()) {
1266 Diag(Tok, diag::err_expected_semi_for);
1267 } else {
1268 // Skip until semicolon or rparen, don't consume it.
1269 SkipUntil(tok::r_paren, true, true);
1270 if (Tok.is(tok::semi))
1271 ConsumeToken();
1272 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001273 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001274 }
Richard Smith02e85f32011-04-14 22:09:26 +00001275 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001276 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001277 // Parse the second part of the for specifier.
1278 if (Tok.is(tok::semi)) { // for (...;;
1279 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001280 } else if (Tok.is(tok::r_paren)) {
1281 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001282 } else {
John McCalldadc5752010-08-24 06:29:42 +00001283 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001284 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001285 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1286 else {
1287 Second = ParseExpression();
1288 if (!Second.isInvalid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00001289 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001290 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001291 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001292 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001293 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001294 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001295
Douglas Gregor230a7e62011-02-17 03:38:46 +00001296 if (Tok.isNot(tok::semi)) {
1297 if (!SecondPartIsInvalid || SecondVar)
1298 Diag(Tok, diag::err_expected_semi_for);
1299 else
1300 // Skip until semicolon or rparen, don't consume it.
1301 SkipUntil(tok::r_paren, true, true);
1302 }
1303
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001304 if (Tok.is(tok::semi)) {
1305 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001306 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001307
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001308 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001309 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001310 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001311 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001312 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001313 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001314 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +00001315 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001316
Richard Smith02e85f32011-04-14 22:09:26 +00001317 // We need to perform most of the semantic analysis for a C++0x for-range
1318 // statememt before parsing the body, in order to be able to deduce the type
1319 // of an auto-typed loop variable.
1320 StmtResult ForRangeStmt;
1321 if (ForRange)
1322 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1323 FirstPart.take(),
1324 ForRangeInit.ColonLoc,
1325 ForRangeInit.RangeExpr.get(),
1326 RParenLoc);
1327
Chris Lattner8fb26252007-08-22 05:28:50 +00001328 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001329 // there is no compound stmt. C90 does not have this clause. We only do this
1330 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001331 //
1332 // C++ 6.5p2:
1333 // The substatement in an iteration-statement implicitly defines a local scope
1334 // which is entered and exited each time through the loop.
1335 //
1336 // See comments in ParseIfStatement for why we create a scope for
1337 // for-init-statement/condition and a new scope for substatement in C++.
1338 //
Mike Stump11289f42009-09-09 15:08:12 +00001339 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001340 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001341
Chris Lattner9075bd72006-08-10 04:59:57 +00001342 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001343 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001344
Chris Lattner8fb26252007-08-22 05:28:50 +00001345 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001346 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001347
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001348 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001349 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001350
1351 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001352 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001353
Richard Smith02e85f32011-04-14 22:09:26 +00001354 if (ForEach)
1355 // FIXME: It isn't clear how to communicate the late destruction of
1356 // C++ temporaries used to create the collection.
1357 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1358 FirstPart.take(),
1359 Collection.take(), RParenLoc,
1360 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001361
Richard Smith02e85f32011-04-14 22:09:26 +00001362 if (ForRange)
1363 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1364
1365 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1366 SecondVar, ThirdPart, RParenLoc, Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001367}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001368
Chris Lattner503fadc2006-08-10 05:45:44 +00001369/// ParseGotoStatement
1370/// jump-statement:
1371/// 'goto' identifier ';'
1372/// [GNU] 'goto' '*' expression ';'
1373///
1374/// Note: this lets the caller parse the end ';'.
1375///
John McCall53fa7142010-12-24 02:08:15 +00001376StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001377 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001378
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001379 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001380 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001381
John McCalldadc5752010-08-24 06:29:42 +00001382 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001383 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001384 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1385 Tok.getLocation());
1386 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001387 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001388 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001389 // GNU indirect goto extension.
1390 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001391 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001392 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001393 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001394 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001395 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001396 }
John McCallb268a282010-08-23 23:25:46 +00001397 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001398 } else {
1399 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001400 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001401 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001402
Sebastian Redlb62406f2008-12-11 19:48:14 +00001403 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001404}
1405
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001406/// ParseContinueStatement
1407/// jump-statement:
1408/// 'continue' ';'
1409///
1410/// Note: this lets the caller parse the end ';'.
1411///
John McCall53fa7142010-12-24 02:08:15 +00001412StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001413 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001414
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001415 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001416 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001417}
1418
1419/// ParseBreakStatement
1420/// jump-statement:
1421/// 'break' ';'
1422///
1423/// Note: this lets the caller parse the end ';'.
1424///
John McCall53fa7142010-12-24 02:08:15 +00001425StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001426 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001427
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001428 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001429 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001430}
1431
Chris Lattner503fadc2006-08-10 05:45:44 +00001432/// ParseReturnStatement
1433/// jump-statement:
1434/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001435StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001436 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001437
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001438 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001439 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001440
John McCalldadc5752010-08-24 06:29:42 +00001441 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001442 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001443 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001444 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001445 ConsumeCodeCompletionToken();
1446 SkipUntil(tok::semi, false, true);
1447 return StmtError();
1448 }
1449
Douglas Gregore9e27d92011-03-11 23:10:44 +00001450 // FIXME: This is a hack to allow something like C++0x's generalized
1451 // initializer lists, but only enough of this feature to allow Clang to
1452 // parse libstdc++ 4.5's headers.
1453 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1454 R = ParseInitializer();
1455 if (R.isUsable() && !getLang().CPlusPlus0x)
1456 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1457 << R.get()->getSourceRange();
1458 } else
1459 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001460 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001461 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001462 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001463 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001464 }
John McCallb268a282010-08-23 23:25:46 +00001465 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001466}
Chris Lattner0116c472006-08-15 06:03:28 +00001467
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001468/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1469/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001470StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1471 SourceLocation EndLoc;
Steve Naroff4e79d342008-02-07 23:24:32 +00001472 if (Tok.is(tok::l_brace)) {
1473 unsigned short savedBraceCount = BraceCount;
1474 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001475 EndLoc = Tok.getLocation();
Steve Naroff4e79d342008-02-07 23:24:32 +00001476 ConsumeAnyToken();
1477 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001478 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001479 // From the MS website: If used without braces, the __asm keyword means
1480 // that the rest of the line is an assembly-language statement.
1481 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001482 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001483 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001484 do {
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001485 EndLoc = TokLoc;
Steve Naroff8c099c32008-02-08 18:01:27 +00001486 ConsumeAnyToken();
1487 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001488 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1489 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001490 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001491 }
Mike Stump6da5d752009-12-11 00:04:56 +00001492 Token t;
1493 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001494 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001495 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001496 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001497 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001498 ExprVector Constraints(Actions);
1499 ExprVector Exprs(Actions);
1500 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001501 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001502 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001503 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001504 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001505}
1506
Chris Lattner0116c472006-08-15 06:03:28 +00001507/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001508/// asm-statement:
1509/// gnu-asm-statement
1510/// ms-asm-statement
1511///
1512/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001513/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1514///
1515/// [GNU] asm-argument:
1516/// asm-string-literal
1517/// asm-string-literal ':' asm-operands[opt]
1518/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1519/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1520/// ':' asm-clobbers
1521///
1522/// [GNU] asm-clobbers:
1523/// asm-string-literal
1524/// asm-clobbers ',' asm-string-literal
1525///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001526/// [MS] ms-asm-statement:
1527/// '__asm' assembly-instruction ';'[opt]
1528/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1529///
1530/// [MS] assembly-instruction-list:
1531/// assembly-instruction ';'[opt]
1532/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1533///
John McCalldadc5752010-08-24 06:29:42 +00001534StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001535 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001536 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001537
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001538 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001539 msAsm = true;
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001540 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001541 }
John McCall084e83d2011-03-24 11:26:52 +00001542 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001543 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001544 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001545
Chris Lattner0116c472006-08-15 06:03:28 +00001546 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001547 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001548 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001549 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001550 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001551
Chris Lattner0116c472006-08-15 06:03:28 +00001552 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001553 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001554 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001555 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001556 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001557 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001558 }
Chris Lattner04132372006-10-16 06:12:55 +00001559 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001560
John McCalldadc5752010-08-24 06:29:42 +00001561 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001562 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001563 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001564
Anders Carlsson9a020f92010-01-30 22:25:16 +00001565 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001566 ExprVector Constraints(Actions);
1567 ExprVector Exprs(Actions);
1568 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001569
Anders Carlsson19fe1162008-02-05 23:03:50 +00001570 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001571 // We have a simple asm expression like 'asm("foo")'.
1572 SourceLocation RParenLoc = ConsumeParen();
1573 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1574 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1575 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001576 AsmString.take(), move_arg(Clobbers),
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001577 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001578 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001579
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001580 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001581 bool AteExtraColon = false;
1582 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1583 // In C++ mode, parse "::" like ": :".
1584 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001585 ConsumeToken();
1586
Chris Lattner15768502009-12-20 23:08:04 +00001587 if (!AteExtraColon &&
1588 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001589 return StmtError();
1590 }
Chris Lattner15768502009-12-20 23:08:04 +00001591
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001592 unsigned NumOutputs = Names.size();
1593
1594 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001595 if (AteExtraColon ||
1596 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1597 // In C++ mode, parse "::" like ": :".
1598 if (AteExtraColon)
1599 AteExtraColon = false;
1600 else {
1601 AteExtraColon = Tok.is(tok::coloncolon);
1602 ConsumeToken();
1603 }
1604
1605 if (!AteExtraColon &&
1606 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001607 return StmtError();
1608 }
1609
1610 assert(Names.size() == Constraints.size() &&
1611 Constraints.size() == Exprs.size() &&
1612 "Input operand size mismatch!");
1613
1614 unsigned NumInputs = Names.size() - NumOutputs;
1615
1616 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001617 if (AteExtraColon || Tok.is(tok::colon)) {
1618 if (!AteExtraColon)
1619 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001620
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001621 // Parse the asm-string list for clobbers if present.
1622 if (Tok.isNot(tok::r_paren)) {
1623 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001624 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001625
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001626 if (Clobber.isInvalid())
1627 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001628
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001629 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001630
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001631 if (Tok.isNot(tok::comma)) break;
1632 ConsumeToken();
1633 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001634 }
1635 }
1636
1637 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1638 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001639 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001640 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001641 AsmString.take(), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001642 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001643}
1644
1645/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001646/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001647///
1648/// [GNU] asm-operands:
1649/// asm-operand
1650/// asm-operands ',' asm-operand
1651///
1652/// [GNU] asm-operand:
1653/// asm-string-literal '(' expression ')'
1654/// '[' identifier ']' asm-string-literal '(' expression ')'
1655///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001656//
1657// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson9a020f92010-01-30 22:25:16 +00001658bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1659 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1660 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001661 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001662 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001663 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001664
1665 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001666 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001667 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001668 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001669
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001670 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001671 Diag(Tok, diag::err_expected_ident);
1672 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001673 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001674 }
Mike Stump11289f42009-09-09 15:08:12 +00001675
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001676 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001677 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001678
Anders Carlsson9a020f92010-01-30 22:25:16 +00001679 Names.push_back(II);
Chris Lattner0116c472006-08-15 06:03:28 +00001680 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001681 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001682 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001683
John McCalldadc5752010-08-24 06:29:42 +00001684 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001685 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001686 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001687 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001688 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001689 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001690
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001691 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001692 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001693 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001694 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001695 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001696
Chris Lattner0116c472006-08-15 06:03:28 +00001697 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001698 SourceLocation OpenLoc = ConsumeParen();
John McCalldadc5752010-08-24 06:29:42 +00001699 ExprResult Res(ParseExpression());
Eli Friedman47e78572009-05-03 07:49:42 +00001700 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001701 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001702 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001703 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001704 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001705 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001706 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001707 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001708 ConsumeToken();
1709 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001710
1711 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001712}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001713
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001714Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001715 assert(Tok.is(tok::l_brace));
1716 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001717
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001718 if (PP.isCodeCompletionEnabled()) {
1719 if (trySkippingFunctionBodyForCodeCompletion()) {
1720 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001721 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001722 }
1723 }
1724
John McCallfaf5fb42010-08-26 23:41:50 +00001725 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1726 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001727
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001728 // Do not enter a scope for the brace, as the arguments are in the same scope
1729 // (the function body) as the body itself. Instead, just read the statement
1730 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001731 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001732
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001733 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001734 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001735 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001736 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001737
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001738 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001739 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001740}
Sebastian Redlb219c902008-12-21 16:41:36 +00001741
Sebastian Redla7b98a72009-04-26 20:35:05 +00001742/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1743///
1744/// function-try-block:
1745/// 'try' ctor-initializer[opt] compound-statement handler-seq
1746///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001747Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001748 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1749 SourceLocation TryLoc = ConsumeToken();
1750
John McCallfaf5fb42010-08-26 23:41:50 +00001751 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1752 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001753
1754 // Constructor initializer list?
1755 if (Tok.is(tok::colon))
1756 ParseConstructorInitializer(Decl);
1757
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001758 if (PP.isCodeCompletionEnabled()) {
1759 if (trySkippingFunctionBodyForCodeCompletion()) {
1760 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001761 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001762 }
1763 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001764
Sebastian Redld98ecd62009-04-26 21:08:36 +00001765 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001766 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001767 // If we failed to parse the try-catch, we just give the function an empty
1768 // compound statement as the body.
1769 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001770 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001771 MultiStmtArg(Actions), false);
1772
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001773 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001774 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001775}
1776
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001777bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001778 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001779 assert(PP.isCodeCompletionEnabled() &&
1780 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001781
1782 // We're in code-completion mode. Skip parsing for all function bodies unless
1783 // the body contains the code-completion point.
1784 TentativeParsingAction PA(*this);
1785 ConsumeBrace();
1786 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1787 /*StopAtCodeCompletion=*/true)) {
1788 PA.Commit();
1789 return true;
1790 }
1791
1792 PA.Revert();
1793 return false;
1794}
1795
Sebastian Redlb219c902008-12-21 16:41:36 +00001796/// ParseCXXTryBlock - Parse a C++ try-block.
1797///
1798/// try-block:
1799/// 'try' compound-statement handler-seq
1800///
John McCall53fa7142010-12-24 02:08:15 +00001801StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001802 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001803
Sebastian Redlb219c902008-12-21 16:41:36 +00001804 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1805
1806 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001807 return ParseCXXTryBlockCommon(TryLoc);
1808}
1809
1810/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1811/// function-try-block.
1812///
1813/// try-block:
1814/// 'try' compound-statement handler-seq
1815///
1816/// function-try-block:
1817/// 'try' ctor-initializer[opt] compound-statement handler-seq
1818///
1819/// handler-seq:
1820/// handler handler-seq[opt]
1821///
John McCalldadc5752010-08-24 06:29:42 +00001822StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001823 if (Tok.isNot(tok::l_brace))
1824 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00001825 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001826 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001827 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001828 if (TryBlock.isInvalid())
1829 return move(TryBlock);
1830
1831 StmtVector Handlers(Actions);
John McCall53fa7142010-12-24 02:08:15 +00001832 MaybeParseCXX0XAttributes(attrs);
1833 ProhibitAttributes(attrs);
1834
Sebastian Redlb219c902008-12-21 16:41:36 +00001835 if (Tok.isNot(tok::kw_catch))
1836 return StmtError(Diag(Tok, diag::err_expected_catch));
1837 while (Tok.is(tok::kw_catch)) {
John McCalldadc5752010-08-24 06:29:42 +00001838 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redlb219c902008-12-21 16:41:36 +00001839 if (!Handler.isInvalid())
1840 Handlers.push_back(Handler.release());
1841 }
1842 // Don't bother creating the full statement if we don't have any usable
1843 // handlers.
1844 if (Handlers.empty())
1845 return StmtError();
1846
John McCallb268a282010-08-23 23:25:46 +00001847 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001848}
1849
1850/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1851///
1852/// handler:
1853/// 'catch' '(' exception-declaration ')' compound-statement
1854///
1855/// exception-declaration:
1856/// type-specifier-seq declarator
1857/// type-specifier-seq abstract-declarator
1858/// type-specifier-seq
1859/// '...'
1860///
John McCalldadc5752010-08-24 06:29:42 +00001861StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001862 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1863
1864 SourceLocation CatchLoc = ConsumeToken();
1865
1866 SourceLocation LParenLoc = Tok.getLocation();
1867 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1868 return StmtError();
1869
1870 // C++ 3.3.2p3:
1871 // The name in a catch exception-declaration is local to the handler and
1872 // shall not be redeclared in the outermost block of the handler.
1873 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1874
1875 // exception-declaration is equivalent to '...' or a parameter-declaration
1876 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00001877 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00001878 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001879 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00001880 if (ParseCXXTypeSpecifierSeq(DS))
1881 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001882 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1883 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001884 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00001885 } else
1886 ConsumeToken();
1887
1888 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1889 return StmtError();
1890
1891 if (Tok.isNot(tok::l_brace))
1892 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1893
Alexis Hunt96d5c762009-11-21 08:43:09 +00001894 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00001895 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001896 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00001897 if (Block.isInvalid())
1898 return move(Block);
1899
John McCallb268a282010-08-23 23:25:46 +00001900 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00001901}