blob: c908ed9caec69f82a92b5c1e76e7f874efc62ff0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// [GNU] asm-statement
46/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
53/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
62/// expression-statement:
63/// expression[opt] ';'
64///
65/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
70/// [GNU] 'goto' '*' expression ';'
71///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000074/// [OBC] '@' 'throw' ';'
75///
Sebastian Redl61364dd2008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000078 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000080
81 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000082
Sean Huntbbd37c62009-11-21 08:43:09 +000083 CXX0XAttributeList Attr;
84 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
85 Attr = ParseCXX0XAttributes();
Ted Kremenek1e377652010-02-11 02:19:13 +000086 llvm::OwningPtr<AttributeList> AttrList(Attr.AttrList);
Sean Huntbbd37c62009-11-21 08:43:09 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // Cases in this switch statement should fall through if the parser expects
89 // the token to end in a semicolon (in which case SemiError should be set),
90 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 tok::TokenKind Kind = Tok.getKind();
92 SourceLocation AtLoc;
93 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000094 case tok::at: // May be a @try or @throw statement
95 {
96 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000097 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000098 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000099
Douglas Gregor791215b2009-09-21 20:51:25 +0000100 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000101 Actions.CodeCompleteOrdinaryName(getCurScope(), Action::CCC_Statement);
Douglas Gregorc8bddde2010-05-28 00:22:41 +0000102 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +0000103 return ParseStatementOrDeclaration(OnlyStatement);
104
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000105 case tok::identifier:
106 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
107 // identifier ':' statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000108 return ParseLabeledStatement(AttrList.take());
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000109 }
110 // PASS THROUGH.
111
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000112 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000113 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000114 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek1e377652010-02-11 02:19:13 +0000115 AttrList.take(); //Passing 'Attr' to ParseDeclaration transfers ownership.
Sean Huntbbd37c62009-11-21 08:43:09 +0000116 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd,
117 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000118 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000119 }
120
121 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000123 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Sean Huntbbd37c62009-11-21 08:43:09 +0000126 // FIXME: Use the attributes
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000127 // expression[opt] ';'
128 OwningExprResult Expr(ParseExpression());
129 if (Expr.isInvalid()) {
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000130 // If the expression is invalid, skip ahead to the next semicolon or '}'.
131 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000132 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000133 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
134 if (Tok.is(tok::semi))
135 ConsumeToken();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000136 return StmtError();
137 }
138 // Otherwise, eat the semicolon.
139 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000140 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000141 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000142
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case tok::kw_case: // C99 6.8.1: labeled-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000144 return ParseCaseStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 case tok::kw_default: // C99 6.8.1: labeled-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000146 return ParseDefaultStatement(AttrList.take());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 case tok::l_brace: // C99 6.8.2: compound-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000149 return ParseCompoundStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000151 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 case tok::kw_if: // C99 6.8.4.1: if-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000154 return ParseIfStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000156 return ParseSwitchStatement(AttrList.take());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 case tok::kw_while: // C99 6.8.5.1: while-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000159 return ParseWhileStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000161 Res = ParseDoStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000162 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 break;
164 case tok::kw_for: // C99 6.8.5.3: for-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000165 return ParseForStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
167 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000168 Res = ParseGotoStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000169 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 break;
171 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000172 Res = ParseContinueStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000173 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 break;
175 case tok::kw_break: // C99 6.8.6.3: break-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000176 Res = ParseBreakStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000177 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 break;
179 case tok::kw_return: // C99 6.8.6.4: return-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000180 Res = ParseReturnStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000181 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000183
Sebastian Redla0fd8652008-12-21 16:41:36 +0000184 case tok::kw_asm: {
Sean Huntbbd37c62009-11-21 08:43:09 +0000185 if (Attr.HasAttr)
186 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
187 << Attr.Range;
Steve Naroffd62701b2008-02-07 03:50:06 +0000188 bool msAsm = false;
189 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000190 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000191 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 break;
193 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000194
Sebastian Redla0fd8652008-12-21 16:41:36 +0000195 case tok::kw_try: // C++ 15: try-block
Ted Kremenek1e377652010-02-11 02:19:13 +0000196 return ParseCXXTryBlock(AttrList.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +0000197 }
198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000200 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000202 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000203 // If the result was valid, then we do want to diagnose this. Use
204 // ExpectAndConsume to emit the diagnostic, even though we know it won't
205 // succeed.
206 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000207 // Skip until we see a } or ;, but don't eat it.
208 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Sebastian Redl61364dd2008-12-11 19:30:53 +0000211 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000212}
213
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000214/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000215///
216/// labeled-statement:
217/// identifier ':' statement
218/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000219///
Sean Huntbbd37c62009-11-21 08:43:09 +0000220Parser::OwningStmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000221 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
222 "Not an identifier!");
223
Ted Kremenek1e377652010-02-11 02:19:13 +0000224 llvm::OwningPtr<AttributeList> AttrList(Attr);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000225 Token IdentTok = Tok; // Save the whole token.
226 ConsumeToken(); // eat the identifier.
227
228 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000229
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000230 // identifier ':' statement
231 SourceLocation ColonLoc = ConsumeToken();
232
233 // Read label attributes, if present.
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000234 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000235 AttrList.reset(addAttributeLists(AttrList.take(), ParseGNUAttributes()));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000236
Sebastian Redl61364dd2008-12-11 19:30:53 +0000237 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000238
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000239 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000240 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000241 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000242
Ted Kremenek1e377652010-02-11 02:19:13 +0000243 // FIXME: use attributes?
Sebastian Redlde307472009-01-11 00:38:46 +0000244 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
245 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000246 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000247}
Reid Spencer5f016e22007-07-11 17:01:13 +0000248
249/// ParseCaseStatement
250/// labeled-statement:
251/// 'case' constant-expression ':' statement
252/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
253///
Sean Huntbbd37c62009-11-21 08:43:09 +0000254Parser::OwningStmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000255 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000256 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000257 delete Attr;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner24e1e702009-03-04 04:23:07 +0000259 // It is very very common for code to contain many case statements recursively
260 // nested, as in (but usually without indentation):
261 // case 1:
262 // case 2:
263 // case 3:
264 // case 4:
265 // case 5: etc.
266 //
267 // Parsing this naively works, but is both inefficient and can cause us to run
268 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000269 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000270 // but all the grossness is constrained to ParseCaseStatement (and some
271 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner24e1e702009-03-04 04:23:07 +0000273 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
274 // example above.
275 OwningStmtResult TopLevelCase(Actions, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner24e1e702009-03-04 04:23:07 +0000277 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
278 // gets updated each time a new case is parsed, and whose body is unset so
279 // far. When parsing 'case 4', this is the 'case 3' node.
280 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner24e1e702009-03-04 04:23:07 +0000282 // While we have case statements, eat and stack them.
283 do {
284 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000286 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000287 Actions.CodeCompleteCase(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000288 ConsumeCodeCompletionToken();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000289 }
290
Chris Lattner6fb09c82009-12-10 00:38:54 +0000291 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
292 /// Disable this form of error recovery while we're parsing the case
293 /// expression.
294 ColonProtectionRAIIObject ColonProtection(*this);
295
Chris Lattner24e1e702009-03-04 04:23:07 +0000296 OwningExprResult LHS(ParseConstantExpression());
297 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000299 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000301
Chris Lattner24e1e702009-03-04 04:23:07 +0000302 // GNU case range extension.
303 SourceLocation DotDotDotLoc;
304 OwningExprResult RHS(Actions);
305 if (Tok.is(tok::ellipsis)) {
306 Diag(Tok, diag::ext_gnu_case_range);
307 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000308
Chris Lattner24e1e702009-03-04 04:23:07 +0000309 RHS = ParseConstantExpression();
310 if (RHS.isInvalid()) {
311 SkipUntil(tok::colon);
312 return StmtError();
313 }
314 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000315
316 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000317
Chris Lattner24e1e702009-03-04 04:23:07 +0000318 if (Tok.isNot(tok::colon)) {
319 Diag(Tok, diag::err_expected_colon_after) << "'case'";
320 SkipUntil(tok::colon);
321 return StmtError();
322 }
323
324 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner24e1e702009-03-04 04:23:07 +0000326 OwningStmtResult Case =
327 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
328 move(RHS), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner24e1e702009-03-04 04:23:07 +0000330 // If we had a sema error parsing this case, then just ignore it and
331 // continue parsing the sub-stmt.
332 if (Case.isInvalid()) {
333 if (TopLevelCase.isInvalid()) // No parsed case stmts.
334 return ParseStatement();
335 // Otherwise, just don't add it as a nested case.
336 } else {
337 // If this is the first case statement we parsed, it becomes TopLevelCase.
338 // Otherwise we link it into the current chain.
339 StmtTy *NextDeepest = Case.get();
340 if (TopLevelCase.isInvalid())
341 TopLevelCase = move(Case);
342 else
343 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
344 DeepestParsedCaseStmt = NextDeepest;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner24e1e702009-03-04 04:23:07 +0000347 // Handle all case statements.
348 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner24e1e702009-03-04 04:23:07 +0000350 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner24e1e702009-03-04 04:23:07 +0000352 // If we found a non-case statement, start by parsing it.
353 OwningStmtResult SubStmt(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner24e1e702009-03-04 04:23:07 +0000355 if (Tok.isNot(tok::r_brace)) {
356 SubStmt = ParseStatement();
357 } else {
358 // Nicely diagnose the common error "switch (X) { case 4: }", which is
359 // not valid.
360 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000362 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner24e1e702009-03-04 04:23:07 +0000365 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000366 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000367 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattner24e1e702009-03-04 04:23:07 +0000369 // Install the body into the most deeply-nested case.
370 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000371
Chris Lattner24e1e702009-03-04 04:23:07 +0000372 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000373 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000374}
375
376/// ParseDefaultStatement
377/// labeled-statement:
378/// 'default' ':' statement
379/// Note that this does not parse the 'statement' at the end.
380///
Sean Huntbbd37c62009-11-21 08:43:09 +0000381Parser::OwningStmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
382 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000383 delete Attr;
384
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000385 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
387
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000388 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000389 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000391 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000395
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000397 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000399 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
401
Sebastian Redl61364dd2008-12-11 19:30:53 +0000402 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000403 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000404 return StmtError();
405
Sebastian Redl117054a2008-12-28 16:13:43 +0000406 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000407 move(SubStmt), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000408}
409
410
411/// ParseCompoundStatement - Parse a "{}" block.
412///
413/// compound-statement: [C99 6.8.2]
414/// { block-item-list[opt] }
415/// [GNU] { label-declarations block-item-list } [TODO]
416///
417/// block-item-list:
418/// block-item
419/// block-item-list block-item
420///
421/// block-item:
422/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000423/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000424/// statement
425/// [OMP] openmp-directive [TODO]
426///
427/// [GNU] label-declarations:
428/// [GNU] label-declaration
429/// [GNU] label-declarations label-declaration
430///
431/// [GNU] label-declaration:
432/// [GNU] '__label__' identifier-list ';'
433///
434/// [OMP] openmp-directive: [TODO]
435/// [OMP] barrier-directive
436/// [OMP] flush-directive
437///
Sean Huntbbd37c62009-11-21 08:43:09 +0000438Parser::OwningStmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
439 bool isStmtExpr) {
440 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000441 delete Attr;
442
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000443 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000444
Chris Lattner31e05722007-08-26 06:24:45 +0000445 // Enter a scope to hold everything within the compound stmt. Compound
446 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000447 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000448
449 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000450 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000451}
452
453
454/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000455/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000456/// consume the '}' at the end of the block. It does not manipulate the scope
457/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000458Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000459 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000460 Tok.getLocation(),
461 "in compound statement ('{}')");
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
464
465 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000466 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000467
468 typedef StmtVector StmtsTy;
469 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000470 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000471 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000472 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000473 R = ParseStatementOrDeclaration(false);
474 } else {
475 // __extension__ can start declarations and it can also be a unary
476 // operator for expressions. Consume multiple __extension__ markers here
477 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000478 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000479 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000480 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000481 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000482
Sean Huntbbd37c62009-11-21 08:43:09 +0000483 CXX0XAttributeList Attr;
484 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
485 Attr = ParseCXX0XAttributes();
486
Chris Lattner45a566c2007-08-27 01:01:57 +0000487 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000488 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000489 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000490 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000491 ExtensionRAIIObject O(Diags);
492
Chris Lattner97144fc2009-04-02 04:16:50 +0000493 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000494 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
495 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000496 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000497 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000498 // Otherwise this was a unary __extension__ marker.
499 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000500
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000501 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000502 SkipUntil(tok::semi);
503 continue;
504 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000505
Sean Huntbbd37c62009-11-21 08:43:09 +0000506 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000507 // Eat the semicolon at the end of stmt and convert the expr into a
508 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000509 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000510 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000511 }
512 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000513
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000514 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000515 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000517
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000519 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000521 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000523
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000525 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000526 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000527}
528
Chris Lattner15ff1112008-12-12 06:31:07 +0000529/// ParseParenExprOrCondition:
530/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000531/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000532///
533/// This function parses and performs error recovery on the specified condition
534/// or expression (depending on whether we're in C++ or C mode). This function
535/// goes out of its way to recover well. It returns true if there was a parser
536/// error (the right paren couldn't be found), which indicates that the caller
537/// should try to recover harder. It returns false if the condition is
538/// successfully parsed. Note that a successful parse can still have semantic
539/// errors in the condition.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000540bool Parser::ParseParenExprOrCondition(OwningExprResult &ExprResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000541 DeclPtrTy &DeclResult,
542 SourceLocation Loc,
543 bool ConvertToBoolean) {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000544 bool ParseError = false;
545
Chris Lattner15ff1112008-12-12 06:31:07 +0000546 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000547 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +0000548 ParseError = ParseCXXCondition(ExprResult, DeclResult, Loc,
549 ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000550 else {
551 ExprResult = ParseExpression();
552 DeclResult = DeclPtrTy();
Douglas Gregor586596f2010-05-06 17:25:47 +0000553
554 // If required, convert to a boolean value.
555 if (!ExprResult.isInvalid() && ConvertToBoolean)
556 ExprResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000557 = Actions.ActOnBooleanCondition(getCurScope(), Loc, move(ExprResult));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner15ff1112008-12-12 06:31:07 +0000560 // If the parser was confused by the condition and we don't have a ')', try to
561 // recover by skipping ahead to a semi and bailing out. If condexp is
562 // semantically invalid but we have well formed code, keep going.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000563 if (ExprResult.isInvalid() && !DeclResult.get() && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000564 SkipUntil(tok::semi);
565 // Skipping may have stopped if it found the containing ')'. If so, we can
566 // continue parsing the if statement.
567 if (Tok.isNot(tok::r_paren))
568 return true;
569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Chris Lattner15ff1112008-12-12 06:31:07 +0000571 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000572 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000573 return false;
574}
575
576
Reid Spencer5f016e22007-07-11 17:01:13 +0000577/// ParseIfStatement
578/// if-statement: [C99 6.8.4.1]
579/// 'if' '(' expression ')' statement
580/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000581/// [C++] 'if' '(' condition ')' statement
582/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000583///
Sean Huntbbd37c62009-11-21 08:43:09 +0000584Parser::OwningStmtResult Parser::ParseIfStatement(AttributeList *Attr) {
585 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000586 delete Attr;
587
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000588 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
590
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000591 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000592 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000594 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000596
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000597 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
598
Chris Lattner22153252007-08-26 23:08:06 +0000599 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
600 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000601 //
602 // C++ 6.4p3:
603 // A name introduced by a declaration in a condition is in scope from its
604 // point of declaration until the end of the substatements controlled by the
605 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000606 // C++ 3.3.2p4:
607 // Names declared in the for-init-statement, and in the condition of if,
608 // while, for, and switch statements are local to the if, while, for, or
609 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000610 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000611 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000612
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000614 OwningExprResult CondExp(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000615 DeclPtrTy CondVar;
Douglas Gregor586596f2010-05-06 17:25:47 +0000616 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000617 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000618
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000619 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp));
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattner0ecea032007-08-22 05:28:50 +0000621 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000622 // there is no compound stmt. C90 does not have this clause. We only do this
623 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000624 //
625 // C++ 6.4p1:
626 // The substatement in a selection-statement (each substatement, in the else
627 // form of the if statement) implicitly defines a local scope.
628 //
629 // For C++ we create a scope for the condition and a new scope for
630 // substatements because:
631 // -When the 'then' scope exits, we want the condition declaration to still be
632 // active for the 'else' scope too.
633 // -Sema will detect name clashes by considering declarations of a
634 // 'ControlScope' as part of its direct subscope.
635 // -If we wanted the condition and substatement to be in the same scope, we
636 // would have to notify ParseStatement not to create a new scope. It's
637 // simpler to let it create a new scope.
638 //
Mike Stump1eb44332009-09-09 15:08:12 +0000639 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000640 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000641
Chris Lattnerb96728d2007-10-29 05:08:52 +0000642 // Read the 'then' stmt.
643 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000644 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000645
Chris Lattnera36ce712007-08-22 05:16:28 +0000646 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000647 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 // If it has an else, parse it.
650 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000651 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000652 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000653
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000654 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000656 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000657
Chris Lattner0ecea032007-08-22 05:28:50 +0000658 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000659 // there is no compound stmt. C90 does not have this clause. We only do
660 // this if the body isn't a compound statement to avoid push/pop in common
661 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000662 //
663 // C++ 6.4p1:
664 // The substatement in a selection-statement (each substatement, in the else
665 // form of the if statement) implicitly defines a local scope.
666 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000667 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000668 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000669
Chris Lattner966c78b2010-04-12 06:12:50 +0000670 // Regardless of whether or not InnerScope actually pushed a scope, set the
671 // ElseScope flag for the innermost scope so we can diagnose use of the if
672 // condition variable in C++.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000673 unsigned OldFlags = getCurScope()->getFlags();
674 getCurScope()->setFlags(OldFlags | Scope::ElseScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 ElseStmt = ParseStatement();
Douglas Gregor23c94db2010-07-02 17:43:08 +0000676 getCurScope()->setFlags(OldFlags);
Chris Lattner966c78b2010-04-12 06:12:50 +0000677
Chris Lattnera36ce712007-08-22 05:16:28 +0000678 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000679 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000681
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000682 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Chris Lattner18914bc2008-12-12 06:19:11 +0000684 // If the condition was invalid, discard the if statement. We could recover
685 // better by replacing it with a valid expr, but don't do that yet.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000686 if (CondExp.isInvalid() && !CondVar.get())
Chris Lattner18914bc2008-12-12 06:19:11 +0000687 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000688
Chris Lattnerb96728d2007-10-29 05:08:52 +0000689 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000690 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000691 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000692 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
693 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
694 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000695 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000696 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000697 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000698
Chris Lattnerb96728d2007-10-29 05:08:52 +0000699 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000700 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000701 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000702 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000703 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000704
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000705 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000706 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000707}
708
709/// ParseSwitchStatement
710/// switch-statement:
711/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000712/// [C++] 'switch' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000713Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
714 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000715 delete Attr;
716
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000717 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
719
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000720 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000721 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000723 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 }
Chris Lattner22153252007-08-26 23:08:06 +0000725
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000726 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
727
Chris Lattner22153252007-08-26 23:08:06 +0000728 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
729 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000730 //
731 // C++ 6.4p3:
732 // A name introduced by a declaration in a condition is in scope from its
733 // point of declaration until the end of the substatements controlled by the
734 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000735 // C++ 3.3.2p4:
736 // Names declared in the for-init-statement, and in the condition of if,
737 // while, for, and switch statements are local to the if, while, for, or
738 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000739 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000740 unsigned ScopeFlags = Scope::BreakScope;
741 if (C99orCXX)
742 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000743 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000744
745 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000746 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000747 DeclPtrTy CondVar;
Douglas Gregor586596f2010-05-06 17:25:47 +0000748 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +0000749 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000750
Douglas Gregor586596f2010-05-06 17:25:47 +0000751 OwningStmtResult Switch
752 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, move(Cond), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000753
Douglas Gregor586596f2010-05-06 17:25:47 +0000754 if (Switch.isInvalid()) {
755 // Skip the switch body.
756 // FIXME: This is not optimal recovery, but parsing the body is more
757 // dangerous due to the presence of case and default statements, which
758 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +0000759 if (Tok.is(tok::l_brace)) {
760 ConsumeBrace();
761 SkipUntil(tok::r_brace, false, false);
762 } else
Douglas Gregor586596f2010-05-06 17:25:47 +0000763 SkipUntil(tok::semi);
764 return move(Switch);
765 }
766
Chris Lattner0ecea032007-08-22 05:28:50 +0000767 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000768 // there is no compound stmt. C90 does not have this clause. We only do this
769 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000770 //
771 // C++ 6.4p1:
772 // The substatement in a selection-statement (each substatement, in the else
773 // form of the if statement) implicitly defines a local scope.
774 //
775 // See comments in ParseIfStatement for why we create a scope for the
776 // condition and a new scope for substatement in C++.
777 //
Mike Stump1eb44332009-09-09 15:08:12 +0000778 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000779 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000782 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000783
Chris Lattner7e52de42010-01-24 01:50:29 +0000784 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000785 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000786 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000787
Chris Lattner7e52de42010-01-24 01:50:29 +0000788 if (Body.isInvalid())
789 // FIXME: Remove the case statement list from the Switch statement.
790 Body = Actions.ActOnNullStmt(Tok.getLocation());
791
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000792 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000793}
794
795/// ParseWhileStatement
796/// while-statement: [C99 6.8.5.1]
797/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000798/// [C++] 'while' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000799Parser::OwningStmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
800 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000801 delete Attr;
802
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000803 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 SourceLocation WhileLoc = Tok.getLocation();
805 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000806
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000807 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000808 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000810 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000812
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000813 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
814
Chris Lattner22153252007-08-26 23:08:06 +0000815 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
816 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000817 //
818 // C++ 6.4p3:
819 // A name introduced by a declaration in a condition is in scope from its
820 // point of declaration until the end of the substatements controlled by the
821 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000822 // C++ 3.3.2p4:
823 // Names declared in the for-init-statement, and in the condition of if,
824 // while, for, and switch statements are local to the if, while, for, or
825 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000826 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000827 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000828 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000829 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
830 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000831 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000832 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
833 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
835 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000836 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000837 DeclPtrTy CondVar;
Douglas Gregor586596f2010-05-06 17:25:47 +0000838 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000839 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000840
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000841 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattner0ecea032007-08-22 05:28:50 +0000843 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000844 // there is no compound stmt. C90 does not have this clause. We only do this
845 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000846 //
847 // C++ 6.5p2:
848 // The substatement in an iteration-statement implicitly defines a local scope
849 // which is entered and exited each time through the loop.
850 //
851 // See comments in ParseIfStatement for why we create a scope for the
852 // condition and a new scope for substatement in C++.
853 //
Mike Stump1eb44332009-09-09 15:08:12 +0000854 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000855 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000856
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000858 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000859
Chris Lattner0ecea032007-08-22 05:28:50 +0000860 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000861 InnerScope.Exit();
862 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000863
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000864 if ((Cond.isInvalid() && !CondVar.get()) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000865 return StmtError();
866
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000867 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000868}
869
870/// ParseDoStatement
871/// do-statement: [C99 6.8.5.2]
872/// 'do' statement 'while' '(' expression ')' ';'
873/// Note: this lets the caller parse the end ';'.
Sean Huntbbd37c62009-11-21 08:43:09 +0000874Parser::OwningStmtResult Parser::ParseDoStatement(AttributeList *Attr) {
875 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000876 delete Attr;
877
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000878 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000880
Chris Lattner22153252007-08-26 23:08:06 +0000881 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
882 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000883 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000884 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000885 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000886 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000887 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000888
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000889 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000890
Chris Lattner0ecea032007-08-22 05:28:50 +0000891 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000892 // there is no compound stmt. C90 does not have this clause. We only do this
893 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000894 //
895 // C++ 6.5p2:
896 // The substatement in an iteration-statement implicitly defines a local scope
897 // which is entered and exited each time through the loop.
898 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000899 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000900 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000901 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000904 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000905
Chris Lattner0ecea032007-08-22 05:28:50 +0000906 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000907 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000908
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000909 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000910 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000911 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000912 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000913 SkipUntil(tok::semi, false, true);
914 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000915 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 }
917 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000918
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000919 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000920 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000921 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000922 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000924
Chris Lattnerff871fb2008-12-12 06:35:28 +0000925 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000926 SourceLocation LPLoc = ConsumeParen();
927 OwningExprResult Cond = ParseExpression();
928 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000929 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000930
Sebastian Redl9a920342008-12-11 19:48:14 +0000931 if (Cond.isInvalid() || Body.isInvalid())
932 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000933
Chris Lattner98913592009-06-12 23:04:47 +0000934 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
935 move(Cond), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000936}
937
938/// ParseForStatement
939/// for-statement: [C99 6.8.5.3]
940/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
941/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000942/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
943/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000944/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
945/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000946///
947/// [C++] for-init-statement:
948/// [C++] expression-statement
949/// [C++] simple-declaration
950///
Sean Huntbbd37c62009-11-21 08:43:09 +0000951Parser::OwningStmtResult Parser::ParseForStatement(AttributeList *Attr) {
952 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000953 delete Attr;
954
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000955 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000957
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000958 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000959 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000961 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000963
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000964 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000965
Chris Lattner22153252007-08-26 23:08:06 +0000966 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
967 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000968 //
969 // C++ 6.4p3:
970 // A name introduced by a declaration in a condition is in scope from its
971 // point of declaration until the end of the substatements controlled by the
972 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000973 // C++ 3.3.2p4:
974 // Names declared in the for-init-statement, and in the condition of if,
975 // while, for, and switch statements are local to the if, while, for, or
976 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000977 // C++ 6.5.3p1:
978 // Names declared in the for-init-statement are in the same declarative-region
979 // as those declared in the condition.
980 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000981 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000982 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000983 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
984 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000985 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000986 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
987
988 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000989
990 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000991 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000992
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000993 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000994 OwningStmtResult FirstPart(Actions);
Douglas Gregoreecf38f2010-05-06 21:39:56 +0000995 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +0000996 FullExprArg SecondPart(Actions);
997 OwningExprResult Collection(Actions);
998 FullExprArg ThirdPart(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000999 DeclPtrTy SecondVar;
1000
Douglas Gregor791215b2009-09-21 20:51:25 +00001001 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001002 Actions.CodeCompleteOrdinaryName(getCurScope(),
Douglas Gregor01dfea02010-01-10 23:08:15 +00001003 C99orCXXorObjC? Action::CCC_ForInit
1004 : Action::CCC_Expression);
Douglas Gregordc845342010-05-25 05:58:43 +00001005 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +00001006 }
1007
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001009 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 // no first part, eat the ';'.
1011 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001012 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001014 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001016
Sean Huntbbd37c62009-11-21 08:43:09 +00001017 AttributeList *AttrList = 0;
1018 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
1019 AttrList = ParseCXX0XAttributes().AttrList;
1020
Chris Lattner97144fc2009-04-02 04:16:50 +00001021 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +00001022 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +00001023 AttrList, false);
Chris Lattnercd147752009-03-29 17:27:48 +00001024 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattnercd147752009-03-29 17:27:48 +00001026 if (Tok.is(tok::semi)) { // for (int x = 4;
1027 ConsumeToken();
1028 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001029 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001030 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001031 ConsumeToken(); // consume 'in'
Douglas Gregor586596f2010-05-06 17:25:47 +00001032 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001033 } else {
1034 Diag(Tok, diag::err_expected_semi_for);
1035 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001036 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 } else {
1038 Value = ParseExpression();
1039
1040 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001041 if (!Value.isInvalid())
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001042 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +00001043
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001044 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001046 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001047 ConsumeToken(); // consume 'in'
Douglas Gregor586596f2010-05-06 17:25:47 +00001048 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001049 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001050 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 SkipUntil(tok::semi);
1052 }
1053 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001054 if (!ForEach) {
Douglas Gregor586596f2010-05-06 17:25:47 +00001055 assert(!SecondPart->get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001056 // Parse the second part of the for specifier.
1057 if (Tok.is(tok::semi)) { // for (...;;
1058 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001059 } else {
Douglas Gregor586596f2010-05-06 17:25:47 +00001060 OwningExprResult Second(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001061 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001062 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1063 else {
1064 Second = ParseExpression();
1065 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001066 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
Douglas Gregor586596f2010-05-06 17:25:47 +00001067 move(Second));
1068 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001069 SecondPartIsInvalid = Second.isInvalid();
Douglas Gregor586596f2010-05-06 17:25:47 +00001070 SecondPart = Actions.MakeFullExpr(Second);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001071 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001072
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001073 if (Tok.is(tok::semi)) {
1074 ConsumeToken();
1075 } else {
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001076 if (!SecondPartIsInvalid || SecondVar.get())
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001077 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001078 SkipUntil(tok::semi);
1079 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001080
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001081 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001082 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
1083 OwningExprResult Third = ParseExpression();
1084 ThirdPart = Actions.MakeFullExpr(Third);
1085 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 // Match the ')'.
1088 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001089
Chris Lattner0ecea032007-08-22 05:28:50 +00001090 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001091 // there is no compound stmt. C90 does not have this clause. We only do this
1092 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001093 //
1094 // C++ 6.5p2:
1095 // The substatement in an iteration-statement implicitly defines a local scope
1096 // which is entered and exited each time through the loop.
1097 //
1098 // See comments in ParseIfStatement for why we create a scope for
1099 // for-init-statement/condition and a new scope for substatement in C++.
1100 //
Mike Stump1eb44332009-09-09 15:08:12 +00001101 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001102 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001103
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001105 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001106
Chris Lattner0ecea032007-08-22 05:28:50 +00001107 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001108 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001109
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001111 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001112
1113 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001114 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001115
1116 if (!ForEach)
Douglas Gregor586596f2010-05-06 17:25:47 +00001117 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart), SecondPart,
1118 SecondVar, ThirdPart, RParenLoc, move(Body));
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Douglas Gregor586596f2010-05-06 17:25:47 +00001120 // FIXME: It isn't clear how to communicate the late destruction of
1121 // C++ temporaries used to create the collection.
1122 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, move(FirstPart),
1123 move(Collection), RParenLoc,
1124 move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001125}
1126
1127/// ParseGotoStatement
1128/// jump-statement:
1129/// 'goto' identifier ';'
1130/// [GNU] 'goto' '*' expression ';'
1131///
1132/// Note: this lets the caller parse the end ';'.
1133///
Sean Huntbbd37c62009-11-21 08:43:09 +00001134Parser::OwningStmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
1135 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001136 delete Attr;
1137
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001138 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001140
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001141 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001142 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001143 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 Tok.getIdentifierInfo());
1145 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001146 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 // GNU indirect goto extension.
1148 Diag(Tok, diag::ext_gnu_indirect_goto);
1149 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001150 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001151 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001153 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001155 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001156 } else {
1157 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001158 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001160
Sebastian Redl9a920342008-12-11 19:48:14 +00001161 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162}
1163
1164/// ParseContinueStatement
1165/// jump-statement:
1166/// 'continue' ';'
1167///
1168/// Note: this lets the caller parse the end ';'.
1169///
Sean Huntbbd37c62009-11-21 08:43:09 +00001170Parser::OwningStmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
1171 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001172 delete Attr;
1173
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001175 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001176}
1177
1178/// ParseBreakStatement
1179/// jump-statement:
1180/// 'break' ';'
1181///
1182/// Note: this lets the caller parse the end ';'.
1183///
Sean Huntbbd37c62009-11-21 08:43:09 +00001184Parser::OwningStmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
1185 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001186 delete Attr;
1187
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001189 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001190}
1191
1192/// ParseReturnStatement
1193/// jump-statement:
1194/// 'return' expression[opt] ';'
Sean Huntbbd37c62009-11-21 08:43:09 +00001195Parser::OwningStmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
1196 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001197 delete Attr;
1198
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001199 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001201
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001202 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001203 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001204 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001205 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001206 ConsumeCodeCompletionToken();
1207 SkipUntil(tok::semi, false, true);
1208 return StmtError();
1209 }
1210
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001212 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001214 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 }
1216 }
Anders Carlssonf53b4432009-08-18 16:11:00 +00001217 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001218}
1219
Steve Naroff5f8aa692008-02-11 23:15:56 +00001220/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1221/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001222Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001223 if (Tok.is(tok::l_brace)) {
1224 unsigned short savedBraceCount = BraceCount;
1225 do {
1226 ConsumeAnyToken();
1227 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001228 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001229 // From the MS website: If used without braces, the __asm keyword means
1230 // that the rest of the line is an assembly-language statement.
1231 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001232 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001233 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001234 do {
1235 ConsumeAnyToken();
1236 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001237 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1238 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001239 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001240 }
Mike Stump95059b52009-12-11 00:04:56 +00001241 Token t;
1242 t.setKind(tok::string_literal);
1243 t.setLiteralData("\"FIXME: not done\"");
1244 t.clearFlag(Token::NeedsCleaning);
1245 t.setLength(17);
1246 OwningExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1247 ExprVector Constraints(Actions);
1248 ExprVector Exprs(Actions);
1249 ExprVector Clobbers(Actions);
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001250 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001251 move_arg(Constraints), move_arg(Exprs),
1252 move(AsmString), move_arg(Clobbers),
Mike Stump3b11fd32010-01-04 22:37:17 +00001253 Tok.getLocation(), true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001254}
1255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001257/// asm-statement:
1258/// gnu-asm-statement
1259/// ms-asm-statement
1260///
1261/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001262/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1263///
1264/// [GNU] asm-argument:
1265/// asm-string-literal
1266/// asm-string-literal ':' asm-operands[opt]
1267/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1268/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1269/// ':' asm-clobbers
1270///
1271/// [GNU] asm-clobbers:
1272/// asm-string-literal
1273/// asm-clobbers ',' asm-string-literal
1274///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001275/// [MS] ms-asm-statement:
1276/// '__asm' assembly-instruction ';'[opt]
1277/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1278///
1279/// [MS] assembly-instruction-list:
1280/// assembly-instruction ';'[opt]
1281/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1282///
Sebastian Redl9a920342008-12-11 19:48:14 +00001283Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001284 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001285 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001286
Steve Naroff5f8aa692008-02-11 23:15:56 +00001287 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001288 msAsm = true;
1289 return FuzzyParseMicrosoftAsmStatement();
1290 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 DeclSpec DS;
1292 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001293 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1296 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001297 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001299 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001300
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001302 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001303 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001304 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001306 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 }
1308 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001309
Sebastian Redleffa8d12008-12-10 00:02:53 +00001310 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001311 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001312 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001313
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001314 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001315 ExprVector Constraints(Actions);
1316 ExprVector Exprs(Actions);
1317 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001318
Anders Carlssondfab34a2008-02-05 23:03:50 +00001319 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001320 // We have a simple asm expression like 'asm("foo")'.
1321 SourceLocation RParenLoc = ConsumeParen();
1322 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1323 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1324 move_arg(Constraints), move_arg(Exprs),
1325 move(AsmString), move_arg(Clobbers),
1326 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001328
Chris Lattner64cb4752009-12-20 23:00:41 +00001329 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001330 bool AteExtraColon = false;
1331 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1332 // In C++ mode, parse "::" like ": :".
1333 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001334 ConsumeToken();
1335
Chris Lattner64056462009-12-20 23:08:04 +00001336 if (!AteExtraColon &&
1337 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001338 return StmtError();
1339 }
Chris Lattner64056462009-12-20 23:08:04 +00001340
Chris Lattner64cb4752009-12-20 23:00:41 +00001341 unsigned NumOutputs = Names.size();
1342
1343 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001344 if (AteExtraColon ||
1345 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1346 // In C++ mode, parse "::" like ": :".
1347 if (AteExtraColon)
1348 AteExtraColon = false;
1349 else {
1350 AteExtraColon = Tok.is(tok::coloncolon);
1351 ConsumeToken();
1352 }
1353
1354 if (!AteExtraColon &&
1355 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001356 return StmtError();
1357 }
1358
1359 assert(Names.size() == Constraints.size() &&
1360 Constraints.size() == Exprs.size() &&
1361 "Input operand size mismatch!");
1362
1363 unsigned NumInputs = Names.size() - NumOutputs;
1364
1365 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001366 if (AteExtraColon || Tok.is(tok::colon)) {
1367 if (!AteExtraColon)
1368 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001369
1370 // Parse the asm-string list for clobbers.
1371 while (1) {
1372 OwningExprResult Clobber(ParseAsmStringLiteral());
1373
1374 if (Clobber.isInvalid())
1375 break;
1376
1377 Clobbers.push_back(Clobber.release());
1378
1379 if (Tok.isNot(tok::comma)) break;
1380 ConsumeToken();
1381 }
1382 }
1383
1384 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1385 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001386 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001387 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001388 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001389 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001390}
1391
1392/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001393/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001394///
1395/// [GNU] asm-operands:
1396/// asm-operand
1397/// asm-operands ',' asm-operand
1398///
1399/// [GNU] asm-operand:
1400/// asm-string-literal '(' expression ')'
1401/// '[' identifier ']' asm-string-literal '(' expression ')'
1402///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001403//
1404// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001405bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1406 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1407 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001409 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001410 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001411
1412 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001414 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001417 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 Diag(Tok, diag::err_expected_ident);
1419 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001420 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001421 }
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Anders Carlssonb235fc22007-11-22 01:36:19 +00001423 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001424 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001425
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001426 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001428 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001429 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001430
Sebastian Redleffa8d12008-12-10 00:02:53 +00001431 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001432 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001433 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001434 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001435 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001436 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001437
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001438 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001439 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001441 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001443
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001445 SourceLocation OpenLoc = ConsumeParen();
1446 OwningExprResult Res(ParseExpression());
1447 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001448 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001450 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001452 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001454 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 ConsumeToken();
1456 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001457
1458 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001459}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001460
Chris Lattnerb28317a2009-03-28 19:18:32 +00001461Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001462 assert(Tok.is(tok::l_brace));
1463 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001464
Chris Lattner49f28ca2009-03-05 08:00:35 +00001465 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1466 PP.getSourceManager(),
1467 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001469 // Do not enter a scope for the brace, as the arguments are in the same scope
1470 // (the function body) as the body itself. Instead, just read the statement
1471 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001472 OwningStmtResult FnBody(ParseCompoundStatementBody());
1473
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001474 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001475 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001476 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001477 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001478
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001479 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001480}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001481
Sebastian Redld3a413d2009-04-26 20:35:05 +00001482/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1483///
1484/// function-try-block:
1485/// 'try' ctor-initializer[opt] compound-statement handler-seq
1486///
1487Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1488 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1489 SourceLocation TryLoc = ConsumeToken();
1490
1491 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1492 PP.getSourceManager(),
1493 "parsing function try block");
1494
1495 // Constructor initializer list?
1496 if (Tok.is(tok::colon))
1497 ParseConstructorInitializer(Decl);
1498
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001499 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001500 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1501 // If we failed to parse the try-catch, we just give the function an empty
1502 // compound statement as the body.
1503 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001504 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001505 MultiStmtArg(Actions), false);
1506
1507 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1508}
1509
Sebastian Redla0fd8652008-12-21 16:41:36 +00001510/// ParseCXXTryBlock - Parse a C++ try-block.
1511///
1512/// try-block:
1513/// 'try' compound-statement handler-seq
1514///
Sean Huntbbd37c62009-11-21 08:43:09 +00001515Parser::OwningStmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
1516 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001517 delete Attr;
1518
Sebastian Redla0fd8652008-12-21 16:41:36 +00001519 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1520
1521 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001522 return ParseCXXTryBlockCommon(TryLoc);
1523}
1524
1525/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1526/// function-try-block.
1527///
1528/// try-block:
1529/// 'try' compound-statement handler-seq
1530///
1531/// function-try-block:
1532/// 'try' ctor-initializer[opt] compound-statement handler-seq
1533///
1534/// handler-seq:
1535/// handler handler-seq[opt]
1536///
1537Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001538 if (Tok.isNot(tok::l_brace))
1539 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001540 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1541 OwningStmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001542 if (TryBlock.isInvalid())
1543 return move(TryBlock);
1544
1545 StmtVector Handlers(Actions);
Sean Huntbbd37c62009-11-21 08:43:09 +00001546 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1547 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1548 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1549 << Attr.Range;
1550 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00001551 if (Tok.isNot(tok::kw_catch))
1552 return StmtError(Diag(Tok, diag::err_expected_catch));
1553 while (Tok.is(tok::kw_catch)) {
1554 OwningStmtResult Handler(ParseCXXCatchBlock());
1555 if (!Handler.isInvalid())
1556 Handlers.push_back(Handler.release());
1557 }
1558 // Don't bother creating the full statement if we don't have any usable
1559 // handlers.
1560 if (Handlers.empty())
1561 return StmtError();
1562
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001563 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001564}
1565
1566/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1567///
1568/// handler:
1569/// 'catch' '(' exception-declaration ')' compound-statement
1570///
1571/// exception-declaration:
1572/// type-specifier-seq declarator
1573/// type-specifier-seq abstract-declarator
1574/// type-specifier-seq
1575/// '...'
1576///
1577Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1578 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1579
1580 SourceLocation CatchLoc = ConsumeToken();
1581
1582 SourceLocation LParenLoc = Tok.getLocation();
1583 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1584 return StmtError();
1585
1586 // C++ 3.3.2p3:
1587 // The name in a catch exception-declaration is local to the handler and
1588 // shall not be redeclared in the outermost block of the handler.
1589 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1590
1591 // exception-declaration is equivalent to '...' or a parameter-declaration
1592 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001593 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001594 if (Tok.isNot(tok::ellipsis)) {
1595 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001596 if (ParseCXXTypeSpecifierSeq(DS))
1597 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001598 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1599 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001600 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001601 } else
1602 ConsumeToken();
1603
1604 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1605 return StmtError();
1606
1607 if (Tok.isNot(tok::l_brace))
1608 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1609
Sean Huntbbd37c62009-11-21 08:43:09 +00001610 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1611 OwningStmtResult Block(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001612 if (Block.isInvalid())
1613 return move(Block);
1614
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001615 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001616}