blob: a2b7cdd81b8761cfa2fb3ab0a1be43bbd1b1f12e [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000023using namespace clang;
24
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 Kyrtzidisdee82912008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000042/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000043/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000044/// [OBC] objc-throw-statement
45/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000046/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000047/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// [OMP] openmp-construct [TODO]
49///
50/// labeled-statement:
51/// identifier ':' statement
52/// 'case' constant-expression ':' statement
53/// 'default' ':' statement
54///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000055/// selection-statement:
56/// if-statement
57/// switch-statement
58///
59/// iteration-statement:
60/// while-statement
61/// do-statement
62/// for-statement
63///
Chris Lattner9075bd72006-08-10 04:59:57 +000064/// expression-statement:
65/// expression[opt] ';'
66///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000067/// jump-statement:
68/// 'goto' identifier ';'
69/// 'continue' ';'
70/// 'break' ';'
71/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000072/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000073///
Fariborz Jahanian90814572007-10-04 20:19:06 +000074/// [OBC] objc-throw-statement:
75/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000076/// [OBC] '@' 'throw' ';'
77///
John McCalldadc5752010-08-24 06:29:42 +000078StmtResult
Fariborz Jahanian1db5c942010-09-28 20:42:35 +000079Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000080 const char *SemiError = 0;
John McCalldadc5752010-08-24 06:29:42 +000081 StmtResult Res;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +000082
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000083 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000084
John McCall084e83d2011-03-24 11:26:52 +000085 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000086 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +000087
Chris Lattner503fadc2006-08-10 05:45:44 +000088 // Cases in this switch statement should fall through if the parser expects
89 // the token to end in a semicolon (in which case SemiError should be set),
90 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +000091Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000092 tok::TokenKind Kind = Tok.getKind();
93 SourceLocation AtLoc;
94 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000095 case tok::at: // May be a @try or @throw statement
96 {
97 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000098 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000099 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000100
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000101 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000102 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000103 cutOffParsing();
104 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000105
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000106 case tok::identifier: {
107 Token Next = NextToken();
108 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000109 // identifier ':' statement
John McCall53fa7142010-12-24 02:08:15 +0000110 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000111 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000112
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000113 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000114 CXXScopeSpec SS;
115 IdentifierInfo *Name = Tok.getIdentifierInfo();
116 SourceLocation NameLoc = Tok.getLocation();
Richard Trieu01fc0012011-09-19 19:01:00 +0000117
118 if (getLang().CPlusPlus)
119 CheckForTemplateAndDigraph(Next, ParsedType(),
120 /*EnteringContext=*/false, *Name, SS);
121
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000122 Sema::NameClassification Classification
123 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
124 switch (Classification.getKind()) {
125 case Sema::NC_Keyword:
126 // The identifier was corrected to a keyword. Update the token
127 // to this keyword, and try again.
128 if (Name->getTokenID() != tok::identifier) {
129 Tok.setIdentifierInfo(Name);
130 Tok.setKind(Name->getTokenID());
131 goto Retry;
132 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000133
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000134 // Fall through via the normal error path.
135 // FIXME: This seems like it could only happen for context-sensitive
136 // keywords.
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000137
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000138 case Sema::NC_Error:
139 // Handle errors here by skipping up to the next semicolon or '}', and
140 // eat the semicolon if that's what stopped us.
141 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
142 if (Tok.is(tok::semi))
143 ConsumeToken();
144 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000145
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000146 case Sema::NC_Unknown:
147 // Either we don't know anything about this identifier, or we know that
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000148 // we're in a syntactic context we haven't handled yet.
149 break;
150
Douglas Gregor19b7acf2011-04-27 05:41:15 +0000151 case Sema::NC_Type:
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000152 Tok.setKind(tok::annot_typename);
153 setTypeAnnotation(Tok, Classification.getType());
154 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000155 PP.AnnotateCachedTokens(Tok);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000156 break;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000157
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000158 case Sema::NC_Expression:
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000159 Tok.setKind(tok::annot_primary_expr);
160 setExprAnnotation(Tok, Classification.getExpression());
161 Tok.setAnnotationEndLoc(NameLoc);
162 PP.AnnotateCachedTokens(Tok);
163 break;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000164
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000165 case Sema::NC_TypeTemplate:
166 case Sema::NC_FunctionTemplate: {
167 ConsumeToken(); // the identifier
168 UnqualifiedId Id;
169 Id.setIdentifier(Name, NameLoc);
170 if (AnnotateTemplateIdToken(
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000171 TemplateTy::make(Classification.getTemplateName()),
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000172 Classification.getTemplateNameKind(),
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000173 SS, Id, SourceLocation(),
174 /*AllowTypeAnnotation=*/false)) {
175 // Handle errors here by skipping up to the next semicolon or '}', and
176 // eat the semicolon if that's what stopped us.
177 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
178 if (Tok.is(tok::semi))
179 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000180 return StmtError();
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000181 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000182
183 // If the next token is '::', jump right into parsing a
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000184 // nested-name-specifier. We don't want to leave the template-id
185 // hanging.
186 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000187 // Handle errors here by skipping up to the next semicolon or '}', and
188 // eat the semicolon if that's what stopped us.
189 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
190 if (Tok.is(tok::semi))
191 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000192 return StmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000193 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000194
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000195 // We've annotated a template-id, so try again now.
196 goto Retry;
197 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000198
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000199 case Sema::NC_NestedNameSpecifier:
200 // FIXME: Implement this!
201 break;
202 }
203 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000204
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000205 // Fall through
206 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000207
Chris Lattner803802d2009-03-24 17:04:48 +0000208 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000209 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000210 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000211 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall53fa7142010-12-24 02:08:15 +0000212 DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000213 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000214 }
215
216 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000217 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000218 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000219 }
Mike Stump11289f42009-09-09 15:08:12 +0000220
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000221 return ParseExprStatement(attrs);
Chris Lattner803802d2009-03-24 17:04:48 +0000222 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000223
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000224 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000225 return ParseCaseStatement(attrs);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000226 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall53fa7142010-12-24 02:08:15 +0000227 return ParseDefaultStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000228
Chris Lattner9075bd72006-08-10 04:59:57 +0000229 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall53fa7142010-12-24 02:08:15 +0000230 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000231 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000232 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
233 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000234 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000235
Chris Lattner9075bd72006-08-10 04:59:57 +0000236 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall53fa7142010-12-24 02:08:15 +0000237 return ParseIfStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000238 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall53fa7142010-12-24 02:08:15 +0000239 return ParseSwitchStatement(attrs);
Sebastian Redl042ad952008-12-11 19:30:53 +0000240
Chris Lattner9075bd72006-08-10 04:59:57 +0000241 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall53fa7142010-12-24 02:08:15 +0000242 return ParseWhileStatement(attrs);
Chris Lattner9075bd72006-08-10 04:59:57 +0000243 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall53fa7142010-12-24 02:08:15 +0000244 Res = ParseDoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000245 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000246 break;
247 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall53fa7142010-12-24 02:08:15 +0000248 return ParseForStatement(attrs);
Chris Lattner503fadc2006-08-10 05:45:44 +0000249
250 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall53fa7142010-12-24 02:08:15 +0000251 Res = ParseGotoStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000252 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000253 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000254 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall53fa7142010-12-24 02:08:15 +0000255 Res = ParseContinueStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000256 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000257 break;
258 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall53fa7142010-12-24 02:08:15 +0000259 Res = ParseBreakStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000260 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000261 break;
262 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall53fa7142010-12-24 02:08:15 +0000263 Res = ParseReturnStatement(attrs);
Chris Lattner34a95662009-06-14 00:07:48 +0000264 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000265 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000266
Sebastian Redlb219c902008-12-21 16:41:36 +0000267 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000268 ProhibitAttributes(attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000269 bool msAsm = false;
270 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000271 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000272 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000273 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000274 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000275 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000276
Sebastian Redlb219c902008-12-21 16:41:36 +0000277 case tok::kw_try: // C++ 15: try-block
John McCall53fa7142010-12-24 02:08:15 +0000278 return ParseCXXTryBlock(attrs);
John Wiegley1c0675e2011-04-28 01:08:34 +0000279
280 case tok::kw___try:
281 return ParseSEHTryBlock(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000282 }
283
Chris Lattner503fadc2006-08-10 05:45:44 +0000284 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000285 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000286 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000287 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000288 // If the result was valid, then we do want to diagnose this. Use
289 // ExpectAndConsume to emit the diagnostic, even though we know it won't
290 // succeed.
291 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000292 // Skip until we see a } or ;, but don't eat it.
293 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000294 }
Mike Stump11289f42009-09-09 15:08:12 +0000295
Sebastian Redl042ad952008-12-11 19:30:53 +0000296 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000297}
298
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000299/// \brief Parse an expression statement.
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000300StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000301 // If a case keyword is missing, this is where it should be inserted.
302 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000303
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000304 // FIXME: Use the attributes
305 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000306 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000307 if (Expr.isInvalid()) {
308 // If the expression is invalid, skip ahead to the next semicolon or '}'.
309 // Not doing this opens us up to the possibility of infinite loops if
310 // ParseExpression does not consume any tokens.
311 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
312 if (Tok.is(tok::semi))
313 ConsumeToken();
314 return StmtError();
315 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000316
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000317 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
318 Actions.CheckCaseExpression(Expr.get())) {
319 // If a constant expression is followed by a colon inside a switch block,
320 // suggest a missing case keyword.
321 Diag(OldToken, diag::err_expected_case_before_expression)
322 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000323
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000324 // Recover parsing as a case statement.
325 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
326 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000327
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000328 // Otherwise, eat the semicolon.
329 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
330 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley1c0675e2011-04-28 01:08:34 +0000331}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000332
John Wiegley1c0675e2011-04-28 01:08:34 +0000333StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
334 assert(Tok.is(tok::kw___try) && "Expected '__try'");
335 SourceLocation Loc = ConsumeToken();
336 return ParseSEHTryBlockCommon(Loc);
337}
338
339/// ParseSEHTryBlockCommon
340///
341/// seh-try-block:
342/// '__try' compound-statement seh-handler
343///
344/// seh-handler:
345/// seh-except-block
346/// seh-finally-block
347///
348StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
349 if(Tok.isNot(tok::l_brace))
350 return StmtError(Diag(Tok,diag::err_expected_lbrace));
351
352 ParsedAttributesWithRange attrs(AttrFactory);
353 StmtResult TryBlock(ParseCompoundStatement(attrs));
354 if(TryBlock.isInvalid())
355 return move(TryBlock);
356
357 StmtResult Handler;
358 if(Tok.is(tok::kw___except)) {
359 SourceLocation Loc = ConsumeToken();
360 Handler = ParseSEHExceptBlock(Loc);
361 } else if (Tok.is(tok::kw___finally)) {
362 SourceLocation Loc = ConsumeToken();
363 Handler = ParseSEHFinallyBlock(Loc);
364 } else {
365 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
366 }
367
368 if(Handler.isInvalid())
369 return move(Handler);
370
371 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
372 TryLoc,
373 TryBlock.take(),
374 Handler.take());
375}
376
377/// ParseSEHExceptBlock - Handle __except
378///
379/// seh-except-block:
380/// '__except' '(' seh-filter-expression ')' compound-statement
381///
382StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
383 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
384 raii2(Ident___exception_code, false),
385 raii3(Ident_GetExceptionCode, false);
386
387 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
388 return StmtError();
389
390 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
391
Francois Pichetbfaf4772011-04-28 03:14:31 +0000392 if (getLang().Borland) {
393 Ident__exception_info->setIsPoisoned(false);
394 Ident___exception_info->setIsPoisoned(false);
395 Ident_GetExceptionInfo->setIsPoisoned(false);
396 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000397 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000398
399 if (getLang().Borland) {
400 Ident__exception_info->setIsPoisoned(true);
401 Ident___exception_info->setIsPoisoned(true);
402 Ident_GetExceptionInfo->setIsPoisoned(true);
403 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000404
405 if(FilterExpr.isInvalid())
406 return StmtError();
407
408 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
409 return StmtError();
410
411 ParsedAttributesWithRange attrs(AttrFactory);
412 StmtResult Block(ParseCompoundStatement(attrs));
413
414 if(Block.isInvalid())
415 return move(Block);
416
417 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
418}
419
420/// ParseSEHFinallyBlock - Handle __finally
421///
422/// seh-finally-block:
423/// '__finally' compound-statement
424///
425StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
426 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
427 raii2(Ident___abnormal_termination, false),
428 raii3(Ident_AbnormalTermination, false);
429
430 ParsedAttributesWithRange attrs(AttrFactory);
431 StmtResult Block(ParseCompoundStatement(attrs));
432 if(Block.isInvalid())
433 return move(Block);
434
435 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000436}
437
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000438/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000439///
440/// labeled-statement:
441/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000442/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000443///
John McCall53fa7142010-12-24 02:08:15 +0000444StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000445 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
446 "Not an identifier!");
447
448 Token IdentTok = Tok; // Save the whole token.
449 ConsumeToken(); // eat the identifier.
450
451 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000452
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000453 // identifier ':' statement
454 SourceLocation ColonLoc = ConsumeToken();
455
456 // Read label attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +0000457 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000458
John McCalldadc5752010-08-24 06:29:42 +0000459 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000460
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000461 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000462 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000463 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000464
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000465 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
466 IdentTok.getLocation());
467 if (AttributeList *Attrs = attrs.getList())
468 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000469
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000470 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
471 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000472}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000473
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000474/// ParseCaseStatement
475/// labeled-statement:
476/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000477/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000478///
Richard Trieu2c850c02011-04-21 21:44:26 +0000479StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
480 ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000481 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Alexis Hunt96d5c762009-11-21 08:43:09 +0000482 // FIXME: Use attributes?
Mike Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattner34a22092009-03-04 04:23:07 +0000484 // It is very very common for code to contain many case statements recursively
485 // nested, as in (but usually without indentation):
486 // case 1:
487 // case 2:
488 // case 3:
489 // case 4:
490 // case 5: etc.
491 //
492 // Parsing this naively works, but is both inefficient and can cause us to run
493 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000494 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000495 // but all the grossness is constrained to ParseCaseStatement (and some
496 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattner34a22092009-03-04 04:23:07 +0000498 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
499 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000500 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner34a22092009-03-04 04:23:07 +0000502 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
503 // gets updated each time a new case is parsed, and whose body is unset so
504 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieu3481fcd2011-09-09 02:16:15 +0000505 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattner34a22092009-03-04 04:23:07 +0000507 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000508 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000509 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000510 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
511 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000512
Douglas Gregord328d572009-09-21 18:10:23 +0000513 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000514 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000515 cutOffParsing();
516 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000517 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000518
Chris Lattner125c0ee2009-12-10 00:38:54 +0000519 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
520 /// Disable this form of error recovery while we're parsing the case
521 /// expression.
522 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000523
Richard Trieu2c850c02011-04-21 21:44:26 +0000524 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
525 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000526 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000527 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000528 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000529 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000530
Chris Lattner34a22092009-03-04 04:23:07 +0000531 // GNU case range extension.
532 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000533 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000534 if (Tok.is(tok::ellipsis)) {
535 Diag(Tok, diag::ext_gnu_case_range);
536 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000537
Chris Lattner34a22092009-03-04 04:23:07 +0000538 RHS = ParseConstantExpression();
539 if (RHS.isInvalid()) {
540 SkipUntil(tok::colon);
541 return StmtError();
542 }
543 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000544
Chris Lattner125c0ee2009-12-10 00:38:54 +0000545 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000546
John McCall0140bfe2011-01-22 09:28:32 +0000547 if (Tok.is(tok::colon)) {
548 ColonLoc = ConsumeToken();
549
550 // Treat "case blah;" as a typo for "case blah:".
551 } else if (Tok.is(tok::semi)) {
552 ColonLoc = ConsumeToken();
553 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
554 << FixItHint::CreateReplacement(ColonLoc, ":");
555 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000556 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
557 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
558 << FixItHint::CreateInsertion(ExpectedLoc, ":");
559 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000560 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000561
John McCalldadc5752010-08-24 06:29:42 +0000562 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000563 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
564 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000565
Chris Lattner34a22092009-03-04 04:23:07 +0000566 // If we had a sema error parsing this case, then just ignore it and
567 // continue parsing the sub-stmt.
568 if (Case.isInvalid()) {
569 if (TopLevelCase.isInvalid()) // No parsed case stmts.
570 return ParseStatement();
571 // Otherwise, just don't add it as a nested case.
572 } else {
573 // If this is the first case statement we parsed, it becomes TopLevelCase.
574 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000575 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000576 if (TopLevelCase.isInvalid())
577 TopLevelCase = move(Case);
578 else
John McCallb268a282010-08-23 23:25:46 +0000579 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000580 DeepestParsedCaseStmt = NextDeepest;
581 }
Mike Stump11289f42009-09-09 15:08:12 +0000582
Chris Lattner34a22092009-03-04 04:23:07 +0000583 // Handle all case statements.
584 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner34a22092009-03-04 04:23:07 +0000586 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner34a22092009-03-04 04:23:07 +0000588 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000589 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000590
Chris Lattner34a22092009-03-04 04:23:07 +0000591 if (Tok.isNot(tok::r_brace)) {
592 SubStmt = ParseStatement();
593 } else {
594 // Nicely diagnose the common error "switch (X) { case 4: }", which is
595 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000596 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
597 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000598 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Chris Lattner34a22092009-03-04 04:23:07 +0000601 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000602 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000603 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner34a22092009-03-04 04:23:07 +0000605 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000606 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000607
Chris Lattner34a22092009-03-04 04:23:07 +0000608 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000609 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000610}
611
612/// ParseDefaultStatement
613/// labeled-statement:
614/// 'default' ':' statement
615/// Note that this does not parse the 'statement' at the end.
616///
John McCall53fa7142010-12-24 02:08:15 +0000617StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000618 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000619
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000620 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000621 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000622
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000623 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000624 if (Tok.is(tok::colon)) {
625 ColonLoc = ConsumeToken();
626
627 // Treat "default;" as a typo for "default:".
628 } else if (Tok.is(tok::semi)) {
629 ColonLoc = ConsumeToken();
630 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
631 << FixItHint::CreateReplacement(ColonLoc, ":");
632 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000633 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
634 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
635 << FixItHint::CreateInsertion(ExpectedLoc, ":");
636 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000637 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000638
Chris Lattner30f910e2006-10-16 05:52:41 +0000639 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000640 if (Tok.is(tok::r_brace)) {
David Majnemerc6a99872011-06-14 15:24:38 +0000641 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
642 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000643 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000644 }
645
John McCalldadc5752010-08-24 06:29:42 +0000646 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000647 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000648 return StmtError();
649
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000650 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000651 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000652}
653
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000654StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
655 bool isStmtExpr) {
656 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
657}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000658
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000659/// ParseCompoundStatement - Parse a "{}" block.
660///
661/// compound-statement: [C99 6.8.2]
662/// { block-item-list[opt] }
663/// [GNU] { label-declarations block-item-list } [TODO]
664///
665/// block-item-list:
666/// block-item
667/// block-item-list block-item
668///
669/// block-item:
670/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000671/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000672/// statement
673/// [OMP] openmp-directive [TODO]
674///
675/// [GNU] label-declarations:
676/// [GNU] label-declaration
677/// [GNU] label-declarations label-declaration
678///
679/// [GNU] label-declaration:
680/// [GNU] '__label__' identifier-list ';'
681///
682/// [OMP] openmp-directive: [TODO]
683/// [OMP] barrier-directive
684/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000685///
John McCall53fa7142010-12-24 02:08:15 +0000686StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000687 bool isStmtExpr,
688 unsigned ScopeFlags) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000689 //FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000690
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000691 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000692
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000693 // Enter a scope to hold everything within the compound stmt. Compound
694 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000695 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000696
697 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000698 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000699}
700
701
702/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000703/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000704/// consume the '}' at the end of the block. It does not manipulate the scope
705/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000706StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000707 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000708 Tok.getLocation(),
709 "in compound statement ('{}')");
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000710 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000711 BalancedDelimiterTracker T(*this, tok::l_brace);
712 if (T.consumeOpen())
713 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000714
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000715 StmtVector Stmts(Actions);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000716
Chris Lattner43e7f312011-02-18 02:08:43 +0000717 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
718 // only allowed at the start of a compound stmt regardless of the language.
719 while (Tok.is(tok::kw___label__)) {
720 SourceLocation LabelLoc = ConsumeToken();
721 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000722
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000723 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000724 while (1) {
725 if (Tok.isNot(tok::identifier)) {
726 Diag(Tok, diag::err_expected_ident);
727 break;
728 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000729
Chris Lattner43e7f312011-02-18 02:08:43 +0000730 IdentifierInfo *II = Tok.getIdentifierInfo();
731 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000732 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000733
Chris Lattner43e7f312011-02-18 02:08:43 +0000734 if (!Tok.is(tok::comma))
735 break;
736 ConsumeToken();
737 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000738
John McCall084e83d2011-03-24 11:26:52 +0000739 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000740 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
741 DeclsInGroup.data(), DeclsInGroup.size());
742 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000743
Chris Lattner43e7f312011-02-18 02:08:43 +0000744 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
745 if (R.isUsable())
746 Stmts.push_back(R.release());
747 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000748
Chris Lattner43e7f312011-02-18 02:08:43 +0000749 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000750 if (Tok.is(tok::annot_pragma_unused)) {
751 HandlePragmaUnused();
752 continue;
753 }
754
Francois Pichet0706d202011-09-17 17:15:52 +0000755 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000756 Tok.is(tok::kw___if_not_exists))) {
757 ParseMicrosoftIfExistsStatement(Stmts);
758 continue;
759 }
760
John McCalldadc5752010-08-24 06:29:42 +0000761 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000762 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000763 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000764 } else {
765 // __extension__ can start declarations and it can also be a unary
766 // operator for expressions. Consume multiple __extension__ markers here
767 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000768 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000769 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000770 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000771 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000772
John McCall084e83d2011-03-24 11:26:52 +0000773 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000774 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000775
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000776 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000777 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000778 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000779 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000780 ExtensionRAIIObject O(Diags);
781
Chris Lattner49836b42009-04-02 04:16:50 +0000782 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000783 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
784 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000785 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000786 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000787 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000788 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000789 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000790
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000791 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000792 SkipUntil(tok::semi);
793 continue;
794 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000795
Alexis Hunt96d5c762009-11-21 08:43:09 +0000796 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000797 // Eat the semicolon at the end of stmt and convert the expr into a
798 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000799 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000800 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000801 }
802 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000803
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000804 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000805 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000806 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000807
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000808 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000809 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000810 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000811 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Sebastian Redl042ad952008-12-11 19:30:53 +0000812 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000813 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000814
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000815 if (T.consumeClose())
816 return StmtError();
817
818 return Actions.ActOnCompoundStmt(T.getOpenLocation(), T.getCloseLocation(),
819 move_arg(Stmts), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000820}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000821
Chris Lattnerc0081db2008-12-12 06:31:07 +0000822/// ParseParenExprOrCondition:
823/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000824/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000825///
826/// This function parses and performs error recovery on the specified condition
827/// or expression (depending on whether we're in C++ or C mode). This function
828/// goes out of its way to recover well. It returns true if there was a parser
829/// error (the right paren couldn't be found), which indicates that the caller
830/// should try to recover harder. It returns false if the condition is
831/// successfully parsed. Note that a successful parse can still have semantic
832/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000833bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000834 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000835 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000836 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000837 BalancedDelimiterTracker T(*this, tok::l_paren);
838 T.consumeOpen();
839
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000840 if (getLang().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000841 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000842 else {
843 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000844 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000845
Douglas Gregore60e41a2010-05-06 17:25:47 +0000846 // If required, convert to a boolean value.
847 if (!ExprResult.isInvalid() && ConvertToBoolean)
848 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000849 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattnerc0081db2008-12-12 06:31:07 +0000852 // If the parser was confused by the condition and we don't have a ')', try to
853 // recover by skipping ahead to a semi and bailing out. If condexp is
854 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000855 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000856 SkipUntil(tok::semi);
857 // Skipping may have stopped if it found the containing ')'. If so, we can
858 // continue parsing the if statement.
859 if (Tok.isNot(tok::r_paren))
860 return true;
861 }
Mike Stump11289f42009-09-09 15:08:12 +0000862
Chris Lattnerc0081db2008-12-12 06:31:07 +0000863 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000864 T.consumeClose();
Chris Lattnerc0081db2008-12-12 06:31:07 +0000865 return false;
866}
867
868
Chris Lattnerc951dae2006-08-10 04:23:57 +0000869/// ParseIfStatement
870/// if-statement: [C99 6.8.4.1]
871/// 'if' '(' expression ')' statement
872/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000873/// [C++] 'if' '(' condition ')' statement
874/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000875///
John McCall53fa7142010-12-24 02:08:15 +0000876StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000877 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000878
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000879 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000880 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000881
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000882 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000883 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000884 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000885 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000886 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000887
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000888 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
889
Chris Lattner2dd1b722007-08-26 23:08:06 +0000890 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
891 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000892 //
893 // C++ 6.4p3:
894 // A name introduced by a declaration in a condition is in scope from its
895 // point of declaration until the end of the substatements controlled by the
896 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000897 // C++ 3.3.2p4:
898 // Names declared in the for-init-statement, and in the condition of if,
899 // while, for, and switch statements are local to the if, while, for, or
900 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000901 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000902 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000903
Chris Lattnerc951dae2006-08-10 04:23:57 +0000904 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000905 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000906 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000907 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000908 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000909
John McCallb268a282010-08-23 23:25:46 +0000910 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump11289f42009-09-09 15:08:12 +0000911
Chris Lattner8fb26252007-08-22 05:28:50 +0000912 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000913 // there is no compound stmt. C90 does not have this clause. We only do this
914 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000915 //
916 // C++ 6.4p1:
917 // The substatement in a selection-statement (each substatement, in the else
918 // form of the if statement) implicitly defines a local scope.
919 //
920 // For C++ we create a scope for the condition and a new scope for
921 // substatements because:
922 // -When the 'then' scope exits, we want the condition declaration to still be
923 // active for the 'else' scope too.
924 // -Sema will detect name clashes by considering declarations of a
925 // 'ControlScope' as part of its direct subscope.
926 // -If we wanted the condition and substatement to be in the same scope, we
927 // would have to notify ParseStatement not to create a new scope. It's
928 // simpler to let it create a new scope.
929 //
Mike Stump11289f42009-09-09 15:08:12 +0000930 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000931 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000932
Chris Lattner5c5808a2007-10-29 05:08:52 +0000933 // Read the 'then' stmt.
934 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +0000935 StmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000936
Chris Lattner37e54f42007-08-22 05:16:28 +0000937 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000938 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000939
Chris Lattnerc951dae2006-08-10 04:23:57 +0000940 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000941 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000942 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000943 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000944
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000945 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000946 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000947 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000948
Chris Lattner8fb26252007-08-22 05:28:50 +0000949 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000950 // there is no compound stmt. C90 does not have this clause. We only do
951 // this if the body isn't a compound statement to avoid push/pop in common
952 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000953 //
954 // C++ 6.4p1:
955 // The substatement in a selection-statement (each substatement, in the else
956 // form of the if statement) implicitly defines a local scope.
957 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000958 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000959 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000960
Chris Lattner30f910e2006-10-16 05:52:41 +0000961 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000962
Chris Lattner37e54f42007-08-22 05:16:28 +0000963 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000964 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +0000965 } else if (Tok.is(tok::code_completion)) {
966 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000967 cutOffParsing();
968 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000969 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000970
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000971 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000972
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000973 // If the condition was invalid, discard the if statement. We could recover
974 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +0000975 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000976 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000977
Chris Lattner5c5808a2007-10-29 05:08:52 +0000978 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000979 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000980 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000981 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
982 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
983 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000984 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000985 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000986 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000987
Chris Lattner5c5808a2007-10-29 05:08:52 +0000988 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000989 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000990 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000991 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000992 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000993
John McCallb268a282010-08-23 23:25:46 +0000994 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000995 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +0000996}
997
Chris Lattner9075bd72006-08-10 04:59:57 +0000998/// ParseSwitchStatement
999/// switch-statement:
1000/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001001/// [C++] 'switch' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +00001002StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001003 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001004
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001005 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001006 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001007
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001008 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001009 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001010 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001011 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001012 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001013
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001014 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1015
Chris Lattner2dd1b722007-08-26 23:08:06 +00001016 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1017 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001018 //
1019 // C++ 6.4p3:
1020 // A name introduced by a declaration in a condition is in scope from its
1021 // point of declaration until the end of the substatements controlled by the
1022 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001023 // C++ 3.3.2p4:
1024 // Names declared in the for-init-statement, and in the condition of if,
1025 // while, for, and switch statements are local to the if, while, for, or
1026 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001027 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001028 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001029 if (C99orCXX)
1030 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001031 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001032
Chris Lattner9075bd72006-08-10 04:59:57 +00001033 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001034 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001035 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001036 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001037 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001038
John McCalldadc5752010-08-24 06:29:42 +00001039 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001040 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001041
Douglas Gregore60e41a2010-05-06 17:25:47 +00001042 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001043 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001044 // FIXME: This is not optimal recovery, but parsing the body is more
1045 // dangerous due to the presence of case and default statements, which
1046 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001047 if (Tok.is(tok::l_brace)) {
1048 ConsumeBrace();
1049 SkipUntil(tok::r_brace, false, false);
1050 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001051 SkipUntil(tok::semi);
1052 return move(Switch);
1053 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001054
Chris Lattner8fb26252007-08-22 05:28:50 +00001055 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001056 // there is no compound stmt. C90 does not have this clause. We only do this
1057 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001058 //
1059 // C++ 6.4p1:
1060 // The substatement in a selection-statement (each substatement, in the else
1061 // form of the if statement) implicitly defines a local scope.
1062 //
1063 // See comments in ParseIfStatement for why we create a scope for the
1064 // condition and a new scope for substatement in C++.
1065 //
Mike Stump11289f42009-09-09 15:08:12 +00001066 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001067 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001068
Chris Lattner9075bd72006-08-10 04:59:57 +00001069 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001070 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001071
Chris Lattner8fd2d012010-01-24 01:50:29 +00001072 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001073 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001074 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001075
Chris Lattner8fd2d012010-01-24 01:50:29 +00001076 if (Body.isInvalid())
1077 // FIXME: Remove the case statement list from the Switch statement.
1078 Body = Actions.ActOnNullStmt(Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001079
John McCallb268a282010-08-23 23:25:46 +00001080 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001081}
1082
1083/// ParseWhileStatement
1084/// while-statement: [C99 6.8.5.1]
1085/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001086/// [C++] 'while' '(' condition ')' statement
John McCall53fa7142010-12-24 02:08:15 +00001087StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001088 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001089
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001090 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001091 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001092 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001093
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001094 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001095 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001096 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001097 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001098 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001099
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001100 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1101
Chris Lattner2dd1b722007-08-26 23:08:06 +00001102 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1103 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001104 //
1105 // C++ 6.4p3:
1106 // A name introduced by a declaration in a condition is in scope from its
1107 // point of declaration until the end of the substatements controlled by the
1108 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001109 // C++ 3.3.2p4:
1110 // Names declared in the for-init-statement, and in the condition of if,
1111 // while, for, and switch statements are local to the if, while, for, or
1112 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001113 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001114 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001115 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001116 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1117 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001118 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001119 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1120 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001121
Chris Lattner9075bd72006-08-10 04:59:57 +00001122 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001123 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001124 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001125 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001126 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001127
John McCallb268a282010-08-23 23:25:46 +00001128 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattner8fb26252007-08-22 05:28:50 +00001130 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001131 // there is no compound stmt. C90 does not have this clause. We only do this
1132 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001133 //
1134 // C++ 6.5p2:
1135 // The substatement in an iteration-statement implicitly defines a local scope
1136 // which is entered and exited each time through the loop.
1137 //
1138 // See comments in ParseIfStatement for why we create a scope for the
1139 // condition and a new scope for substatement in C++.
1140 //
Mike Stump11289f42009-09-09 15:08:12 +00001141 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001142 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001143
Chris Lattner9075bd72006-08-10 04:59:57 +00001144 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001145 StmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001146
Chris Lattner8fb26252007-08-22 05:28:50 +00001147 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001148 InnerScope.Exit();
1149 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001150
John McCall48871652010-08-21 09:40:31 +00001151 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001152 return StmtError();
1153
John McCallb268a282010-08-23 23:25:46 +00001154 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001155}
1156
1157/// ParseDoStatement
1158/// do-statement: [C99 6.8.5.2]
1159/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001160/// Note: this lets the caller parse the end ';'.
John McCall53fa7142010-12-24 02:08:15 +00001161StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001162 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001163
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001164 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001165 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001166
Chris Lattner2dd1b722007-08-26 23:08:06 +00001167 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1168 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001169 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001170 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001171 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001172 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001173 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001174
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001175 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001176
Chris Lattner8fb26252007-08-22 05:28:50 +00001177 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001178 // there is no compound stmt. C90 does not have this clause. We only do this
1179 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001180 //
1181 // C++ 6.5p2:
1182 // The substatement in an iteration-statement implicitly defines a local scope
1183 // which is entered and exited each time through the loop.
1184 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001185 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +00001186 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001187 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001188
Chris Lattner9075bd72006-08-10 04:59:57 +00001189 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001190 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001191
Chris Lattner8fb26252007-08-22 05:28:50 +00001192 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001193 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001194
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001195 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001196 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001197 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001198 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001199 SkipUntil(tok::semi, false, true);
1200 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001201 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001202 }
Chris Lattneraf635312006-10-16 06:06:51 +00001203 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001204
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001205 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001206 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001207 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001208 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001209 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001210
Chris Lattner10da53c2008-12-12 06:35:28 +00001211 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001212 BalancedDelimiterTracker T(*this, tok::l_paren);
1213 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001214 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001215 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001216 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001217
Sebastian Redlb62406f2008-12-11 19:48:14 +00001218 if (Cond.isInvalid() || Body.isInvalid())
1219 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001220
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001221 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1222 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001223}
1224
1225/// ParseForStatement
1226/// for-statement: [C99 6.8.5.3]
1227/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1228/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001229/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1230/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001231/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001232/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1233/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001234///
1235/// [C++] for-init-statement:
1236/// [C++] expression-statement
1237/// [C++] simple-declaration
1238///
Richard Smith02e85f32011-04-14 22:09:26 +00001239/// [C++0x] for-range-declaration:
1240/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1241/// [C++0x] for-range-initializer:
1242/// [C++0x] expression
1243/// [C++0x] braced-init-list [TODO]
John McCall53fa7142010-12-24 02:08:15 +00001244StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001245 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001246
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001247 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001248 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001249
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001250 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001251 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001252 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001253 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001254 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001255
Chris Lattner934074c2009-04-22 00:54:41 +00001256 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001257
Chris Lattner2dd1b722007-08-26 23:08:06 +00001258 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1259 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001260 //
1261 // C++ 6.4p3:
1262 // A name introduced by a declaration in a condition is in scope from its
1263 // point of declaration until the end of the substatements controlled by the
1264 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001265 // C++ 3.3.2p4:
1266 // Names declared in the for-init-statement, and in the condition of if,
1267 // while, for, and switch statements are local to the if, while, for, or
1268 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001269 // C++ 6.5.3p1:
1270 // Names declared in the for-init-statement are in the same declarative-region
1271 // as those declared in the condition.
1272 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001273 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001274 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001275 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1276 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001277 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001278 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1279
1280 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001281
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001282 BalancedDelimiterTracker T(*this, tok::l_paren);
1283 T.consumeOpen();
1284
John McCalldadc5752010-08-24 06:29:42 +00001285 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001286
Richard Smith02e85f32011-04-14 22:09:26 +00001287 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001288 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001289 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001290 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001291 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001292 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001293 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001294 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001295
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001296 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001297 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001298 C99orCXXorObjC? Sema::PCC_ForInit
1299 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001300 cutOffParsing();
1301 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001302 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001303
Chris Lattner9075bd72006-08-10 04:59:57 +00001304 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001305 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +00001306 // no first part, eat the ';'.
1307 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +00001308 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001309 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001310 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001311 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001312
John McCall084e83d2011-03-24 11:26:52 +00001313 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001314 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001315
Richard Smith02e85f32011-04-14 22:09:26 +00001316 // In C++0x, "for (T NS:a" might not be a typo for ::
1317 bool MightBeForRangeStmt = getLang().CPlusPlus;
1318 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1319
Chris Lattner49836b42009-04-02 04:16:50 +00001320 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001321 StmtVector Stmts(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001322 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001323 DeclEnd, attrs, false,
1324 MightBeForRangeStmt ?
1325 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001326 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001327
Richard Smith02e85f32011-04-14 22:09:26 +00001328 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith58c74332011-09-04 19:54:14 +00001329 if (!getLang().CPlusPlus0x)
1330 Diag(ForRangeInit.ColonLoc, diag::ext_for_range);
1331
Richard Smith02e85f32011-04-14 22:09:26 +00001332 ForRange = true;
1333 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001334 ConsumeToken();
1335 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001336 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001337 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001338 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001339
Douglas Gregor68762e72010-08-23 21:17:50 +00001340 if (Tok.is(tok::code_completion)) {
1341 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001342 cutOffParsing();
1343 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001344 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001345 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001346 } else {
1347 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001348 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001349 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001350 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001351
John McCall34376a62010-12-04 03:47:34 +00001352 ForEach = isTokIdentifier_in();
1353
Chris Lattnercd68f642007-06-27 01:06:29 +00001354 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001355 if (!Value.isInvalid()) {
1356 if (ForEach)
1357 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1358 else
1359 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1360 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001361
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001362 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001363 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001364 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001365 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001366
Douglas Gregor68762e72010-08-23 21:17:50 +00001367 if (Tok.is(tok::code_completion)) {
1368 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001369 cutOffParsing();
1370 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001371 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001372 Collection = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001373 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001374 if (!Value.isInvalid()) {
1375 Diag(Tok, diag::err_expected_semi_for);
1376 } else {
1377 // Skip until semicolon or rparen, don't consume it.
1378 SkipUntil(tok::r_paren, true, true);
1379 if (Tok.is(tok::semi))
1380 ConsumeToken();
1381 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001382 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001383 }
Richard Smith02e85f32011-04-14 22:09:26 +00001384 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001385 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001386 // Parse the second part of the for specifier.
1387 if (Tok.is(tok::semi)) { // for (...;;
1388 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001389 } else if (Tok.is(tok::r_paren)) {
1390 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001391 } else {
John McCalldadc5752010-08-24 06:29:42 +00001392 ExprResult Second;
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001393 if (getLang().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001394 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1395 else {
1396 Second = ParseExpression();
1397 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001398 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001399 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001400 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001401 SecondPartIsInvalid = Second.isInvalid();
John McCallb268a282010-08-23 23:25:46 +00001402 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001403 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001404
Douglas Gregor230a7e62011-02-17 03:38:46 +00001405 if (Tok.isNot(tok::semi)) {
1406 if (!SecondPartIsInvalid || SecondVar)
1407 Diag(Tok, diag::err_expected_semi_for);
1408 else
1409 // Skip until semicolon or rparen, don't consume it.
1410 SkipUntil(tok::r_paren, true, true);
1411 }
1412
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001413 if (Tok.is(tok::semi)) {
1414 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001415 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001416
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001417 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001418 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001419 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001420 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001421 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001422 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001423 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001424 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001425
Richard Smith02e85f32011-04-14 22:09:26 +00001426 // We need to perform most of the semantic analysis for a C++0x for-range
1427 // statememt before parsing the body, in order to be able to deduce the type
1428 // of an auto-typed loop variable.
1429 StmtResult ForRangeStmt;
John McCall53848232011-07-27 01:07:15 +00001430 if (ForRange) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001431 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smith02e85f32011-04-14 22:09:26 +00001432 FirstPart.take(),
1433 ForRangeInit.ColonLoc,
1434 ForRangeInit.RangeExpr.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001435 T.getCloseLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001436
John McCall53848232011-07-27 01:07:15 +00001437
1438 // Similarly, we need to do the semantic analysis for a for-range
1439 // statement immediately in order to close over temporaries correctly.
1440 } else if (ForEach) {
1441 if (!Collection.isInvalid())
1442 Collection =
1443 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1444 }
1445
Chris Lattner8fb26252007-08-22 05:28:50 +00001446 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001447 // there is no compound stmt. C90 does not have this clause. We only do this
1448 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001449 //
1450 // C++ 6.5p2:
1451 // The substatement in an iteration-statement implicitly defines a local scope
1452 // which is entered and exited each time through the loop.
1453 //
1454 // See comments in ParseIfStatement for why we create a scope for
1455 // for-init-statement/condition and a new scope for substatement in C++.
1456 //
Mike Stump11289f42009-09-09 15:08:12 +00001457 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001458 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001459
Chris Lattner9075bd72006-08-10 04:59:57 +00001460 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001461 StmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001462
Chris Lattner8fb26252007-08-22 05:28:50 +00001463 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001464 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001465
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001466 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001467 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001468
1469 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001470 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001471
Richard Smith02e85f32011-04-14 22:09:26 +00001472 if (ForEach)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001473 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1474 FirstPart.take(),
1475 Collection.take(),
1476 T.getCloseLocation(),
1477 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001478
Richard Smith02e85f32011-04-14 22:09:26 +00001479 if (ForRange)
1480 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1481
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001482 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1483 SecondPart, SecondVar, ThirdPart,
1484 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001485}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001486
Chris Lattner503fadc2006-08-10 05:45:44 +00001487/// ParseGotoStatement
1488/// jump-statement:
1489/// 'goto' identifier ';'
1490/// [GNU] 'goto' '*' expression ';'
1491///
1492/// Note: this lets the caller parse the end ';'.
1493///
John McCall53fa7142010-12-24 02:08:15 +00001494StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001495 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001496
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001497 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001498 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001499
John McCalldadc5752010-08-24 06:29:42 +00001500 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001501 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001502 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1503 Tok.getLocation());
1504 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001505 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001506 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001507 // GNU indirect goto extension.
1508 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001509 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001510 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001511 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001512 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001513 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001514 }
John McCallb268a282010-08-23 23:25:46 +00001515 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001516 } else {
1517 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001518 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001519 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001520
Sebastian Redlb62406f2008-12-11 19:48:14 +00001521 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001522}
1523
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001524/// ParseContinueStatement
1525/// jump-statement:
1526/// 'continue' ';'
1527///
1528/// Note: this lets the caller parse the end ';'.
1529///
John McCall53fa7142010-12-24 02:08:15 +00001530StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001531 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001532
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001533 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001534 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001535}
1536
1537/// ParseBreakStatement
1538/// jump-statement:
1539/// 'break' ';'
1540///
1541/// Note: this lets the caller parse the end ';'.
1542///
John McCall53fa7142010-12-24 02:08:15 +00001543StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001544 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001545
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001546 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001547 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001548}
1549
Chris Lattner503fadc2006-08-10 05:45:44 +00001550/// ParseReturnStatement
1551/// jump-statement:
1552/// 'return' expression[opt] ';'
John McCall53fa7142010-12-24 02:08:15 +00001553StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00001554 // FIXME: Use attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001555
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001556 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001557 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001558
John McCalldadc5752010-08-24 06:29:42 +00001559 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001560 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001561 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001562 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001563 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001564 return StmtError();
1565 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001566
Douglas Gregore9e27d92011-03-11 23:10:44 +00001567 // FIXME: This is a hack to allow something like C++0x's generalized
1568 // initializer lists, but only enough of this feature to allow Clang to
1569 // parse libstdc++ 4.5's headers.
1570 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1571 R = ParseInitializer();
1572 if (R.isUsable() && !getLang().CPlusPlus0x)
1573 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1574 << R.get()->getSourceRange();
1575 } else
1576 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001577 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001578 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001579 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001580 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001581 }
John McCallb268a282010-08-23 23:25:46 +00001582 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001583}
Chris Lattner0116c472006-08-15 06:03:28 +00001584
Eli Friedmana4b02c32011-09-30 01:13:51 +00001585/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1586/// this routine is called to collect the tokens for an MS asm statement.
1587StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1588 SourceManager &SrcMgr = PP.getSourceManager();
1589 SourceLocation EndLoc = AsmLoc;
1590 do {
1591 bool InBraces = false;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001592 unsigned short savedBraceCount = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001593 bool InAsmComment = false;
1594 FileID FID;
NAKAMURA Takumi93eafc62011-10-08 11:31:53 +00001595 unsigned LineNo = 0;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001596 unsigned NumTokensRead = 0;
1597 SourceLocation LBraceLoc;
1598
1599 if (Tok.is(tok::l_brace)) {
1600 // Braced inline asm: consume the opening brace.
1601 InBraces = true;
1602 savedBraceCount = BraceCount;
1603 EndLoc = LBraceLoc = ConsumeBrace();
1604 ++NumTokensRead;
1605 } else {
1606 // Single-line inline asm; compute which line it is on.
1607 std::pair<FileID, unsigned> ExpAsmLoc =
1608 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1609 FID = ExpAsmLoc.first;
1610 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1611 }
1612
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001613 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001614 do {
Eli Friedmana4b02c32011-09-30 01:13:51 +00001615 // If we hit EOF, we're done, period.
1616 if (Tok.is(tok::eof))
1617 break;
1618 // When we consume the closing brace, we're done.
1619 if (InBraces && BraceCount == savedBraceCount)
1620 break;
1621
1622 if (!InAsmComment && Tok.is(tok::semi)) {
1623 // A semicolon in an asm is the start of a comment.
1624 InAsmComment = true;
1625 if (InBraces) {
1626 // Compute which line the comment is on.
1627 std::pair<FileID, unsigned> ExpSemiLoc =
1628 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1629 FID = ExpSemiLoc.first;
1630 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1631 }
1632 } else if (!InBraces || InAsmComment) {
1633 // If end-of-line is significant, check whether this token is on a
1634 // new line.
1635 std::pair<FileID, unsigned> ExpLoc =
1636 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1637 if (ExpLoc.first != FID ||
1638 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1639 // If this is a single-line __asm, we're done.
1640 if (!InBraces)
1641 break;
1642 // We're no longer in a comment.
1643 InAsmComment = false;
1644 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1645 // Single-line asm always ends when a closing brace is seen.
1646 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1647 // does MSVC do here?
1648 break;
1649 }
1650 }
1651
1652 // Consume the next token; make sure we don't modify the brace count etc.
1653 // if we are in a comment.
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001654 EndLoc = TokLoc;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001655 if (InAsmComment)
1656 PP.Lex(Tok);
1657 else
1658 ConsumeAnyToken();
Steve Naroff8c099c32008-02-08 18:01:27 +00001659 TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001660 ++NumTokensRead;
1661 } while (1);
1662
1663 if (InBraces && BraceCount != savedBraceCount) {
1664 // __asm without closing brace (this can happen at EOF).
1665 Diag(Tok, diag::err_expected_rbrace);
1666 Diag(LBraceLoc, diag::note_matching) << "{";
1667 return StmtError();
1668 } else if (NumTokensRead == 0) {
1669 // Empty __asm.
1670 Diag(Tok, diag::err_expected_lbrace);
1671 return StmtError();
1672 }
1673 // Multiple adjacent asm's form together into a single asm statement
1674 // in the AST.
1675 if (!Tok.is(tok::kw_asm))
1676 break;
1677 EndLoc = ConsumeToken();
1678 } while (1);
1679 // FIXME: Need to actually grab the data and pass it on to Sema. Ideally,
1680 // what Sema wants is a string of the entire inline asm, with one instruction
1681 // per line and all the __asm keywords stripped out, and a way of mapping
1682 // from any character of that string to its location in the original source
1683 // code. I'm not entirely sure how to go about that, though.
Mike Stump6da5d752009-12-11 00:04:56 +00001684 Token t;
1685 t.setKind(tok::string_literal);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001686 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump6da5d752009-12-11 00:04:56 +00001687 t.clearFlag(Token::NeedsCleaning);
Chris Lattnerd28e6cc2010-08-17 16:00:12 +00001688 t.setLength(21);
Alexis Hunt3b791862010-08-30 17:47:05 +00001689 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump6da5d752009-12-11 00:04:56 +00001690 ExprVector Constraints(Actions);
1691 ExprVector Exprs(Actions);
1692 ExprVector Clobbers(Actions);
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001693 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump6da5d752009-12-11 00:04:56 +00001694 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001695 AsmString.take(), move_arg(Clobbers),
Abramo Bagnarae0acd852010-12-02 18:34:55 +00001696 EndLoc, true);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001697}
1698
Chris Lattner0116c472006-08-15 06:03:28 +00001699/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001700/// asm-statement:
1701/// gnu-asm-statement
1702/// ms-asm-statement
1703///
1704/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001705/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1706///
1707/// [GNU] asm-argument:
1708/// asm-string-literal
1709/// asm-string-literal ':' asm-operands[opt]
1710/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1711/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1712/// ':' asm-clobbers
1713///
1714/// [GNU] asm-clobbers:
1715/// asm-string-literal
1716/// asm-clobbers ',' asm-string-literal
1717///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001718/// [MS] ms-asm-statement:
Eli Friedmana4b02c32011-09-30 01:13:51 +00001719/// ms-asm-block
1720/// ms-asm-block ms-asm-statement
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001721///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001722/// [MS] ms-asm-block:
1723/// '__asm' ms-asm-line '\n'
1724/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1725///
1726/// [MS] ms-asm-instruction-block
1727/// ms-asm-line
1728/// ms-asm-line '\n' ms-asm-instruction-block
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001729///
John McCalldadc5752010-08-24 06:29:42 +00001730StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001731 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001732 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001733
Francois Pichet0706d202011-09-17 17:15:52 +00001734 if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001735 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001736 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001737 }
John McCall084e83d2011-03-24 11:26:52 +00001738 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001739 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001740 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001741
Chris Lattner0116c472006-08-15 06:03:28 +00001742 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001743 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001744 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001745 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001746 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001747
Chris Lattner0116c472006-08-15 06:03:28 +00001748 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001749 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001750 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001751 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001752 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001753 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001754 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001755 BalancedDelimiterTracker T(*this, tok::l_paren);
1756 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001757
John McCalldadc5752010-08-24 06:29:42 +00001758 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001759 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001760 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001761
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001762 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001763 ExprVector Constraints(Actions);
1764 ExprVector Exprs(Actions);
1765 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001766
Anders Carlsson19fe1162008-02-05 23:03:50 +00001767 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001768 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001769 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001770 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001771 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001772 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001773 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001774 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001775 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001776
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001777 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001778 bool AteExtraColon = false;
1779 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1780 // In C++ mode, parse "::" like ": :".
1781 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001782 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001783
Chris Lattner15768502009-12-20 23:08:04 +00001784 if (!AteExtraColon &&
1785 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001786 return StmtError();
1787 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001788
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001789 unsigned NumOutputs = Names.size();
1790
1791 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001792 if (AteExtraColon ||
1793 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1794 // In C++ mode, parse "::" like ": :".
1795 if (AteExtraColon)
1796 AteExtraColon = false;
1797 else {
1798 AteExtraColon = Tok.is(tok::coloncolon);
1799 ConsumeToken();
1800 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001801
Chris Lattner15768502009-12-20 23:08:04 +00001802 if (!AteExtraColon &&
1803 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001804 return StmtError();
1805 }
1806
1807 assert(Names.size() == Constraints.size() &&
1808 Constraints.size() == Exprs.size() &&
1809 "Input operand size mismatch!");
1810
1811 unsigned NumInputs = Names.size() - NumOutputs;
1812
1813 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001814 if (AteExtraColon || Tok.is(tok::colon)) {
1815 if (!AteExtraColon)
1816 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001817
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001818 // Parse the asm-string list for clobbers if present.
1819 if (Tok.isNot(tok::r_paren)) {
1820 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001821 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001822
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001823 if (Clobber.isInvalid())
1824 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001825
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001826 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001827
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001828 if (Tok.isNot(tok::comma)) break;
1829 ConsumeToken();
1830 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001831 }
1832 }
1833
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001834 T.consumeClose();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001835 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001836 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001837 move_arg(Constraints), move_arg(Exprs),
John McCallb268a282010-08-23 23:25:46 +00001838 AsmString.take(), move_arg(Clobbers),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001839 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001840}
1841
1842/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001843/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001844///
1845/// [GNU] asm-operands:
1846/// asm-operand
1847/// asm-operands ',' asm-operand
1848///
1849/// [GNU] asm-operand:
1850/// asm-string-literal '(' expression ')'
1851/// '[' identifier ']' asm-string-literal '(' expression ')'
1852///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001853//
1854// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001855bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001856 SmallVectorImpl<Expr *> &Constraints,
1857 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001858 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001859 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001860 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001861
1862 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001863 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001864 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001865 BalancedDelimiterTracker T(*this, tok::l_square);
1866 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001867
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001868 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001869 Diag(Tok, diag::err_expected_ident);
1870 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001871 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001872 }
Mike Stump11289f42009-09-09 15:08:12 +00001873
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001874 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001875 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001876
Anders Carlsson9a020f92010-01-30 22:25:16 +00001877 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001878 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001879 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001880 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001881
John McCalldadc5752010-08-24 06:29:42 +00001882 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001883 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001884 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001885 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001886 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001887 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001888
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001889 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001890 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001891 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001892 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001893 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001894
Chris Lattner0116c472006-08-15 06:03:28 +00001895 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001896 BalancedDelimiterTracker T(*this, tok::l_paren);
1897 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001898 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001899 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001900 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001901 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001902 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001903 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001904 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001905 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001906 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001907 ConsumeToken();
1908 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001909
1910 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001911}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001912
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001913Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001914 assert(Tok.is(tok::l_brace));
1915 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001916
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001917 if (PP.isCodeCompletionEnabled()) {
1918 if (trySkippingFunctionBodyForCodeCompletion()) {
1919 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001920 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001921 }
1922 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001923
John McCallfaf5fb42010-08-26 23:41:50 +00001924 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1925 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001926
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001927 // Do not enter a scope for the brace, as the arguments are in the same scope
1928 // (the function body) as the body itself. Instead, just read the statement
1929 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001930 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001931
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001932 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001933 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001934 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001935 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001936
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001937 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001938 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001939}
Sebastian Redlb219c902008-12-21 16:41:36 +00001940
Sebastian Redla7b98a72009-04-26 20:35:05 +00001941/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1942///
1943/// function-try-block:
1944/// 'try' ctor-initializer[opt] compound-statement handler-seq
1945///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001946Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001947 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1948 SourceLocation TryLoc = ConsumeToken();
1949
John McCallfaf5fb42010-08-26 23:41:50 +00001950 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1951 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001952
1953 // Constructor initializer list?
1954 if (Tok.is(tok::colon))
1955 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001956 else
1957 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001958
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001959 if (PP.isCodeCompletionEnabled()) {
1960 if (trySkippingFunctionBodyForCodeCompletion()) {
1961 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001962 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001963 }
1964 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001965
Sebastian Redld98ecd62009-04-26 21:08:36 +00001966 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00001967 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001968 // If we failed to parse the try-catch, we just give the function an empty
1969 // compound statement as the body.
1970 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001971 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001972 MultiStmtArg(Actions), false);
1973
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001974 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001975 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001976}
1977
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001978bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001979 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001980 assert(PP.isCodeCompletionEnabled() &&
1981 "Should only be called when in code-completion mode");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001982
1983 // We're in code-completion mode. Skip parsing for all function bodies unless
1984 // the body contains the code-completion point.
1985 TentativeParsingAction PA(*this);
1986 ConsumeBrace();
1987 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1988 /*StopAtCodeCompletion=*/true)) {
1989 PA.Commit();
1990 return true;
1991 }
1992
1993 PA.Revert();
1994 return false;
1995}
1996
Sebastian Redlb219c902008-12-21 16:41:36 +00001997/// ParseCXXTryBlock - Parse a C++ try-block.
1998///
1999/// try-block:
2000/// 'try' compound-statement handler-seq
2001///
John McCall53fa7142010-12-24 02:08:15 +00002002StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002003 // FIXME: Add attributes?
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002004
Sebastian Redlb219c902008-12-21 16:41:36 +00002005 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2006
2007 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002008 return ParseCXXTryBlockCommon(TryLoc);
2009}
2010
2011/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2012/// function-try-block.
2013///
2014/// try-block:
2015/// 'try' compound-statement handler-seq
2016///
2017/// function-try-block:
2018/// 'try' ctor-initializer[opt] compound-statement handler-seq
2019///
2020/// handler-seq:
2021/// handler handler-seq[opt]
2022///
John Wiegley1c0675e2011-04-28 01:08:34 +00002023/// [Borland] try-block:
2024/// 'try' compound-statement seh-except-block
2025/// 'try' compound-statment seh-finally-block
2026///
John McCalldadc5752010-08-24 06:29:42 +00002027StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002028 if (Tok.isNot(tok::l_brace))
2029 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002030 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002031 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002032 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
2033 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002034 if (TryBlock.isInvalid())
2035 return move(TryBlock);
2036
John Wiegley1c0675e2011-04-28 01:08:34 +00002037 // Borland allows SEH-handlers with 'try'
2038 if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
2039 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2040 StmtResult Handler;
2041 if(Tok.is(tok::kw___except)) {
2042 SourceLocation Loc = ConsumeToken();
2043 Handler = ParseSEHExceptBlock(Loc);
2044 }
2045 else {
2046 SourceLocation Loc = ConsumeToken();
2047 Handler = ParseSEHFinallyBlock(Loc);
2048 }
2049 if(Handler.isInvalid())
2050 return move(Handler);
John McCall53fa7142010-12-24 02:08:15 +00002051
John Wiegley1c0675e2011-04-28 01:08:34 +00002052 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2053 TryLoc,
2054 TryBlock.take(),
2055 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002056 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002057 else {
2058 StmtVector Handlers(Actions);
2059 MaybeParseCXX0XAttributes(attrs);
2060 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002061
John Wiegley1c0675e2011-04-28 01:08:34 +00002062 if (Tok.isNot(tok::kw_catch))
2063 return StmtError(Diag(Tok, diag::err_expected_catch));
2064 while (Tok.is(tok::kw_catch)) {
2065 StmtResult Handler(ParseCXXCatchBlock());
2066 if (!Handler.isInvalid())
2067 Handlers.push_back(Handler.release());
2068 }
2069 // Don't bother creating the full statement if we don't have any usable
2070 // handlers.
2071 if (Handlers.empty())
2072 return StmtError();
2073
2074 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2075 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002076}
2077
2078/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2079///
2080/// handler:
2081/// 'catch' '(' exception-declaration ')' compound-statement
2082///
2083/// exception-declaration:
2084/// type-specifier-seq declarator
2085/// type-specifier-seq abstract-declarator
2086/// type-specifier-seq
2087/// '...'
2088///
John McCalldadc5752010-08-24 06:29:42 +00002089StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002090 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2091
2092 SourceLocation CatchLoc = ConsumeToken();
2093
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002094 BalancedDelimiterTracker T(*this, tok::l_paren);
2095 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002096 return StmtError();
2097
2098 // C++ 3.3.2p3:
2099 // The name in a catch exception-declaration is local to the handler and
2100 // shall not be redeclared in the outermost block of the handler.
2101 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2102
2103 // exception-declaration is equivalent to '...' or a parameter-declaration
2104 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002105 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002106 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002107 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002108 if (ParseCXXTypeSpecifierSeq(DS))
2109 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002110 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2111 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002112 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002113 } else
2114 ConsumeToken();
2115
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002116 T.consumeClose();
2117 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002118 return StmtError();
2119
2120 if (Tok.isNot(tok::l_brace))
2121 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2122
Alexis Hunt96d5c762009-11-21 08:43:09 +00002123 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall084e83d2011-03-24 11:26:52 +00002124 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002125 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redlb219c902008-12-21 16:41:36 +00002126 if (Block.isInvalid())
2127 return move(Block);
2128
John McCallb268a282010-08-23 23:25:46 +00002129 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002130}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002131
2132void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002133 bool Result;
2134 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002135 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002136
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002137 if (Tok.isNot(tok::l_brace)) {
2138 Diag(Tok, diag::err_expected_lbrace);
2139 return;
2140 }
2141 ConsumeBrace();
2142
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002143 // Condition is false skip all inside the {}.
2144 if (!Result) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002145 SkipUntil(tok::r_brace, false);
2146 return;
2147 }
2148
2149 // Condition is true, parse the statements.
2150 while (Tok.isNot(tok::r_brace)) {
2151 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2152 if (R.isUsable())
2153 Stmts.push_back(R.release());
2154 }
2155
2156 if (Tok.isNot(tok::r_brace)) {
2157 Diag(Tok, diag::err_expected_rbrace);
2158 return;
2159 }
2160 ConsumeBrace();
2161}