blob: 4a7bd57d786a347b2d03a8c224f44e7e9d8c4ded [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner39146d62008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// [GNU] asm-statement
46/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
53/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
62/// expression-statement:
63/// expression[opt] ';'
64///
65/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
70/// [GNU] 'goto' '*' expression ';'
71///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
74/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000075///
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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Jahanian397fcc12007-09-19 19:14:32 +000084 tok::TokenKind Kind = Tok.getKind();
85 SourceLocation AtLoc;
86 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000090 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092
Argyrios Kyrtzidisb9f930d2008-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 Lattnerf919bfe2009-03-24 17:04:48 +0000100 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000101 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-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 Lattnerf919bfe2009-03-24 17:04:48 +0000105 }
106
107 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000109 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 }
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000111
112 // 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 Carlsson6b1d2832009-05-17 21:11:30 +0000123 return Actions.ActOnExprStmt(Actions.FullExpr(Expr));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000124 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000125
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 case tok::kw_case: // C99 6.8.1: labeled-statement
127 return ParseCaseStatement();
128 case tok::kw_default: // C99 6.8.1: labeled-statement
129 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 case tok::l_brace: // C99 6.8.2: compound-statement
132 return ParseCompoundStatement();
133 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000134 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000135
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 case tok::kw_if: // C99 6.8.4.1: if-statement
137 return ParseIfStatement();
138 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000139 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000142 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case tok::kw_do: // C99 6.8.5.2: do-statement
144 Res = ParseDoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000145 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 break;
147 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000148 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000149
150 case tok::kw_goto: // C99 6.8.6.1: goto-statement
151 Res = ParseGotoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000152 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 break;
154 case tok::kw_continue: // C99 6.8.6.2: continue-statement
155 Res = ParseContinueStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000156 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 break;
158 case tok::kw_break: // C99 6.8.6.3: break-statement
159 Res = ParseBreakStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000160 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 break;
162 case tok::kw_return: // C99 6.8.6.4: return-statement
163 Res = ParseReturnStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000164 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000166
Sebastian Redla0fd8652008-12-21 16:41:36 +0000167 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000168 bool msAsm = false;
169 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000170 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000171 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 break;
173 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000174
Sebastian Redla0fd8652008-12-21 16:41:36 +0000175 case tok::kw_try: // C++ 15: try-block
176 return ParseCXXTryBlock();
177 }
178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000180 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000182 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-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 Lattner19504402008-11-13 18:52:53 +0000187 // Skip until we see a } or ;, but don't eat it.
188 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 }
Chris Lattner7b3684a2009-06-14 00:23:56 +0000190
Sebastian Redl61364dd2008-12-11 19:30:53 +0000191 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000194/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000195///
196/// labeled-statement:
197/// identifier ':' statement
198/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000199///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000200Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-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 Redl61364dd2008-12-11 19:30:53 +0000208
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000209 // identifier ':' statement
210 SourceLocation ColonLoc = ConsumeToken();
211
212 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000213 Action::AttrTy *AttrList = 0;
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000214 if (Tok.is(tok::kw___attribute))
215 // TODO: save these somewhere.
216 AttrList = ParseAttributes();
217
Sebastian Redl61364dd2008-12-11 19:30:53 +0000218 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000219
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000220 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000221 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000222 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000223
Sebastian Redlde307472009-01-11 00:38:46 +0000224 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
225 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000226 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000227}
Reid Spencer5f016e22007-07-11 17:01:13 +0000228
229/// ParseCaseStatement
230/// labeled-statement:
231/// 'case' constant-expression ':' statement
232/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
233///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000234Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000235 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattner24e1e702009-03-04 04:23:07 +0000236
237 // 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 Lattner26140c62009-03-04 18:24:58 +0000247 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-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 :).
250
251 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
252 // example above.
253 OwningStmtResult TopLevelCase(Actions, true);
254
255 // 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;
259
260 // While we have case statements, eat and stack them.
261 do {
262 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
263
264 OwningExprResult LHS(ParseConstantExpression());
265 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000267 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000269
Chris Lattner24e1e702009-03-04 04:23:07 +0000270 // GNU case range extension.
271 SourceLocation DotDotDotLoc;
272 OwningExprResult RHS(Actions);
273 if (Tok.is(tok::ellipsis)) {
274 Diag(Tok, diag::ext_gnu_case_range);
275 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000276
Chris Lattner24e1e702009-03-04 04:23:07 +0000277 RHS = ParseConstantExpression();
278 if (RHS.isInvalid()) {
279 SkipUntil(tok::colon);
280 return StmtError();
281 }
282 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000283
Chris Lattner24e1e702009-03-04 04:23:07 +0000284 if (Tok.isNot(tok::colon)) {
285 Diag(Tok, diag::err_expected_colon_after) << "'case'";
286 SkipUntil(tok::colon);
287 return StmtError();
288 }
289
290 SourceLocation ColonLoc = ConsumeToken();
291
292 OwningStmtResult Case =
293 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
294 move(RHS), ColonLoc);
295
296 // If we had a sema error parsing this case, then just ignore it and
297 // continue parsing the sub-stmt.
298 if (Case.isInvalid()) {
299 if (TopLevelCase.isInvalid()) // No parsed case stmts.
300 return ParseStatement();
301 // Otherwise, just don't add it as a nested case.
302 } else {
303 // If this is the first case statement we parsed, it becomes TopLevelCase.
304 // Otherwise we link it into the current chain.
305 StmtTy *NextDeepest = Case.get();
306 if (TopLevelCase.isInvalid())
307 TopLevelCase = move(Case);
308 else
309 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
310 DeepestParsedCaseStmt = NextDeepest;
311 }
312
313 // Handle all case statements.
314 } while (Tok.is(tok::kw_case));
315
316 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
317
318 // If we found a non-case statement, start by parsing it.
319 OwningStmtResult SubStmt(Actions);
320
321 if (Tok.isNot(tok::r_brace)) {
322 SubStmt = ParseStatement();
323 } else {
324 // Nicely diagnose the common error "switch (X) { case 4: }", which is
325 // not valid.
326 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000328 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 }
Chris Lattner24e1e702009-03-04 04:23:07 +0000330
331 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000332 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000333 SubStmt = Actions.ActOnNullStmt(SourceLocation());
334
335 // Install the body into the most deeply-nested case.
336 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000337
Chris Lattner24e1e702009-03-04 04:23:07 +0000338 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000339 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000340}
341
342/// ParseDefaultStatement
343/// labeled-statement:
344/// 'default' ':' statement
345/// Note that this does not parse the 'statement' at the end.
346///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000347Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000348 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
350
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000351 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000352 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000354 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000358
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000360 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000362 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
364
Sebastian Redl61364dd2008-12-11 19:30:53 +0000365 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000366 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000367 return StmtError();
368
Sebastian Redl117054a2008-12-28 16:13:43 +0000369 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000370 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
373
374/// ParseCompoundStatement - Parse a "{}" block.
375///
376/// compound-statement: [C99 6.8.2]
377/// { block-item-list[opt] }
378/// [GNU] { label-declarations block-item-list } [TODO]
379///
380/// block-item-list:
381/// block-item
382/// block-item-list block-item
383///
384/// block-item:
385/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000386/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000387/// statement
388/// [OMP] openmp-directive [TODO]
389///
390/// [GNU] label-declarations:
391/// [GNU] label-declaration
392/// [GNU] label-declarations label-declaration
393///
394/// [GNU] label-declaration:
395/// [GNU] '__label__' identifier-list ';'
396///
397/// [OMP] openmp-directive: [TODO]
398/// [OMP] barrier-directive
399/// [OMP] flush-directive
400///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000401Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000402 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000403
Chris Lattner31e05722007-08-26 06:24:45 +0000404 // Enter a scope to hold everything within the compound stmt. Compound
405 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000406 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000407
408 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000409 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000410}
411
412
413/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000414/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000415/// consume the '}' at the end of the block. It does not manipulate the scope
416/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000417Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerae50fa02009-03-05 00:00:31 +0000418 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
419 Tok.getLocation(),
420 "in compound statement ('{}')");
421
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
423
424 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000425 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000426
427 typedef StmtVector StmtsTy;
428 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000429 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000430 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000431 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000432 R = ParseStatementOrDeclaration(false);
433 } else {
434 // __extension__ can start declarations and it can also be a unary
435 // operator for expressions. Consume multiple __extension__ markers here
436 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000437 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000438 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000439 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000440 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000441
Chris Lattner45a566c2007-08-27 01:01:57 +0000442 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000443 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000444 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000445 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000446 ExtensionRAIIObject O(Diags);
447
Chris Lattner97144fc2009-04-02 04:16:50 +0000448 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
449 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext,DeclEnd);
450 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000451 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000452 // Otherwise this was a unary __extension__ marker.
453 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000454
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000455 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000456 SkipUntil(tok::semi);
457 continue;
458 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000459
Chris Lattner39146d62008-10-20 06:51:33 +0000460 // Eat the semicolon at the end of stmt and convert the expr into a
461 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000462 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000463 R = Actions.ActOnExprStmt(Actions.FullExpr(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000464 }
465 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000466
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000467 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000468 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000470
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000472 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000474 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000478 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000479 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000480}
481
Chris Lattner15ff1112008-12-12 06:31:07 +0000482/// ParseParenExprOrCondition:
483/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000484/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000485///
486/// This function parses and performs error recovery on the specified condition
487/// or expression (depending on whether we're in C++ or C mode). This function
488/// goes out of its way to recover well. It returns true if there was a parser
489/// error (the right paren couldn't be found), which indicates that the caller
490/// should try to recover harder. It returns false if the condition is
491/// successfully parsed. Note that a successful parse can still have semantic
492/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000493bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
Chris Lattner98913592009-06-12 23:04:47 +0000494 bool OnlyAllowCondition,
495 SourceLocation *LParenLocPtr,
496 SourceLocation *RParenLocPtr) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000497 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner98913592009-06-12 23:04:47 +0000498 if (LParenLocPtr) *LParenLocPtr = LParenLoc;
Chris Lattner15ff1112008-12-12 06:31:07 +0000499
500 if (getLang().CPlusPlus)
501 CondExp = ParseCXXCondition();
502 else
503 CondExp = ParseExpression();
504
505 // If the parser was confused by the condition and we don't have a ')', try to
506 // recover by skipping ahead to a semi and bailing out. If condexp is
507 // semantically invalid but we have well formed code, keep going.
508 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
509 SkipUntil(tok::semi);
510 // Skipping may have stopped if it found the containing ')'. If so, we can
511 // continue parsing the if statement.
512 if (Tok.isNot(tok::r_paren))
513 return true;
514 }
515
516 // Otherwise the condition is valid or the rparen is present.
Chris Lattner98913592009-06-12 23:04:47 +0000517 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
518 if (RParenLocPtr) *RParenLocPtr = RPLoc;
Chris Lattner15ff1112008-12-12 06:31:07 +0000519 return false;
520}
521
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523/// ParseIfStatement
524/// if-statement: [C99 6.8.4.1]
525/// 'if' '(' expression ')' statement
526/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000527/// [C++] 'if' '(' condition ')' statement
528/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000529///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000530Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000531 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
533
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000534 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000535 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000537 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000539
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000540 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
541
Chris Lattner22153252007-08-26 23:08:06 +0000542 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
543 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000544 //
545 // C++ 6.4p3:
546 // A name introduced by a declaration in a condition is in scope from its
547 // point of declaration until the end of the substatements controlled by the
548 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000549 // C++ 3.3.2p4:
550 // Names declared in the for-init-statement, and in the condition of if,
551 // while, for, and switch statements are local to the if, while, for, or
552 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000553 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000554 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000557 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000558 if (ParseParenExprOrCondition(CondExp))
559 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000560
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000561 FullExprArg FullCondExp(Actions.FullExpr(CondExp));
562
Chris Lattner0ecea032007-08-22 05:28:50 +0000563 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000564 // there is no compound stmt. C90 does not have this clause. We only do this
565 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000566 //
567 // C++ 6.4p1:
568 // The substatement in a selection-statement (each substatement, in the else
569 // form of the if statement) implicitly defines a local scope.
570 //
571 // For C++ we create a scope for the condition and a new scope for
572 // substatements because:
573 // -When the 'then' scope exits, we want the condition declaration to still be
574 // active for the 'else' scope too.
575 // -Sema will detect name clashes by considering declarations of a
576 // 'ControlScope' as part of its direct subscope.
577 // -If we wanted the condition and substatement to be in the same scope, we
578 // would have to notify ParseStatement not to create a new scope. It's
579 // simpler to let it create a new scope.
580 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000581 ParseScope InnerScope(this, Scope::DeclScope,
582 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000583
Chris Lattnerb96728d2007-10-29 05:08:52 +0000584 // Read the 'then' stmt.
585 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000586 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000587
Chris Lattnera36ce712007-08-22 05:16:28 +0000588 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000589 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 // If it has an else, parse it.
592 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000593 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000594 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000595
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000596 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000598
Chris Lattner0ecea032007-08-22 05:28:50 +0000599 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000600 // there is no compound stmt. C90 does not have this clause. We only do
601 // this if the body isn't a compound statement to avoid push/pop in common
602 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000603 //
604 // C++ 6.4p1:
605 // The substatement in a selection-statement (each substatement, in the else
606 // form of the if statement) implicitly defines a local scope.
607 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000608 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000609 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000610
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000611 bool WithinElse = CurScope->isWithinElse();
612 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000613 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000615 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000616
617 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000618 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000620
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000621 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000622
623 // If the condition was invalid, discard the if statement. We could recover
624 // better by replacing it with a valid expr, but don't do that yet.
625 if (CondExp.isInvalid())
626 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000627
Chris Lattnerb96728d2007-10-29 05:08:52 +0000628 // If the then or else stmt is invalid and the other is valid (and present),
629 // make turn the invalid one into a null stmt to avoid dropping the other
630 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000631 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
632 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
633 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000634 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000635 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000636 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000637
Chris Lattnerb96728d2007-10-29 05:08:52 +0000638 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000639 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000640 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000641 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000642 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000643
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000644 return Actions.ActOnIfStmt(IfLoc, FullCondExp, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000645 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000646}
647
648/// ParseSwitchStatement
649/// switch-statement:
650/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000651/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000652Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000653 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
655
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000656 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000657 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000659 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 }
Chris Lattner22153252007-08-26 23:08:06 +0000661
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000662 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
663
Chris Lattner22153252007-08-26 23:08:06 +0000664 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
665 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000666 //
667 // C++ 6.4p3:
668 // A name introduced by a declaration in a condition is in scope from its
669 // point of declaration until the end of the substatements controlled by the
670 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000671 // C++ 3.3.2p4:
672 // Names declared in the for-init-statement, and in the condition of if,
673 // while, for, and switch statements are local to the if, while, for, or
674 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000675 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000676 unsigned ScopeFlags = Scope::BreakScope;
677 if (C99orCXX)
678 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000679 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000680
681 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000682 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000683 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000684 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000685
686 OwningStmtResult Switch(Actions);
687 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000688 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000689
Chris Lattner0ecea032007-08-22 05:28:50 +0000690 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000691 // there is no compound stmt. C90 does not have this clause. We only do this
692 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000693 //
694 // C++ 6.4p1:
695 // The substatement in a selection-statement (each substatement, in the else
696 // form of the if statement) implicitly defines a local scope.
697 //
698 // See comments in ParseIfStatement for why we create a scope for the
699 // condition and a new scope for substatement in C++.
700 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000701 ParseScope InnerScope(this, Scope::DeclScope,
702 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000703
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000705 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000706
Chris Lattner0ecea032007-08-22 05:28:50 +0000707 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000708 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000709
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000710 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000711 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000712 // FIXME: Remove the case statement list from the Switch statement.
713 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000714
715 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000716
Chris Lattner15ff1112008-12-12 06:31:07 +0000717 if (Cond.isInvalid())
718 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000719
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000720 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000721}
722
723/// ParseWhileStatement
724/// while-statement: [C99 6.8.5.1]
725/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000726/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000727Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000728 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 SourceLocation WhileLoc = Tok.getLocation();
730 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000731
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000732 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000733 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000735 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000737
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000738 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
739
Chris Lattner22153252007-08-26 23:08:06 +0000740 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
741 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000742 //
743 // C++ 6.4p3:
744 // A name introduced by a declaration in a condition is in scope from its
745 // point of declaration until the end of the substatements controlled by the
746 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000747 // C++ 3.3.2p4:
748 // Names declared in the for-init-statement, and in the condition of if,
749 // while, for, and switch statements are local to the if, while, for, or
750 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000751 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000752 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000753 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000754 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
755 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000756 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000757 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
758 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000759
760 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000761 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000762 if (ParseParenExprOrCondition(Cond))
763 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000764
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000765 FullExprArg FullCond(Actions.FullExpr(Cond));
766
Chris Lattner0ecea032007-08-22 05:28:50 +0000767 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000768 // there is no compound stmt. C90 does not have this clause. We only do this
769 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000770 //
771 // C++ 6.5p2:
772 // The substatement in an iteration-statement implicitly defines a local scope
773 // which is entered and exited each time through the loop.
774 //
775 // See comments in ParseIfStatement for why we create a scope for the
776 // condition and a new scope for substatement in C++.
777 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000778 ParseScope InnerScope(this, Scope::DeclScope,
779 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000782 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000783
Chris Lattner0ecea032007-08-22 05:28:50 +0000784 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000785 InnerScope.Exit();
786 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000787
788 if (Cond.isInvalid() || Body.isInvalid())
789 return StmtError();
790
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000791 return Actions.ActOnWhileStmt(WhileLoc, FullCond, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000792}
793
794/// ParseDoStatement
795/// do-statement: [C99 6.8.5.2]
796/// 'do' statement 'while' '(' expression ')' ';'
797/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000798Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000799 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000801
Chris Lattner22153252007-08-26 23:08:06 +0000802 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
803 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000804 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000805 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000806 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000807 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000808 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000809
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000810 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000811
Chris Lattner0ecea032007-08-22 05:28:50 +0000812 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000813 // there is no compound stmt. C90 does not have this clause. We only do this
814 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000815 //
816 // C++ 6.5p2:
817 // The substatement in an iteration-statement implicitly defines a local scope
818 // which is entered and exited each time through the loop.
819 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000820 ParseScope InnerScope(this, Scope::DeclScope,
821 (getLang().C99 || getLang().CPlusPlus) &&
822 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000823
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000825 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000826
Chris Lattner0ecea032007-08-22 05:28:50 +0000827 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000828 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000829
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000830 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000831 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000832 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000833 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000834 SkipUntil(tok::semi, false, true);
835 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000836 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 }
838 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000839
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000840 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000841 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000842 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000843 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000845
Chris Lattnerff871fb2008-12-12 06:35:28 +0000846 // Parse the parenthesized condition.
847 OwningExprResult Cond(Actions);
Chris Lattner98913592009-06-12 23:04:47 +0000848 SourceLocation LPLoc, RPLoc;
849 ParseParenExprOrCondition(Cond, true, &LPLoc, &RPLoc);
Chris Lattnerff871fb2008-12-12 06:35:28 +0000850
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000851 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000852
Sebastian Redl9a920342008-12-11 19:48:14 +0000853 if (Cond.isInvalid() || Body.isInvalid())
854 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000855
Chris Lattner98913592009-06-12 23:04:47 +0000856 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
857 move(Cond), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000858}
859
860/// ParseForStatement
861/// for-statement: [C99 6.8.5.3]
862/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
863/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000864/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
865/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000866/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
867/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000868///
869/// [C++] for-init-statement:
870/// [C++] expression-statement
871/// [C++] simple-declaration
872///
Sebastian Redl9a920342008-12-11 19:48:14 +0000873Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000874 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000876
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000877 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000878 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000880 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000882
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000883 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000884
Chris Lattner22153252007-08-26 23:08:06 +0000885 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
886 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000887 //
888 // C++ 6.4p3:
889 // A name introduced by a declaration in a condition is in scope from its
890 // point of declaration until the end of the substatements controlled by the
891 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000892 // C++ 3.3.2p4:
893 // Names declared in the for-init-statement, and in the condition of if,
894 // while, for, and switch statements are local to the if, while, for, or
895 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000896 // C++ 6.5.3p1:
897 // Names declared in the for-init-statement are in the same declarative-region
898 // as those declared in the condition.
899 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000900 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000901 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000902 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
903 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000904 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000905 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
906
907 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000908
909 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000910 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000911
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000912 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000913 OwningStmtResult FirstPart(Actions);
914 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000915
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000917 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 // no first part, eat the ';'.
919 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000920 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000922 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000924
Chris Lattner97144fc2009-04-02 04:16:50 +0000925 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
926 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
927 false);
Chris Lattnercd147752009-03-29 17:27:48 +0000928 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
929
930 if (Tok.is(tok::semi)) { // for (int x = 4;
931 ConsumeToken();
932 } else if ((ForEach = isTokIdentifier_in())) {
933 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000934 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000935 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000936 } else {
937 Diag(Tok, diag::err_expected_semi_for);
938 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000939 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 } else {
941 Value = ParseExpression();
942
943 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000944 if (!Value.isInvalid())
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000945 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000946
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000947 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000949 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000950 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000951 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000952 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000953 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 SkipUntil(tok::semi);
955 }
956 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000957 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000958 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000959 // Parse the second part of the for specifier.
960 if (Tok.is(tok::semi)) { // for (...;;
961 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000962 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000963 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000964 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000965
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000966 if (Tok.is(tok::semi)) {
967 ConsumeToken();
968 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000969 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000970 SkipUntil(tok::semi);
971 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000972
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000973 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000974 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000975 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 // Match the ')'.
978 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000979
Chris Lattner0ecea032007-08-22 05:28:50 +0000980 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000981 // there is no compound stmt. C90 does not have this clause. We only do this
982 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000983 //
984 // C++ 6.5p2:
985 // The substatement in an iteration-statement implicitly defines a local scope
986 // which is entered and exited each time through the loop.
987 //
988 // See comments in ParseIfStatement for why we create a scope for
989 // for-init-statement/condition and a new scope for substatement in C++.
990 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000991 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000992 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000993
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000995 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000996
Chris Lattner0ecea032007-08-22 05:28:50 +0000997 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000998 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000999
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001001 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001002
1003 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001004 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001005
1006 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001007 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
1008 move(SecondPart), move(ThirdPart),
1009 RParenLoc, move(Body));
Chris Lattner682bf922009-03-29 16:50:03 +00001010
1011 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1012 move(FirstPart),
1013 move(SecondPart),
1014 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001015}
1016
1017/// ParseGotoStatement
1018/// jump-statement:
1019/// 'goto' identifier ';'
1020/// [GNU] 'goto' '*' expression ';'
1021///
1022/// Note: this lets the caller parse the end ';'.
1023///
Sebastian Redl9a920342008-12-11 19:48:14 +00001024Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001025 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001027
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001028 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001029 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001030 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 Tok.getIdentifierInfo());
1032 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001033 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 // GNU indirect goto extension.
1035 Diag(Tok, diag::ext_gnu_indirect_goto);
1036 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001037 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001038 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001040 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001042 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001043 } else {
1044 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001045 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001047
Sebastian Redl9a920342008-12-11 19:48:14 +00001048 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049}
1050
1051/// ParseContinueStatement
1052/// jump-statement:
1053/// 'continue' ';'
1054///
1055/// Note: this lets the caller parse the end ';'.
1056///
Sebastian Redl9a920342008-12-11 19:48:14 +00001057Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001059 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001060}
1061
1062/// ParseBreakStatement
1063/// jump-statement:
1064/// 'break' ';'
1065///
1066/// Note: this lets the caller parse the end ';'.
1067///
Sebastian Redl9a920342008-12-11 19:48:14 +00001068Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001070 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001071}
1072
1073/// ParseReturnStatement
1074/// jump-statement:
1075/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001076Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001077 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001079
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001080 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001081 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001083 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001085 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 }
1087 }
Anders Carlssonf53b4432009-08-18 16:11:00 +00001088 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001089}
1090
Steve Naroff5f8aa692008-02-11 23:15:56 +00001091/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1092/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001093Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001094 if (Tok.is(tok::l_brace)) {
1095 unsigned short savedBraceCount = BraceCount;
1096 do {
1097 ConsumeAnyToken();
1098 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1099 } else {
1100 // From the MS website: If used without braces, the __asm keyword means
1101 // that the rest of the line is an assembly-language statement.
1102 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001103 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001104 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001105 do {
1106 ConsumeAnyToken();
1107 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001108 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001109 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1110 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001111 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001112 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001113}
1114
Reid Spencer5f016e22007-07-11 17:01:13 +00001115/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001116/// asm-statement:
1117/// gnu-asm-statement
1118/// ms-asm-statement
1119///
1120/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001121/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1122///
1123/// [GNU] asm-argument:
1124/// asm-string-literal
1125/// asm-string-literal ':' asm-operands[opt]
1126/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1127/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1128/// ':' asm-clobbers
1129///
1130/// [GNU] asm-clobbers:
1131/// asm-string-literal
1132/// asm-clobbers ',' asm-string-literal
1133///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001134/// [MS] ms-asm-statement:
1135/// '__asm' assembly-instruction ';'[opt]
1136/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1137///
1138/// [MS] assembly-instruction-list:
1139/// assembly-instruction ';'[opt]
1140/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1141///
Sebastian Redl9a920342008-12-11 19:48:14 +00001142Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001143 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001144 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001145
Steve Naroff5f8aa692008-02-11 23:15:56 +00001146 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001147 msAsm = true;
1148 return FuzzyParseMicrosoftAsmStatement();
1149 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 DeclSpec DS;
1151 SourceLocation Loc = Tok.getLocation();
1152 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1155 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001156 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001158 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001161 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001162 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001163 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001164 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001166 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 }
1168 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001169
Sebastian Redleffa8d12008-12-10 00:02:53 +00001170 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001171 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001172 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001173
Anders Carlssonb235fc22007-11-22 01:36:19 +00001174 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001175 ExprVector Constraints(Actions);
1176 ExprVector Exprs(Actions);
1177 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178
Anders Carlssondfab34a2008-02-05 23:03:50 +00001179 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001180
Anders Carlssondfab34a2008-02-05 23:03:50 +00001181 SourceLocation RParenLoc;
1182 if (Tok.is(tok::r_paren)) {
1183 // We have a simple asm expression
1184 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001185
Anders Carlssondfab34a2008-02-05 23:03:50 +00001186 RParenLoc = ConsumeParen();
1187 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001188 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001189 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001190 return StmtError();
1191
Anders Carlssondfab34a2008-02-05 23:03:50 +00001192 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001193
Anders Carlssondfab34a2008-02-05 23:03:50 +00001194 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001195 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001196 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001197
Anders Carlssondfab34a2008-02-05 23:03:50 +00001198 assert(Names.size() == Constraints.size() &&
1199 Constraints.size() == Exprs.size()
1200 && "Input operand size mismatch!");
1201
1202 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001203
Anders Carlssondfab34a2008-02-05 23:03:50 +00001204 // Parse the clobbers, if present.
1205 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001206 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001207
Anders Carlssondfab34a2008-02-05 23:03:50 +00001208 // Parse the asm-string list for clobbers.
1209 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001210 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001211
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001212 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001213 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001214
1215 Clobbers.push_back(Clobber.release());
1216
Anders Carlssondfab34a2008-02-05 23:03:50 +00001217 if (Tok.isNot(tok::comma)) break;
1218 ConsumeToken();
1219 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001221
Anders Carlssondfab34a2008-02-05 23:03:50 +00001222 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001224
Sebastian Redl3037ed02009-01-18 16:53:17 +00001225 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001226 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001227 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001228 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001229 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230}
1231
1232/// ParseAsmOperands - Parse the asm-operands production as used by
1233/// asm-statement. We also parse a leading ':' token. If the leading colon is
1234/// not present, we do not parse anything.
1235///
1236/// [GNU] asm-operands:
1237/// asm-operand
1238/// asm-operands ',' asm-operand
1239///
1240/// [GNU] asm-operand:
1241/// asm-string-literal '(' expression ')'
1242/// '[' identifier ']' asm-string-literal '(' expression ')'
1243///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001244bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001245 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1246 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001248 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 ConsumeToken();
1250
1251 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001252 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001253 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001254
Anders Carlssonb235fc22007-11-22 01:36:19 +00001255 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001257 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 SourceLocation Loc = ConsumeBracket();
1259
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001260 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 Diag(Tok, diag::err_expected_ident);
1262 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001263 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 }
Chris Lattner69efba72007-10-29 04:06:22 +00001265
Anders Carlssonb235fc22007-11-22 01:36:19 +00001266 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001267 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001268
1269 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001271 } else
1272 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001273
Sebastian Redleffa8d12008-12-10 00:02:53 +00001274 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001275 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001276 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001277 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001278 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001279 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001280
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001281 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001282 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001284 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001286
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001288 SourceLocation OpenLoc = ConsumeParen();
1289 OwningExprResult Res(ParseExpression());
1290 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001291 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001293 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001295 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001297 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 ConsumeToken();
1299 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001300
1301 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001302}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001303
Chris Lattnerb28317a2009-03-28 19:18:32 +00001304Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001305 assert(Tok.is(tok::l_brace));
1306 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001307
Chris Lattner49f28ca2009-03-05 08:00:35 +00001308 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1309 PP.getSourceManager(),
1310 "parsing function body");
Chris Lattner21ff9c92009-03-05 01:25:28 +00001311
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001312 // Do not enter a scope for the brace, as the arguments are in the same scope
1313 // (the function body) as the body itself. Instead, just read the statement
1314 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001315 OwningStmtResult FnBody(ParseCompoundStatementBody());
1316
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001317 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001318 if (FnBody.isInvalid())
Chris Lattner40e9bc82009-03-05 00:49:17 +00001319 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
1320 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001321
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001322 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001323}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001324
Sebastian Redld3a413d2009-04-26 20:35:05 +00001325/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1326///
1327/// function-try-block:
1328/// 'try' ctor-initializer[opt] compound-statement handler-seq
1329///
1330Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1331 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1332 SourceLocation TryLoc = ConsumeToken();
1333
1334 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1335 PP.getSourceManager(),
1336 "parsing function try block");
1337
1338 // Constructor initializer list?
1339 if (Tok.is(tok::colon))
1340 ParseConstructorInitializer(Decl);
1341
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001342 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001343 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1344 // If we failed to parse the try-catch, we just give the function an empty
1345 // compound statement as the body.
1346 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001347 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001348 MultiStmtArg(Actions), false);
1349
1350 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1351}
1352
Sebastian Redla0fd8652008-12-21 16:41:36 +00001353/// ParseCXXTryBlock - Parse a C++ try-block.
1354///
1355/// try-block:
1356/// 'try' compound-statement handler-seq
1357///
Sebastian Redla0fd8652008-12-21 16:41:36 +00001358Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1359 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1360
1361 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001362 return ParseCXXTryBlockCommon(TryLoc);
1363}
1364
1365/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1366/// function-try-block.
1367///
1368/// try-block:
1369/// 'try' compound-statement handler-seq
1370///
1371/// function-try-block:
1372/// 'try' ctor-initializer[opt] compound-statement handler-seq
1373///
1374/// handler-seq:
1375/// handler handler-seq[opt]
1376///
1377Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001378 if (Tok.isNot(tok::l_brace))
1379 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1380 OwningStmtResult TryBlock(ParseCompoundStatement());
1381 if (TryBlock.isInvalid())
1382 return move(TryBlock);
1383
1384 StmtVector Handlers(Actions);
1385 if (Tok.isNot(tok::kw_catch))
1386 return StmtError(Diag(Tok, diag::err_expected_catch));
1387 while (Tok.is(tok::kw_catch)) {
1388 OwningStmtResult Handler(ParseCXXCatchBlock());
1389 if (!Handler.isInvalid())
1390 Handlers.push_back(Handler.release());
1391 }
1392 // Don't bother creating the full statement if we don't have any usable
1393 // handlers.
1394 if (Handlers.empty())
1395 return StmtError();
1396
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001397 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001398}
1399
1400/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1401///
1402/// handler:
1403/// 'catch' '(' exception-declaration ')' compound-statement
1404///
1405/// exception-declaration:
1406/// type-specifier-seq declarator
1407/// type-specifier-seq abstract-declarator
1408/// type-specifier-seq
1409/// '...'
1410///
1411Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1412 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1413
1414 SourceLocation CatchLoc = ConsumeToken();
1415
1416 SourceLocation LParenLoc = Tok.getLocation();
1417 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1418 return StmtError();
1419
1420 // C++ 3.3.2p3:
1421 // The name in a catch exception-declaration is local to the handler and
1422 // shall not be redeclared in the outermost block of the handler.
1423 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1424
1425 // exception-declaration is equivalent to '...' or a parameter-declaration
1426 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001427 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001428 if (Tok.isNot(tok::ellipsis)) {
1429 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001430 if (ParseCXXTypeSpecifierSeq(DS))
1431 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001432 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1433 ParseDeclarator(ExDecl);
1434 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1435 } else
1436 ConsumeToken();
1437
1438 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1439 return StmtError();
1440
1441 if (Tok.isNot(tok::l_brace))
1442 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1443
1444 OwningStmtResult Block(ParseCompoundStatement());
1445 if (Block.isInvalid())
1446 return move(Block);
1447
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001448 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001449}