blob: 2022fa51cfde5a8df34ab47aa34a4ccfd3daf195 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner39146d62008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// [GNU] asm-statement
46/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
53/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
62/// expression-statement:
63/// expression[opt] ';'
64///
65/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
70/// [GNU] 'goto' '*' expression ';'
71///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000074/// [OBC] '@' 'throw' ';'
75///
Sebastian Redl61364dd2008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000078 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000080
Sean Huntbbd37c62009-11-21 08:43:09 +000081 CXX0XAttributeList Attr;
82 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
83 Attr = ParseCXX0XAttributes();
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085 // Cases in this switch statement should fall through if the parser expects
86 // the token to end in a semicolon (in which case SemiError should be set),
87 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000088 tok::TokenKind Kind = Tok.getKind();
89 SourceLocation AtLoc;
90 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 case tok::at: // May be a @try or @throw statement
92 {
93 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000094 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000095 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000096
Douglas Gregor791215b2009-09-21 20:51:25 +000097 case tok::code_completion:
98 Actions.CodeCompleteOrdinaryName(CurScope);
99 ConsumeToken();
100 return ParseStatementOrDeclaration(OnlyStatement);
101
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000102 case tok::identifier:
103 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
104 // identifier ':' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000105 return ParseLabeledStatement(Attr.AttrList);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000106 }
107 // PASS THROUGH.
108
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000109 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000110 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000111 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000112 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd,
113 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000114 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000115 }
116
117 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000119 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Sean Huntbbd37c62009-11-21 08:43:09 +0000122 // FIXME: Use the attributes
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000123 // expression[opt] ';'
124 OwningExprResult Expr(ParseExpression());
125 if (Expr.isInvalid()) {
126 // If the expression is invalid, skip ahead to the next semicolon. Not
127 // doing this opens us up to the possibility of infinite loops if
128 // ParseExpression does not consume any tokens.
129 SkipUntil(tok::semi);
130 return StmtError();
131 }
132 // Otherwise, eat the semicolon.
133 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000134 return Actions.ActOnExprStmt(Actions.FullExpr(Expr));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000135 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 case tok::kw_case: // C99 6.8.1: labeled-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000138 return ParseCaseStatement(Attr.AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 case tok::kw_default: // C99 6.8.1: labeled-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000140 return ParseDefaultStatement(Attr.AttrList);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 case tok::l_brace: // C99 6.8.2: compound-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000143 return ParseCompoundStatement(Attr.AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000145 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 case tok::kw_if: // C99 6.8.4.1: if-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000148 return ParseIfStatement(Attr.AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000150 return ParseSwitchStatement(Attr.AttrList);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 case tok::kw_while: // C99 6.8.5.1: while-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000153 return ParseWhileStatement(Attr.AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 case tok::kw_do: // C99 6.8.5.2: do-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000155 Res = ParseDoStatement(Attr.AttrList);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000156 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 break;
158 case tok::kw_for: // C99 6.8.5.3: for-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000159 return ParseForStatement(Attr.AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160
161 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000162 Res = ParseGotoStatement(Attr.AttrList);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000163 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 break;
165 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000166 Res = ParseContinueStatement(Attr.AttrList);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000167 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 break;
169 case tok::kw_break: // C99 6.8.6.3: break-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000170 Res = ParseBreakStatement(Attr.AttrList);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000171 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 break;
173 case tok::kw_return: // C99 6.8.6.4: return-statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000174 Res = ParseReturnStatement(Attr.AttrList);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000175 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000177
Sebastian Redla0fd8652008-12-21 16:41:36 +0000178 case tok::kw_asm: {
Sean Huntbbd37c62009-11-21 08:43:09 +0000179 if (Attr.HasAttr)
180 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
181 << Attr.Range;
Steve Naroffd62701b2008-02-07 03:50:06 +0000182 bool msAsm = false;
183 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000184 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000185 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 break;
187 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000188
Sebastian Redla0fd8652008-12-21 16:41:36 +0000189 case tok::kw_try: // C++ 15: try-block
Sean Huntbbd37c62009-11-21 08:43:09 +0000190 return ParseCXXTryBlock(Attr.AttrList);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000191 }
192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000194 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000196 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000197 // If the result was valid, then we do want to diagnose this. Use
198 // ExpectAndConsume to emit the diagnostic, even though we know it won't
199 // succeed.
200 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000201 // Skip until we see a } or ;, but don't eat it.
202 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Sebastian Redl61364dd2008-12-11 19:30:53 +0000205 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000208/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000209///
210/// labeled-statement:
211/// identifier ':' statement
212/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000213///
Sean Huntbbd37c62009-11-21 08:43:09 +0000214Parser::OwningStmtResult Parser::ParseLabeledStatement(AttributeList *Attr) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000215 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
216 "Not an identifier!");
217
218 Token IdentTok = Tok; // Save the whole token.
219 ConsumeToken(); // eat the identifier.
220
221 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000222
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000223 // identifier ':' statement
224 SourceLocation ColonLoc = ConsumeToken();
225
226 // Read label attributes, if present.
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000227 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000228 Attr = addAttributeLists(Attr, ParseGNUAttributes());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000229
Sebastian Redl61364dd2008-12-11 19:30:53 +0000230 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000231
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000232 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000233 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000234 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000235
Sebastian Redlde307472009-01-11 00:38:46 +0000236 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
237 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000238 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000239}
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
241/// ParseCaseStatement
242/// labeled-statement:
243/// 'case' constant-expression ':' statement
244/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
245///
Sean Huntbbd37c62009-11-21 08:43:09 +0000246Parser::OwningStmtResult Parser::ParseCaseStatement(AttributeList *Attr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000247 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000248 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattner24e1e702009-03-04 04:23:07 +0000250 // It is very very common for code to contain many case statements recursively
251 // nested, as in (but usually without indentation):
252 // case 1:
253 // case 2:
254 // case 3:
255 // case 4:
256 // case 5: etc.
257 //
258 // Parsing this naively works, but is both inefficient and can cause us to run
259 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000260 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000261 // but all the grossness is constrained to ParseCaseStatement (and some
262 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner24e1e702009-03-04 04:23:07 +0000264 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
265 // example above.
266 OwningStmtResult TopLevelCase(Actions, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattner24e1e702009-03-04 04:23:07 +0000268 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
269 // gets updated each time a new case is parsed, and whose body is unset so
270 // far. When parsing 'case 4', this is the 'case 3' node.
271 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner24e1e702009-03-04 04:23:07 +0000273 // While we have case statements, eat and stack them.
274 do {
275 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000277 if (Tok.is(tok::code_completion)) {
278 Actions.CodeCompleteCase(CurScope);
279 ConsumeToken();
280 }
281
Chris Lattner24e1e702009-03-04 04:23:07 +0000282 OwningExprResult LHS(ParseConstantExpression());
283 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000285 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000287
Chris Lattner24e1e702009-03-04 04:23:07 +0000288 // GNU case range extension.
289 SourceLocation DotDotDotLoc;
290 OwningExprResult RHS(Actions);
291 if (Tok.is(tok::ellipsis)) {
292 Diag(Tok, diag::ext_gnu_case_range);
293 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000294
Chris Lattner24e1e702009-03-04 04:23:07 +0000295 RHS = ParseConstantExpression();
296 if (RHS.isInvalid()) {
297 SkipUntil(tok::colon);
298 return StmtError();
299 }
300 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000301
Chris Lattner24e1e702009-03-04 04:23:07 +0000302 if (Tok.isNot(tok::colon)) {
303 Diag(Tok, diag::err_expected_colon_after) << "'case'";
304 SkipUntil(tok::colon);
305 return StmtError();
306 }
307
308 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner24e1e702009-03-04 04:23:07 +0000310 OwningStmtResult Case =
311 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
312 move(RHS), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Chris Lattner24e1e702009-03-04 04:23:07 +0000314 // If we had a sema error parsing this case, then just ignore it and
315 // continue parsing the sub-stmt.
316 if (Case.isInvalid()) {
317 if (TopLevelCase.isInvalid()) // No parsed case stmts.
318 return ParseStatement();
319 // Otherwise, just don't add it as a nested case.
320 } else {
321 // If this is the first case statement we parsed, it becomes TopLevelCase.
322 // Otherwise we link it into the current chain.
323 StmtTy *NextDeepest = Case.get();
324 if (TopLevelCase.isInvalid())
325 TopLevelCase = move(Case);
326 else
327 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
328 DeepestParsedCaseStmt = NextDeepest;
329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Chris Lattner24e1e702009-03-04 04:23:07 +0000331 // Handle all case statements.
332 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattner24e1e702009-03-04 04:23:07 +0000334 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner24e1e702009-03-04 04:23:07 +0000336 // If we found a non-case statement, start by parsing it.
337 OwningStmtResult SubStmt(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattner24e1e702009-03-04 04:23:07 +0000339 if (Tok.isNot(tok::r_brace)) {
340 SubStmt = ParseStatement();
341 } else {
342 // Nicely diagnose the common error "switch (X) { case 4: }", which is
343 // not valid.
344 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000346 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattner24e1e702009-03-04 04:23:07 +0000349 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000350 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000351 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattner24e1e702009-03-04 04:23:07 +0000353 // Install the body into the most deeply-nested case.
354 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000355
Chris Lattner24e1e702009-03-04 04:23:07 +0000356 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000357 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358}
359
360/// ParseDefaultStatement
361/// labeled-statement:
362/// 'default' ':' statement
363/// Note that this does not parse the 'statement' at the end.
364///
Sean Huntbbd37c62009-11-21 08:43:09 +0000365Parser::OwningStmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
366 //FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000367 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
369
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000370 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000371 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000373 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000377
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000379 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000381 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 }
383
Sebastian Redl61364dd2008-12-11 19:30:53 +0000384 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000385 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000386 return StmtError();
387
Sebastian Redl117054a2008-12-28 16:13:43 +0000388 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000389 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000390}
391
392
393/// ParseCompoundStatement - Parse a "{}" block.
394///
395/// compound-statement: [C99 6.8.2]
396/// { block-item-list[opt] }
397/// [GNU] { label-declarations block-item-list } [TODO]
398///
399/// block-item-list:
400/// block-item
401/// block-item-list block-item
402///
403/// block-item:
404/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000405/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000406/// statement
407/// [OMP] openmp-directive [TODO]
408///
409/// [GNU] label-declarations:
410/// [GNU] label-declaration
411/// [GNU] label-declarations label-declaration
412///
413/// [GNU] label-declaration:
414/// [GNU] '__label__' identifier-list ';'
415///
416/// [OMP] openmp-directive: [TODO]
417/// [OMP] barrier-directive
418/// [OMP] flush-directive
419///
Sean Huntbbd37c62009-11-21 08:43:09 +0000420Parser::OwningStmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
421 bool isStmtExpr) {
422 //FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000423 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000424
Chris Lattner31e05722007-08-26 06:24:45 +0000425 // Enter a scope to hold everything within the compound stmt. Compound
426 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000427 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000428
429 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000430 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000431}
432
433
434/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000435/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000436/// consume the '}' at the end of the block. It does not manipulate the scope
437/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000438Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000439 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000440 Tok.getLocation(),
441 "in compound statement ('{}')");
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
444
445 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000446 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000447
448 typedef StmtVector StmtsTy;
449 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000450 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000451 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000452 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000453 R = ParseStatementOrDeclaration(false);
454 } else {
455 // __extension__ can start declarations and it can also be a unary
456 // operator for expressions. Consume multiple __extension__ markers here
457 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000458 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000459 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000460 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000461 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000462
Sean Huntbbd37c62009-11-21 08:43:09 +0000463 CXX0XAttributeList Attr;
464 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
465 Attr = ParseCXX0XAttributes();
466
Chris Lattner45a566c2007-08-27 01:01:57 +0000467 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000468 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000469 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000470 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000471 ExtensionRAIIObject O(Diags);
472
Chris Lattner97144fc2009-04-02 04:16:50 +0000473 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000474 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
475 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000476 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000477 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000478 // Otherwise this was a unary __extension__ marker.
479 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000480
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000481 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000482 SkipUntil(tok::semi);
483 continue;
484 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000485
Sean Huntbbd37c62009-11-21 08:43:09 +0000486 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000487 // Eat the semicolon at the end of stmt and convert the expr into a
488 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000489 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000490 R = Actions.ActOnExprStmt(Actions.FullExpr(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000491 }
492 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000493
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000494 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000495 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000499 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000501 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000505 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000506 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000507}
508
Chris Lattner15ff1112008-12-12 06:31:07 +0000509/// ParseParenExprOrCondition:
510/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000511/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000512///
513/// This function parses and performs error recovery on the specified condition
514/// or expression (depending on whether we're in C++ or C mode). This function
515/// goes out of its way to recover well. It returns true if there was a parser
516/// error (the right paren couldn't be found), which indicates that the caller
517/// should try to recover harder. It returns false if the condition is
518/// successfully parsed. Note that a successful parse can still have semantic
519/// errors in the condition.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000520bool Parser::ParseParenExprOrCondition(OwningExprResult &ExprResult,
521 DeclPtrTy &DeclResult) {
522 bool ParseError = false;
523
Chris Lattner15ff1112008-12-12 06:31:07 +0000524 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000525 if (getLang().CPlusPlus)
526 ParseError = ParseCXXCondition(ExprResult, DeclResult);
527 else {
528 ExprResult = ParseExpression();
529 DeclResult = DeclPtrTy();
530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattner15ff1112008-12-12 06:31:07 +0000532 // If the parser was confused by the condition and we don't have a ')', try to
533 // recover by skipping ahead to a semi and bailing out. If condexp is
534 // semantically invalid but we have well formed code, keep going.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000535 if (ExprResult.isInvalid() && !DeclResult.get() && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000536 SkipUntil(tok::semi);
537 // Skipping may have stopped if it found the containing ')'. If so, we can
538 // continue parsing the if statement.
539 if (Tok.isNot(tok::r_paren))
540 return true;
541 }
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattner15ff1112008-12-12 06:31:07 +0000543 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000544 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000545 return false;
546}
547
548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549/// ParseIfStatement
550/// if-statement: [C99 6.8.4.1]
551/// 'if' '(' expression ')' statement
552/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000553/// [C++] 'if' '(' condition ')' statement
554/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000555///
Sean Huntbbd37c62009-11-21 08:43:09 +0000556Parser::OwningStmtResult Parser::ParseIfStatement(AttributeList *Attr) {
557 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000558 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
560
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000561 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000562 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000564 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000566
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000567 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
568
Chris Lattner22153252007-08-26 23:08:06 +0000569 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
570 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000571 //
572 // C++ 6.4p3:
573 // A name introduced by a declaration in a condition is in scope from its
574 // point of declaration until the end of the substatements controlled by the
575 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000576 // C++ 3.3.2p4:
577 // Names declared in the for-init-statement, and in the condition of if,
578 // while, for, and switch statements are local to the if, while, for, or
579 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000580 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000581 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000584 OwningExprResult CondExp(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000585 DeclPtrTy CondVar;
586 if (ParseParenExprOrCondition(CondExp, CondVar))
Chris Lattner15ff1112008-12-12 06:31:07 +0000587 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000588
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000589 FullExprArg FullCondExp(Actions.FullExpr(CondExp));
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattner0ecea032007-08-22 05:28:50 +0000591 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000592 // there is no compound stmt. C90 does not have this clause. We only do this
593 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000594 //
595 // C++ 6.4p1:
596 // The substatement in a selection-statement (each substatement, in the else
597 // form of the if statement) implicitly defines a local scope.
598 //
599 // For C++ we create a scope for the condition and a new scope for
600 // substatements because:
601 // -When the 'then' scope exits, we want the condition declaration to still be
602 // active for the 'else' scope too.
603 // -Sema will detect name clashes by considering declarations of a
604 // 'ControlScope' as part of its direct subscope.
605 // -If we wanted the condition and substatement to be in the same scope, we
606 // would have to notify ParseStatement not to create a new scope. It's
607 // simpler to let it create a new scope.
608 //
Mike Stump1eb44332009-09-09 15:08:12 +0000609 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000610 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000611
Chris Lattnerb96728d2007-10-29 05:08:52 +0000612 // Read the 'then' stmt.
613 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000614 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000615
Chris Lattnera36ce712007-08-22 05:16:28 +0000616 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000617 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000618
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 // If it has an else, parse it.
620 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000621 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000622 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000623
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000624 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000626
Chris Lattner0ecea032007-08-22 05:28:50 +0000627 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000628 // there is no compound stmt. C90 does not have this clause. We only do
629 // this if the body isn't a compound statement to avoid push/pop in common
630 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000631 //
632 // C++ 6.4p1:
633 // The substatement in a selection-statement (each substatement, in the else
634 // form of the if statement) implicitly defines a local scope.
635 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000636 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000637 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000638
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000639 bool WithinElse = CurScope->isWithinElse();
640 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000641 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000643 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000644
645 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000646 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000648
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000649 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Chris Lattner18914bc2008-12-12 06:19:11 +0000651 // If the condition was invalid, discard the if statement. We could recover
652 // better by replacing it with a valid expr, but don't do that yet.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000653 if (CondExp.isInvalid() && !CondVar.get())
Chris Lattner18914bc2008-12-12 06:19:11 +0000654 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000655
Chris Lattnerb96728d2007-10-29 05:08:52 +0000656 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000657 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000658 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000659 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
660 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
661 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000662 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000663 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000664 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000665
Chris Lattnerb96728d2007-10-29 05:08:52 +0000666 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000667 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000668 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000669 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000670 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000671
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000672 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000673 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000674}
675
676/// ParseSwitchStatement
677/// switch-statement:
678/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000679/// [C++] 'switch' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000680Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
681 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000682 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
684
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000685 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000686 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000688 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 }
Chris Lattner22153252007-08-26 23:08:06 +0000690
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000691 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
692
Chris Lattner22153252007-08-26 23:08:06 +0000693 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
694 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000695 //
696 // C++ 6.4p3:
697 // A name introduced by a declaration in a condition is in scope from its
698 // point of declaration until the end of the substatements controlled by the
699 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000700 // C++ 3.3.2p4:
701 // Names declared in the for-init-statement, and in the condition of if,
702 // while, for, and switch statements are local to the if, while, for, or
703 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000704 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000705 unsigned ScopeFlags = Scope::BreakScope;
706 if (C99orCXX)
707 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000708 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000709
710 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000711 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000712 DeclPtrTy CondVar;
713 if (ParseParenExprOrCondition(Cond, CondVar))
Sebastian Redl9a920342008-12-11 19:48:14 +0000714 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000715
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000716 FullExprArg FullCond(Actions.FullExpr(Cond));
717
Eli Friedman2342ef72008-12-17 22:19:57 +0000718 OwningStmtResult Switch(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000719 if (!Cond.isInvalid() || CondVar.get())
720 Switch = Actions.ActOnStartOfSwitchStmt(FullCond, CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000721
Chris Lattner0ecea032007-08-22 05:28:50 +0000722 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000723 // there is no compound stmt. C90 does not have this clause. We only do this
724 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000725 //
726 // C++ 6.4p1:
727 // The substatement in a selection-statement (each substatement, in the else
728 // form of the if statement) implicitly defines a local scope.
729 //
730 // See comments in ParseIfStatement for why we create a scope for the
731 // condition and a new scope for substatement in C++.
732 //
Mike Stump1eb44332009-09-09 15:08:12 +0000733 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000734 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000735
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000737 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000738
Chris Lattner0ecea032007-08-22 05:28:50 +0000739 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000740 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000741
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000742 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000743 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000744 // FIXME: Remove the case statement list from the Switch statement.
745 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000746
747 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000748
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000749 if (Cond.isInvalid() && !CondVar.get())
Chris Lattner15ff1112008-12-12 06:31:07 +0000750 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000751
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000752 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000753}
754
755/// ParseWhileStatement
756/// while-statement: [C99 6.8.5.1]
757/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000758/// [C++] 'while' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000759Parser::OwningStmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
760 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000761 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 SourceLocation WhileLoc = Tok.getLocation();
763 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000764
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000765 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000766 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000768 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000770
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000771 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
772
Chris Lattner22153252007-08-26 23:08:06 +0000773 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
774 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000775 //
776 // C++ 6.4p3:
777 // A name introduced by a declaration in a condition is in scope from its
778 // point of declaration until the end of the substatements controlled by the
779 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000780 // C++ 3.3.2p4:
781 // Names declared in the for-init-statement, and in the condition of if,
782 // while, for, and switch statements are local to the if, while, for, or
783 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000784 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000785 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000786 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000787 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
788 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000789 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000790 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
791 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000792
793 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000794 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000795 DeclPtrTy CondVar;
796 if (ParseParenExprOrCondition(Cond, CondVar))
Chris Lattner15ff1112008-12-12 06:31:07 +0000797 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000798
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000799 FullExprArg FullCond(Actions.FullExpr(Cond));
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattner0ecea032007-08-22 05:28:50 +0000801 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000802 // there is no compound stmt. C90 does not have this clause. We only do this
803 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000804 //
805 // C++ 6.5p2:
806 // The substatement in an iteration-statement implicitly defines a local scope
807 // which is entered and exited each time through the loop.
808 //
809 // See comments in ParseIfStatement for why we create a scope for the
810 // condition and a new scope for substatement in C++.
811 //
Mike Stump1eb44332009-09-09 15:08:12 +0000812 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000813 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000814
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000816 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000817
Chris Lattner0ecea032007-08-22 05:28:50 +0000818 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000819 InnerScope.Exit();
820 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000821
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000822 if ((Cond.isInvalid() && !CondVar.get()) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000823 return StmtError();
824
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000825 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000826}
827
828/// ParseDoStatement
829/// do-statement: [C99 6.8.5.2]
830/// 'do' statement 'while' '(' expression ')' ';'
831/// Note: this lets the caller parse the end ';'.
Sean Huntbbd37c62009-11-21 08:43:09 +0000832Parser::OwningStmtResult Parser::ParseDoStatement(AttributeList *Attr) {
833 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000834 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000836
Chris Lattner22153252007-08-26 23:08:06 +0000837 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
838 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000839 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000840 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000841 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000842 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000843 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000844
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000845 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000846
Chris Lattner0ecea032007-08-22 05:28:50 +0000847 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000848 // there is no compound stmt. C90 does not have this clause. We only do this
849 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000850 //
851 // C++ 6.5p2:
852 // The substatement in an iteration-statement implicitly defines a local scope
853 // which is entered and exited each time through the loop.
854 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000855 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000856 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000857 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000860 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000861
Chris Lattner0ecea032007-08-22 05:28:50 +0000862 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000863 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000864
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000865 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000866 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000867 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000868 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000869 SkipUntil(tok::semi, false, true);
870 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000871 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 }
873 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000874
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000875 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000876 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000877 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000878 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000880
Chris Lattnerff871fb2008-12-12 06:35:28 +0000881 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000882 SourceLocation LPLoc = ConsumeParen();
883 OwningExprResult Cond = ParseExpression();
884 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000885 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000886
Sebastian Redl9a920342008-12-11 19:48:14 +0000887 if (Cond.isInvalid() || Body.isInvalid())
888 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000889
Chris Lattner98913592009-06-12 23:04:47 +0000890 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
891 move(Cond), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000892}
893
894/// ParseForStatement
895/// for-statement: [C99 6.8.5.3]
896/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
897/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000898/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
899/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000900/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
901/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000902///
903/// [C++] for-init-statement:
904/// [C++] expression-statement
905/// [C++] simple-declaration
906///
Sean Huntbbd37c62009-11-21 08:43:09 +0000907Parser::OwningStmtResult Parser::ParseForStatement(AttributeList *Attr) {
908 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000909 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000911
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000912 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000913 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000915 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000917
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000918 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000919
Chris Lattner22153252007-08-26 23:08:06 +0000920 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
921 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000922 //
923 // C++ 6.4p3:
924 // A name introduced by a declaration in a condition is in scope from its
925 // point of declaration until the end of the substatements controlled by the
926 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000927 // C++ 3.3.2p4:
928 // Names declared in the for-init-statement, and in the condition of if,
929 // while, for, and switch statements are local to the if, while, for, or
930 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000931 // C++ 6.5.3p1:
932 // Names declared in the for-init-statement are in the same declarative-region
933 // as those declared in the condition.
934 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000935 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000936 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000937 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
938 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000939 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000940 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
941
942 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000943
944 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000945 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000946
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000947 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000948 OwningStmtResult FirstPart(Actions);
949 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000950 DeclPtrTy SecondVar;
951
Douglas Gregor791215b2009-09-21 20:51:25 +0000952 if (Tok.is(tok::code_completion)) {
953 Actions.CodeCompleteOrdinaryName(CurScope);
954 ConsumeToken();
955 }
956
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000958 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 // no first part, eat the ';'.
960 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000961 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000963 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000965
Sean Huntbbd37c62009-11-21 08:43:09 +0000966 AttributeList *AttrList = 0;
967 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
968 AttrList = ParseCXX0XAttributes().AttrList;
969
Chris Lattner97144fc2009-04-02 04:16:50 +0000970 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000971 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
972 AttrList);
Chris Lattnercd147752009-03-29 17:27:48 +0000973 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Chris Lattnercd147752009-03-29 17:27:48 +0000975 if (Tok.is(tok::semi)) { // for (int x = 4;
976 ConsumeToken();
977 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +0000978 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +0000979 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000980 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000981 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000982 } else {
983 Diag(Tok, diag::err_expected_semi_for);
984 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000985 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 } else {
987 Value = ParseExpression();
988
989 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000990 if (!Value.isInvalid())
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000991 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000992
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000993 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000995 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000996 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000997 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000998 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000999 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 SkipUntil(tok::semi);
1001 }
1002 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001003 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001004 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001005 // Parse the second part of the for specifier.
1006 if (Tok.is(tok::semi)) { // for (...;;
1007 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001008 } else {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001009 if (getLang().CPlusPlus)
1010 ParseCXXCondition(SecondPart, SecondVar);
1011 else
1012 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001013 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001014
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001015 if (Tok.is(tok::semi)) {
1016 ConsumeToken();
1017 } else {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001018 if (!SecondPart.isInvalid() || SecondVar.get())
1019 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001020 SkipUntil(tok::semi);
1021 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001022
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001023 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +00001024 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +00001025 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 // Match the ')'.
1028 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001029
Chris Lattner0ecea032007-08-22 05:28:50 +00001030 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001031 // there is no compound stmt. C90 does not have this clause. We only do this
1032 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001033 //
1034 // C++ 6.5p2:
1035 // The substatement in an iteration-statement implicitly defines a local scope
1036 // which is entered and exited each time through the loop.
1037 //
1038 // See comments in ParseIfStatement for why we create a scope for
1039 // for-init-statement/condition and a new scope for substatement in C++.
1040 //
Mike Stump1eb44332009-09-09 15:08:12 +00001041 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001042 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001043
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001045 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001046
Chris Lattner0ecea032007-08-22 05:28:50 +00001047 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001048 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001049
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001051 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001052
1053 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001054 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001055
1056 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001057 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001058 Actions.FullExpr(SecondPart), SecondVar,
1059 Actions.FullExpr(ThirdPart), RParenLoc,
1060 move(Body));
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Chris Lattner682bf922009-03-29 16:50:03 +00001062 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1063 move(FirstPart),
1064 move(SecondPart),
1065 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001066}
1067
1068/// ParseGotoStatement
1069/// jump-statement:
1070/// 'goto' identifier ';'
1071/// [GNU] 'goto' '*' expression ';'
1072///
1073/// Note: this lets the caller parse the end ';'.
1074///
Sean Huntbbd37c62009-11-21 08:43:09 +00001075Parser::OwningStmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
1076 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001077 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001079
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001080 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001081 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001082 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 Tok.getIdentifierInfo());
1084 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001085 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 // GNU indirect goto extension.
1087 Diag(Tok, diag::ext_gnu_indirect_goto);
1088 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001089 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001090 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001092 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001094 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001095 } else {
1096 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001097 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001099
Sebastian Redl9a920342008-12-11 19:48:14 +00001100 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001101}
1102
1103/// ParseContinueStatement
1104/// jump-statement:
1105/// 'continue' ';'
1106///
1107/// Note: this lets the caller parse the end ';'.
1108///
Sean Huntbbd37c62009-11-21 08:43:09 +00001109Parser::OwningStmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
1110 // FIXME: Use attributes?
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001112 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001113}
1114
1115/// ParseBreakStatement
1116/// jump-statement:
1117/// 'break' ';'
1118///
1119/// Note: this lets the caller parse the end ';'.
1120///
Sean Huntbbd37c62009-11-21 08:43:09 +00001121Parser::OwningStmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
1122 // FIXME: Use attributes?
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001124 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125}
1126
1127/// ParseReturnStatement
1128/// jump-statement:
1129/// 'return' expression[opt] ';'
Sean Huntbbd37c62009-11-21 08:43:09 +00001130Parser::OwningStmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
1131 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001132 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001134
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001135 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001136 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001138 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001140 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 }
1142 }
Anders Carlssonf53b4432009-08-18 16:11:00 +00001143 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001144}
1145
Steve Naroff5f8aa692008-02-11 23:15:56 +00001146/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1147/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001148Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001149 if (Tok.is(tok::l_brace)) {
1150 unsigned short savedBraceCount = BraceCount;
1151 do {
1152 ConsumeAnyToken();
1153 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001154 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001155 // From the MS website: If used without braces, the __asm keyword means
1156 // that the rest of the line is an assembly-language statement.
1157 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001158 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001159 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001160 do {
1161 ConsumeAnyToken();
1162 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001163 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1164 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001165 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001166 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001167 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001168}
1169
Reid Spencer5f016e22007-07-11 17:01:13 +00001170/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001171/// asm-statement:
1172/// gnu-asm-statement
1173/// ms-asm-statement
1174///
1175/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001176/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1177///
1178/// [GNU] asm-argument:
1179/// asm-string-literal
1180/// asm-string-literal ':' asm-operands[opt]
1181/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1182/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1183/// ':' asm-clobbers
1184///
1185/// [GNU] asm-clobbers:
1186/// asm-string-literal
1187/// asm-clobbers ',' asm-string-literal
1188///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001189/// [MS] ms-asm-statement:
1190/// '__asm' assembly-instruction ';'[opt]
1191/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1192///
1193/// [MS] assembly-instruction-list:
1194/// assembly-instruction ';'[opt]
1195/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1196///
Sebastian Redl9a920342008-12-11 19:48:14 +00001197Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001198 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001199 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001200
Steve Naroff5f8aa692008-02-11 23:15:56 +00001201 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001202 msAsm = true;
1203 return FuzzyParseMicrosoftAsmStatement();
1204 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 DeclSpec DS;
1206 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001207 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001208
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1210 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001211 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001213 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001214
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001216 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001217 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001218 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001219 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001221 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 }
1223 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001224
Sebastian Redleffa8d12008-12-10 00:02:53 +00001225 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001226 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001227 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001228
Anders Carlssonb235fc22007-11-22 01:36:19 +00001229 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001230 ExprVector Constraints(Actions);
1231 ExprVector Exprs(Actions);
1232 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001233
Anders Carlssondfab34a2008-02-05 23:03:50 +00001234 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001235
Anders Carlssondfab34a2008-02-05 23:03:50 +00001236 SourceLocation RParenLoc;
1237 if (Tok.is(tok::r_paren)) {
1238 // We have a simple asm expression
1239 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001240
Anders Carlssondfab34a2008-02-05 23:03:50 +00001241 RParenLoc = ConsumeParen();
1242 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001243 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001244 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001245 return StmtError();
1246
Anders Carlssondfab34a2008-02-05 23:03:50 +00001247 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001248
Anders Carlssondfab34a2008-02-05 23:03:50 +00001249 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001250 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001251 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001252
Anders Carlssondfab34a2008-02-05 23:03:50 +00001253 assert(Names.size() == Constraints.size() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001254 Constraints.size() == Exprs.size()
Anders Carlssondfab34a2008-02-05 23:03:50 +00001255 && "Input operand size mismatch!");
1256
1257 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001258
Anders Carlssondfab34a2008-02-05 23:03:50 +00001259 // Parse the clobbers, if present.
1260 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001261 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001262
Anders Carlssondfab34a2008-02-05 23:03:50 +00001263 // Parse the asm-string list for clobbers.
1264 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001265 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001266
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001267 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001268 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001269
1270 Clobbers.push_back(Clobber.release());
1271
Anders Carlssondfab34a2008-02-05 23:03:50 +00001272 if (Tok.isNot(tok::comma)) break;
1273 ConsumeToken();
1274 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001276
Anders Carlssondfab34a2008-02-05 23:03:50 +00001277 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001279
Sebastian Redl3037ed02009-01-18 16:53:17 +00001280 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001281 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001282 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001283 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001284 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001285}
1286
1287/// ParseAsmOperands - Parse the asm-operands production as used by
1288/// asm-statement. We also parse a leading ':' token. If the leading colon is
1289/// not present, we do not parse anything.
1290///
1291/// [GNU] asm-operands:
1292/// asm-operand
1293/// asm-operands ',' asm-operand
1294///
1295/// [GNU] asm-operand:
1296/// asm-string-literal '(' expression ')'
1297/// '[' identifier ']' asm-string-literal '(' expression ')'
1298///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001299//
1300// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001301bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001302 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1303 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001305 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001309 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001310 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
1312 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001314 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001317 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 Diag(Tok, diag::err_expected_ident);
1319 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001320 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 }
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Anders Carlssonb235fc22007-11-22 01:36:19 +00001323 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001324 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001325
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001326 Names.push_back(II->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001328 } else
1329 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001330
Sebastian Redleffa8d12008-12-10 00:02:53 +00001331 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001332 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001333 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001334 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001335 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001336 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001337
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001338 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001339 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001341 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001343
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001345 SourceLocation OpenLoc = ConsumeParen();
1346 OwningExprResult Res(ParseExpression());
1347 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001348 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001350 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001352 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001354 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 ConsumeToken();
1356 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001357
1358 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001359}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001360
Chris Lattnerb28317a2009-03-28 19:18:32 +00001361Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001362 assert(Tok.is(tok::l_brace));
1363 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001364
Chris Lattner49f28ca2009-03-05 08:00:35 +00001365 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1366 PP.getSourceManager(),
1367 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001369 // Do not enter a scope for the brace, as the arguments are in the same scope
1370 // (the function body) as the body itself. Instead, just read the statement
1371 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001372 OwningStmtResult FnBody(ParseCompoundStatementBody());
1373
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001374 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001375 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001376 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001377 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001378
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001379 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001380}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001381
Sebastian Redld3a413d2009-04-26 20:35:05 +00001382/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1383///
1384/// function-try-block:
1385/// 'try' ctor-initializer[opt] compound-statement handler-seq
1386///
1387Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1388 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1389 SourceLocation TryLoc = ConsumeToken();
1390
1391 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1392 PP.getSourceManager(),
1393 "parsing function try block");
1394
1395 // Constructor initializer list?
1396 if (Tok.is(tok::colon))
1397 ParseConstructorInitializer(Decl);
1398
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001399 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001400 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1401 // If we failed to parse the try-catch, we just give the function an empty
1402 // compound statement as the body.
1403 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001404 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001405 MultiStmtArg(Actions), false);
1406
1407 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1408}
1409
Sebastian Redla0fd8652008-12-21 16:41:36 +00001410/// ParseCXXTryBlock - Parse a C++ try-block.
1411///
1412/// try-block:
1413/// 'try' compound-statement handler-seq
1414///
Sean Huntbbd37c62009-11-21 08:43:09 +00001415Parser::OwningStmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
1416 // FIXME: Add attributes?
Sebastian Redla0fd8652008-12-21 16:41:36 +00001417 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1418
1419 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001420 return ParseCXXTryBlockCommon(TryLoc);
1421}
1422
1423/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1424/// function-try-block.
1425///
1426/// try-block:
1427/// 'try' compound-statement handler-seq
1428///
1429/// function-try-block:
1430/// 'try' ctor-initializer[opt] compound-statement handler-seq
1431///
1432/// handler-seq:
1433/// handler handler-seq[opt]
1434///
1435Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001436 if (Tok.isNot(tok::l_brace))
1437 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001438 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1439 OwningStmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001440 if (TryBlock.isInvalid())
1441 return move(TryBlock);
1442
1443 StmtVector Handlers(Actions);
Sean Huntbbd37c62009-11-21 08:43:09 +00001444 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1445 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1446 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1447 << Attr.Range;
1448 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00001449 if (Tok.isNot(tok::kw_catch))
1450 return StmtError(Diag(Tok, diag::err_expected_catch));
1451 while (Tok.is(tok::kw_catch)) {
1452 OwningStmtResult Handler(ParseCXXCatchBlock());
1453 if (!Handler.isInvalid())
1454 Handlers.push_back(Handler.release());
1455 }
1456 // Don't bother creating the full statement if we don't have any usable
1457 // handlers.
1458 if (Handlers.empty())
1459 return StmtError();
1460
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001461 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001462}
1463
1464/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1465///
1466/// handler:
1467/// 'catch' '(' exception-declaration ')' compound-statement
1468///
1469/// exception-declaration:
1470/// type-specifier-seq declarator
1471/// type-specifier-seq abstract-declarator
1472/// type-specifier-seq
1473/// '...'
1474///
1475Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1476 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1477
1478 SourceLocation CatchLoc = ConsumeToken();
1479
1480 SourceLocation LParenLoc = Tok.getLocation();
1481 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1482 return StmtError();
1483
1484 // C++ 3.3.2p3:
1485 // The name in a catch exception-declaration is local to the handler and
1486 // shall not be redeclared in the outermost block of the handler.
1487 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1488
1489 // exception-declaration is equivalent to '...' or a parameter-declaration
1490 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001491 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001492 if (Tok.isNot(tok::ellipsis)) {
1493 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001494 if (ParseCXXTypeSpecifierSeq(DS))
1495 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001496 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1497 ParseDeclarator(ExDecl);
1498 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1499 } else
1500 ConsumeToken();
1501
1502 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1503 return StmtError();
1504
1505 if (Tok.isNot(tok::l_brace))
1506 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1507
Sean Huntbbd37c62009-11-21 08:43:09 +00001508 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1509 OwningStmtResult Block(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001510 if (Block.isInvalid())
1511 return move(Block);
1512
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001513 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001514}