blob: 74c2472578a2bab5c9ca992cd63f33900d77ad2a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000042/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000043/// [OBC] objc-throw-statement
44/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000045/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000046/// [GNU] asm-statement
47/// [OMP] openmp-construct [TODO]
48///
49/// labeled-statement:
50/// identifier ':' statement
51/// 'case' constant-expression ':' statement
52/// 'default' ':' statement
53///
54/// selection-statement:
55/// if-statement
56/// switch-statement
57///
58/// iteration-statement:
59/// while-statement
60/// do-statement
61/// for-statement
62///
63/// expression-statement:
64/// expression[opt] ';'
65///
66/// jump-statement:
67/// 'goto' identifier ';'
68/// 'continue' ';'
69/// 'break' ';'
70/// 'return' expression[opt] ';'
71/// [GNU] 'goto' '*' expression ';'
72///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000073/// [OBC] objc-throw-statement:
74/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000075/// [OBC] '@' 'throw' ';'
76///
John McCall60d7b3a2010-08-24 06:29:42 +000077StmtResult
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +000078Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000079 const char *SemiError = 0;
John McCall60d7b3a2010-08-24 06:29:42 +000080 StmtResult Res;
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000081
82 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000083
John McCall0b7e6782011-03-24 11:26:52 +000084 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000085 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 // Cases in this switch statement should fall through if the parser expects
88 // the token to end in a semicolon (in which case SemiError should be set),
89 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000090 tok::TokenKind Kind = Tok.getKind();
91 SourceLocation AtLoc;
92 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000093 case tok::at: // May be a @try or @throw statement
94 {
95 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000096 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000097 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000098
Douglas Gregor791215b2009-09-21 20:51:25 +000099 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000100 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Douglas Gregorc8bddde2010-05-28 00:22:41 +0000101 ConsumeCodeCompletionToken();
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000102 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
Douglas Gregor791215b2009-09-21 20:51:25 +0000103
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000104 case tok::identifier:
105 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
106 // identifier ':' statement
John McCall7f040a92010-12-24 02:08:15 +0000107 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000108 }
109 // PASS THROUGH.
110
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000111 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000112 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000113 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000114 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall7f040a92010-12-24 02:08:15 +0000115 DeclEnd, attrs);
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
Richard Trieubb9b80c2011-04-21 21:44:26 +0000124 // If a case keyword is missing, this is where it should be inserted.
125 Token OldToken = Tok;
126
Sean Huntbbd37c62009-11-21 08:43:09 +0000127 // FIXME: Use the attributes
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000128 // expression[opt] ';'
John McCall60d7b3a2010-08-24 06:29:42 +0000129 ExprResult Expr(ParseExpression());
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000130 if (Expr.isInvalid()) {
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000131 // If the expression is invalid, skip ahead to the next semicolon or '}'.
132 // Not doing this opens us up to the possibility of infinite loops if
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000133 // ParseExpression does not consume any tokens.
Argyrios Kyrtzidisb57c7572010-03-31 00:37:59 +0000134 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
135 if (Tok.is(tok::semi))
136 ConsumeToken();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000137 return StmtError();
138 }
Richard Trieubb9b80c2011-04-21 21:44:26 +0000139
140 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
141 Actions.CheckCaseExpression(Expr.get())) {
142 // If a constant expression is followed by a colon inside a switch block,
143 // suggest a missing case keywork.
144 Diag(OldToken, diag::err_expected_case_before_expression)
145 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
146
147 // Recover parsing as a case statement.
148 return ParseCaseStatement(attrs, /*MissingCase=*/true, Expr);
149 }
150
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000151 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000152 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000153 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000154 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000157 return ParseCaseStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000159 return ParseDefaultStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall7f040a92010-12-24 02:08:15 +0000162 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000163 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
164 bool LeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
165 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacro);
166 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall7f040a92010-12-24 02:08:15 +0000169 return ParseIfStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall7f040a92010-12-24 02:08:15 +0000171 return ParseSwitchStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall7f040a92010-12-24 02:08:15 +0000174 return ParseWhileStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000176 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000177 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 break;
179 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall7f040a92010-12-24 02:08:15 +0000180 return ParseForStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181
182 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000183 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000184 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 break;
186 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000187 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000188 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 break;
190 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000191 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000192 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 break;
194 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000195 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000196 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000198
Sebastian Redla0fd8652008-12-21 16:41:36 +0000199 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000200 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000201 bool msAsm = false;
202 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000203 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000204 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000205 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 break;
207 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000208
Sebastian Redla0fd8652008-12-21 16:41:36 +0000209 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000210 return ParseCXXTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000211 }
212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000214 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000216 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000217 // If the result was valid, then we do want to diagnose this. Use
218 // ExpectAndConsume to emit the diagnostic, even though we know it won't
219 // succeed.
220 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000221 // Skip until we see a } or ;, but don't eat it.
222 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 }
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Sebastian Redl61364dd2008-12-11 19:30:53 +0000225 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000226}
227
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000228/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000229///
230/// labeled-statement:
231/// identifier ':' statement
232/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000233///
John McCall7f040a92010-12-24 02:08:15 +0000234StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000235 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
236 "Not an identifier!");
237
238 Token IdentTok = Tok; // Save the whole token.
239 ConsumeToken(); // eat the identifier.
240
241 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000242
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000243 // identifier ':' statement
244 SourceLocation ColonLoc = ConsumeToken();
245
246 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000247 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000248
John McCall60d7b3a2010-08-24 06:29:42 +0000249 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000250
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000251 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000252 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000253 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner337e5502011-02-18 01:27:55 +0000254
255 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
256 IdentTok.getLocation());
257 if (AttributeList *Attrs = attrs.getList())
258 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
259
260 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
261 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000262}
Reid Spencer5f016e22007-07-11 17:01:13 +0000263
264/// ParseCaseStatement
265/// labeled-statement:
266/// 'case' constant-expression ':' statement
267/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
268///
Richard Trieubb9b80c2011-04-21 21:44:26 +0000269StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
270 ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000271 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000272 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner24e1e702009-03-04 04:23:07 +0000274 // It is very very common for code to contain many case statements recursively
275 // nested, as in (but usually without indentation):
276 // case 1:
277 // case 2:
278 // case 3:
279 // case 4:
280 // case 5: etc.
281 //
282 // Parsing this naively works, but is both inefficient and can cause us to run
283 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000284 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000285 // but all the grossness is constrained to ParseCaseStatement (and some
286 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner24e1e702009-03-04 04:23:07 +0000288 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
289 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000290 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner24e1e702009-03-04 04:23:07 +0000292 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
293 // gets updated each time a new case is parsed, and whose body is unset so
294 // far. When parsing 'case 4', this is the 'case 3' node.
295 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattner24e1e702009-03-04 04:23:07 +0000297 // While we have case statements, eat and stack them.
298 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000299 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
300 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000302 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000303 Actions.CodeCompleteCase(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000304 ConsumeCodeCompletionToken();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000305 }
306
Chris Lattner6fb09c82009-12-10 00:38:54 +0000307 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
308 /// Disable this form of error recovery while we're parsing the case
309 /// expression.
310 ColonProtectionRAIIObject ColonProtection(*this);
311
Richard Trieubb9b80c2011-04-21 21:44:26 +0000312 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
313 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000314 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000316 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000318
Chris Lattner24e1e702009-03-04 04:23:07 +0000319 // GNU case range extension.
320 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000321 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000322 if (Tok.is(tok::ellipsis)) {
323 Diag(Tok, diag::ext_gnu_case_range);
324 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000325
Chris Lattner24e1e702009-03-04 04:23:07 +0000326 RHS = ParseConstantExpression();
327 if (RHS.isInvalid()) {
328 SkipUntil(tok::colon);
329 return StmtError();
330 }
331 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000332
333 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000334
Douglas Gregor662a4822010-12-23 22:56:40 +0000335 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000336 if (Tok.is(tok::colon)) {
337 ColonLoc = ConsumeToken();
338
339 // Treat "case blah;" as a typo for "case blah:".
340 } else if (Tok.is(tok::semi)) {
341 ColonLoc = ConsumeToken();
342 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
343 << FixItHint::CreateReplacement(ColonLoc, ":");
344 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000345 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
346 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
347 << FixItHint::CreateInsertion(ExpectedLoc, ":");
348 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000349 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000350
John McCall60d7b3a2010-08-24 06:29:42 +0000351 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000352 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
353 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner24e1e702009-03-04 04:23:07 +0000355 // If we had a sema error parsing this case, then just ignore it and
356 // continue parsing the sub-stmt.
357 if (Case.isInvalid()) {
358 if (TopLevelCase.isInvalid()) // No parsed case stmts.
359 return ParseStatement();
360 // Otherwise, just don't add it as a nested case.
361 } else {
362 // If this is the first case statement we parsed, it becomes TopLevelCase.
363 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000364 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000365 if (TopLevelCase.isInvalid())
366 TopLevelCase = move(Case);
367 else
John McCall9ae2f072010-08-23 23:25:46 +0000368 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000369 DeepestParsedCaseStmt = NextDeepest;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner24e1e702009-03-04 04:23:07 +0000372 // Handle all case statements.
373 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner24e1e702009-03-04 04:23:07 +0000375 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner24e1e702009-03-04 04:23:07 +0000377 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000378 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattner24e1e702009-03-04 04:23:07 +0000380 if (Tok.isNot(tok::r_brace)) {
381 SubStmt = ParseStatement();
382 } else {
383 // Nicely diagnose the common error "switch (X) { case 4: }", which is
384 // not valid.
385 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000387 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner24e1e702009-03-04 04:23:07 +0000390 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000391 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000392 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner24e1e702009-03-04 04:23:07 +0000394 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000395 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000396
Chris Lattner24e1e702009-03-04 04:23:07 +0000397 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000398 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
400
401/// ParseDefaultStatement
402/// labeled-statement:
403/// 'default' ':' statement
404/// Note that this does not parse the 'statement' at the end.
405///
John McCall7f040a92010-12-24 02:08:15 +0000406StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000407 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000408
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000409 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
411
Douglas Gregor662a4822010-12-23 22:56:40 +0000412 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000413 if (Tok.is(tok::colon)) {
414 ColonLoc = ConsumeToken();
415
416 // Treat "default;" as a typo for "default:".
417 } else if (Tok.is(tok::semi)) {
418 ColonLoc = ConsumeToken();
419 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
420 << FixItHint::CreateReplacement(ColonLoc, ":");
421 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000422 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
423 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
424 << FixItHint::CreateInsertion(ExpectedLoc, ":");
425 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000427
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000429 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000431 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 }
433
John McCall60d7b3a2010-08-24 06:29:42 +0000434 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000435 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000436 return StmtError();
437
Sebastian Redl117054a2008-12-28 16:13:43 +0000438 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000439 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000440}
441
442
443/// ParseCompoundStatement - Parse a "{}" block.
444///
445/// compound-statement: [C99 6.8.2]
446/// { block-item-list[opt] }
447/// [GNU] { label-declarations block-item-list } [TODO]
448///
449/// block-item-list:
450/// block-item
451/// block-item-list block-item
452///
453/// block-item:
454/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000455/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000456/// statement
457/// [OMP] openmp-directive [TODO]
458///
459/// [GNU] label-declarations:
460/// [GNU] label-declaration
461/// [GNU] label-declarations label-declaration
462///
463/// [GNU] label-declaration:
464/// [GNU] '__label__' identifier-list ';'
465///
466/// [OMP] openmp-directive: [TODO]
467/// [OMP] barrier-directive
468/// [OMP] flush-directive
469///
John McCall7f040a92010-12-24 02:08:15 +0000470StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Sean Huntbbd37c62009-11-21 08:43:09 +0000471 bool isStmtExpr) {
472 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000473
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000474 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000475
Chris Lattner31e05722007-08-26 06:24:45 +0000476 // Enter a scope to hold everything within the compound stmt. Compound
477 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000478 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000479
480 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000481 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482}
483
484
485/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000486/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000487/// consume the '}' at the end of the block. It does not manipulate the scope
488/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000489StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000490 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000491 Tok.getLocation(),
492 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000493 InMessageExpressionRAIIObject InMessage(*this, false);
494
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
496
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000497 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000498
Chris Lattner4ae493c2011-02-18 02:08:43 +0000499 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
500 // only allowed at the start of a compound stmt regardless of the language.
501 while (Tok.is(tok::kw___label__)) {
502 SourceLocation LabelLoc = ConsumeToken();
503 Diag(LabelLoc, diag::ext_gnu_local_label);
504
505 llvm::SmallVector<Decl *, 8> DeclsInGroup;
506 while (1) {
507 if (Tok.isNot(tok::identifier)) {
508 Diag(Tok, diag::err_expected_ident);
509 break;
510 }
511
512 IdentifierInfo *II = Tok.getIdentifierInfo();
513 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000514 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner4ae493c2011-02-18 02:08:43 +0000515
516 if (!Tok.is(tok::comma))
517 break;
518 ConsumeToken();
519 }
520
John McCall0b7e6782011-03-24 11:26:52 +0000521 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000522 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
523 DeclsInGroup.data(), DeclsInGroup.size());
524 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
525
526 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
527 if (R.isUsable())
528 Stmts.push_back(R.release());
529 }
530
531 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000532 if (Tok.is(tok::annot_pragma_unused)) {
533 HandlePragmaUnused();
534 continue;
535 }
536
John McCall60d7b3a2010-08-24 06:29:42 +0000537 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000538 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000539 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000540 } else {
541 // __extension__ can start declarations and it can also be a unary
542 // operator for expressions. Consume multiple __extension__ markers here
543 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000544 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000545 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000546 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000547 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000548
John McCall0b7e6782011-03-24 11:26:52 +0000549 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000550 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000551
Chris Lattner45a566c2007-08-27 01:01:57 +0000552 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000553 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000554 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000555 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000556 ExtensionRAIIObject O(Diags);
557
Chris Lattner97144fc2009-04-02 04:16:50 +0000558 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000559 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
560 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000561 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000562 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000563 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000564 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000565 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000566
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000567 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000568 SkipUntil(tok::semi);
569 continue;
570 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000571
Sean Huntbbd37c62009-11-21 08:43:09 +0000572 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000573 // Eat the semicolon at the end of stmt and convert the expr into a
574 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000575 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000576 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000577 }
578 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000579
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000580 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000581 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000585 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 Diag(Tok, diag::err_expected_rbrace);
Chris Lattnerf65086b2010-09-01 15:49:26 +0000587 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000588 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000592 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000593 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594}
595
Chris Lattner15ff1112008-12-12 06:31:07 +0000596/// ParseParenExprOrCondition:
597/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000598/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000599///
600/// This function parses and performs error recovery on the specified condition
601/// or expression (depending on whether we're in C++ or C mode). This function
602/// goes out of its way to recover well. It returns true if there was a parser
603/// error (the right paren couldn't be found), which indicates that the caller
604/// should try to recover harder. It returns false if the condition is
605/// successfully parsed. Note that a successful parse can still have semantic
606/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000607bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000608 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000609 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000610 bool ConvertToBoolean) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000611 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000612 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000613 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000614 else {
615 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000616 DeclResult = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000617
618 // If required, convert to a boolean value.
619 if (!ExprResult.isInvalid() && ConvertToBoolean)
620 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000621 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Chris Lattner15ff1112008-12-12 06:31:07 +0000624 // If the parser was confused by the condition and we don't have a ')', try to
625 // recover by skipping ahead to a semi and bailing out. If condexp is
626 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000627 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000628 SkipUntil(tok::semi);
629 // Skipping may have stopped if it found the containing ')'. If so, we can
630 // continue parsing the if statement.
631 if (Tok.isNot(tok::r_paren))
632 return true;
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattner15ff1112008-12-12 06:31:07 +0000635 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000636 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000637 return false;
638}
639
640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641/// ParseIfStatement
642/// if-statement: [C99 6.8.4.1]
643/// 'if' '(' expression ')' statement
644/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000645/// [C++] 'if' '(' condition ')' statement
646/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000647///
John McCall7f040a92010-12-24 02:08:15 +0000648StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000649 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000650
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000651 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
653
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000654 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000655 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000657 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000659
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000660 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
661
Chris Lattner22153252007-08-26 23:08:06 +0000662 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
663 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000664 //
665 // C++ 6.4p3:
666 // A name introduced by a declaration in a condition is in scope from its
667 // point of declaration until the end of the substatements controlled by the
668 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000669 // C++ 3.3.2p4:
670 // Names declared in the for-init-statement, and in the condition of if,
671 // while, for, and switch statements are local to the if, while, for, or
672 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000673 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000674 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000677 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000678 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000679 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000680 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000681
John McCall9ae2f072010-08-23 23:25:46 +0000682 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Chris Lattner0ecea032007-08-22 05:28:50 +0000684 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000685 // there is no compound stmt. C90 does not have this clause. We only do this
686 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000687 //
688 // C++ 6.4p1:
689 // The substatement in a selection-statement (each substatement, in the else
690 // form of the if statement) implicitly defines a local scope.
691 //
692 // For C++ we create a scope for the condition and a new scope for
693 // substatements because:
694 // -When the 'then' scope exits, we want the condition declaration to still be
695 // active for the 'else' scope too.
696 // -Sema will detect name clashes by considering declarations of a
697 // 'ControlScope' as part of its direct subscope.
698 // -If we wanted the condition and substatement to be in the same scope, we
699 // would have to notify ParseStatement not to create a new scope. It's
700 // simpler to let it create a new scope.
701 //
Mike Stump1eb44332009-09-09 15:08:12 +0000702 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000703 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000704
Chris Lattnerb96728d2007-10-29 05:08:52 +0000705 // Read the 'then' stmt.
706 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000707 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000708
Chris Lattnera36ce712007-08-22 05:16:28 +0000709 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000710 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 // If it has an else, parse it.
713 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000714 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000715 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000716
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000717 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000719 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000720
Chris Lattner0ecea032007-08-22 05:28:50 +0000721 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000722 // there is no compound stmt. C90 does not have this clause. We only do
723 // this if the body isn't a compound statement to avoid push/pop in common
724 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000725 //
726 // C++ 6.4p1:
727 // The substatement in a selection-statement (each substatement, in the else
728 // form of the if statement) implicitly defines a local scope.
729 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000730 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000731 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000734
Chris Lattnera36ce712007-08-22 05:16:28 +0000735 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000736 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000738
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000739 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattner18914bc2008-12-12 06:19:11 +0000741 // If the condition was invalid, discard the if statement. We could recover
742 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000743 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000744 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000745
Chris Lattnerb96728d2007-10-29 05:08:52 +0000746 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000747 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000748 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000749 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
750 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
751 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000752 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000753 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000754 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000755
Chris Lattnerb96728d2007-10-29 05:08:52 +0000756 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000757 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000758 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000759 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000760 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000761
John McCall9ae2f072010-08-23 23:25:46 +0000762 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000763 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000764}
765
766/// ParseSwitchStatement
767/// switch-statement:
768/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000769/// [C++] 'switch' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000770StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000771 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000772
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000773 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
775
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000776 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000777 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000779 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 }
Chris Lattner22153252007-08-26 23:08:06 +0000781
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000782 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
783
Chris Lattner22153252007-08-26 23:08:06 +0000784 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
785 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000786 //
787 // C++ 6.4p3:
788 // A name introduced by a declaration in a condition is in scope from its
789 // point of declaration until the end of the substatements controlled by the
790 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000791 // C++ 3.3.2p4:
792 // Names declared in the for-init-statement, and in the condition of if,
793 // while, for, and switch statements are local to the if, while, for, or
794 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000795 //
Richard Trieubb9b80c2011-04-21 21:44:26 +0000796 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +0000797 if (C99orCXX)
798 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000799 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000800
801 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000802 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000803 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000804 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +0000805 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000806
John McCall60d7b3a2010-08-24 06:29:42 +0000807 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +0000808 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000809
Douglas Gregor586596f2010-05-06 17:25:47 +0000810 if (Switch.isInvalid()) {
811 // Skip the switch body.
812 // FIXME: This is not optimal recovery, but parsing the body is more
813 // dangerous due to the presence of case and default statements, which
814 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +0000815 if (Tok.is(tok::l_brace)) {
816 ConsumeBrace();
817 SkipUntil(tok::r_brace, false, false);
818 } else
Douglas Gregor586596f2010-05-06 17:25:47 +0000819 SkipUntil(tok::semi);
820 return move(Switch);
821 }
822
Chris Lattner0ecea032007-08-22 05:28:50 +0000823 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000824 // there is no compound stmt. C90 does not have this clause. We only do this
825 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000826 //
827 // C++ 6.4p1:
828 // The substatement in a selection-statement (each substatement, in the else
829 // form of the if statement) implicitly defines a local scope.
830 //
831 // See comments in ParseIfStatement for why we create a scope for the
832 // condition and a new scope for substatement in C++.
833 //
Mike Stump1eb44332009-09-09 15:08:12 +0000834 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000835 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000836
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000838 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000839
Chris Lattner7e52de42010-01-24 01:50:29 +0000840 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000841 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000842 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000843
Chris Lattner7e52de42010-01-24 01:50:29 +0000844 if (Body.isInvalid())
845 // FIXME: Remove the case statement list from the Switch statement.
846 Body = Actions.ActOnNullStmt(Tok.getLocation());
847
John McCall9ae2f072010-08-23 23:25:46 +0000848 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000849}
850
851/// ParseWhileStatement
852/// while-statement: [C99 6.8.5.1]
853/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000854/// [C++] 'while' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000855StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000856 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000857
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000858 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 SourceLocation WhileLoc = Tok.getLocation();
860 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000861
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000862 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000863 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000865 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000867
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000868 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
869
Chris Lattner22153252007-08-26 23:08:06 +0000870 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
871 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000872 //
873 // C++ 6.4p3:
874 // A name introduced by a declaration in a condition is in scope from its
875 // point of declaration until the end of the substatements controlled by the
876 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000877 // C++ 3.3.2p4:
878 // Names declared in the for-init-statement, and in the condition of if,
879 // while, for, and switch statements are local to the if, while, for, or
880 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000881 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000882 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000883 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000884 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
885 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000886 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000887 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
888 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000889
890 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000891 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +0000892 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000893 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000894 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000895
John McCall9ae2f072010-08-23 23:25:46 +0000896 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Chris Lattner0ecea032007-08-22 05:28:50 +0000898 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000899 // there is no compound stmt. C90 does not have this clause. We only do this
900 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000901 //
902 // C++ 6.5p2:
903 // The substatement in an iteration-statement implicitly defines a local scope
904 // which is entered and exited each time through the loop.
905 //
906 // See comments in ParseIfStatement for why we create a scope for the
907 // condition and a new scope for substatement in C++.
908 //
Mike Stump1eb44332009-09-09 15:08:12 +0000909 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000910 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000913 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000914
Chris Lattner0ecea032007-08-22 05:28:50 +0000915 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000916 InnerScope.Exit();
917 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000918
John McCalld226f652010-08-21 09:40:31 +0000919 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000920 return StmtError();
921
John McCall9ae2f072010-08-23 23:25:46 +0000922 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000923}
924
925/// ParseDoStatement
926/// do-statement: [C99 6.8.5.2]
927/// 'do' statement 'while' '(' expression ')' ';'
928/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +0000929StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000930 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000931
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000932 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000934
Chris Lattner22153252007-08-26 23:08:06 +0000935 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
936 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000937 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000938 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000939 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000940 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000941 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000942
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000943 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000944
Chris Lattner0ecea032007-08-22 05:28:50 +0000945 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000946 // there is no compound stmt. C90 does not have this clause. We only do this
947 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000948 //
949 // C++ 6.5p2:
950 // The substatement in an iteration-statement implicitly defines a local scope
951 // which is entered and exited each time through the loop.
952 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000953 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000954 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000955 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000956
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +0000958 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000959
Chris Lattner0ecea032007-08-22 05:28:50 +0000960 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000961 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000962
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000963 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000964 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000965 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000966 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000967 SkipUntil(tok::semi, false, true);
968 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000969 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 }
971 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000972
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000973 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000974 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000975 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000976 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000978
Chris Lattnerff871fb2008-12-12 06:35:28 +0000979 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000980 SourceLocation LPLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +0000981 ExprResult Cond = ParseExpression();
Douglas Gregor04895d32009-11-24 21:34:32 +0000982 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000983 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000984
Sebastian Redl9a920342008-12-11 19:48:14 +0000985 if (Cond.isInvalid() || Body.isInvalid())
986 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000987
John McCall9ae2f072010-08-23 23:25:46 +0000988 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
989 Cond.get(), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000990}
991
992/// ParseForStatement
993/// for-statement: [C99 6.8.5.3]
994/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
995/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000996/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
997/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +0000998/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000999/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1000/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001001///
1002/// [C++] for-init-statement:
1003/// [C++] expression-statement
1004/// [C++] simple-declaration
1005///
Richard Smithad762fc2011-04-14 22:09:26 +00001006/// [C++0x] for-range-declaration:
1007/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1008/// [C++0x] for-range-initializer:
1009/// [C++0x] expression
1010/// [C++0x] braced-init-list [TODO]
John McCall7f040a92010-12-24 02:08:15 +00001011StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001012 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001013
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001014 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001016
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001017 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001018 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001020 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001022
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001023 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001024
Chris Lattner22153252007-08-26 23:08:06 +00001025 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1026 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001027 //
1028 // C++ 6.4p3:
1029 // A name introduced by a declaration in a condition is in scope from its
1030 // point of declaration until the end of the substatements controlled by the
1031 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001032 // C++ 3.3.2p4:
1033 // Names declared in the for-init-statement, and in the condition of if,
1034 // while, for, and switch statements are local to the if, while, for, or
1035 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001036 // C++ 6.5.3p1:
1037 // Names declared in the for-init-statement are in the same declarative-region
1038 // as those declared in the condition.
1039 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001040 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001041 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001042 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1043 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001044 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001045 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1046
1047 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001048
1049 SourceLocation LParenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001050 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001051
Richard Smithad762fc2011-04-14 22:09:26 +00001052 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001053 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001054 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001055 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001056 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001057 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001058 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001059 Decl *SecondVar = 0;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001060
Douglas Gregor791215b2009-09-21 20:51:25 +00001061 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001062 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001063 C99orCXXorObjC? Sema::PCC_ForInit
1064 : Sema::PCC_Expression);
Douglas Gregordc845342010-05-25 05:58:43 +00001065 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +00001066 }
1067
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001069 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 // no first part, eat the ';'.
1071 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001072 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001074 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001076
John McCall0b7e6782011-03-24 11:26:52 +00001077 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001078 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001079
Richard Smithad762fc2011-04-14 22:09:26 +00001080 // In C++0x, "for (T NS:a" might not be a typo for ::
1081 bool MightBeForRangeStmt = getLang().CPlusPlus;
1082 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1083
Chris Lattner97144fc2009-04-02 04:16:50 +00001084 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001085 StmtVector Stmts(Actions);
1086 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001087 DeclEnd, attrs, false,
1088 MightBeForRangeStmt ?
1089 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001090 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Richard Smithad762fc2011-04-14 22:09:26 +00001092 if (ForRangeInit.ParsedForRangeDecl()) {
1093 ForRange = true;
1094 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001095 ConsumeToken();
1096 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001097 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001098 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001099 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001100
1101 if (Tok.is(tok::code_completion)) {
1102 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1103 ConsumeCodeCompletionToken();
1104 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001105 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001106 } else {
1107 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001108 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 } else {
1110 Value = ParseExpression();
1111
John McCallf6a16482010-12-04 03:47:34 +00001112 ForEach = isTokIdentifier_in();
1113
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001115 if (!Value.isInvalid()) {
1116 if (ForEach)
1117 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1118 else
1119 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1120 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001121
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001122 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001124 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001125 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001126
1127 if (Tok.is(tok::code_completion)) {
1128 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1129 ConsumeCodeCompletionToken();
1130 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001131 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001132 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001133 if (!Value.isInvalid()) {
1134 Diag(Tok, diag::err_expected_semi_for);
1135 } else {
1136 // Skip until semicolon or rparen, don't consume it.
1137 SkipUntil(tok::r_paren, true, true);
1138 if (Tok.is(tok::semi))
1139 ConsumeToken();
1140 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 }
1142 }
Richard Smithad762fc2011-04-14 22:09:26 +00001143 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001144 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001145 // Parse the second part of the for specifier.
1146 if (Tok.is(tok::semi)) { // for (...;;
1147 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001148 } else if (Tok.is(tok::r_paren)) {
1149 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001150 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001151 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001152 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001153 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1154 else {
1155 Second = ParseExpression();
1156 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001157 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001158 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001159 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001160 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001161 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001162 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001163
Douglas Gregorb72c7782011-02-17 03:38:46 +00001164 if (Tok.isNot(tok::semi)) {
1165 if (!SecondPartIsInvalid || SecondVar)
1166 Diag(Tok, diag::err_expected_semi_for);
1167 else
1168 // Skip until semicolon or rparen, don't consume it.
1169 SkipUntil(tok::r_paren, true, true);
1170 }
1171
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001172 if (Tok.is(tok::semi)) {
1173 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001174 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001175
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001176 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001177 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001178 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001179 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001180 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 // Match the ')'.
1183 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001184
Richard Smithad762fc2011-04-14 22:09:26 +00001185 // We need to perform most of the semantic analysis for a C++0x for-range
1186 // statememt before parsing the body, in order to be able to deduce the type
1187 // of an auto-typed loop variable.
1188 StmtResult ForRangeStmt;
1189 if (ForRange)
1190 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1191 FirstPart.take(),
1192 ForRangeInit.ColonLoc,
1193 ForRangeInit.RangeExpr.get(),
1194 RParenLoc);
1195
Chris Lattner0ecea032007-08-22 05:28:50 +00001196 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001197 // there is no compound stmt. C90 does not have this clause. We only do this
1198 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001199 //
1200 // C++ 6.5p2:
1201 // The substatement in an iteration-statement implicitly defines a local scope
1202 // which is entered and exited each time through the loop.
1203 //
1204 // See comments in ParseIfStatement for why we create a scope for
1205 // for-init-statement/condition and a new scope for substatement in C++.
1206 //
Mike Stump1eb44332009-09-09 15:08:12 +00001207 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001208 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001209
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001211 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001212
Chris Lattner0ecea032007-08-22 05:28:50 +00001213 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001214 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001215
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001217 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001218
1219 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001220 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001221
Richard Smithad762fc2011-04-14 22:09:26 +00001222 if (ForEach)
1223 // FIXME: It isn't clear how to communicate the late destruction of
1224 // C++ temporaries used to create the collection.
1225 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1226 FirstPart.take(),
1227 Collection.take(), RParenLoc,
1228 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Richard Smithad762fc2011-04-14 22:09:26 +00001230 if (ForRange)
1231 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1232
1233 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1234 SecondVar, ThirdPart, RParenLoc, Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001235}
1236
1237/// ParseGotoStatement
1238/// jump-statement:
1239/// 'goto' identifier ';'
1240/// [GNU] 'goto' '*' expression ';'
1241///
1242/// Note: this lets the caller parse the end ';'.
1243///
John McCall7f040a92010-12-24 02:08:15 +00001244StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001245 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001246
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001247 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001249
John McCall60d7b3a2010-08-24 06:29:42 +00001250 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001251 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001252 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1253 Tok.getLocation());
1254 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001256 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 // GNU indirect goto extension.
1258 Diag(Tok, diag::ext_gnu_indirect_goto);
1259 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001260 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001261 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001263 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 }
John McCall9ae2f072010-08-23 23:25:46 +00001265 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001266 } else {
1267 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001268 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001270
Sebastian Redl9a920342008-12-11 19:48:14 +00001271 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001272}
1273
1274/// ParseContinueStatement
1275/// jump-statement:
1276/// 'continue' ';'
1277///
1278/// Note: this lets the caller parse the end ';'.
1279///
John McCall7f040a92010-12-24 02:08:15 +00001280StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001281 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001282
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001284 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001285}
1286
1287/// ParseBreakStatement
1288/// jump-statement:
1289/// 'break' ';'
1290///
1291/// Note: this lets the caller parse the end ';'.
1292///
John McCall7f040a92010-12-24 02:08:15 +00001293StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001294 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001295
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001297 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001298}
1299
1300/// ParseReturnStatement
1301/// jump-statement:
1302/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001303StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001304 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001305
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001306 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001308
John McCall60d7b3a2010-08-24 06:29:42 +00001309 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001310 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001311 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001312 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001313 ConsumeCodeCompletionToken();
1314 SkipUntil(tok::semi, false, true);
1315 return StmtError();
1316 }
1317
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001318 // FIXME: This is a hack to allow something like C++0x's generalized
1319 // initializer lists, but only enough of this feature to allow Clang to
1320 // parse libstdc++ 4.5's headers.
1321 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1322 R = ParseInitializer();
1323 if (R.isUsable() && !getLang().CPlusPlus0x)
1324 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1325 << R.get()->getSourceRange();
1326 } else
1327 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001328 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001330 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 }
1332 }
John McCall9ae2f072010-08-23 23:25:46 +00001333 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001334}
1335
Steve Naroff5f8aa692008-02-11 23:15:56 +00001336/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1337/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001338StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1339 SourceLocation EndLoc;
Steve Naroffb746ce82008-02-07 23:24:32 +00001340 if (Tok.is(tok::l_brace)) {
1341 unsigned short savedBraceCount = BraceCount;
1342 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001343 EndLoc = Tok.getLocation();
Steve Naroffb746ce82008-02-07 23:24:32 +00001344 ConsumeAnyToken();
1345 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001346 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001347 // From the MS website: If used without braces, the __asm keyword means
1348 // that the rest of the line is an assembly-language statement.
1349 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001350 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001351 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001352 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001353 EndLoc = TokLoc;
Steve Naroff36280972008-02-08 18:01:27 +00001354 ConsumeAnyToken();
1355 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001356 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1357 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001358 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001359 }
Mike Stump95059b52009-12-11 00:04:56 +00001360 Token t;
1361 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001362 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001363 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001364 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001365 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001366 ExprVector Constraints(Actions);
1367 ExprVector Exprs(Actions);
1368 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001369 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001370 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001371 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001372 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001373}
1374
Reid Spencer5f016e22007-07-11 17:01:13 +00001375/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001376/// asm-statement:
1377/// gnu-asm-statement
1378/// ms-asm-statement
1379///
1380/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001381/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1382///
1383/// [GNU] asm-argument:
1384/// asm-string-literal
1385/// asm-string-literal ':' asm-operands[opt]
1386/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1387/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1388/// ':' asm-clobbers
1389///
1390/// [GNU] asm-clobbers:
1391/// asm-string-literal
1392/// asm-clobbers ',' asm-string-literal
1393///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001394/// [MS] ms-asm-statement:
1395/// '__asm' assembly-instruction ';'[opt]
1396/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1397///
1398/// [MS] assembly-instruction-list:
1399/// assembly-instruction ';'[opt]
1400/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1401///
John McCall60d7b3a2010-08-24 06:29:42 +00001402StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001403 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001404 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001405
Steve Naroff5f8aa692008-02-11 23:15:56 +00001406 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001407 msAsm = true;
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001408 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001409 }
John McCall0b7e6782011-03-24 11:26:52 +00001410 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001412 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001413
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1415 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001416 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001418 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001419
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001421 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001422 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001423 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001425 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001426 }
1427 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001428
John McCall60d7b3a2010-08-24 06:29:42 +00001429 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001430 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001431 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001432
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001433 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001434 ExprVector Constraints(Actions);
1435 ExprVector Exprs(Actions);
1436 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001437
Anders Carlssondfab34a2008-02-05 23:03:50 +00001438 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001439 // We have a simple asm expression like 'asm("foo")'.
1440 SourceLocation RParenLoc = ConsumeParen();
1441 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1442 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1443 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001444 AsmString.take(), move_arg(Clobbers),
Chris Lattner64cb4752009-12-20 23:00:41 +00001445 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001447
Chris Lattner64cb4752009-12-20 23:00:41 +00001448 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001449 bool AteExtraColon = false;
1450 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1451 // In C++ mode, parse "::" like ": :".
1452 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001453 ConsumeToken();
1454
Chris Lattner64056462009-12-20 23:08:04 +00001455 if (!AteExtraColon &&
1456 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001457 return StmtError();
1458 }
Chris Lattner64056462009-12-20 23:08:04 +00001459
Chris Lattner64cb4752009-12-20 23:00:41 +00001460 unsigned NumOutputs = Names.size();
1461
1462 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001463 if (AteExtraColon ||
1464 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1465 // In C++ mode, parse "::" like ": :".
1466 if (AteExtraColon)
1467 AteExtraColon = false;
1468 else {
1469 AteExtraColon = Tok.is(tok::coloncolon);
1470 ConsumeToken();
1471 }
1472
1473 if (!AteExtraColon &&
1474 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001475 return StmtError();
1476 }
1477
1478 assert(Names.size() == Constraints.size() &&
1479 Constraints.size() == Exprs.size() &&
1480 "Input operand size mismatch!");
1481
1482 unsigned NumInputs = Names.size() - NumOutputs;
1483
1484 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001485 if (AteExtraColon || Tok.is(tok::colon)) {
1486 if (!AteExtraColon)
1487 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001488
Chandler Carruth102e1b62010-07-22 07:11:21 +00001489 // Parse the asm-string list for clobbers if present.
1490 if (Tok.isNot(tok::r_paren)) {
1491 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001492 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001493
Chandler Carruth102e1b62010-07-22 07:11:21 +00001494 if (Clobber.isInvalid())
1495 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001496
Chandler Carruth102e1b62010-07-22 07:11:21 +00001497 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001498
Chandler Carruth102e1b62010-07-22 07:11:21 +00001499 if (Tok.isNot(tok::comma)) break;
1500 ConsumeToken();
1501 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001502 }
1503 }
1504
1505 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1506 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001507 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001508 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001509 AsmString.take(), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001510 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001511}
1512
1513/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001514/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001515///
1516/// [GNU] asm-operands:
1517/// asm-operand
1518/// asm-operands ',' asm-operand
1519///
1520/// [GNU] asm-operand:
1521/// asm-string-literal '(' expression ')'
1522/// '[' identifier ']' asm-string-literal '(' expression ')'
1523///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001524//
1525// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001526bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1527 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1528 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001530 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001531 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
1533 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001535 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001538 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 Diag(Tok, diag::err_expected_ident);
1540 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001541 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Anders Carlssonb235fc22007-11-22 01:36:19 +00001544 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001545 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001546
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001547 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001549 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001550 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001551
John McCall60d7b3a2010-08-24 06:29:42 +00001552 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001553 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001554 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001555 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001556 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001557 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001558
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001559 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001560 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001561 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001562 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001563 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001566 SourceLocation OpenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001567 ExprResult Res(ParseExpression());
Eli Friedman72056a22009-05-03 07:49:42 +00001568 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001569 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001571 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001573 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001575 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001576 ConsumeToken();
1577 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001578
1579 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001580}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001581
Douglas Gregorc9977d02011-03-16 17:05:57 +00001582Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001583 assert(Tok.is(tok::l_brace));
1584 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001585
Douglas Gregorc9977d02011-03-16 17:05:57 +00001586 if (PP.isCodeCompletionEnabled()) {
1587 if (trySkippingFunctionBodyForCodeCompletion()) {
1588 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001589 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001590 }
1591 }
1592
John McCallf312b1e2010-08-26 23:41:50 +00001593 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1594 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001596 // Do not enter a scope for the brace, as the arguments are in the same scope
1597 // (the function body) as the body itself. Instead, just read the statement
1598 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001599 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001600
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001601 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001602 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001603 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001604 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001605
Douglas Gregorc9977d02011-03-16 17:05:57 +00001606 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001607 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001608}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001609
Sebastian Redld3a413d2009-04-26 20:35:05 +00001610/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1611///
1612/// function-try-block:
1613/// 'try' ctor-initializer[opt] compound-statement handler-seq
1614///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001615Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001616 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1617 SourceLocation TryLoc = ConsumeToken();
1618
John McCallf312b1e2010-08-26 23:41:50 +00001619 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1620 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001621
1622 // Constructor initializer list?
1623 if (Tok.is(tok::colon))
1624 ParseConstructorInitializer(Decl);
1625
Douglas Gregorc9977d02011-03-16 17:05:57 +00001626 if (PP.isCodeCompletionEnabled()) {
1627 if (trySkippingFunctionBodyForCodeCompletion()) {
1628 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001629 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001630 }
1631 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001632
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001633 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001634 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001635 // If we failed to parse the try-catch, we just give the function an empty
1636 // compound statement as the body.
1637 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001638 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001639 MultiStmtArg(Actions), false);
1640
Douglas Gregorc9977d02011-03-16 17:05:57 +00001641 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001642 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001643}
1644
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001645bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001646 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001647 assert(PP.isCodeCompletionEnabled() &&
1648 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001649
1650 // We're in code-completion mode. Skip parsing for all function bodies unless
1651 // the body contains the code-completion point.
1652 TentativeParsingAction PA(*this);
1653 ConsumeBrace();
1654 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1655 /*StopAtCodeCompletion=*/true)) {
1656 PA.Commit();
1657 return true;
1658 }
1659
1660 PA.Revert();
1661 return false;
1662}
1663
Sebastian Redla0fd8652008-12-21 16:41:36 +00001664/// ParseCXXTryBlock - Parse a C++ try-block.
1665///
1666/// try-block:
1667/// 'try' compound-statement handler-seq
1668///
John McCall7f040a92010-12-24 02:08:15 +00001669StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001670 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001671
Sebastian Redla0fd8652008-12-21 16:41:36 +00001672 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1673
1674 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001675 return ParseCXXTryBlockCommon(TryLoc);
1676}
1677
1678/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1679/// function-try-block.
1680///
1681/// try-block:
1682/// 'try' compound-statement handler-seq
1683///
1684/// function-try-block:
1685/// 'try' ctor-initializer[opt] compound-statement handler-seq
1686///
1687/// handler-seq:
1688/// handler handler-seq[opt]
1689///
John McCall60d7b3a2010-08-24 06:29:42 +00001690StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001691 if (Tok.isNot(tok::l_brace))
1692 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001693 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00001694 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001695 StmtResult TryBlock(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001696 if (TryBlock.isInvalid())
1697 return move(TryBlock);
1698
1699 StmtVector Handlers(Actions);
John McCall7f040a92010-12-24 02:08:15 +00001700 MaybeParseCXX0XAttributes(attrs);
1701 ProhibitAttributes(attrs);
1702
Sebastian Redla0fd8652008-12-21 16:41:36 +00001703 if (Tok.isNot(tok::kw_catch))
1704 return StmtError(Diag(Tok, diag::err_expected_catch));
1705 while (Tok.is(tok::kw_catch)) {
John McCall60d7b3a2010-08-24 06:29:42 +00001706 StmtResult Handler(ParseCXXCatchBlock());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001707 if (!Handler.isInvalid())
1708 Handlers.push_back(Handler.release());
1709 }
1710 // Don't bother creating the full statement if we don't have any usable
1711 // handlers.
1712 if (Handlers.empty())
1713 return StmtError();
1714
John McCall9ae2f072010-08-23 23:25:46 +00001715 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001716}
1717
1718/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1719///
1720/// handler:
1721/// 'catch' '(' exception-declaration ')' compound-statement
1722///
1723/// exception-declaration:
1724/// type-specifier-seq declarator
1725/// type-specifier-seq abstract-declarator
1726/// type-specifier-seq
1727/// '...'
1728///
John McCall60d7b3a2010-08-24 06:29:42 +00001729StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001730 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1731
1732 SourceLocation CatchLoc = ConsumeToken();
1733
1734 SourceLocation LParenLoc = Tok.getLocation();
1735 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1736 return StmtError();
1737
1738 // C++ 3.3.2p3:
1739 // The name in a catch exception-declaration is local to the handler and
1740 // shall not be redeclared in the outermost block of the handler.
1741 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1742
1743 // exception-declaration is equivalent to '...' or a parameter-declaration
1744 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00001745 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001746 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001747 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00001748 if (ParseCXXTypeSpecifierSeq(DS))
1749 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001750 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1751 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001752 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001753 } else
1754 ConsumeToken();
1755
1756 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1757 return StmtError();
1758
1759 if (Tok.isNot(tok::l_brace))
1760 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1761
Sean Huntbbd37c62009-11-21 08:43:09 +00001762 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00001763 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001764 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001765 if (Block.isInvalid())
1766 return move(Block);
1767
John McCall9ae2f072010-08-23 23:25:46 +00001768 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001769}