blob: 21e960aa81739fdcf5af2860ab653e39fb5d4949 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// [GNU] asm-statement
46/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
53/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
62/// expression-statement:
63/// expression[opt] ';'
64///
65/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
70/// [GNU] 'goto' '*' expression ';'
71///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000074/// [OBC] '@' 'throw' ';'
75///
Sebastian Redl61364dd2008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000078 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000080
Sean Huntbbd37c62009-11-21 08:43:09 +000081 CXX0XAttributeList Attr;
82 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
83 Attr = ParseCXX0XAttributes();
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:
Douglas Gregor01dfea02010-01-10 23:08:15 +000098 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Statement);
Douglas Gregor791215b2009-09-21 20:51:25 +000099 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 Carlsson5ee56e92009-12-16 02:09:40 +0000134 return Actions.ActOnExprStmt(Actions.MakeFullExpr(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 Lattner6fb09c82009-12-10 00:38:54 +0000282 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
283 /// Disable this form of error recovery while we're parsing the case
284 /// expression.
285 ColonProtectionRAIIObject ColonProtection(*this);
286
Chris Lattner24e1e702009-03-04 04:23:07 +0000287 OwningExprResult LHS(ParseConstantExpression());
288 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000290 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000292
Chris Lattner24e1e702009-03-04 04:23:07 +0000293 // GNU case range extension.
294 SourceLocation DotDotDotLoc;
295 OwningExprResult RHS(Actions);
296 if (Tok.is(tok::ellipsis)) {
297 Diag(Tok, diag::ext_gnu_case_range);
298 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000299
Chris Lattner24e1e702009-03-04 04:23:07 +0000300 RHS = ParseConstantExpression();
301 if (RHS.isInvalid()) {
302 SkipUntil(tok::colon);
303 return StmtError();
304 }
305 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000306
307 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000308
Chris Lattner24e1e702009-03-04 04:23:07 +0000309 if (Tok.isNot(tok::colon)) {
310 Diag(Tok, diag::err_expected_colon_after) << "'case'";
311 SkipUntil(tok::colon);
312 return StmtError();
313 }
314
315 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Chris Lattner24e1e702009-03-04 04:23:07 +0000317 OwningStmtResult Case =
318 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
319 move(RHS), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chris Lattner24e1e702009-03-04 04:23:07 +0000321 // If we had a sema error parsing this case, then just ignore it and
322 // continue parsing the sub-stmt.
323 if (Case.isInvalid()) {
324 if (TopLevelCase.isInvalid()) // No parsed case stmts.
325 return ParseStatement();
326 // Otherwise, just don't add it as a nested case.
327 } else {
328 // If this is the first case statement we parsed, it becomes TopLevelCase.
329 // Otherwise we link it into the current chain.
330 StmtTy *NextDeepest = Case.get();
331 if (TopLevelCase.isInvalid())
332 TopLevelCase = move(Case);
333 else
334 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
335 DeepestParsedCaseStmt = NextDeepest;
336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattner24e1e702009-03-04 04:23:07 +0000338 // Handle all case statements.
339 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattner24e1e702009-03-04 04:23:07 +0000341 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattner24e1e702009-03-04 04:23:07 +0000343 // If we found a non-case statement, start by parsing it.
344 OwningStmtResult SubStmt(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner24e1e702009-03-04 04:23:07 +0000346 if (Tok.isNot(tok::r_brace)) {
347 SubStmt = ParseStatement();
348 } else {
349 // Nicely diagnose the common error "switch (X) { case 4: }", which is
350 // not valid.
351 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000353 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner24e1e702009-03-04 04:23:07 +0000356 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000357 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000358 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattner24e1e702009-03-04 04:23:07 +0000360 // Install the body into the most deeply-nested case.
361 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000362
Chris Lattner24e1e702009-03-04 04:23:07 +0000363 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000364 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365}
366
367/// ParseDefaultStatement
368/// labeled-statement:
369/// 'default' ':' statement
370/// Note that this does not parse the 'statement' at the end.
371///
Sean Huntbbd37c62009-11-21 08:43:09 +0000372Parser::OwningStmtResult Parser::ParseDefaultStatement(AttributeList *Attr) {
373 //FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000374 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
376
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000377 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000378 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000380 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000386 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000388 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 }
390
Sebastian Redl61364dd2008-12-11 19:30:53 +0000391 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000392 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000393 return StmtError();
394
Sebastian Redl117054a2008-12-28 16:13:43 +0000395 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000396 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397}
398
399
400/// ParseCompoundStatement - Parse a "{}" block.
401///
402/// compound-statement: [C99 6.8.2]
403/// { block-item-list[opt] }
404/// [GNU] { label-declarations block-item-list } [TODO]
405///
406/// block-item-list:
407/// block-item
408/// block-item-list block-item
409///
410/// block-item:
411/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000412/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000413/// statement
414/// [OMP] openmp-directive [TODO]
415///
416/// [GNU] label-declarations:
417/// [GNU] label-declaration
418/// [GNU] label-declarations label-declaration
419///
420/// [GNU] label-declaration:
421/// [GNU] '__label__' identifier-list ';'
422///
423/// [OMP] openmp-directive: [TODO]
424/// [OMP] barrier-directive
425/// [OMP] flush-directive
426///
Sean Huntbbd37c62009-11-21 08:43:09 +0000427Parser::OwningStmtResult Parser::ParseCompoundStatement(AttributeList *Attr,
428 bool isStmtExpr) {
429 //FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000430 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000431
Chris Lattner31e05722007-08-26 06:24:45 +0000432 // Enter a scope to hold everything within the compound stmt. Compound
433 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000434 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000435
436 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000437 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438}
439
440
441/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000442/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000443/// consume the '}' at the end of the block. It does not manipulate the scope
444/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000445Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000446 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000447 Tok.getLocation(),
448 "in compound statement ('{}')");
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
451
452 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000453 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000454
455 typedef StmtVector StmtsTy;
456 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000457 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000458 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000459 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000460 R = ParseStatementOrDeclaration(false);
461 } else {
462 // __extension__ can start declarations and it can also be a unary
463 // operator for expressions. Consume multiple __extension__ markers here
464 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000465 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000466 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000467 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000468 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000469
Sean Huntbbd37c62009-11-21 08:43:09 +0000470 CXX0XAttributeList Attr;
471 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
472 Attr = ParseCXX0XAttributes();
473
Chris Lattner45a566c2007-08-27 01:01:57 +0000474 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000475 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000476 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000477 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000478 ExtensionRAIIObject O(Diags);
479
Chris Lattner97144fc2009-04-02 04:16:50 +0000480 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000481 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
482 Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000483 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000484 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000485 // Otherwise this was a unary __extension__ marker.
486 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000487
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000488 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000489 SkipUntil(tok::semi);
490 continue;
491 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000492
Sean Huntbbd37c62009-11-21 08:43:09 +0000493 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000494 // Eat the semicolon at the end of stmt and convert the expr into a
495 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000496 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000497 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000498 }
499 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000500
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000501 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000502 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000506 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000508 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000510
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000512 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000513 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000514}
515
Chris Lattner15ff1112008-12-12 06:31:07 +0000516/// ParseParenExprOrCondition:
517/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000518/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000519///
520/// This function parses and performs error recovery on the specified condition
521/// or expression (depending on whether we're in C++ or C mode). This function
522/// goes out of its way to recover well. It returns true if there was a parser
523/// error (the right paren couldn't be found), which indicates that the caller
524/// should try to recover harder. It returns false if the condition is
525/// successfully parsed. Note that a successful parse can still have semantic
526/// errors in the condition.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000527bool Parser::ParseParenExprOrCondition(OwningExprResult &ExprResult,
528 DeclPtrTy &DeclResult) {
529 bool ParseError = false;
530
Chris Lattner15ff1112008-12-12 06:31:07 +0000531 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000532 if (getLang().CPlusPlus)
533 ParseError = ParseCXXCondition(ExprResult, DeclResult);
534 else {
535 ExprResult = ParseExpression();
536 DeclResult = DeclPtrTy();
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner15ff1112008-12-12 06:31:07 +0000539 // If the parser was confused by the condition and we don't have a ')', try to
540 // recover by skipping ahead to a semi and bailing out. If condexp is
541 // semantically invalid but we have well formed code, keep going.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000542 if (ExprResult.isInvalid() && !DeclResult.get() && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000543 SkipUntil(tok::semi);
544 // Skipping may have stopped if it found the containing ')'. If so, we can
545 // continue parsing the if statement.
546 if (Tok.isNot(tok::r_paren))
547 return true;
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner15ff1112008-12-12 06:31:07 +0000550 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000551 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000552 return false;
553}
554
555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556/// ParseIfStatement
557/// if-statement: [C99 6.8.4.1]
558/// 'if' '(' expression ')' statement
559/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000560/// [C++] 'if' '(' condition ')' statement
561/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000562///
Sean Huntbbd37c62009-11-21 08:43:09 +0000563Parser::OwningStmtResult Parser::ParseIfStatement(AttributeList *Attr) {
564 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000565 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
567
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000568 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000569 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000571 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000573
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000574 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
575
Chris Lattner22153252007-08-26 23:08:06 +0000576 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
577 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000578 //
579 // C++ 6.4p3:
580 // A name introduced by a declaration in a condition is in scope from its
581 // point of declaration until the end of the substatements controlled by the
582 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000583 // C++ 3.3.2p4:
584 // Names declared in the for-init-statement, and in the condition of if,
585 // while, for, and switch statements are local to the if, while, for, or
586 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000587 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000588 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000589
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000591 OwningExprResult CondExp(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000592 DeclPtrTy CondVar;
593 if (ParseParenExprOrCondition(CondExp, CondVar))
Chris Lattner15ff1112008-12-12 06:31:07 +0000594 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000595
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000596 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp));
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Chris Lattner0ecea032007-08-22 05:28:50 +0000598 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000599 // there is no compound stmt. C90 does not have this clause. We only do this
600 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000601 //
602 // C++ 6.4p1:
603 // The substatement in a selection-statement (each substatement, in the else
604 // form of the if statement) implicitly defines a local scope.
605 //
606 // For C++ we create a scope for the condition and a new scope for
607 // substatements because:
608 // -When the 'then' scope exits, we want the condition declaration to still be
609 // active for the 'else' scope too.
610 // -Sema will detect name clashes by considering declarations of a
611 // 'ControlScope' as part of its direct subscope.
612 // -If we wanted the condition and substatement to be in the same scope, we
613 // would have to notify ParseStatement not to create a new scope. It's
614 // simpler to let it create a new scope.
615 //
Mike Stump1eb44332009-09-09 15:08:12 +0000616 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000617 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000618
Chris Lattnerb96728d2007-10-29 05:08:52 +0000619 // Read the 'then' stmt.
620 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000621 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000622
Chris Lattnera36ce712007-08-22 05:16:28 +0000623 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000624 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // If it has an else, parse it.
627 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000628 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000629 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000630
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000631 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000633
Chris Lattner0ecea032007-08-22 05:28:50 +0000634 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000635 // there is no compound stmt. C90 does not have this clause. We only do
636 // this if the body isn't a compound statement to avoid push/pop in common
637 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000638 //
639 // C++ 6.4p1:
640 // The substatement in a selection-statement (each substatement, in the else
641 // form of the if statement) implicitly defines a local scope.
642 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000643 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000644 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000645
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000646 bool WithinElse = CurScope->isWithinElse();
647 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000648 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000650 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000651
652 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000653 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000655
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000656 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Chris Lattner18914bc2008-12-12 06:19:11 +0000658 // If the condition was invalid, discard the if statement. We could recover
659 // better by replacing it with a valid expr, but don't do that yet.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000660 if (CondExp.isInvalid() && !CondVar.get())
Chris Lattner18914bc2008-12-12 06:19:11 +0000661 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000662
Chris Lattnerb96728d2007-10-29 05:08:52 +0000663 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000664 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000665 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000666 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
667 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
668 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000669 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000670 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000671 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000672
Chris Lattnerb96728d2007-10-29 05:08:52 +0000673 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000674 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000675 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000676 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000677 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000678
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000679 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000680 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000681}
682
683/// ParseSwitchStatement
684/// switch-statement:
685/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000686/// [C++] 'switch' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000687Parser::OwningStmtResult Parser::ParseSwitchStatement(AttributeList *Attr) {
688 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000689 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
691
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000692 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000693 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000695 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 }
Chris Lattner22153252007-08-26 23:08:06 +0000697
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000698 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
699
Chris Lattner22153252007-08-26 23:08:06 +0000700 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
701 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000702 //
703 // C++ 6.4p3:
704 // A name introduced by a declaration in a condition is in scope from its
705 // point of declaration until the end of the substatements controlled by the
706 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000707 // C++ 3.3.2p4:
708 // Names declared in the for-init-statement, and in the condition of if,
709 // while, for, and switch statements are local to the if, while, for, or
710 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000711 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000712 unsigned ScopeFlags = Scope::BreakScope;
713 if (C99orCXX)
714 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000715 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000716
717 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000718 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000719 DeclPtrTy CondVar;
720 if (ParseParenExprOrCondition(Cond, CondVar))
Sebastian Redl9a920342008-12-11 19:48:14 +0000721 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000722
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000723 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000724
Douglas Gregorbe724ba2009-11-25 06:20:02 +0000725 OwningStmtResult Switch = Actions.ActOnStartOfSwitchStmt(FullCond, CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000726
Chris Lattner0ecea032007-08-22 05:28:50 +0000727 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000728 // there is no compound stmt. C90 does not have this clause. We only do this
729 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000730 //
731 // C++ 6.4p1:
732 // The substatement in a selection-statement (each substatement, in the else
733 // form of the if statement) implicitly defines a local scope.
734 //
735 // See comments in ParseIfStatement for why we create a scope for the
736 // condition and a new scope for substatement in C++.
737 //
Mike Stump1eb44332009-09-09 15:08:12 +0000738 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000739 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000740
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000742 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000743
Chris Lattner0ecea032007-08-22 05:28:50 +0000744 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000745 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000746
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000747 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000748 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000749 // FIXME: Remove the case statement list from the Switch statement.
750 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000751
752 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000753
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000754 if (Cond.isInvalid() && !CondVar.get())
Chris Lattner15ff1112008-12-12 06:31:07 +0000755 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000756
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000757 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000758}
759
760/// ParseWhileStatement
761/// while-statement: [C99 6.8.5.1]
762/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000763/// [C++] 'while' '(' condition ')' statement
Sean Huntbbd37c62009-11-21 08:43:09 +0000764Parser::OwningStmtResult Parser::ParseWhileStatement(AttributeList *Attr) {
765 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000766 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 SourceLocation WhileLoc = Tok.getLocation();
768 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000769
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000770 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000771 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000773 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000775
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000776 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
777
Chris Lattner22153252007-08-26 23:08:06 +0000778 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
779 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000780 //
781 // C++ 6.4p3:
782 // A name introduced by a declaration in a condition is in scope from its
783 // point of declaration until the end of the substatements controlled by the
784 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000785 // C++ 3.3.2p4:
786 // Names declared in the for-init-statement, and in the condition of if,
787 // while, for, and switch statements are local to the if, while, for, or
788 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000789 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000790 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000791 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000792 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
793 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000794 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000795 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
796 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000797
798 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000799 OwningExprResult Cond(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000800 DeclPtrTy CondVar;
801 if (ParseParenExprOrCondition(Cond, CondVar))
Chris Lattner15ff1112008-12-12 06:31:07 +0000802 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000803
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000804 FullExprArg FullCond(Actions.MakeFullExpr(Cond));
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattner0ecea032007-08-22 05:28:50 +0000806 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000807 // there is no compound stmt. C90 does not have this clause. We only do this
808 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000809 //
810 // C++ 6.5p2:
811 // The substatement in an iteration-statement implicitly defines a local scope
812 // which is entered and exited each time through the loop.
813 //
814 // See comments in ParseIfStatement for why we create a scope for the
815 // condition and a new scope for substatement in C++.
816 //
Mike Stump1eb44332009-09-09 15:08:12 +0000817 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000818 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000821 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000822
Chris Lattner0ecea032007-08-22 05:28:50 +0000823 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000824 InnerScope.Exit();
825 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000826
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000827 if ((Cond.isInvalid() && !CondVar.get()) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000828 return StmtError();
829
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000830 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000831}
832
833/// ParseDoStatement
834/// do-statement: [C99 6.8.5.2]
835/// 'do' statement 'while' '(' expression ')' ';'
836/// Note: this lets the caller parse the end ';'.
Sean Huntbbd37c62009-11-21 08:43:09 +0000837Parser::OwningStmtResult Parser::ParseDoStatement(AttributeList *Attr) {
838 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000839 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000841
Chris Lattner22153252007-08-26 23:08:06 +0000842 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
843 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000844 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000845 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000846 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000847 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000848 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000849
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000850 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000851
Chris Lattner0ecea032007-08-22 05:28:50 +0000852 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000853 // there is no compound stmt. C90 does not have this clause. We only do this
854 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000855 //
856 // C++ 6.5p2:
857 // The substatement in an iteration-statement implicitly defines a local scope
858 // which is entered and exited each time through the loop.
859 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000860 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000861 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000862 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000863
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000865 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000866
Chris Lattner0ecea032007-08-22 05:28:50 +0000867 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000868 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000869
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000870 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000871 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000872 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000873 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000874 SkipUntil(tok::semi, false, true);
875 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000876 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 }
878 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000879
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000880 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000881 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000882 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000883 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000885
Chris Lattnerff871fb2008-12-12 06:35:28 +0000886 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +0000887 SourceLocation LPLoc = ConsumeParen();
888 OwningExprResult Cond = ParseExpression();
889 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000890 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000891
Sebastian Redl9a920342008-12-11 19:48:14 +0000892 if (Cond.isInvalid() || Body.isInvalid())
893 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000894
Chris Lattner98913592009-06-12 23:04:47 +0000895 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
896 move(Cond), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000897}
898
899/// ParseForStatement
900/// for-statement: [C99 6.8.5.3]
901/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
902/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000903/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
904/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000905/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
906/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000907///
908/// [C++] for-init-statement:
909/// [C++] expression-statement
910/// [C++] simple-declaration
911///
Sean Huntbbd37c62009-11-21 08:43:09 +0000912Parser::OwningStmtResult Parser::ParseForStatement(AttributeList *Attr) {
913 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000914 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000916
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000917 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000918 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000920 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000922
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000923 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000924
Chris Lattner22153252007-08-26 23:08:06 +0000925 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
926 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000927 //
928 // C++ 6.4p3:
929 // A name introduced by a declaration in a condition is in scope from its
930 // point of declaration until the end of the substatements controlled by the
931 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000932 // C++ 3.3.2p4:
933 // Names declared in the for-init-statement, and in the condition of if,
934 // while, for, and switch statements are local to the if, while, for, or
935 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000936 // C++ 6.5.3p1:
937 // Names declared in the for-init-statement are in the same declarative-region
938 // as those declared in the condition.
939 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000940 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000941 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000942 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
943 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000944 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000945 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
946
947 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000948
949 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000950 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000951
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000952 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000953 OwningStmtResult FirstPart(Actions);
954 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000955 DeclPtrTy SecondVar;
956
Douglas Gregor791215b2009-09-21 20:51:25 +0000957 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000958 Actions.CodeCompleteOrdinaryName(CurScope,
959 C99orCXXorObjC? Action::CCC_ForInit
960 : Action::CCC_Expression);
Douglas Gregor791215b2009-09-21 20:51:25 +0000961 ConsumeToken();
962 }
963
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000965 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 // no first part, eat the ';'.
967 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000968 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000970 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000972
Sean Huntbbd37c62009-11-21 08:43:09 +0000973 AttributeList *AttrList = 0;
974 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
975 AttrList = ParseCXX0XAttributes().AttrList;
976
Chris Lattner97144fc2009-04-02 04:16:50 +0000977 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000978 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
979 AttrList);
Chris Lattnercd147752009-03-29 17:27:48 +0000980 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattnercd147752009-03-29 17:27:48 +0000982 if (Tok.is(tok::semi)) { // for (int x = 4;
983 ConsumeToken();
984 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +0000985 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +0000986 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000987 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000988 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000989 } else {
990 Diag(Tok, diag::err_expected_semi_for);
991 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000992 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 } else {
994 Value = ParseExpression();
995
996 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000997 if (!Value.isInvalid())
Anders Carlsson5ee56e92009-12-16 02:09:40 +0000998 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000999
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001000 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001002 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001003 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001004 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001005 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001006 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 SkipUntil(tok::semi);
1008 }
1009 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +00001010 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001011 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001012 // Parse the second part of the for specifier.
1013 if (Tok.is(tok::semi)) { // for (...;;
1014 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001015 } else {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001016 if (getLang().CPlusPlus)
1017 ParseCXXCondition(SecondPart, SecondVar);
1018 else
1019 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001020 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001021
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001022 if (Tok.is(tok::semi)) {
1023 ConsumeToken();
1024 } else {
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001025 if (!SecondPart.isInvalid() || SecondVar.get())
1026 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001027 SkipUntil(tok::semi);
1028 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001029
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001030 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +00001031 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +00001032 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 // Match the ')'.
1035 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001036
Chris Lattner0ecea032007-08-22 05:28:50 +00001037 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001038 // there is no compound stmt. C90 does not have this clause. We only do this
1039 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001040 //
1041 // C++ 6.5p2:
1042 // The substatement in an iteration-statement implicitly defines a local scope
1043 // which is entered and exited each time through the loop.
1044 //
1045 // See comments in ParseIfStatement for why we create a scope for
1046 // for-init-statement/condition and a new scope for substatement in C++.
1047 //
Mike Stump1eb44332009-09-09 15:08:12 +00001048 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001049 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001050
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001052 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001053
Chris Lattner0ecea032007-08-22 05:28:50 +00001054 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001055 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001056
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001058 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001059
1060 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001061 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001062
1063 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001064 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001065 Actions.MakeFullExpr(SecondPart), SecondVar,
1066 Actions.MakeFullExpr(ThirdPart), RParenLoc,
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001067 move(Body));
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Chris Lattner682bf922009-03-29 16:50:03 +00001069 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1070 move(FirstPart),
1071 move(SecondPart),
1072 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001073}
1074
1075/// ParseGotoStatement
1076/// jump-statement:
1077/// 'goto' identifier ';'
1078/// [GNU] 'goto' '*' expression ';'
1079///
1080/// Note: this lets the caller parse the end ';'.
1081///
Sean Huntbbd37c62009-11-21 08:43:09 +00001082Parser::OwningStmtResult Parser::ParseGotoStatement(AttributeList *Attr) {
1083 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001084 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001086
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001087 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001088 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001089 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 Tok.getIdentifierInfo());
1091 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001092 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 // GNU indirect goto extension.
1094 Diag(Tok, diag::ext_gnu_indirect_goto);
1095 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001096 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001097 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001099 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001101 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001102 } else {
1103 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001104 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001106
Sebastian Redl9a920342008-12-11 19:48:14 +00001107 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001108}
1109
1110/// ParseContinueStatement
1111/// jump-statement:
1112/// 'continue' ';'
1113///
1114/// Note: this lets the caller parse the end ';'.
1115///
Sean Huntbbd37c62009-11-21 08:43:09 +00001116Parser::OwningStmtResult Parser::ParseContinueStatement(AttributeList *Attr) {
1117 // FIXME: Use attributes?
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001119 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001120}
1121
1122/// ParseBreakStatement
1123/// jump-statement:
1124/// 'break' ';'
1125///
1126/// Note: this lets the caller parse the end ';'.
1127///
Sean Huntbbd37c62009-11-21 08:43:09 +00001128Parser::OwningStmtResult Parser::ParseBreakStatement(AttributeList *Attr) {
1129 // FIXME: Use attributes?
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001131 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132}
1133
1134/// ParseReturnStatement
1135/// jump-statement:
1136/// 'return' expression[opt] ';'
Sean Huntbbd37c62009-11-21 08:43:09 +00001137Parser::OwningStmtResult Parser::ParseReturnStatement(AttributeList *Attr) {
1138 // FIXME: Use attributes?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001139 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001141
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001142 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001143 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001145 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001147 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 }
1149 }
Anders Carlssonf53b4432009-08-18 16:11:00 +00001150 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001151}
1152
Steve Naroff5f8aa692008-02-11 23:15:56 +00001153/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1154/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001155Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001156 if (Tok.is(tok::l_brace)) {
1157 unsigned short savedBraceCount = BraceCount;
1158 do {
1159 ConsumeAnyToken();
1160 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001161 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001162 // From the MS website: If used without braces, the __asm keyword means
1163 // that the rest of the line is an assembly-language statement.
1164 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001165 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001166 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001167 do {
1168 ConsumeAnyToken();
1169 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001170 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1171 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001172 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001173 }
Mike Stump95059b52009-12-11 00:04:56 +00001174 llvm::SmallVector<std::string, 4> Names;
1175 Token t;
1176 t.setKind(tok::string_literal);
1177 t.setLiteralData("\"FIXME: not done\"");
1178 t.clearFlag(Token::NeedsCleaning);
1179 t.setLength(17);
1180 OwningExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1181 ExprVector Constraints(Actions);
1182 ExprVector Exprs(Actions);
1183 ExprVector Clobbers(Actions);
1184 return Actions.ActOnAsmStmt(Tok.getLocation(), true, true, 0, 0, Names.data(),
1185 move_arg(Constraints), move_arg(Exprs),
1186 move(AsmString), move_arg(Clobbers),
Mike Stump3b11fd32010-01-04 22:37:17 +00001187 Tok.getLocation(), true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001188}
1189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001191/// asm-statement:
1192/// gnu-asm-statement
1193/// ms-asm-statement
1194///
1195/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001196/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1197///
1198/// [GNU] asm-argument:
1199/// asm-string-literal
1200/// asm-string-literal ':' asm-operands[opt]
1201/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1202/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1203/// ':' asm-clobbers
1204///
1205/// [GNU] asm-clobbers:
1206/// asm-string-literal
1207/// asm-clobbers ',' asm-string-literal
1208///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001209/// [MS] ms-asm-statement:
1210/// '__asm' assembly-instruction ';'[opt]
1211/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1212///
1213/// [MS] assembly-instruction-list:
1214/// assembly-instruction ';'[opt]
1215/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1216///
Sebastian Redl9a920342008-12-11 19:48:14 +00001217Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001218 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001219 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001220
Steve Naroff5f8aa692008-02-11 23:15:56 +00001221 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001222 msAsm = true;
1223 return FuzzyParseMicrosoftAsmStatement();
1224 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 DeclSpec DS;
1226 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001227 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001228
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1230 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001231 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001233 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001234
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001236 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001237 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001238 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001240 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 }
1242 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001243
Sebastian Redleffa8d12008-12-10 00:02:53 +00001244 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001245 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001246 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001247
Anders Carlssonb235fc22007-11-22 01:36:19 +00001248 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001249 ExprVector Constraints(Actions);
1250 ExprVector Exprs(Actions);
1251 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001252
Anders Carlssondfab34a2008-02-05 23:03:50 +00001253 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001254 // We have a simple asm expression like 'asm("foo")'.
1255 SourceLocation RParenLoc = ConsumeParen();
1256 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1257 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1258 move_arg(Constraints), move_arg(Exprs),
1259 move(AsmString), move_arg(Clobbers),
1260 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001262
Chris Lattner64cb4752009-12-20 23:00:41 +00001263 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001264 bool AteExtraColon = false;
1265 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1266 // In C++ mode, parse "::" like ": :".
1267 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001268 ConsumeToken();
1269
Chris Lattner64056462009-12-20 23:08:04 +00001270 if (!AteExtraColon &&
1271 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001272 return StmtError();
1273 }
Chris Lattner64056462009-12-20 23:08:04 +00001274
Chris Lattner64cb4752009-12-20 23:00:41 +00001275 unsigned NumOutputs = Names.size();
1276
1277 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001278 if (AteExtraColon ||
1279 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1280 // In C++ mode, parse "::" like ": :".
1281 if (AteExtraColon)
1282 AteExtraColon = false;
1283 else {
1284 AteExtraColon = Tok.is(tok::coloncolon);
1285 ConsumeToken();
1286 }
1287
1288 if (!AteExtraColon &&
1289 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001290 return StmtError();
1291 }
1292
1293 assert(Names.size() == Constraints.size() &&
1294 Constraints.size() == Exprs.size() &&
1295 "Input operand size mismatch!");
1296
1297 unsigned NumInputs = Names.size() - NumOutputs;
1298
1299 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001300 if (AteExtraColon || Tok.is(tok::colon)) {
1301 if (!AteExtraColon)
1302 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001303
1304 // Parse the asm-string list for clobbers.
1305 while (1) {
1306 OwningExprResult Clobber(ParseAsmStringLiteral());
1307
1308 if (Clobber.isInvalid())
1309 break;
1310
1311 Clobbers.push_back(Clobber.release());
1312
1313 if (Tok.isNot(tok::comma)) break;
1314 ConsumeToken();
1315 }
1316 }
1317
1318 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1319 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001320 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001321 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001322 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001323 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001324}
1325
1326/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001327/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001328///
1329/// [GNU] asm-operands:
1330/// asm-operand
1331/// asm-operands ',' asm-operand
1332///
1333/// [GNU] asm-operand:
1334/// asm-string-literal '(' expression ')'
1335/// '[' identifier ']' asm-string-literal '(' expression ')'
1336///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001337//
1338// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001339bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001340 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1341 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001343 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001344 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001345
1346 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001348 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001351 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001352 Diag(Tok, diag::err_expected_ident);
1353 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001354 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 }
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Anders Carlssonb235fc22007-11-22 01:36:19 +00001357 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001358 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001359
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001360 Names.push_back(II->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001362 } else
1363 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001364
Sebastian Redleffa8d12008-12-10 00:02:53 +00001365 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001366 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001367 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001368 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001369 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001370 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001371
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001372 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001373 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001375 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001377
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001379 SourceLocation OpenLoc = ConsumeParen();
1380 OwningExprResult Res(ParseExpression());
1381 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001382 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001384 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001386 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001388 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 ConsumeToken();
1390 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001391
1392 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001393}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001394
Chris Lattnerb28317a2009-03-28 19:18:32 +00001395Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001396 assert(Tok.is(tok::l_brace));
1397 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001398
Chris Lattner49f28ca2009-03-05 08:00:35 +00001399 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1400 PP.getSourceManager(),
1401 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001403 // Do not enter a scope for the brace, as the arguments are in the same scope
1404 // (the function body) as the body itself. Instead, just read the statement
1405 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001406 OwningStmtResult FnBody(ParseCompoundStatementBody());
1407
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001408 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001409 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001410 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001411 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001412
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001413 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001414}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001415
Sebastian Redld3a413d2009-04-26 20:35:05 +00001416/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1417///
1418/// function-try-block:
1419/// 'try' ctor-initializer[opt] compound-statement handler-seq
1420///
1421Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1422 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1423 SourceLocation TryLoc = ConsumeToken();
1424
1425 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1426 PP.getSourceManager(),
1427 "parsing function try block");
1428
1429 // Constructor initializer list?
1430 if (Tok.is(tok::colon))
1431 ParseConstructorInitializer(Decl);
1432
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001433 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001434 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1435 // If we failed to parse the try-catch, we just give the function an empty
1436 // compound statement as the body.
1437 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001438 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001439 MultiStmtArg(Actions), false);
1440
1441 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1442}
1443
Sebastian Redla0fd8652008-12-21 16:41:36 +00001444/// ParseCXXTryBlock - Parse a C++ try-block.
1445///
1446/// try-block:
1447/// 'try' compound-statement handler-seq
1448///
Sean Huntbbd37c62009-11-21 08:43:09 +00001449Parser::OwningStmtResult Parser::ParseCXXTryBlock(AttributeList* Attr) {
1450 // FIXME: Add attributes?
Sebastian Redla0fd8652008-12-21 16:41:36 +00001451 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1452
1453 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001454 return ParseCXXTryBlockCommon(TryLoc);
1455}
1456
1457/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1458/// function-try-block.
1459///
1460/// try-block:
1461/// 'try' compound-statement handler-seq
1462///
1463/// function-try-block:
1464/// 'try' ctor-initializer[opt] compound-statement handler-seq
1465///
1466/// handler-seq:
1467/// handler handler-seq[opt]
1468///
1469Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001470 if (Tok.isNot(tok::l_brace))
1471 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001472 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1473 OwningStmtResult TryBlock(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001474 if (TryBlock.isInvalid())
1475 return move(TryBlock);
1476
1477 StmtVector Handlers(Actions);
Sean Huntbbd37c62009-11-21 08:43:09 +00001478 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
1479 CXX0XAttributeList Attr = ParseCXX0XAttributes();
1480 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
1481 << Attr.Range;
1482 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00001483 if (Tok.isNot(tok::kw_catch))
1484 return StmtError(Diag(Tok, diag::err_expected_catch));
1485 while (Tok.is(tok::kw_catch)) {
1486 OwningStmtResult Handler(ParseCXXCatchBlock());
1487 if (!Handler.isInvalid())
1488 Handlers.push_back(Handler.release());
1489 }
1490 // Don't bother creating the full statement if we don't have any usable
1491 // handlers.
1492 if (Handlers.empty())
1493 return StmtError();
1494
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001495 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001496}
1497
1498/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1499///
1500/// handler:
1501/// 'catch' '(' exception-declaration ')' compound-statement
1502///
1503/// exception-declaration:
1504/// type-specifier-seq declarator
1505/// type-specifier-seq abstract-declarator
1506/// type-specifier-seq
1507/// '...'
1508///
1509Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1510 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1511
1512 SourceLocation CatchLoc = ConsumeToken();
1513
1514 SourceLocation LParenLoc = Tok.getLocation();
1515 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1516 return StmtError();
1517
1518 // C++ 3.3.2p3:
1519 // The name in a catch exception-declaration is local to the handler and
1520 // shall not be redeclared in the outermost block of the handler.
1521 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1522
1523 // exception-declaration is equivalent to '...' or a parameter-declaration
1524 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001525 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001526 if (Tok.isNot(tok::ellipsis)) {
1527 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001528 if (ParseCXXTypeSpecifierSeq(DS))
1529 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001530 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1531 ParseDeclarator(ExDecl);
1532 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1533 } else
1534 ConsumeToken();
1535
1536 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1537 return StmtError();
1538
1539 if (Tok.isNot(tok::l_brace))
1540 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1541
Sean Huntbbd37c62009-11-21 08:43:09 +00001542 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
1543 OwningStmtResult Block(ParseCompoundStatement(0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001544 if (Block.isInvalid())
1545 return move(Block);
1546
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001547 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001548}