blob: 4fdcf2a077f3551c0b6c76aa11d2bbd5f03766fc [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner1ff6e732008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner33ad2ca2006-11-05 23:47:55 +000018#include "clang/Parse/Scope.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +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 Kyrtzidisdee82912008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000045/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000046/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000053/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
Chris Lattner9075bd72006-08-10 04:59:57 +000062/// expression-statement:
63/// expression[opt] ';'
64///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000065/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000070/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000071///
Fariborz Jahanian90814572007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000074/// [OBC] '@' 'throw' ';'
75///
Sebastian Redl042ad952008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000078 const char *SemiError = 0;
Sebastian Redlc13f2682008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000080
Chris Lattner503fadc2006-08-10 05:45:44 +000081 // Cases in this switch statement should fall through if the parser expects
82 // the token to end in a semicolon (in which case SemiError should be set),
83 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000084 tok::TokenKind Kind = Tok.getKind();
85 SourceLocation AtLoc;
86 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +000090 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000091 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000092
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +000093 case tok::identifier:
94 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
95 // identifier ':' statement
96 return ParseLabeledStatement();
97 }
98 // PASS THROUGH.
99
Chris Lattner803802d2009-03-24 17:04:48 +0000100 default: {
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000101 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000102 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
103 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd);
104 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000105 }
106
107 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000108 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000109 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000110 }
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattner803802d2009-03-24 17:04:48 +0000112 // expression[opt] ';'
113 OwningExprResult Expr(ParseExpression());
114 if (Expr.isInvalid()) {
115 // If the expression is invalid, skip ahead to the next semicolon. Not
116 // doing this opens us up to the possibility of infinite loops if
117 // ParseExpression does not consume any tokens.
118 SkipUntil(tok::semi);
119 return StmtError();
120 }
121 // Otherwise, eat the semicolon.
122 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +0000123 return Actions.ActOnExprStmt(Actions.FullExpr(Expr));
Chris Lattner803802d2009-03-24 17:04:48 +0000124 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000125
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000126 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000127 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000128 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000129 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000130
Chris Lattner9075bd72006-08-10 04:59:57 +0000131 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000132 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000133 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000134 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl042ad952008-12-11 19:30:53 +0000135
Chris Lattner9075bd72006-08-10 04:59:57 +0000136 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000137 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000138 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redlb62406f2008-12-11 19:48:14 +0000139 return ParseSwitchStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000140
Chris Lattner9075bd72006-08-10 04:59:57 +0000141 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redlb62406f2008-12-11 19:48:14 +0000142 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000143 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000144 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000145 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000146 break;
147 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redlb62406f2008-12-11 19:48:14 +0000148 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000149
150 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000151 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000152 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000153 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000154 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000155 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000156 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000157 break;
158 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000159 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000160 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000161 break;
162 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000163 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000164 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000165 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000166
Sebastian Redlb219c902008-12-21 16:41:36 +0000167 case tok::kw_asm: {
Steve Naroffb2c80c72008-02-07 03:50:06 +0000168 bool msAsm = false;
169 Res = ParseAsmStatement(msAsm);
Sebastian Redl042ad952008-12-11 19:30:53 +0000170 if (msAsm) return move(Res);
Chris Lattner34a95662009-06-14 00:07:48 +0000171 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000172 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000173 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000174
Sebastian Redlb219c902008-12-21 16:41:36 +0000175 case tok::kw_try: // C++ 15: try-block
176 return ParseCXXTryBlock();
177 }
178
Chris Lattner503fadc2006-08-10 05:45:44 +0000179 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000180 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000181 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000182 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000183 // If the result was valid, then we do want to diagnose this. Use
184 // ExpectAndConsume to emit the diagnostic, even though we know it won't
185 // succeed.
186 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000187 // Skip until we see a } or ;, but don't eat it.
188 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000189 }
Mike Stump11289f42009-09-09 15:08:12 +0000190
Sebastian Redl042ad952008-12-11 19:30:53 +0000191 return move(Res);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000192}
193
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000194/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000195///
196/// labeled-statement:
197/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000198/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000199///
Sebastian Redl042ad952008-12-11 19:30:53 +0000200Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000201 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
202 "Not an identifier!");
203
204 Token IdentTok = Tok; // Save the whole token.
205 ConsumeToken(); // eat the identifier.
206
207 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000208
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000209 // identifier ':' statement
210 SourceLocation ColonLoc = ConsumeToken();
211
212 // Read label attributes, if present.
Chris Lattner83f095c2009-03-28 19:18:32 +0000213 Action::AttrTy *AttrList = 0;
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000214 if (Tok.is(tok::kw___attribute))
215 // TODO: save these somewhere.
216 AttrList = ParseAttributes();
217
Sebastian Redl042ad952008-12-11 19:30:53 +0000218 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000219
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000220 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000221 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000222 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000223
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000224 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
225 IdentTok.getIdentifierInfo(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000226 ColonLoc, move(SubStmt));
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000227}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000228
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000229/// ParseCaseStatement
230/// labeled-statement:
231/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000232/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000233///
Sebastian Redl042ad952008-12-11 19:30:53 +0000234Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000235 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattner34a22092009-03-04 04:23:07 +0000237 // It is very very common for code to contain many case statements recursively
238 // nested, as in (but usually without indentation):
239 // case 1:
240 // case 2:
241 // case 3:
242 // case 4:
243 // case 5: etc.
244 //
245 // Parsing this naively works, but is both inefficient and can cause us to run
246 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000247 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000248 // but all the grossness is constrained to ParseCaseStatement (and some
249 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000250
Chris Lattner34a22092009-03-04 04:23:07 +0000251 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
252 // example above.
253 OwningStmtResult TopLevelCase(Actions, true);
Mike Stump11289f42009-09-09 15:08:12 +0000254
Chris Lattner34a22092009-03-04 04:23:07 +0000255 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
256 // gets updated each time a new case is parsed, and whose body is unset so
257 // far. When parsing 'case 4', this is the 'case 3' node.
258 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Chris Lattner34a22092009-03-04 04:23:07 +0000260 // While we have case statements, eat and stack them.
261 do {
262 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000263
Douglas Gregord328d572009-09-21 18:10:23 +0000264 if (Tok.is(tok::code_completion)) {
265 Actions.CodeCompleteCase(CurScope);
266 ConsumeToken();
267 }
268
Chris Lattner34a22092009-03-04 04:23:07 +0000269 OwningExprResult LHS(ParseConstantExpression());
270 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000271 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000272 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000273 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000274
Chris Lattner34a22092009-03-04 04:23:07 +0000275 // GNU case range extension.
276 SourceLocation DotDotDotLoc;
277 OwningExprResult RHS(Actions);
278 if (Tok.is(tok::ellipsis)) {
279 Diag(Tok, diag::ext_gnu_case_range);
280 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000281
Chris Lattner34a22092009-03-04 04:23:07 +0000282 RHS = ParseConstantExpression();
283 if (RHS.isInvalid()) {
284 SkipUntil(tok::colon);
285 return StmtError();
286 }
287 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000288
Chris Lattner34a22092009-03-04 04:23:07 +0000289 if (Tok.isNot(tok::colon)) {
290 Diag(Tok, diag::err_expected_colon_after) << "'case'";
291 SkipUntil(tok::colon);
292 return StmtError();
293 }
294
295 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattner34a22092009-03-04 04:23:07 +0000297 OwningStmtResult Case =
298 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
299 move(RHS), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattner34a22092009-03-04 04:23:07 +0000301 // If we had a sema error parsing this case, then just ignore it and
302 // continue parsing the sub-stmt.
303 if (Case.isInvalid()) {
304 if (TopLevelCase.isInvalid()) // No parsed case stmts.
305 return ParseStatement();
306 // Otherwise, just don't add it as a nested case.
307 } else {
308 // If this is the first case statement we parsed, it becomes TopLevelCase.
309 // Otherwise we link it into the current chain.
310 StmtTy *NextDeepest = Case.get();
311 if (TopLevelCase.isInvalid())
312 TopLevelCase = move(Case);
313 else
314 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
315 DeepestParsedCaseStmt = NextDeepest;
316 }
Mike Stump11289f42009-09-09 15:08:12 +0000317
Chris Lattner34a22092009-03-04 04:23:07 +0000318 // Handle all case statements.
319 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000320
Chris Lattner34a22092009-03-04 04:23:07 +0000321 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000322
Chris Lattner34a22092009-03-04 04:23:07 +0000323 // If we found a non-case statement, start by parsing it.
324 OwningStmtResult SubStmt(Actions);
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattner34a22092009-03-04 04:23:07 +0000326 if (Tok.isNot(tok::r_brace)) {
327 SubStmt = ParseStatement();
328 } else {
329 // Nicely diagnose the common error "switch (X) { case 4: }", which is
330 // not valid.
331 // FIXME: add insertion hint.
Chris Lattner30f910e2006-10-16 05:52:41 +0000332 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner34a22092009-03-04 04:23:07 +0000333 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Chris Lattner34a22092009-03-04 04:23:07 +0000336 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000337 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000338 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000339
Chris Lattner34a22092009-03-04 04:23:07 +0000340 // Install the body into the most deeply-nested case.
341 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl042ad952008-12-11 19:30:53 +0000342
Chris Lattner34a22092009-03-04 04:23:07 +0000343 // Return the top level parsed statement tree.
Chris Lattner2b19a6582009-03-04 18:24:58 +0000344 return move(TopLevelCase);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000345}
346
347/// ParseDefaultStatement
348/// labeled-statement:
349/// 'default' ':' statement
350/// Note that this does not parse the 'statement' at the end.
351///
Sebastian Redl042ad952008-12-11 19:30:53 +0000352Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000353 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000354 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000355
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000356 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000357 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000358 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000359 return StmtError();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000360 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000361
Chris Lattneraf635312006-10-16 06:06:51 +0000362 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000363
Chris Lattner30f910e2006-10-16 05:52:41 +0000364 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000365 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000366 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000367 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000368 }
369
Sebastian Redl042ad952008-12-11 19:30:53 +0000370 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000371 if (SubStmt.isInvalid())
Sebastian Redl042ad952008-12-11 19:30:53 +0000372 return StmtError();
373
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000374 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +0000375 move(SubStmt), CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000376}
377
378
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000379/// ParseCompoundStatement - Parse a "{}" block.
380///
381/// compound-statement: [C99 6.8.2]
382/// { block-item-list[opt] }
383/// [GNU] { label-declarations block-item-list } [TODO]
384///
385/// block-item-list:
386/// block-item
387/// block-item-list block-item
388///
389/// block-item:
390/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000391/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000392/// statement
393/// [OMP] openmp-directive [TODO]
394///
395/// [GNU] label-declarations:
396/// [GNU] label-declaration
397/// [GNU] label-declarations label-declaration
398///
399/// [GNU] label-declaration:
400/// [GNU] '__label__' identifier-list ';'
401///
402/// [OMP] openmp-directive: [TODO]
403/// [OMP] barrier-directive
404/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000405///
Sebastian Redl042ad952008-12-11 19:30:53 +0000406Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000407 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000408
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000409 // Enter a scope to hold everything within the compound stmt. Compound
410 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000411 ParseScope CompoundScope(this, Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000412
413 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000414 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000415}
416
417
418/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000419/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000420/// consume the '}' at the end of the block. It does not manipulate the scope
421/// stack.
Sebastian Redl042ad952008-12-11 19:30:53 +0000422Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000423 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000424 Tok.getLocation(),
425 "in compound statement ('{}')");
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerf2978802007-01-21 06:52:16 +0000427 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
428
Chris Lattner010015a2007-05-28 07:23:54 +0000429 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000430 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000431
432 typedef StmtVector StmtsTy;
433 StmtsTy Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000434 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redlc13f2682008-12-09 20:22:58 +0000435 OwningStmtResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000436 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000437 R = ParseStatementOrDeclaration(false);
438 } else {
439 // __extension__ can start declarations and it can also be a unary
440 // operator for expressions. Consume multiple __extension__ markers here
441 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000442 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000443 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000444 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000445 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000446
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000447 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000448 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000449 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000450 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000451 ExtensionRAIIObject O(Diags);
452
Chris Lattner49836b42009-04-02 04:16:50 +0000453 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
454 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext,DeclEnd);
455 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000456 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000457 // Otherwise this was a unary __extension__ marker.
458 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000459
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000460 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000461 SkipUntil(tok::semi);
462 continue;
463 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000464
Chris Lattner1ff6e732008-10-20 06:51:33 +0000465 // Eat the semicolon at the end of stmt and convert the expr into a
466 // statement.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000467 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +0000468 R = Actions.ActOnExprStmt(Actions.FullExpr(Res));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000469 }
470 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000471
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000472 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000473 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000474 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000475
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000476 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000477 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000478 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl042ad952008-12-11 19:30:53 +0000479 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000480 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000481
Chris Lattner04132372006-10-16 06:12:55 +0000482 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000483 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redl52f03ba2008-12-21 12:04:03 +0000484 isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000485}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000486
Chris Lattnerc0081db2008-12-12 06:31:07 +0000487/// ParseParenExprOrCondition:
488/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000489/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000490///
491/// This function parses and performs error recovery on the specified condition
492/// or expression (depending on whether we're in C++ or C mode). This function
493/// goes out of its way to recover well. It returns true if there was a parser
494/// error (the right paren couldn't be found), which indicates that the caller
495/// should try to recover harder. It returns false if the condition is
496/// successfully parsed. Note that a successful parse can still have semantic
497/// errors in the condition.
Chris Lattner10da53c2008-12-12 06:35:28 +0000498bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
Chris Lattner815b70e2009-06-12 23:04:47 +0000499 bool OnlyAllowCondition,
500 SourceLocation *LParenLocPtr,
501 SourceLocation *RParenLocPtr) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000502 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner815b70e2009-06-12 23:04:47 +0000503 if (LParenLocPtr) *LParenLocPtr = LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattnerc0081db2008-12-12 06:31:07 +0000505 if (getLang().CPlusPlus)
506 CondExp = ParseCXXCondition();
507 else
508 CondExp = ParseExpression();
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattnerc0081db2008-12-12 06:31:07 +0000510 // If the parser was confused by the condition and we don't have a ')', try to
511 // recover by skipping ahead to a semi and bailing out. If condexp is
512 // semantically invalid but we have well formed code, keep going.
513 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
514 SkipUntil(tok::semi);
515 // Skipping may have stopped if it found the containing ')'. If so, we can
516 // continue parsing the if statement.
517 if (Tok.isNot(tok::r_paren))
518 return true;
519 }
Mike Stump11289f42009-09-09 15:08:12 +0000520
Chris Lattnerc0081db2008-12-12 06:31:07 +0000521 // Otherwise the condition is valid or the rparen is present.
Chris Lattner815b70e2009-06-12 23:04:47 +0000522 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
523 if (RParenLocPtr) *RParenLocPtr = RPLoc;
Chris Lattnerc0081db2008-12-12 06:31:07 +0000524 return false;
525}
526
527
Chris Lattnerc951dae2006-08-10 04:23:57 +0000528/// ParseIfStatement
529/// if-statement: [C99 6.8.4.1]
530/// 'if' '(' expression ')' statement
531/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000532/// [C++] 'if' '(' condition ')' statement
533/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000534///
Sebastian Redl042ad952008-12-11 19:30:53 +0000535Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000536 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000537 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000538
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000539 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000540 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000541 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000542 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000543 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000544
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000545 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
546
Chris Lattner2dd1b722007-08-26 23:08:06 +0000547 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
548 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000549 //
550 // C++ 6.4p3:
551 // A name introduced by a declaration in a condition is in scope from its
552 // point of declaration until the end of the substatements controlled by the
553 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000554 // C++ 3.3.2p4:
555 // Names declared in the for-init-statement, and in the condition of if,
556 // while, for, and switch statements are local to the if, while, for, or
557 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000558 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000559 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000560
Chris Lattnerc951dae2006-08-10 04:23:57 +0000561 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000562 OwningExprResult CondExp(Actions);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000563 if (ParseParenExprOrCondition(CondExp))
564 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000565
Anders Carlsson89360092009-06-04 02:18:15 +0000566 FullExprArg FullCondExp(Actions.FullExpr(CondExp));
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner8fb26252007-08-22 05:28:50 +0000568 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000569 // there is no compound stmt. C90 does not have this clause. We only do this
570 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000571 //
572 // C++ 6.4p1:
573 // The substatement in a selection-statement (each substatement, in the else
574 // form of the if statement) implicitly defines a local scope.
575 //
576 // For C++ we create a scope for the condition and a new scope for
577 // substatements because:
578 // -When the 'then' scope exits, we want the condition declaration to still be
579 // active for the 'else' scope too.
580 // -Sema will detect name clashes by considering declarations of a
581 // 'ControlScope' as part of its direct subscope.
582 // -If we wanted the condition and substatement to be in the same scope, we
583 // would have to notify ParseStatement not to create a new scope. It's
584 // simpler to let it create a new scope.
585 //
Mike Stump11289f42009-09-09 15:08:12 +0000586 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000587 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000588
Chris Lattner5c5808a2007-10-29 05:08:52 +0000589 // Read the 'then' stmt.
590 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000591 OwningStmtResult ThenStmt(ParseStatement());
Chris Lattnerac4471c2007-05-28 05:38:24 +0000592
Chris Lattner37e54f42007-08-22 05:16:28 +0000593 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000594 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000595
Chris Lattnerc951dae2006-08-10 04:23:57 +0000596 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000597 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000598 SourceLocation ElseStmtLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +0000599 OwningStmtResult ElseStmt(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000600
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000601 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000602 ElseLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000603
Chris Lattner8fb26252007-08-22 05:28:50 +0000604 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000605 // there is no compound stmt. C90 does not have this clause. We only do
606 // this if the body isn't a compound statement to avoid push/pop in common
607 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000608 //
609 // C++ 6.4p1:
610 // The substatement in a selection-statement (each substatement, in the else
611 // form of the if statement) implicitly defines a local scope.
612 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000613 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000614 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000615
Douglas Gregor85970ca2008-12-10 23:01:14 +0000616 bool WithinElse = CurScope->isWithinElse();
617 CurScope->setWithinElse(true);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000618 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000619 ElseStmt = ParseStatement();
Douglas Gregor85970ca2008-12-10 23:01:14 +0000620 CurScope->setWithinElse(WithinElse);
Chris Lattner37e54f42007-08-22 05:16:28 +0000621
622 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000623 InnerScope.Exit();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000624 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000625
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000626 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +0000627
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000628 // If the condition was invalid, discard the if statement. We could recover
629 // better by replacing it with a valid expr, but don't do that yet.
630 if (CondExp.isInvalid())
631 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +0000632
Chris Lattner5c5808a2007-10-29 05:08:52 +0000633 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +0000634 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +0000635 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000636 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
637 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
638 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000639 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +0000640 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +0000641 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000642
Chris Lattner5c5808a2007-10-29 05:08:52 +0000643 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000644 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000645 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000646 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +0000647 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000648
Mike Stump11289f42009-09-09 15:08:12 +0000649 return Actions.ActOnIfStmt(IfLoc, FullCondExp, move(ThenStmt),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000650 ElseLoc, move(ElseStmt));
Chris Lattnerc951dae2006-08-10 04:23:57 +0000651}
652
Chris Lattner9075bd72006-08-10 04:59:57 +0000653/// ParseSwitchStatement
654/// switch-statement:
655/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000656/// [C++] 'switch' '(' condition ')' statement
Sebastian Redlb62406f2008-12-11 19:48:14 +0000657Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000658 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000659 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000660
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000661 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000662 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000663 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000664 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000665 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000666
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000667 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
668
Chris Lattner2dd1b722007-08-26 23:08:06 +0000669 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
670 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000671 //
672 // C++ 6.4p3:
673 // A name introduced by a declaration in a condition is in scope from its
674 // point of declaration until the end of the substatements controlled by the
675 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000676 // C++ 3.3.2p4:
677 // Names declared in the for-init-statement, and in the condition of if,
678 // while, for, and switch statements are local to the if, while, for, or
679 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000680 //
Chris Lattnerc0081db2008-12-12 06:31:07 +0000681 unsigned ScopeFlags = Scope::BreakScope;
682 if (C99orCXX)
683 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000684 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000685
Chris Lattner9075bd72006-08-10 04:59:57 +0000686 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000687 OwningExprResult Cond(Actions);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000688 if (ParseParenExprOrCondition(Cond))
Sebastian Redlb62406f2008-12-11 19:48:14 +0000689 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +0000690
691 OwningStmtResult Switch(Actions);
692 if (!Cond.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000693 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000694
Chris Lattner8fb26252007-08-22 05:28:50 +0000695 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000696 // there is no compound stmt. C90 does not have this clause. We only do this
697 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000698 //
699 // C++ 6.4p1:
700 // The substatement in a selection-statement (each substatement, in the else
701 // form of the if statement) implicitly defines a local scope.
702 //
703 // See comments in ParseIfStatement for why we create a scope for the
704 // condition and a new scope for substatement in C++.
705 //
Mike Stump11289f42009-09-09 15:08:12 +0000706 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000707 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +0000708
Chris Lattner9075bd72006-08-10 04:59:57 +0000709 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000710 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000711
Chris Lattner8fb26252007-08-22 05:28:50 +0000712 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000713 InnerScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000714
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000715 if (Body.isInvalid()) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000716 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000717 // FIXME: Remove the case statement list from the Switch statement.
718 }
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000719
720 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000721
Chris Lattnerc0081db2008-12-12 06:31:07 +0000722 if (Cond.isInvalid())
723 return StmtError();
Sebastian Redl6a8002e2009-01-11 00:38:46 +0000724
Sebastian Redl726a0d92009-02-05 15:02:23 +0000725 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000726}
727
728/// ParseWhileStatement
729/// while-statement: [C99 6.8.5.1]
730/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000731/// [C++] 'while' '(' condition ')' statement
Sebastian Redlb62406f2008-12-11 19:48:14 +0000732Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000733 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000734 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000735 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000736
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000737 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000738 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000739 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000740 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000741 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000742
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000743 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
744
Chris Lattner2dd1b722007-08-26 23:08:06 +0000745 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
746 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000747 //
748 // C++ 6.4p3:
749 // A name introduced by a declaration in a condition is in scope from its
750 // point of declaration until the end of the substatements controlled by the
751 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000752 // C++ 3.3.2p4:
753 // Names declared in the for-init-statement, and in the condition of if,
754 // while, for, and switch statements are local to the if, while, for, or
755 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000756 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000757 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000758 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000759 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
760 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000761 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000762 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
763 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000764
Chris Lattner9075bd72006-08-10 04:59:57 +0000765 // Parse the condition.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000766 OwningExprResult Cond(Actions);
Chris Lattnerc0081db2008-12-12 06:31:07 +0000767 if (ParseParenExprOrCondition(Cond))
768 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000769
Anders Carlsson89360092009-06-04 02:18:15 +0000770 FullExprArg FullCond(Actions.FullExpr(Cond));
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner8fb26252007-08-22 05:28:50 +0000772 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000773 // there is no compound stmt. C90 does not have this clause. We only do this
774 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000775 //
776 // C++ 6.5p2:
777 // The substatement in an iteration-statement implicitly defines a local scope
778 // which is entered and exited each time through the loop.
779 //
780 // See comments in ParseIfStatement for why we create a scope for the
781 // condition and a new scope for substatement in C++.
782 //
Mike Stump11289f42009-09-09 15:08:12 +0000783 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000784 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000785
Chris Lattner9075bd72006-08-10 04:59:57 +0000786 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000787 OwningStmtResult Body(ParseStatement());
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000788
Chris Lattner8fb26252007-08-22 05:28:50 +0000789 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000790 InnerScope.Exit();
791 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000792
793 if (Cond.isInvalid() || Body.isInvalid())
794 return StmtError();
795
Anders Carlsson89360092009-06-04 02:18:15 +0000796 return Actions.ActOnWhileStmt(WhileLoc, FullCond, move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +0000797}
798
799/// ParseDoStatement
800/// do-statement: [C99 6.8.5.2]
801/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000802/// Note: this lets the caller parse the end ';'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000803Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000804 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000805 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000806
Chris Lattner2dd1b722007-08-26 23:08:06 +0000807 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
808 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000809 unsigned ScopeFlags;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000810 if (getLang().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000811 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000812 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000813 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +0000814
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000815 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000816
Chris Lattner8fb26252007-08-22 05:28:50 +0000817 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000818 // there is no compound stmt. C90 does not have this clause. We only do this
819 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000820 //
821 // C++ 6.5p2:
822 // The substatement in an iteration-statement implicitly defines a local scope
823 // which is entered and exited each time through the loop.
824 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000825 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump11289f42009-09-09 15:08:12 +0000826 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000827 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +0000828
Chris Lattner9075bd72006-08-10 04:59:57 +0000829 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +0000830 OwningStmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +0000831
Chris Lattner8fb26252007-08-22 05:28:50 +0000832 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000833 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +0000834
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000835 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000836 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +0000837 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000838 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000839 SkipUntil(tok::semi, false, true);
840 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000841 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000842 }
Chris Lattneraf635312006-10-16 06:06:51 +0000843 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +0000844
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000845 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000846 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000847 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000848 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000849 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000850
Chris Lattner10da53c2008-12-12 06:35:28 +0000851 // Parse the parenthesized condition.
852 OwningExprResult Cond(Actions);
Chris Lattner815b70e2009-06-12 23:04:47 +0000853 SourceLocation LPLoc, RPLoc;
854 ParseParenExprOrCondition(Cond, true, &LPLoc, &RPLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000855
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000856 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000857
Sebastian Redlb62406f2008-12-11 19:48:14 +0000858 if (Cond.isInvalid() || Body.isInvalid())
859 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000860
Chris Lattner815b70e2009-06-12 23:04:47 +0000861 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
862 move(Cond), RPLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000863}
864
865/// ParseForStatement
866/// for-statement: [C99 6.8.5.3]
867/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
868/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000869/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
870/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000871/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
872/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000873///
874/// [C++] for-init-statement:
875/// [C++] expression-statement
876/// [C++] simple-declaration
877///
Sebastian Redlb62406f2008-12-11 19:48:14 +0000878Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000879 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000880 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +0000881
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000882 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000883 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000884 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000885 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +0000886 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000887
Chris Lattner934074c2009-04-22 00:54:41 +0000888 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000889
Chris Lattner2dd1b722007-08-26 23:08:06 +0000890 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
891 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000892 //
893 // C++ 6.4p3:
894 // A name introduced by a declaration in a condition is in scope from its
895 // point of declaration until the end of the substatements controlled by the
896 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000897 // C++ 3.3.2p4:
898 // Names declared in the for-init-statement, and in the condition of if,
899 // while, for, and switch statements are local to the if, while, for, or
900 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000901 // C++ 6.5.3p1:
902 // Names declared in the for-init-statement are in the same declarative-region
903 // as those declared in the condition.
904 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000905 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +0000906 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000907 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
908 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +0000909 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000910 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
911
912 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +0000913
Chris Lattner04132372006-10-16 06:12:55 +0000914 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlc13f2682008-12-09 20:22:58 +0000915 OwningExprResult Value(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000916
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000917 bool ForEach = false;
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000918 OwningStmtResult FirstPart(Actions);
919 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000920
Chris Lattner9075bd72006-08-10 04:59:57 +0000921 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000922 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000923 // no first part, eat the ';'.
924 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +0000925 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000926 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +0000927 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +0000928 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +0000929
Chris Lattner49836b42009-04-02 04:16:50 +0000930 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
931 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
932 false);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000933 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattner32dc41c2009-03-29 17:27:48 +0000935 if (Tok.is(tok::semi)) { // for (int x = 4;
936 ConsumeToken();
937 } else if ((ForEach = isTokIdentifier_in())) {
Mike Stump11289f42009-09-09 15:08:12 +0000938 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000939 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000940 SecondPart = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +0000941 } else {
942 Diag(Tok, diag::err_expected_semi_for);
943 SkipUntil(tok::semi);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000944 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000945 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000946 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000947
Chris Lattnercd68f642007-06-27 01:06:29 +0000948 // Turn the expression into a stmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000949 if (!Value.isInvalid())
Anders Carlsson24824e52009-05-17 21:11:30 +0000950 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000951
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000952 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000953 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000954 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000955 ConsumeToken(); // consume 'in'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000956 SecondPart = ParseExpression();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000957 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000958 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000959 SkipUntil(tok::semi);
960 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000961 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000962 if (!ForEach) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000963 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000964 // Parse the second part of the for specifier.
965 if (Tok.is(tok::semi)) { // for (...;;
966 // no second part.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000967 } else {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000968 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000969 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000970
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000971 if (Tok.is(tok::semi)) {
972 ConsumeToken();
973 } else {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000974 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000975 SkipUntil(tok::semi);
976 }
Sebastian Redlb62406f2008-12-11 19:48:14 +0000977
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000978 // Parse the third part of the for specifier.
Chris Lattnerbe36eb02009-03-29 17:29:28 +0000979 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlfbfaafc2009-01-16 23:28:06 +0000980 ThirdPart = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000981 }
Chris Lattner4564bc12006-08-10 23:14:52 +0000982 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000983 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000984
Chris Lattner8fb26252007-08-22 05:28:50 +0000985 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000986 // there is no compound stmt. C90 does not have this clause. We only do this
987 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000988 //
989 // C++ 6.5p2:
990 // The substatement in an iteration-statement implicitly defines a local scope
991 // which is entered and exited each time through the loop.
992 //
993 // See comments in ParseIfStatement for why we create a scope for
994 // for-init-statement/condition and a new scope for substatement in C++.
995 //
Mike Stump11289f42009-09-09 15:08:12 +0000996 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +0000997 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000998
Chris Lattner9075bd72006-08-10 04:59:57 +0000999 // Read the body statement.
Sebastian Redl042ad952008-12-11 19:30:53 +00001000 OwningStmtResult Body(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001001
Chris Lattner8fb26252007-08-22 05:28:50 +00001002 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001003 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001004
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001005 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001006 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001007
1008 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001009 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001010
1011 if (!ForEach)
Sebastian Redl726a0d92009-02-05 15:02:23 +00001012 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
1013 move(SecondPart), move(ThirdPart),
1014 RParenLoc, move(Body));
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001016 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1017 move(FirstPart),
1018 move(SecondPart),
1019 RParenLoc, move(Body));
Chris Lattner9075bd72006-08-10 04:59:57 +00001020}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001021
Chris Lattner503fadc2006-08-10 05:45:44 +00001022/// ParseGotoStatement
1023/// jump-statement:
1024/// 'goto' identifier ';'
1025/// [GNU] 'goto' '*' expression ';'
1026///
1027/// Note: this lets the caller parse the end ';'.
1028///
Sebastian Redlb62406f2008-12-11 19:48:14 +00001029Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001030 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001031 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001032
Sebastian Redlc13f2682008-12-09 20:22:58 +00001033 OwningStmtResult Res(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001034 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +00001035 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +00001036 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +00001037 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001038 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001039 // GNU indirect goto extension.
1040 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001041 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001042 OwningExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001043 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001044 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001045 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001046 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001047 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattnere34b2c22007-07-22 04:13:33 +00001048 } else {
1049 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001050 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001051 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001052
Sebastian Redlb62406f2008-12-11 19:48:14 +00001053 return move(Res);
Chris Lattner503fadc2006-08-10 05:45:44 +00001054}
1055
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001056/// ParseContinueStatement
1057/// jump-statement:
1058/// 'continue' ';'
1059///
1060/// Note: this lets the caller parse the end ';'.
1061///
Sebastian Redlb62406f2008-12-11 19:48:14 +00001062Parser::OwningStmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001063 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl573feed2009-01-18 13:19:59 +00001064 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001065}
1066
1067/// ParseBreakStatement
1068/// jump-statement:
1069/// 'break' ';'
1070///
1071/// Note: this lets the caller parse the end ';'.
1072///
Sebastian Redlb62406f2008-12-11 19:48:14 +00001073Parser::OwningStmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001074 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl573feed2009-01-18 13:19:59 +00001075 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001076}
1077
Chris Lattner503fadc2006-08-10 05:45:44 +00001078/// ParseReturnStatement
1079/// jump-statement:
1080/// 'return' expression[opt] ';'
Sebastian Redlb62406f2008-12-11 19:48:14 +00001081Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001082 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001083 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001084
Sebastian Redlc13f2682008-12-09 20:22:58 +00001085 OwningExprResult R(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001086 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +00001087 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001088 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001089 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001090 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001091 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001092 }
Anders Carlssona1929472009-08-18 16:11:00 +00001093 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Chris Lattner503fadc2006-08-10 05:45:44 +00001094}
Chris Lattner0116c472006-08-15 06:03:28 +00001095
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001096/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1097/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001098Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001099 if (Tok.is(tok::l_brace)) {
1100 unsigned short savedBraceCount = BraceCount;
1101 do {
1102 ConsumeAnyToken();
1103 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump11289f42009-09-09 15:08:12 +00001104 } else {
Steve Naroff4e79d342008-02-07 23:24:32 +00001105 // From the MS website: If used without braces, the __asm keyword means
1106 // that the rest of the line is an assembly-language statement.
1107 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001108 SourceLocation TokLoc = Tok.getLocation();
Chris Lattner8a425862009-01-16 07:36:28 +00001109 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff8c099c32008-02-08 18:01:27 +00001110 do {
1111 ConsumeAnyToken();
1112 TokLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001113 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1114 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff8c099c32008-02-08 18:01:27 +00001115 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001116 }
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001117 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffb2c80c72008-02-07 03:50:06 +00001118}
1119
Chris Lattner0116c472006-08-15 06:03:28 +00001120/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001121/// asm-statement:
1122/// gnu-asm-statement
1123/// ms-asm-statement
1124///
1125/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001126/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1127///
1128/// [GNU] asm-argument:
1129/// asm-string-literal
1130/// asm-string-literal ':' asm-operands[opt]
1131/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1132/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1133/// ':' asm-clobbers
1134///
1135/// [GNU] asm-clobbers:
1136/// asm-string-literal
1137/// asm-clobbers ',' asm-string-literal
1138///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001139/// [MS] ms-asm-statement:
1140/// '__asm' assembly-instruction ';'[opt]
1141/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1142///
1143/// [MS] assembly-instruction-list:
1144/// assembly-instruction ';'[opt]
1145/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1146///
Sebastian Redlb62406f2008-12-11 19:48:14 +00001147Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001148 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001149 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001150
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001151 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001152 msAsm = true;
1153 return FuzzyParseMicrosoftAsmStatement();
1154 }
Chris Lattner0116c472006-08-15 06:03:28 +00001155 DeclSpec DS;
1156 SourceLocation Loc = Tok.getLocation();
1157 ParseTypeQualifierListOpt(DS);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001158
Chris Lattner0116c472006-08-15 06:03:28 +00001159 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001160 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001161 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001162 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001163 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001164
Chris Lattner0116c472006-08-15 06:03:28 +00001165 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001166 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001167 bool isSimple = false;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001168 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001169 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001170 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001171 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001172 }
Chris Lattner04132372006-10-16 06:12:55 +00001173 Loc = ConsumeParen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001174
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001175 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001176 if (AsmString.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001177 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001178
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001179 llvm::SmallVector<std::string, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001180 ExprVector Constraints(Actions);
1181 ExprVector Exprs(Actions);
1182 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001183
Anders Carlsson19fe1162008-02-05 23:03:50 +00001184 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001185
Anders Carlsson19fe1162008-02-05 23:03:50 +00001186 SourceLocation RParenLoc;
1187 if (Tok.is(tok::r_paren)) {
1188 // We have a simple asm expression
1189 isSimple = true;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001190
Anders Carlsson19fe1162008-02-05 23:03:50 +00001191 RParenLoc = ConsumeParen();
1192 } else {
Sebastian Redl511ed552008-11-25 22:21:31 +00001193 // Parse Outputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001194 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001195 return StmtError();
1196
Anders Carlsson19fe1162008-02-05 23:03:50 +00001197 NumOutputs = Names.size();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001198
Anders Carlsson19fe1162008-02-05 23:03:50 +00001199 // Parse Inputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001200 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001201 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001202
Anders Carlsson19fe1162008-02-05 23:03:50 +00001203 assert(Names.size() == Constraints.size() &&
Mike Stump11289f42009-09-09 15:08:12 +00001204 Constraints.size() == Exprs.size()
Anders Carlsson19fe1162008-02-05 23:03:50 +00001205 && "Input operand size mismatch!");
1206
1207 NumInputs = Names.size() - NumOutputs;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001208
Anders Carlsson19fe1162008-02-05 23:03:50 +00001209 // Parse the clobbers, if present.
1210 if (Tok.is(tok::colon)) {
Anders Carlsson091a0592007-11-21 23:27:34 +00001211 ConsumeToken();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001212
Anders Carlsson19fe1162008-02-05 23:03:50 +00001213 // Parse the asm-string list for clobbers.
1214 while (1) {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001215 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlsson19fe1162008-02-05 23:03:50 +00001216
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001217 if (Clobber.isInvalid())
Anders Carlsson19fe1162008-02-05 23:03:50 +00001218 break;
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001219
1220 Clobbers.push_back(Clobber.release());
1221
Anders Carlsson19fe1162008-02-05 23:03:50 +00001222 if (Tok.isNot(tok::comma)) break;
1223 ConsumeToken();
1224 }
Chris Lattner0116c472006-08-15 06:03:28 +00001225 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001226
Anders Carlsson19fe1162008-02-05 23:03:50 +00001227 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner0116c472006-08-15 06:03:28 +00001228 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001229
Sebastian Redl24b8e152009-01-18 16:53:17 +00001230 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foad7d0479f2009-05-21 09:52:38 +00001231 NumOutputs, NumInputs, Names.data(),
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001232 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001233 move(AsmString), move_arg(Clobbers),
Sebastian Redl24b8e152009-01-18 16:53:17 +00001234 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001235}
1236
1237/// ParseAsmOperands - Parse the asm-operands production as used by
1238/// asm-statement. We also parse a leading ':' token. If the leading colon is
1239/// not present, we do not parse anything.
1240///
1241/// [GNU] asm-operands:
1242/// asm-operand
1243/// asm-operands ',' asm-operand
1244///
1245/// [GNU] asm-operand:
1246/// asm-string-literal '(' expression ')'
1247/// '[' identifier ']' asm-string-literal '(' expression ')'
1248///
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001249bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001250 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1251 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001252 // Only do anything if this operand is present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001253 if (Tok.isNot(tok::colon)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001254 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001255
Chris Lattner0116c472006-08-15 06:03:28 +00001256 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001257 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001258 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001259
1260 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001261 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001262 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001263 SourceLocation Loc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00001264
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001265 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001266 Diag(Tok, diag::err_expected_ident);
1267 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001268 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001269 }
Mike Stump11289f42009-09-09 15:08:12 +00001270
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001271 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001272 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001273
1274 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner0116c472006-08-15 06:03:28 +00001275 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001276 } else
1277 Names.push_back(std::string());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001278
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001279 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001280 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001281 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001282 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001283 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001284 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001285
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001286 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001287 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001288 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001289 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001290 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001291
Chris Lattner0116c472006-08-15 06:03:28 +00001292 // Read the parenthesized expression.
Eli Friedman47e78572009-05-03 07:49:42 +00001293 SourceLocation OpenLoc = ConsumeParen();
1294 OwningExprResult Res(ParseExpression());
1295 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001296 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001297 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001298 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001299 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001300 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001301 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001302 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001303 ConsumeToken();
1304 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001305
1306 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001307}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001308
Chris Lattner83f095c2009-03-28 19:18:32 +00001309Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001310 assert(Tok.is(tok::l_brace));
1311 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001312
Chris Lattnereae6cb62009-03-05 08:00:35 +00001313 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1314 PP.getSourceManager(),
1315 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001316
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001317 // Do not enter a scope for the brace, as the arguments are in the same scope
1318 // (the function body) as the body itself. Instead, just read the statement
1319 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl042ad952008-12-11 19:30:53 +00001320 OwningStmtResult FnBody(ParseCompoundStatementBody());
1321
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001322 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001323 if (FnBody.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00001324 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner12f2ea52009-03-05 00:49:17 +00001325 MultiStmtArg(Actions), false);
Sebastian Redl042ad952008-12-11 19:30:53 +00001326
Sebastian Redl726a0d92009-02-05 15:02:23 +00001327 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001328}
Sebastian Redlb219c902008-12-21 16:41:36 +00001329
Sebastian Redla7b98a72009-04-26 20:35:05 +00001330/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1331///
1332/// function-try-block:
1333/// 'try' ctor-initializer[opt] compound-statement handler-seq
1334///
1335Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1336 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1337 SourceLocation TryLoc = ConsumeToken();
1338
1339 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1340 PP.getSourceManager(),
1341 "parsing function try block");
1342
1343 // Constructor initializer list?
1344 if (Tok.is(tok::colon))
1345 ParseConstructorInitializer(Decl);
1346
Sebastian Redld98ecd62009-04-26 21:08:36 +00001347 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001348 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1349 // If we failed to parse the try-catch, we just give the function an empty
1350 // compound statement as the body.
1351 if (FnBody.isInvalid())
Sebastian Redld98ecd62009-04-26 21:08:36 +00001352 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redla7b98a72009-04-26 20:35:05 +00001353 MultiStmtArg(Actions), false);
1354
1355 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1356}
1357
Sebastian Redlb219c902008-12-21 16:41:36 +00001358/// ParseCXXTryBlock - Parse a C++ try-block.
1359///
1360/// try-block:
1361/// 'try' compound-statement handler-seq
1362///
Sebastian Redlb219c902008-12-21 16:41:36 +00001363Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1364 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1365
1366 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001367 return ParseCXXTryBlockCommon(TryLoc);
1368}
1369
1370/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1371/// function-try-block.
1372///
1373/// try-block:
1374/// 'try' compound-statement handler-seq
1375///
1376/// function-try-block:
1377/// 'try' ctor-initializer[opt] compound-statement handler-seq
1378///
1379/// handler-seq:
1380/// handler handler-seq[opt]
1381///
1382Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001383 if (Tok.isNot(tok::l_brace))
1384 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1385 OwningStmtResult TryBlock(ParseCompoundStatement());
1386 if (TryBlock.isInvalid())
1387 return move(TryBlock);
1388
1389 StmtVector Handlers(Actions);
1390 if (Tok.isNot(tok::kw_catch))
1391 return StmtError(Diag(Tok, diag::err_expected_catch));
1392 while (Tok.is(tok::kw_catch)) {
1393 OwningStmtResult Handler(ParseCXXCatchBlock());
1394 if (!Handler.isInvalid())
1395 Handlers.push_back(Handler.release());
1396 }
1397 // Don't bother creating the full statement if we don't have any usable
1398 // handlers.
1399 if (Handlers.empty())
1400 return StmtError();
1401
Sebastian Redl726a0d92009-02-05 15:02:23 +00001402 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redlb219c902008-12-21 16:41:36 +00001403}
1404
1405/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1406///
1407/// handler:
1408/// 'catch' '(' exception-declaration ')' compound-statement
1409///
1410/// exception-declaration:
1411/// type-specifier-seq declarator
1412/// type-specifier-seq abstract-declarator
1413/// type-specifier-seq
1414/// '...'
1415///
1416Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1417 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1418
1419 SourceLocation CatchLoc = ConsumeToken();
1420
1421 SourceLocation LParenLoc = Tok.getLocation();
1422 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1423 return StmtError();
1424
1425 // C++ 3.3.2p3:
1426 // The name in a catch exception-declaration is local to the handler and
1427 // shall not be redeclared in the outermost block of the handler.
1428 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1429
1430 // exception-declaration is equivalent to '...' or a parameter-declaration
1431 // without default arguments.
Chris Lattner83f095c2009-03-28 19:18:32 +00001432 DeclPtrTy ExceptionDecl;
Sebastian Redlb219c902008-12-21 16:41:36 +00001433 if (Tok.isNot(tok::ellipsis)) {
1434 DeclSpec DS;
Sebastian Redl54c04d42008-12-22 19:15:10 +00001435 if (ParseCXXTypeSpecifierSeq(DS))
1436 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00001437 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1438 ParseDeclarator(ExDecl);
1439 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1440 } else
1441 ConsumeToken();
1442
1443 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1444 return StmtError();
1445
1446 if (Tok.isNot(tok::l_brace))
1447 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1448
1449 OwningStmtResult Block(ParseCompoundStatement());
1450 if (Block.isInvalid())
1451 return move(Block);
1452
Sebastian Redl726a0d92009-02-05 15:02:23 +00001453 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redlb219c902008-12-21 16:41:36 +00001454}