blob: 9b2227002f1bbf86114b5b1cda1e0921a7e00c34 [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);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000080
Sean Huntbbd37c62009-11-21 08:43:09 +000081 CXX0XAttributeList Attr;
82 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
83 Attr = ParseCXX0XAttributes();
Ted Kremenek1e377652010-02-11 02:19:13 +000084 llvm::OwningPtr<AttributeList> AttrList(Attr.AttrList);
Sean Huntbbd37c62009-11-21 08:43:09 +000085
Reid Spencer5f016e22007-07-11 17:01:13 +000086 // Cases in this switch statement should fall through if the parser expects
87 // the token to end in a semicolon (in which case SemiError should be set),
88 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000089 tok::TokenKind Kind = Tok.getKind();
90 SourceLocation AtLoc;
91 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092 case tok::at: // May be a @try or @throw statement
93 {
94 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000095 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000096 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000097
Douglas Gregor791215b2009-09-21 20:51:25 +000098 case tok::code_completion:
Douglas Gregor01dfea02010-01-10 23:08:15 +000099 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Statement);
Douglas Gregor791215b2009-09-21 20:51:25 +0000100 ConsumeToken();
101 return ParseStatementOrDeclaration(OnlyStatement);
102
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000103 case tok::identifier:
104 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
105 // identifier ':' statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000106 return ParseLabeledStatement(AttrList.take());
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000107 }
108 // PASS THROUGH.
109
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000110 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000111 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000112 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek1e377652010-02-11 02:19:13 +0000113 AttrList.take(); //Passing 'Attr' to ParseDeclaration transfers ownership.
Sean Huntbbd37c62009-11-21 08:43:09 +0000114 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd,
115 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000116 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000117 }
118
119 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000121 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Sean Huntbbd37c62009-11-21 08:43:09 +0000124 // FIXME: Use the attributes
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000125 // expression[opt] ';'
126 OwningExprResult Expr(ParseExpression());
127 if (Expr.isInvalid()) {
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000128 // If the expression is invalid, skip ahead to the next semicolon or '}'.
129 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000130 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000131 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
132 if (Tok.is(tok::semi))
133 ConsumeToken();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000134 return StmtError();
135 }
136 // Otherwise, eat the semicolon.
137 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000138 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000139 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_case: // C99 6.8.1: labeled-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000142 return ParseCaseStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case tok::kw_default: // C99 6.8.1: labeled-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000144 return ParseDefaultStatement(AttrList.take());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 case tok::l_brace: // C99 6.8.2: compound-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000147 return ParseCompoundStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000149 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000150
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 case tok::kw_if: // C99 6.8.4.1: if-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000152 return ParseIfStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000154 return ParseSwitchStatement(AttrList.take());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 case tok::kw_while: // C99 6.8.5.1: while-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000157 return ParseWhileStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 case tok::kw_do: // C99 6.8.5.2: do-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000159 Res = ParseDoStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000160 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 break;
162 case tok::kw_for: // C99 6.8.5.3: for-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000163 return ParseForStatement(AttrList.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000164
165 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000166 Res = ParseGotoStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000167 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 break;
169 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000170 Res = ParseContinueStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000171 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 break;
173 case tok::kw_break: // C99 6.8.6.3: break-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000174 Res = ParseBreakStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000175 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 break;
177 case tok::kw_return: // C99 6.8.6.4: return-statement
Ted Kremenek1e377652010-02-11 02:19:13 +0000178 Res = ParseReturnStatement(AttrList.take());
Chris Lattner6869d8e2009-06-14 00:07:48 +0000179 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000181
Sebastian Redla0fd8652008-12-21 16:41:36 +0000182 case tok::kw_asm: {
Sean Huntbbd37c62009-11-21 08:43:09 +0000183 if (Attr.HasAttr)
184 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
185 << Attr.Range;
Steve Naroffd62701b2008-02-07 03:50:06 +0000186 bool msAsm = false;
187 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000188 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000189 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 break;
191 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000192
Sebastian Redla0fd8652008-12-21 16:41:36 +0000193 case tok::kw_try: // C++ 15: try-block
Ted Kremenek1e377652010-02-11 02:19:13 +0000194 return ParseCXXTryBlock(AttrList.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +0000195 }
196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000198 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000200 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000201 // If the result was valid, then we do want to diagnose this. Use
202 // ExpectAndConsume to emit the diagnostic, even though we know it won't
203 // succeed.
204 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000205 // Skip until we see a } or ;, but don't eat it.
206 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Sebastian Redl61364dd2008-12-11 19:30:53 +0000209 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000212/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000213///
214/// labeled-statement:
215/// identifier ':' statement
216/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000217///
Sean Huntbbd37c62009-11-21 08:43:09 +0000218Parser::OwningStmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000219 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
220 "Not an identifier!");
221
Ted Kremenek1e377652010-02-11 02:19:13 +0000222 llvm::OwningPtr<AttributeList> AttrList(Attr);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000223 Token IdentTok = Tok; // Save the whole token.
224 ConsumeToken(); // eat the identifier.
225
226 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000227
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000228 // identifier ':' statement
229 SourceLocation ColonLoc = ConsumeToken();
230
231 // Read label attributes, if present.
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000232 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000233 AttrList.reset(addAttributeLists(AttrList.take(), ParseGNUAttributes()));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000234
Sebastian Redl61364dd2008-12-11 19:30:53 +0000235 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000236
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000237 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000238 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000239 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000240
Ted Kremenek1e377652010-02-11 02:19:13 +0000241 // FIXME: use attributes?
Sebastian Redlde307472009-01-11 00:38:46 +0000242 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
243 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000244 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000245}
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
247/// ParseCaseStatement
248/// labeled-statement:
249/// 'case' constant-expression ':' statement
250/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
251///
Sean Huntbbd37c62009-11-21 08:43:09 +0000252Parser::OwningStmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000253 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000254 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000255 delete Attr;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattner24e1e702009-03-04 04:23:07 +0000257 // It is very very common for code to contain many case statements recursively
258 // nested, as in (but usually without indentation):
259 // case 1:
260 // case 2:
261 // case 3:
262 // case 4:
263 // case 5: etc.
264 //
265 // Parsing this naively works, but is both inefficient and can cause us to run
266 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000267 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000268 // but all the grossness is constrained to ParseCaseStatement (and some
269 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner24e1e702009-03-04 04:23:07 +0000271 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
272 // example above.
273 OwningStmtResult TopLevelCase(Actions, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattner24e1e702009-03-04 04:23:07 +0000275 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
276 // gets updated each time a new case is parsed, and whose body is unset so
277 // far. When parsing 'case 4', this is the 'case 3' node.
278 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner24e1e702009-03-04 04:23:07 +0000280 // While we have case statements, eat and stack them.
281 do {
282 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000284 if (Tok.is(tok::code_completion)) {
285 Actions.CodeCompleteCase(CurScope);
286 ConsumeToken();
287 }
288
Chris Lattner6fb09c82009-12-10 00:38:54 +0000289 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
290 /// Disable this form of error recovery while we're parsing the case
291 /// expression.
292 ColonProtectionRAIIObject ColonProtection(*this);
293
Chris Lattner24e1e702009-03-04 04:23:07 +0000294 OwningExprResult LHS(ParseConstantExpression());
295 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000297 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000299
Chris Lattner24e1e702009-03-04 04:23:07 +0000300 // GNU case range extension.
301 SourceLocation DotDotDotLoc;
302 OwningExprResult RHS(Actions);
303 if (Tok.is(tok::ellipsis)) {
304 Diag(Tok, diag::ext_gnu_case_range);
305 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000306
Chris Lattner24e1e702009-03-04 04:23:07 +0000307 RHS = ParseConstantExpression();
308 if (RHS.isInvalid()) {
309 SkipUntil(tok::colon);
310 return StmtError();
311 }
312 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000313
314 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000315
Chris Lattner24e1e702009-03-04 04:23:07 +0000316 if (Tok.isNot(tok::colon)) {
317 Diag(Tok, diag::err_expected_colon_after) << "'case'";
318 SkipUntil(tok::colon);
319 return StmtError();
320 }
321
322 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner24e1e702009-03-04 04:23:07 +0000324 OwningStmtResult Case =
325 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
326 move(RHS), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner24e1e702009-03-04 04:23:07 +0000328 // If we had a sema error parsing this case, then just ignore it and
329 // continue parsing the sub-stmt.
330 if (Case.isInvalid()) {
331 if (TopLevelCase.isInvalid()) // No parsed case stmts.
332 return ParseStatement();
333 // Otherwise, just don't add it as a nested case.
334 } else {
335 // If this is the first case statement we parsed, it becomes TopLevelCase.
336 // Otherwise we link it into the current chain.
337 StmtTy *NextDeepest = Case.get();
338 if (TopLevelCase.isInvalid())
339 TopLevelCase = move(Case);
340 else
341 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
342 DeepestParsedCaseStmt = NextDeepest;
343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattner24e1e702009-03-04 04:23:07 +0000345 // Handle all case statements.
346 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Chris Lattner24e1e702009-03-04 04:23:07 +0000348 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner24e1e702009-03-04 04:23:07 +0000350 // If we found a non-case statement, start by parsing it.
351 OwningStmtResult SubStmt(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattner24e1e702009-03-04 04:23:07 +0000353 if (Tok.isNot(tok::r_brace)) {
354 SubStmt = ParseStatement();
355 } else {
356 // Nicely diagnose the common error "switch (X) { case 4: }", which is
357 // not valid.
358 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000360 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Chris Lattner24e1e702009-03-04 04:23:07 +0000363 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000364 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000365 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Chris Lattner24e1e702009-03-04 04:23:07 +0000367 // Install the body into the most deeply-nested case.
368 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000369
Chris Lattner24e1e702009-03-04 04:23:07 +0000370 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000371 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000372}
373
374/// ParseDefaultStatement
375/// labeled-statement:
376/// 'default' ':' statement
377/// Note that this does not parse the 'statement' at the end.
378///
Sean Huntbbd37c62009-11-21 08:43:09 +0000379Parser::OwningStmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
380 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000381 delete Attr;
382
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000383 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
385
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000386 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000387 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000389 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000391
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000395 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000397 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
399
Sebastian Redl61364dd2008-12-11 19:30:53 +0000400 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000401 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000402 return StmtError();
403
Sebastian Redl117054a2008-12-28 16:13:43 +0000404 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000405 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000406}
407
408
409/// ParseCompoundStatement - Parse a "{}" block.
410///
411/// compound-statement: [C99 6.8.2]
412/// { block-item-list[opt] }
413/// [GNU] { label-declarations block-item-list } [TODO]
414///
415/// block-item-list:
416/// block-item
417/// block-item-list block-item
418///
419/// block-item:
420/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000421/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000422/// statement
423/// [OMP] openmp-directive [TODO]
424///
425/// [GNU] label-declarations:
426/// [GNU] label-declaration
427/// [GNU] label-declarations label-declaration
428///
429/// [GNU] label-declaration:
430/// [GNU] '__label__' identifier-list ';'
431///
432/// [OMP] openmp-directive: [TODO]
433/// [OMP] barrier-directive
434/// [OMP] flush-directive
435///
Sean Huntbbd37c62009-11-21 08:43:09 +0000436Parser::OwningStmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
437 bool isStmtExpr) {
438 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000439 delete Attr;
440
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000441 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000442
Chris Lattner31e05722007-08-26 06:24:45 +0000443 // Enter a scope to hold everything within the compound stmt. Compound
444 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000445 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000446
447 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000448 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000449}
450
451
452/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000453/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000454/// consume the '}' at the end of the block. It does not manipulate the scope
455/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000456Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000457 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000458 Tok.getLocation(),
459 "in compound statement ('{}')");
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
462
463 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000464 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000465
466 typedef StmtVector StmtsTy;
467 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000468 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000469 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000470 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000471 R = ParseStatementOrDeclaration(false);
472 } else {
473 // __extension__ can start declarations and it can also be a unary
474 // operator for expressions. Consume multiple __extension__ markers here
475 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000476 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000477 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000478 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000479 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000480
Sean Huntbbd37c62009-11-21 08:43:09 +0000481 CXX0XAttributeList Attr;
482 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
483 Attr = ParseCXX0XAttributes();
484
Chris Lattner45a566c2007-08-27 01:01:57 +0000485 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000486 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000487 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000488 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000489 ExtensionRAIIObject O(Diags);
490
Chris Lattner97144fc2009-04-02 04:16:50 +0000491 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000492 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
493 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000494 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000495 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000496 // Otherwise this was a unary __extension__ marker.
497 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000498
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000499 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000500 SkipUntil(tok::semi);
501 continue;
502 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000503
Sean Huntbbd37c62009-11-21 08:43:09 +0000504 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000505 // Eat the semicolon at the end of stmt and convert the expr into a
506 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000507 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000508 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000509 }
510 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000511
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000512 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000513 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000517 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000519 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000521
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000523 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000524 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000525}
526
Chris Lattner15ff1112008-12-12 06:31:07 +0000527/// ParseParenExprOrCondition:
528/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000529/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000530///
531/// This function parses and performs error recovery on the specified condition
532/// or expression (depending on whether we're in C++ or C mode). This function
533/// goes out of its way to recover well. It returns true if there was a parser
534/// error (the right paren couldn't be found), which indicates that the caller
535/// should try to recover harder. It returns false if the condition is
536/// successfully parsed. Note that a successful parse can still have semantic
537/// errors in the condition.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000538bool Parser::ParseParenExprOrCondition(OwningExprResult &ExprResult,
539 DeclPtrTy &DeclResult) {
540 bool ParseError = false;
541
Chris Lattner15ff1112008-12-12 06:31:07 +0000542 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000543 if (getLang().CPlusPlus)
544 ParseError = ParseCXXCondition(ExprResult, DeclResult);
545 else {
546 ExprResult = ParseExpression();
547 DeclResult = DeclPtrTy();
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner15ff1112008-12-12 06:31:07 +0000550 // If the parser was confused by the condition and we don't have a ')', try to
551 // recover by skipping ahead to a semi and bailing out. If condexp is
552 // semantically invalid but we have well formed code, keep going.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000553 if (ExprResult.isInvalid() && !DeclResult.get() && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000554 SkipUntil(tok::semi);
555 // Skipping may have stopped if it found the containing ')'. If so, we can
556 // continue parsing the if statement.
557 if (Tok.isNot(tok::r_paren))
558 return true;
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner15ff1112008-12-12 06:31:07 +0000561 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000562 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000563 return false;
564}
565
566
Reid Spencer5f016e22007-07-11 17:01:13 +0000567/// ParseIfStatement
568/// if-statement: [C99 6.8.4.1]
569/// 'if' '(' expression ')' statement
570/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000571/// [C++] 'if' '(' condition ')' statement
572/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000573///
Sean Huntbbd37c62009-11-21 08:43:09 +0000574Parser::OwningStmtResult Parser::ParseIfStatement(AttributeList *Attr) {
575 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000576 delete Attr;
577
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000578 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
580
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000581 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000582 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000584 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000586
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000587 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
588
Chris Lattner22153252007-08-26 23:08:06 +0000589 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
590 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000591 //
592 // C++ 6.4p3:
593 // A name introduced by a declaration in a condition is in scope from its
594 // point of declaration until the end of the substatements controlled by the
595 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000596 // C++ 3.3.2p4:
597 // Names declared in the for-init-statement, and in the condition of if,
598 // while, for, and switch statements are local to the if, while, for, or
599 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000600 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000601 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000604 OwningExprResult CondExp(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000605 DeclPtrTy CondVar;
606 if (ParseParenExprOrCondition(CondExp, CondVar))
Chris Lattner15ff1112008-12-12 06:31:07 +0000607 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000608
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000609 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp));
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Chris Lattner0ecea032007-08-22 05:28:50 +0000611 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000612 // there is no compound stmt. C90 does not have this clause. We only do this
613 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000614 //
615 // C++ 6.4p1:
616 // The substatement in a selection-statement (each substatement, in the else
617 // form of the if statement) implicitly defines a local scope.
618 //
619 // For C++ we create a scope for the condition and a new scope for
620 // substatements because:
621 // -When the 'then' scope exits, we want the condition declaration to still be
622 // active for the 'else' scope too.
623 // -Sema will detect name clashes by considering declarations of a
624 // 'ControlScope' as part of its direct subscope.
625 // -If we wanted the condition and substatement to be in the same scope, we
626 // would have to notify ParseStatement not to create a new scope. It's
627 // simpler to let it create a new scope.
628 //
Mike Stump1eb44332009-09-09 15:08:12 +0000629 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000630 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000631
Chris Lattnerb96728d2007-10-29 05:08:52 +0000632 // Read the 'then' stmt.
633 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000634 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000635
Chris Lattnera36ce712007-08-22 05:16:28 +0000636 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000637 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // If it has an else, parse it.
640 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000641 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000642 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000643
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000644 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000646 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000647
Chris Lattner0ecea032007-08-22 05:28:50 +0000648 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000649 // there is no compound stmt. C90 does not have this clause. We only do
650 // this if the body isn't a compound statement to avoid push/pop in common
651 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000652 //
653 // C++ 6.4p1:
654 // The substatement in a selection-statement (each substatement, in the else
655 // form of the if statement) implicitly defines a local scope.
656 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000657 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000658 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000659
Chris Lattner966c78b2010-04-12 06:12:50 +0000660 // Regardless of whether or not InnerScope actually pushed a scope, set the
661 // ElseScope flag for the innermost scope so we can diagnose use of the if
662 // condition variable in C++.
663 unsigned OldFlags = CurScope->getFlags();
664 CurScope->setFlags(OldFlags | Scope::ElseScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000666 CurScope->setFlags(OldFlags);
667
Chris Lattnera36ce712007-08-22 05:16:28 +0000668 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000669 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000671
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000672 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattner18914bc2008-12-12 06:19:11 +0000674 // If the condition was invalid, discard the if statement. We could recover
675 // better by replacing it with a valid expr, but don't do that yet.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000676 if (CondExp.isInvalid() && !CondVar.get())
Chris Lattner18914bc2008-12-12 06:19:11 +0000677 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000678
Chris Lattnerb96728d2007-10-29 05:08:52 +0000679 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000680 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000681 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000682 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
683 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
684 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000685 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000686 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000687 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000688
Chris Lattnerb96728d2007-10-29 05:08:52 +0000689 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000690 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000691 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000692 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000693 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000694
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000695 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000696 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000697}
698
699/// ParseSwitchStatement
700/// switch-statement:
701/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000702/// [C++] 'switch' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000703Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
704 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000705 delete Attr;
706
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000707 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
709
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000710 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000711 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000713 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 }
Chris Lattner22153252007-08-26 23:08:06 +0000715
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000716 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
717
Chris Lattner22153252007-08-26 23:08:06 +0000718 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
719 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000720 //
721 // C++ 6.4p3:
722 // A name introduced by a declaration in a condition is in scope from its
723 // point of declaration until the end of the substatements controlled by the
724 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000725 // C++ 3.3.2p4:
726 // Names declared in the for-init-statement, and in the condition of if,
727 // while, for, and switch statements are local to the if, while, for, or
728 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000729 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000730 unsigned ScopeFlags = Scope::BreakScope;
731 if (C99orCXX)
732 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000733 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000734
735 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000736 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000737 DeclPtrTy CondVar;
738 if (ParseParenExprOrCondition(Cond, CondVar))
Sebastian Redl9a920342008-12-11 19:48:14 +0000739 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000740
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000741 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000742
Douglas Gregorbe724ba2009-11-25 06:20:02 +0000743 OwningStmtResult Switch = Actions.ActOnStartOfSwitchStmt(FullCond, CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000744
Chris Lattner0ecea032007-08-22 05:28:50 +0000745 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000746 // there is no compound stmt. C90 does not have this clause. We only do this
747 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000748 //
749 // C++ 6.4p1:
750 // The substatement in a selection-statement (each substatement, in the else
751 // form of the if statement) implicitly defines a local scope.
752 //
753 // See comments in ParseIfStatement for why we create a scope for the
754 // condition and a new scope for substatement in C++.
755 //
Mike Stump1eb44332009-09-09 15:08:12 +0000756 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000757 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000758
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000760 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000761
Chris Lattner7e52de42010-01-24 01:50:29 +0000762 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000763 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000764 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000765
Chris Lattner7e52de42010-01-24 01:50:29 +0000766 if (Cond.isInvalid() && !CondVar.get()) {
767 Actions.ActOnSwitchBodyError(SwitchLoc, move(Switch), move(Body));
Chris Lattner15ff1112008-12-12 06:31:07 +0000768 return StmtError();
Chris Lattner7e52de42010-01-24 01:50:29 +0000769 }
Sebastian Redlde307472009-01-11 00:38:46 +0000770
Chris Lattner7e52de42010-01-24 01:50:29 +0000771 if (Body.isInvalid())
772 // FIXME: Remove the case statement list from the Switch statement.
773 Body = Actions.ActOnNullStmt(Tok.getLocation());
774
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000775 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000776}
777
778/// ParseWhileStatement
779/// while-statement: [C99 6.8.5.1]
780/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000781/// [C++] 'while' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000782Parser::OwningStmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
783 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000784 delete Attr;
785
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000786 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 SourceLocation WhileLoc = Tok.getLocation();
788 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000789
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000790 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000791 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000793 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000795
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000796 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
797
Chris Lattner22153252007-08-26 23:08:06 +0000798 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
799 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000800 //
801 // C++ 6.4p3:
802 // A name introduced by a declaration in a condition is in scope from its
803 // point of declaration until the end of the substatements controlled by the
804 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000805 // C++ 3.3.2p4:
806 // Names declared in the for-init-statement, and in the condition of if,
807 // while, for, and switch statements are local to the if, while, for, or
808 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000809 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000810 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000811 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000812 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
813 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000814 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000815 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
816 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000817
818 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000819 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000820 DeclPtrTy CondVar;
821 if (ParseParenExprOrCondition(Cond, CondVar))
Chris Lattner15ff1112008-12-12 06:31:07 +0000822 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000823
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000824 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Chris Lattner0ecea032007-08-22 05:28:50 +0000826 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000827 // there is no compound stmt. C90 does not have this clause. We only do this
828 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000829 //
830 // C++ 6.5p2:
831 // The substatement in an iteration-statement implicitly defines a local scope
832 // which is entered and exited each time through the loop.
833 //
834 // See comments in ParseIfStatement for why we create a scope for the
835 // condition and a new scope for substatement in C++.
836 //
Mike Stump1eb44332009-09-09 15:08:12 +0000837 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000838 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000841 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000842
Chris Lattner0ecea032007-08-22 05:28:50 +0000843 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000844 InnerScope.Exit();
845 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000846
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000847 if ((Cond.isInvalid() && !CondVar.get()) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000848 return StmtError();
849
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000850 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000851}
852
853/// ParseDoStatement
854/// do-statement: [C99 6.8.5.2]
855/// 'do' statement 'while' '(' expression ')' ';'
856/// Note: this lets the caller parse the end ';'.
Sean Huntbbd37c62009-11-21 08:43:09 +0000857Parser::OwningStmtResult Parser::ParseDoStatement(AttributeList *Attr) {
858 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000859 delete Attr;
860
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000861 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000863
Chris Lattner22153252007-08-26 23:08:06 +0000864 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
865 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000866 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000867 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000868 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000869 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000870 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000871
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000872 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873
Chris Lattner0ecea032007-08-22 05:28:50 +0000874 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000875 // there is no compound stmt. C90 does not have this clause. We only do this
876 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000877 //
878 // C++ 6.5p2:
879 // The substatement in an iteration-statement implicitly defines a local scope
880 // which is entered and exited each time through the loop.
881 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000882 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000883 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000884 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000887 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000888
Chris Lattner0ecea032007-08-22 05:28:50 +0000889 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000890 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000891
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000892 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000893 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000894 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000895 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000896 SkipUntil(tok::semi, false, true);
897 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000898 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 }
900 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000901
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000902 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000903 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000904 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000905 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000907
Chris Lattnerff871fb2008-12-12 06:35:28 +0000908 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000909 SourceLocation LPLoc = ConsumeParen();
910 OwningExprResult Cond = ParseExpression();
911 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000912 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000913
Sebastian Redl9a920342008-12-11 19:48:14 +0000914 if (Cond.isInvalid() || Body.isInvalid())
915 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000916
Chris Lattner98913592009-06-12 23:04:47 +0000917 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
918 move(Cond), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000919}
920
921/// ParseForStatement
922/// for-statement: [C99 6.8.5.3]
923/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
924/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000925/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
926/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000927/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
928/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000929///
930/// [C++] for-init-statement:
931/// [C++] expression-statement
932/// [C++] simple-declaration
933///
Sean Huntbbd37c62009-11-21 08:43:09 +0000934Parser::OwningStmtResult Parser::ParseForStatement(AttributeList *Attr) {
935 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000936 delete Attr;
937
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000938 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000940
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000941 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000942 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000944 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000946
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000947 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000948
Chris Lattner22153252007-08-26 23:08:06 +0000949 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
950 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000951 //
952 // C++ 6.4p3:
953 // A name introduced by a declaration in a condition is in scope from its
954 // point of declaration until the end of the substatements controlled by the
955 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000956 // C++ 3.3.2p4:
957 // Names declared in the for-init-statement, and in the condition of if,
958 // while, for, and switch statements are local to the if, while, for, or
959 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000960 // C++ 6.5.3p1:
961 // Names declared in the for-init-statement are in the same declarative-region
962 // as those declared in the condition.
963 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000964 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000965 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000966 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
967 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000968 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000969 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
970
971 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000972
973 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000974 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000975
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000976 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000977 OwningStmtResult FirstPart(Actions);
978 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000979 DeclPtrTy SecondVar;
980
Douglas Gregor791215b2009-09-21 20:51:25 +0000981 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000982 Actions.CodeCompleteOrdinaryName(CurScope,
983 C99orCXXorObjC? Action::CCC_ForInit
984 : Action::CCC_Expression);
Douglas Gregor791215b2009-09-21 20:51:25 +0000985 ConsumeToken();
986 }
987
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000989 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 // no first part, eat the ';'.
991 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000992 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000994 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000996
Sean Huntbbd37c62009-11-21 08:43:09 +0000997 AttributeList *AttrList = 0;
998 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
999 AttrList = ParseCXX0XAttributes().AttrList;
1000
Chris Lattner97144fc2009-04-02 04:16:50 +00001001 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +00001002 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +00001003 AttrList, false);
Chris Lattnercd147752009-03-29 17:27:48 +00001004 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattnercd147752009-03-29 17:27:48 +00001006 if (Tok.is(tok::semi)) { // for (int x = 4;
1007 ConsumeToken();
1008 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001009 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001010 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001011 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001012 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001013 } else {
1014 Diag(Tok, diag::err_expected_semi_for);
1015 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001016 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 } else {
1018 Value = ParseExpression();
1019
1020 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001021 if (!Value.isInvalid())
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001022 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +00001023
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001024 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001026 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001027 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001028 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001029 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001030 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 SkipUntil(tok::semi);
1032 }
1033 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001034 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001035 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001036 // Parse the second part of the for specifier.
1037 if (Tok.is(tok::semi)) { // for (...;;
1038 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001039 } else {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001040 if (getLang().CPlusPlus)
1041 ParseCXXCondition(SecondPart, SecondVar);
1042 else
1043 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001044 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001045
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001046 if (Tok.is(tok::semi)) {
1047 ConsumeToken();
1048 } else {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001049 if (!SecondPart.isInvalid() || SecondVar.get())
1050 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001051 SkipUntil(tok::semi);
1052 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001053
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001054 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +00001055 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +00001056 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 // Match the ')'.
1059 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001060
Chris Lattner0ecea032007-08-22 05:28:50 +00001061 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001062 // there is no compound stmt. C90 does not have this clause. We only do this
1063 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001064 //
1065 // C++ 6.5p2:
1066 // The substatement in an iteration-statement implicitly defines a local scope
1067 // which is entered and exited each time through the loop.
1068 //
1069 // See comments in ParseIfStatement for why we create a scope for
1070 // for-init-statement/condition and a new scope for substatement in C++.
1071 //
Mike Stump1eb44332009-09-09 15:08:12 +00001072 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001073 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001076 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001077
Chris Lattner0ecea032007-08-22 05:28:50 +00001078 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001079 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001080
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001082 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001083
1084 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001085 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001086
1087 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001088 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001089 Actions.MakeFullExpr(SecondPart), SecondVar,
1090 Actions.MakeFullExpr(ThirdPart), RParenLoc,
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001091 move(Body));
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Chris Lattner682bf922009-03-29 16:50:03 +00001093 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1094 move(FirstPart),
1095 move(SecondPart),
1096 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001097}
1098
1099/// ParseGotoStatement
1100/// jump-statement:
1101/// 'goto' identifier ';'
1102/// [GNU] 'goto' '*' expression ';'
1103///
1104/// Note: this lets the caller parse the end ';'.
1105///
Sean Huntbbd37c62009-11-21 08:43:09 +00001106Parser::OwningStmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
1107 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001108 delete Attr;
1109
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001110 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001112
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001113 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001114 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001115 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 Tok.getIdentifierInfo());
1117 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001118 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 // GNU indirect goto extension.
1120 Diag(Tok, diag::ext_gnu_indirect_goto);
1121 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001122 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001123 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001125 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001127 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001128 } else {
1129 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001130 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001132
Sebastian Redl9a920342008-12-11 19:48:14 +00001133 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134}
1135
1136/// ParseContinueStatement
1137/// jump-statement:
1138/// 'continue' ';'
1139///
1140/// Note: this lets the caller parse the end ';'.
1141///
Sean Huntbbd37c62009-11-21 08:43:09 +00001142Parser::OwningStmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
1143 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001144 delete Attr;
1145
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001147 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001148}
1149
1150/// ParseBreakStatement
1151/// jump-statement:
1152/// 'break' ';'
1153///
1154/// Note: this lets the caller parse the end ';'.
1155///
Sean Huntbbd37c62009-11-21 08:43:09 +00001156Parser::OwningStmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
1157 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001158 delete Attr;
1159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001161 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162}
1163
1164/// ParseReturnStatement
1165/// jump-statement:
1166/// 'return' expression[opt] ';'
Sean Huntbbd37c62009-11-21 08:43:09 +00001167Parser::OwningStmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
1168 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001169 delete Attr;
1170
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001171 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001173
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001174 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001175 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001177 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001179 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 }
1181 }
Anders Carlssonf53b4432009-08-18 16:11:00 +00001182 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001183}
1184
Steve Naroff5f8aa692008-02-11 23:15:56 +00001185/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1186/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001187Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001188 if (Tok.is(tok::l_brace)) {
1189 unsigned short savedBraceCount = BraceCount;
1190 do {
1191 ConsumeAnyToken();
1192 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001193 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001194 // From the MS website: If used without braces, the __asm keyword means
1195 // that the rest of the line is an assembly-language statement.
1196 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001197 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001198 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001199 do {
1200 ConsumeAnyToken();
1201 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001202 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1203 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001204 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001205 }
Mike Stump95059b52009-12-11 00:04:56 +00001206 Token t;
1207 t.setKind(tok::string_literal);
1208 t.setLiteralData("\"FIXME: not done\"");
1209 t.clearFlag(Token::NeedsCleaning);
1210 t.setLength(17);
1211 OwningExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1212 ExprVector Constraints(Actions);
1213 ExprVector Exprs(Actions);
1214 ExprVector Clobbers(Actions);
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001215 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001216 move_arg(Constraints), move_arg(Exprs),
1217 move(AsmString), move_arg(Clobbers),
Mike Stump3b11fd32010-01-04 22:37:17 +00001218 Tok.getLocation(), true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001219}
1220
Reid Spencer5f016e22007-07-11 17:01:13 +00001221/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001222/// asm-statement:
1223/// gnu-asm-statement
1224/// ms-asm-statement
1225///
1226/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001227/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1228///
1229/// [GNU] asm-argument:
1230/// asm-string-literal
1231/// asm-string-literal ':' asm-operands[opt]
1232/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1233/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1234/// ':' asm-clobbers
1235///
1236/// [GNU] asm-clobbers:
1237/// asm-string-literal
1238/// asm-clobbers ',' asm-string-literal
1239///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001240/// [MS] ms-asm-statement:
1241/// '__asm' assembly-instruction ';'[opt]
1242/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1243///
1244/// [MS] assembly-instruction-list:
1245/// assembly-instruction ';'[opt]
1246/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1247///
Sebastian Redl9a920342008-12-11 19:48:14 +00001248Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001249 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001250 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001251
Steve Naroff5f8aa692008-02-11 23:15:56 +00001252 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001253 msAsm = true;
1254 return FuzzyParseMicrosoftAsmStatement();
1255 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 DeclSpec DS;
1257 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001258 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001259
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1261 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001262 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001264 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001265
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001267 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001268 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001269 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001271 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 }
1273 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001274
Sebastian Redleffa8d12008-12-10 00:02:53 +00001275 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001276 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001277 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001278
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001279 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001280 ExprVector Constraints(Actions);
1281 ExprVector Exprs(Actions);
1282 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001283
Anders Carlssondfab34a2008-02-05 23:03:50 +00001284 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001285 // We have a simple asm expression like 'asm("foo")'.
1286 SourceLocation RParenLoc = ConsumeParen();
1287 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1288 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1289 move_arg(Constraints), move_arg(Exprs),
1290 move(AsmString), move_arg(Clobbers),
1291 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001293
Chris Lattner64cb4752009-12-20 23:00:41 +00001294 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001295 bool AteExtraColon = false;
1296 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1297 // In C++ mode, parse "::" like ": :".
1298 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001299 ConsumeToken();
1300
Chris Lattner64056462009-12-20 23:08:04 +00001301 if (!AteExtraColon &&
1302 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001303 return StmtError();
1304 }
Chris Lattner64056462009-12-20 23:08:04 +00001305
Chris Lattner64cb4752009-12-20 23:00:41 +00001306 unsigned NumOutputs = Names.size();
1307
1308 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001309 if (AteExtraColon ||
1310 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1311 // In C++ mode, parse "::" like ": :".
1312 if (AteExtraColon)
1313 AteExtraColon = false;
1314 else {
1315 AteExtraColon = Tok.is(tok::coloncolon);
1316 ConsumeToken();
1317 }
1318
1319 if (!AteExtraColon &&
1320 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001321 return StmtError();
1322 }
1323
1324 assert(Names.size() == Constraints.size() &&
1325 Constraints.size() == Exprs.size() &&
1326 "Input operand size mismatch!");
1327
1328 unsigned NumInputs = Names.size() - NumOutputs;
1329
1330 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001331 if (AteExtraColon || Tok.is(tok::colon)) {
1332 if (!AteExtraColon)
1333 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001334
1335 // Parse the asm-string list for clobbers.
1336 while (1) {
1337 OwningExprResult Clobber(ParseAsmStringLiteral());
1338
1339 if (Clobber.isInvalid())
1340 break;
1341
1342 Clobbers.push_back(Clobber.release());
1343
1344 if (Tok.isNot(tok::comma)) break;
1345 ConsumeToken();
1346 }
1347 }
1348
1349 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1350 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001351 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001352 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001353 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001354 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001355}
1356
1357/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001358/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001359///
1360/// [GNU] asm-operands:
1361/// asm-operand
1362/// asm-operands ',' asm-operand
1363///
1364/// [GNU] asm-operand:
1365/// asm-string-literal '(' expression ')'
1366/// '[' identifier ']' asm-string-literal '(' expression ')'
1367///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001368//
1369// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001370bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1371 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1372 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001374 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001375 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001376
1377 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001379 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001382 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 Diag(Tok, diag::err_expected_ident);
1384 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001385 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 }
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Anders Carlssonb235fc22007-11-22 01:36:19 +00001388 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001389 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001390
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001391 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001393 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001394 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001395
Sebastian Redleffa8d12008-12-10 00:02:53 +00001396 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001397 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001398 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001399 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001400 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001401 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001402
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001403 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001404 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001405 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001406 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001408
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001410 SourceLocation OpenLoc = ConsumeParen();
1411 OwningExprResult Res(ParseExpression());
1412 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001413 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001415 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001417 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001419 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 ConsumeToken();
1421 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001422
1423 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001424}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001425
Chris Lattnerb28317a2009-03-28 19:18:32 +00001426Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001427 assert(Tok.is(tok::l_brace));
1428 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001429
Chris Lattner49f28ca2009-03-05 08:00:35 +00001430 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1431 PP.getSourceManager(),
1432 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001434 // Do not enter a scope for the brace, as the arguments are in the same scope
1435 // (the function body) as the body itself. Instead, just read the statement
1436 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001437 OwningStmtResult FnBody(ParseCompoundStatementBody());
1438
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001439 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001440 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001441 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001442 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001443
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001444 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001445}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001446
Sebastian Redld3a413d2009-04-26 20:35:05 +00001447/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1448///
1449/// function-try-block:
1450/// 'try' ctor-initializer[opt] compound-statement handler-seq
1451///
1452Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1453 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1454 SourceLocation TryLoc = ConsumeToken();
1455
1456 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1457 PP.getSourceManager(),
1458 "parsing function try block");
1459
1460 // Constructor initializer list?
1461 if (Tok.is(tok::colon))
1462 ParseConstructorInitializer(Decl);
1463
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001464 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001465 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1466 // If we failed to parse the try-catch, we just give the function an empty
1467 // compound statement as the body.
1468 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001469 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001470 MultiStmtArg(Actions), false);
1471
1472 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1473}
1474
Sebastian Redla0fd8652008-12-21 16:41:36 +00001475/// ParseCXXTryBlock - Parse a C++ try-block.
1476///
1477/// try-block:
1478/// 'try' compound-statement handler-seq
1479///
Sean Huntbbd37c62009-11-21 08:43:09 +00001480Parser::OwningStmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
1481 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001482 delete Attr;
1483
Sebastian Redla0fd8652008-12-21 16:41:36 +00001484 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1485
1486 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001487 return ParseCXXTryBlockCommon(TryLoc);
1488}
1489
1490/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1491/// function-try-block.
1492///
1493/// try-block:
1494/// 'try' compound-statement handler-seq
1495///
1496/// function-try-block:
1497/// 'try' ctor-initializer[opt] compound-statement handler-seq
1498///
1499/// handler-seq:
1500/// handler handler-seq[opt]
1501///
1502Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001503 if (Tok.isNot(tok::l_brace))
1504 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001505 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1506 OwningStmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001507 if (TryBlock.isInvalid())
1508 return move(TryBlock);
1509
1510 StmtVector Handlers(Actions);
Sean Huntbbd37c62009-11-21 08:43:09 +00001511 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1512 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1513 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1514 << Attr.Range;
1515 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00001516 if (Tok.isNot(tok::kw_catch))
1517 return StmtError(Diag(Tok, diag::err_expected_catch));
1518 while (Tok.is(tok::kw_catch)) {
1519 OwningStmtResult Handler(ParseCXXCatchBlock());
1520 if (!Handler.isInvalid())
1521 Handlers.push_back(Handler.release());
1522 }
1523 // Don't bother creating the full statement if we don't have any usable
1524 // handlers.
1525 if (Handlers.empty())
1526 return StmtError();
1527
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001528 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001529}
1530
1531/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1532///
1533/// handler:
1534/// 'catch' '(' exception-declaration ')' compound-statement
1535///
1536/// exception-declaration:
1537/// type-specifier-seq declarator
1538/// type-specifier-seq abstract-declarator
1539/// type-specifier-seq
1540/// '...'
1541///
1542Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1543 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1544
1545 SourceLocation CatchLoc = ConsumeToken();
1546
1547 SourceLocation LParenLoc = Tok.getLocation();
1548 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1549 return StmtError();
1550
1551 // C++ 3.3.2p3:
1552 // The name in a catch exception-declaration is local to the handler and
1553 // shall not be redeclared in the outermost block of the handler.
1554 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1555
1556 // exception-declaration is equivalent to '...' or a parameter-declaration
1557 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001558 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001559 if (Tok.isNot(tok::ellipsis)) {
1560 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001561 if (ParseCXXTypeSpecifierSeq(DS))
1562 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001563 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1564 ParseDeclarator(ExDecl);
1565 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1566 } else
1567 ConsumeToken();
1568
1569 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1570 return StmtError();
1571
1572 if (Tok.isNot(tok::l_brace))
1573 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1574
Sean Huntbbd37c62009-11-21 08:43:09 +00001575 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1576 OwningStmtResult Block(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001577 if (Block.isInvalid())
1578 return move(Block);
1579
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001580 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001581}