blob: 5ff09052f58d5edc5f1c3ebf00afd94c9e44a982 [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"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroffb746ce82008-02-07 23:24:32 +000019#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Parse/DeclSpec.h"
21#include "clang/Parse/Scope.h"
22using 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
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000101 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000102 SourceLocation DeclStart = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000103 DeclTy *Decl = ParseDeclaration(Declarator::BlockContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000104 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redla60528c2008-12-21 12:04:03 +0000105 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000106 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000108 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 } else {
110 // expression[opt] ';'
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000111 OwningExprResult Expr(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000112 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 // If the expression is invalid, skip ahead to the next semicolon. Not
114 // doing this opens us up to the possibility of infinite loops if
115 // ParseExpression does not consume any tokens.
116 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000117 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 // Otherwise, eat the semicolon.
120 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redlf512e822009-01-18 18:03:53 +0000121 return Actions.ActOnExprStmt(move_arg(Expr));
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 case tok::kw_case: // C99 6.8.1: labeled-statement
125 return ParseCaseStatement();
126 case tok::kw_default: // C99 6.8.1: labeled-statement
127 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 case tok::l_brace: // C99 6.8.2: compound-statement
130 return ParseCompoundStatement();
131 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000132 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 case tok::kw_if: // C99 6.8.4.1: if-statement
135 return ParseIfStatement();
136 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000137 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000138
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000140 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_do: // C99 6.8.5.2: do-statement
142 Res = ParseDoStatement();
143 SemiError = "do/while loop";
144 break;
145 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000146 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000147
148 case tok::kw_goto: // C99 6.8.6.1: goto-statement
149 Res = ParseGotoStatement();
150 SemiError = "goto statement";
151 break;
152 case tok::kw_continue: // C99 6.8.6.2: continue-statement
153 Res = ParseContinueStatement();
154 SemiError = "continue statement";
155 break;
156 case tok::kw_break: // C99 6.8.6.3: break-statement
157 Res = ParseBreakStatement();
158 SemiError = "break statement";
159 break;
160 case tok::kw_return: // C99 6.8.6.4: return-statement
161 Res = ParseReturnStatement();
162 SemiError = "return statement";
163 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000164
Sebastian Redla0fd8652008-12-21 16:41:36 +0000165 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000166 bool msAsm = false;
167 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000168 if (msAsm) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 SemiError = "asm statement";
170 break;
171 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000172
Sebastian Redla0fd8652008-12-21 16:41:36 +0000173 case tok::kw_try: // C++ 15: try-block
174 return ParseCXXTryBlock();
175 }
176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000178 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000180 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000181 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000182 // Skip until we see a } or ;, but don't eat it.
183 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000185 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186}
187
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000188/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000189///
190/// labeled-statement:
191/// identifier ':' statement
192/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000193///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000194Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000195 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
196 "Not an identifier!");
197
198 Token IdentTok = Tok; // Save the whole token.
199 ConsumeToken(); // eat the identifier.
200
201 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000202
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000203 // identifier ':' statement
204 SourceLocation ColonLoc = ConsumeToken();
205
206 // Read label attributes, if present.
207 DeclTy *AttrList = 0;
208 if (Tok.is(tok::kw___attribute))
209 // TODO: save these somewhere.
210 AttrList = ParseAttributes();
211
Sebastian Redl61364dd2008-12-11 19:30:53 +0000212 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000213
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000214 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000215 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000216 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000217
Sebastian Redlde307472009-01-11 00:38:46 +0000218 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
219 IdentTok.getIdentifierInfo(),
Sebastian Redlf512e822009-01-18 18:03:53 +0000220 ColonLoc, move_arg(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000221}
Reid Spencer5f016e22007-07-11 17:01:13 +0000222
223/// ParseCaseStatement
224/// labeled-statement:
225/// 'case' constant-expression ':' statement
226/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
227///
228/// Note that this does not parse the 'statement' at the end.
229///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000230Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000231 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
233
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000234 OwningExprResult LHS(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000235 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000237 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // GNU case range extension.
241 SourceLocation DotDotDotLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000242 OwningExprResult RHS(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000243 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 Diag(Tok, diag::ext_gnu_case_range);
245 DotDotDotLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000246
247 RHS = ParseConstantExpression();
248 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000250 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000253
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000254 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000255 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000257 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000263 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000265 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000267
268 OwningStmtResult SubStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000269
270 // Broken substmt shouldn't prevent the case from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000271 if (SubStmt.isInvalid())
Steve Naroff1b273c42007-09-16 14:56:35 +0000272 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000273
Sebastian Redlf512e822009-01-18 18:03:53 +0000274 return Actions.ActOnCaseStmt(CaseLoc, move_arg(LHS), DotDotDotLoc,
275 move_arg(RHS), ColonLoc, move_arg(SubStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
278/// ParseDefaultStatement
279/// labeled-statement:
280/// 'default' ':' statement
281/// Note that this does not parse the 'statement' at the end.
282///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000283Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000284 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
286
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000287 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000288 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000290 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000296 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000298 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 }
300
Sebastian Redl61364dd2008-12-11 19:30:53 +0000301 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000302 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000303 return StmtError();
304
Sebastian Redl117054a2008-12-28 16:13:43 +0000305 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redlf512e822009-01-18 18:03:53 +0000306 move_arg(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000307}
308
309
310/// ParseCompoundStatement - Parse a "{}" block.
311///
312/// compound-statement: [C99 6.8.2]
313/// { block-item-list[opt] }
314/// [GNU] { label-declarations block-item-list } [TODO]
315///
316/// block-item-list:
317/// block-item
318/// block-item-list block-item
319///
320/// block-item:
321/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000322/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000323/// statement
324/// [OMP] openmp-directive [TODO]
325///
326/// [GNU] label-declarations:
327/// [GNU] label-declaration
328/// [GNU] label-declarations label-declaration
329///
330/// [GNU] label-declaration:
331/// [GNU] '__label__' identifier-list ';'
332///
333/// [OMP] openmp-directive: [TODO]
334/// [OMP] barrier-directive
335/// [OMP] flush-directive
336///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000337Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000338 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000339
Chris Lattner31e05722007-08-26 06:24:45 +0000340 // Enter a scope to hold everything within the compound stmt. Compound
341 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000342 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000343
344 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000345 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000346}
347
348
349/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000350/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000351/// consume the '}' at the end of the block. It does not manipulate the scope
352/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000353Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
355
356 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000357 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000358
359 typedef StmtVector StmtsTy;
360 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000361 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000362 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000363 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000364 R = ParseStatementOrDeclaration(false);
365 } else {
366 // __extension__ can start declarations and it can also be a unary
367 // operator for expressions. Consume multiple __extension__ markers here
368 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000369 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000370 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000371 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000372 ConsumeToken();
373
Chris Lattner043a0b52008-03-13 06:32:11 +0000374 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000375 ExtensionRAIIObject O(Diags); // Use RAII to do this.
376
Chris Lattner45a566c2007-08-27 01:01:57 +0000377 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000378 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000379 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000380 SourceLocation DeclStart = Tok.getLocation();
381 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
382 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000383 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000384 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000385 // Otherwise this was a unary __extension__ marker.
386 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000387
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000388 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000389 SkipUntil(tok::semi);
390 continue;
391 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000392
Chris Lattner39146d62008-10-20 06:51:33 +0000393 // Eat the semicolon at the end of stmt and convert the expr into a
394 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000395 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redlf512e822009-01-18 18:03:53 +0000396 R = Actions.ActOnExprStmt(move_arg(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000397 }
398 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000399
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000400 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000401 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000403
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000405 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000407 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000411 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000412 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000413}
414
Chris Lattner15ff1112008-12-12 06:31:07 +0000415/// ParseParenExprOrCondition:
416/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000417/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000418///
419/// This function parses and performs error recovery on the specified condition
420/// or expression (depending on whether we're in C++ or C mode). This function
421/// goes out of its way to recover well. It returns true if there was a parser
422/// error (the right paren couldn't be found), which indicates that the caller
423/// should try to recover harder. It returns false if the condition is
424/// successfully parsed. Note that a successful parse can still have semantic
425/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000426bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
427 bool OnlyAllowCondition) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000428 SourceLocation LParenLoc = ConsumeParen();
429
430 if (getLang().CPlusPlus)
431 CondExp = ParseCXXCondition();
432 else
433 CondExp = ParseExpression();
434
435 // If the parser was confused by the condition and we don't have a ')', try to
436 // recover by skipping ahead to a semi and bailing out. If condexp is
437 // semantically invalid but we have well formed code, keep going.
438 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
439 SkipUntil(tok::semi);
440 // Skipping may have stopped if it found the containing ')'. If so, we can
441 // continue parsing the if statement.
442 if (Tok.isNot(tok::r_paren))
443 return true;
444 }
445
446 // Otherwise the condition is valid or the rparen is present.
447 MatchRHSPunctuation(tok::r_paren, LParenLoc);
448 return false;
449}
450
451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452/// ParseIfStatement
453/// if-statement: [C99 6.8.4.1]
454/// 'if' '(' expression ')' statement
455/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000456/// [C++] 'if' '(' condition ')' statement
457/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000458///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000459Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000460 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
462
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000463 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000464 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000466 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000468
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000469 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
470
Chris Lattner22153252007-08-26 23:08:06 +0000471 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
472 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000473 //
474 // C++ 6.4p3:
475 // A name introduced by a declaration in a condition is in scope from its
476 // point of declaration until the end of the substatements controlled by the
477 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000478 // C++ 3.3.2p4:
479 // Names declared in the for-init-statement, and in the condition of if,
480 // while, for, and switch statements are local to the if, while, for, or
481 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000482 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000483 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000484
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000486 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000487 if (ParseParenExprOrCondition(CondExp))
488 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000489
Chris Lattner0ecea032007-08-22 05:28:50 +0000490 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000491 // there is no compound stmt. C90 does not have this clause. We only do this
492 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000493 //
494 // C++ 6.4p1:
495 // The substatement in a selection-statement (each substatement, in the else
496 // form of the if statement) implicitly defines a local scope.
497 //
498 // For C++ we create a scope for the condition and a new scope for
499 // substatements because:
500 // -When the 'then' scope exits, we want the condition declaration to still be
501 // active for the 'else' scope too.
502 // -Sema will detect name clashes by considering declarations of a
503 // 'ControlScope' as part of its direct subscope.
504 // -If we wanted the condition and substatement to be in the same scope, we
505 // would have to notify ParseStatement not to create a new scope. It's
506 // simpler to let it create a new scope.
507 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000508 ParseScope InnerScope(this, Scope::DeclScope,
509 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000510
Chris Lattnerb96728d2007-10-29 05:08:52 +0000511 // Read the 'then' stmt.
512 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000513 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000514
Chris Lattnera36ce712007-08-22 05:16:28 +0000515 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000516 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000517
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 // If it has an else, parse it.
519 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000520 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000521 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000522
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000523 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000525
Chris Lattner0ecea032007-08-22 05:28:50 +0000526 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000527 // there is no compound stmt. C90 does not have this clause. We only do
528 // this if the body isn't a compound statement to avoid push/pop in common
529 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000530 //
531 // C++ 6.4p1:
532 // The substatement in a selection-statement (each substatement, in the else
533 // form of the if statement) implicitly defines a local scope.
534 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000535 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000536 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000537
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000538 bool WithinElse = CurScope->isWithinElse();
539 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000540 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000542 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000543
544 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000545 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000547
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000548 IfScope.Exit();
Chris Lattner18914bc2008-12-12 06:19:11 +0000549
550 // If the condition was invalid, discard the if statement. We could recover
551 // better by replacing it with a valid expr, but don't do that yet.
552 if (CondExp.isInvalid())
553 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000554
Chris Lattnerb96728d2007-10-29 05:08:52 +0000555 // If the then or else stmt is invalid and the other is valid (and present),
556 // make turn the invalid one into a null stmt to avoid dropping the other
557 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000558 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
559 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
560 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000561 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000562 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000563 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000564
Chris Lattnerb96728d2007-10-29 05:08:52 +0000565 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000566 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000567 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000568 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000569 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000570
Sebastian Redlf512e822009-01-18 18:03:53 +0000571 return Actions.ActOnIfStmt(IfLoc, move_arg(CondExp), move_arg(ThenStmt),
572 ElseLoc, move_arg(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000573}
574
575/// ParseSwitchStatement
576/// switch-statement:
577/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000578/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000579Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000580 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
582
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000583 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000584 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000586 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 }
Chris Lattner22153252007-08-26 23:08:06 +0000588
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000589 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
590
Chris Lattner22153252007-08-26 23:08:06 +0000591 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
592 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000593 //
594 // C++ 6.4p3:
595 // A name introduced by a declaration in a condition is in scope from its
596 // point of declaration until the end of the substatements controlled by the
597 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000598 // C++ 3.3.2p4:
599 // Names declared in the for-init-statement, and in the condition of if,
600 // while, for, and switch statements are local to the if, while, for, or
601 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000602 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000603 unsigned ScopeFlags = Scope::BreakScope;
604 if (C99orCXX)
605 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000606 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607
608 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000609 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000610 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000611 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000612
613 OwningStmtResult Switch(Actions);
614 if (!Cond.isInvalid())
Sebastian Redlf512e822009-01-18 18:03:53 +0000615 Switch = Actions.ActOnStartOfSwitchStmt(move_arg(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000616
Chris Lattner0ecea032007-08-22 05:28:50 +0000617 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000618 // there is no compound stmt. C90 does not have this clause. We only do this
619 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000620 //
621 // C++ 6.4p1:
622 // The substatement in a selection-statement (each substatement, in the else
623 // form of the if statement) implicitly defines a local scope.
624 //
625 // See comments in ParseIfStatement for why we create a scope for the
626 // condition and a new scope for substatement in C++.
627 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000628 ParseScope InnerScope(this, Scope::DeclScope,
629 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000630
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000632 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000633
Chris Lattner0ecea032007-08-22 05:28:50 +0000634 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000635 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000636
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000637 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000638 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000639 // FIXME: Remove the case statement list from the Switch statement.
640 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000641
642 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000643
Chris Lattner15ff1112008-12-12 06:31:07 +0000644 if (Cond.isInvalid())
645 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000646
Sebastian Redlf512e822009-01-18 18:03:53 +0000647 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move_arg(Switch),
648 move_arg(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000649}
650
651/// ParseWhileStatement
652/// while-statement: [C99 6.8.5.1]
653/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000654/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000655Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000656 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 SourceLocation WhileLoc = Tok.getLocation();
658 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000659
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000660 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000661 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000663 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000665
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000666 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
667
Chris Lattner22153252007-08-26 23:08:06 +0000668 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
669 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000670 //
671 // C++ 6.4p3:
672 // A name introduced by a declaration in a condition is in scope from its
673 // point of declaration until the end of the substatements controlled by the
674 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000675 // C++ 3.3.2p4:
676 // Names declared in the for-init-statement, and in the condition of if,
677 // while, for, and switch statements are local to the if, while, for, or
678 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000679 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000680 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000681 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000682 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
683 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000684 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000685 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
686 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687
688 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000689 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000690 if (ParseParenExprOrCondition(Cond))
691 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000692
Chris Lattner0ecea032007-08-22 05:28:50 +0000693 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000694 // there is no compound stmt. C90 does not have this clause. We only do this
695 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000696 //
697 // C++ 6.5p2:
698 // The substatement in an iteration-statement implicitly defines a local scope
699 // which is entered and exited each time through the loop.
700 //
701 // See comments in ParseIfStatement for why we create a scope for the
702 // condition and a new scope for substatement in C++.
703 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000704 ParseScope InnerScope(this, Scope::DeclScope,
705 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000706
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000708 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000709
Chris Lattner0ecea032007-08-22 05:28:50 +0000710 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000711 InnerScope.Exit();
712 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000713
714 if (Cond.isInvalid() || Body.isInvalid())
715 return StmtError();
716
Sebastian Redlf512e822009-01-18 18:03:53 +0000717 return Actions.ActOnWhileStmt(WhileLoc, move_arg(Cond), move_arg(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000718}
719
720/// ParseDoStatement
721/// do-statement: [C99 6.8.5.2]
722/// 'do' statement 'while' '(' expression ')' ';'
723/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000724Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000725 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000727
Chris Lattner22153252007-08-26 23:08:06 +0000728 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
729 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000730 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000731 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000732 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000733 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000734 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000735
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000736 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737
Chris Lattner0ecea032007-08-22 05:28:50 +0000738 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000739 // there is no compound stmt. C90 does not have this clause. We only do this
740 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000741 //
742 // C++ 6.5p2:
743 // The substatement in an iteration-statement implicitly defines a local scope
744 // which is entered and exited each time through the loop.
745 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000746 ParseScope InnerScope(this, Scope::DeclScope,
747 (getLang().C99 || getLang().CPlusPlus) &&
748 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000749
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000751 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000752
Chris Lattner0ecea032007-08-22 05:28:50 +0000753 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000754 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000755
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000756 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000757 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000758 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000759 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000760 SkipUntil(tok::semi, false, true);
761 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000762 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 }
764 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000765
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000766 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000767 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000768 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000769 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000771
Chris Lattnerff871fb2008-12-12 06:35:28 +0000772 // Parse the parenthesized condition.
773 OwningExprResult Cond(Actions);
774 ParseParenExprOrCondition(Cond, true);
775
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000776 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000777
Sebastian Redl9a920342008-12-11 19:48:14 +0000778 if (Cond.isInvalid() || Body.isInvalid())
779 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000780
Sebastian Redlf512e822009-01-18 18:03:53 +0000781 return Actions.ActOnDoStmt(DoLoc, move_arg(Body), WhileLoc, move_arg(Cond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000782}
783
784/// ParseForStatement
785/// for-statement: [C99 6.8.5.3]
786/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
787/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000788/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
789/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000790/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
791/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000792///
793/// [C++] for-init-statement:
794/// [C++] expression-statement
795/// [C++] simple-declaration
796///
Sebastian Redl9a920342008-12-11 19:48:14 +0000797Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000798 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000800
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000801 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000802 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000804 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000806
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000807 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
808
Chris Lattner22153252007-08-26 23:08:06 +0000809 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
810 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000811 //
812 // C++ 6.4p3:
813 // A name introduced by a declaration in a condition is in scope from its
814 // point of declaration until the end of the substatements controlled by the
815 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000816 // C++ 3.3.2p4:
817 // Names declared in the for-init-statement, and in the condition of if,
818 // while, for, and switch statements are local to the if, while, for, or
819 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000820 // C++ 6.5.3p1:
821 // Names declared in the for-init-statement are in the same declarative-region
822 // as those declared in the condition.
823 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000824 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000825 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000826 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
827 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000828 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000829 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
830
831 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832
833 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000834 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000835
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000836 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000837 OwningStmtResult FirstPart(Actions);
838 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000841 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 // no first part, eat the ';'.
843 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000844 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000846 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000848
Chris Lattner81c018d2008-03-13 06:29:04 +0000849 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000850 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000851 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000852 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
853 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000854 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000855 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000856 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000857 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 } else {
859 Value = ParseExpression();
860
861 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000862 if (!Value.isInvalid())
Sebastian Redlf512e822009-01-18 18:03:53 +0000863 FirstPart = Actions.ActOnExprStmt(move_arg(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000864
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000865 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000867 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000868 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000869 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000870 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000871 }
872 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000873 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 SkipUntil(tok::semi);
875 }
876 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000877 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000878 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000879 // Parse the second part of the for specifier.
880 if (Tok.is(tok::semi)) { // for (...;;
881 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000882 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000883 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000884 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000885
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000886 if (Tok.is(tok::semi)) {
887 ConsumeToken();
888 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000889 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000890 SkipUntil(tok::semi);
891 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000892
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000893 // Parse the third part of the for specifier.
894 if (Tok.is(tok::r_paren)) { // for (...;...;)
895 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000896 } else {
Sebastian Redlf05b1522009-01-16 23:28:06 +0000897 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 }
899 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 // Match the ')'.
901 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000902
Chris Lattner0ecea032007-08-22 05:28:50 +0000903 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000904 // there is no compound stmt. C90 does not have this clause. We only do this
905 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000906 //
907 // C++ 6.5p2:
908 // The substatement in an iteration-statement implicitly defines a local scope
909 // which is entered and exited each time through the loop.
910 //
911 // See comments in ParseIfStatement for why we create a scope for
912 // for-init-statement/condition and a new scope for substatement in C++.
913 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000914 ParseScope InnerScope(this, Scope::DeclScope,
915 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000916
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000918 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000919
Chris Lattner0ecea032007-08-22 05:28:50 +0000920 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000921 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000922
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000924 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000925
926 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +0000927 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000928
929 if (!ForEach)
Sebastian Redlf512e822009-01-18 18:03:53 +0000930 return Actions.ActOnForStmt(ForLoc, LParenLoc, move_arg(FirstPart),
931 move_arg(SecondPart), move_arg(ThirdPart),
932 RParenLoc, move_arg(Body));
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000933 else
Sebastian Redlf05b1522009-01-16 23:28:06 +0000934 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
Sebastian Redlf512e822009-01-18 18:03:53 +0000935 move_arg(FirstPart),
936 move_arg(SecondPart),
937 RParenLoc, move_arg(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000938}
939
940/// ParseGotoStatement
941/// jump-statement:
942/// 'goto' identifier ';'
943/// [GNU] 'goto' '*' expression ';'
944///
945/// Note: this lets the caller parse the end ';'.
946///
Sebastian Redl9a920342008-12-11 19:48:14 +0000947Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000948 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000950
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000951 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000952 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000953 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 Tok.getIdentifierInfo());
955 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000956 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 // GNU indirect goto extension.
958 Diag(Tok, diag::ext_gnu_indirect_goto);
959 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000960 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000961 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000963 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000965 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move_arg(R));
Chris Lattner95cfb852007-07-22 04:13:33 +0000966 } else {
967 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +0000968 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000970
Sebastian Redl9a920342008-12-11 19:48:14 +0000971 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000972}
973
974/// ParseContinueStatement
975/// jump-statement:
976/// 'continue' ';'
977///
978/// Note: this lets the caller parse the end ';'.
979///
Sebastian Redl9a920342008-12-11 19:48:14 +0000980Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000982 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000983}
984
985/// ParseBreakStatement
986/// jump-statement:
987/// 'break' ';'
988///
989/// Note: this lets the caller parse the end ';'.
990///
Sebastian Redl9a920342008-12-11 19:48:14 +0000991Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +0000993 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000994}
995
996/// ParseReturnStatement
997/// jump-statement:
998/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +0000999Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001000 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001002
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001003 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001004 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001006 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001008 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 }
1010 }
Sebastian Redlf512e822009-01-18 18:03:53 +00001011 return Actions.ActOnReturnStmt(ReturnLoc, move_arg(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001012}
1013
Steve Naroff5f8aa692008-02-11 23:15:56 +00001014/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1015/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001016Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001017 if (Tok.is(tok::l_brace)) {
1018 unsigned short savedBraceCount = BraceCount;
1019 do {
1020 ConsumeAnyToken();
1021 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1022 } else {
1023 // From the MS website: If used without braces, the __asm keyword means
1024 // that the rest of the line is an assembly-language statement.
1025 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001026 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001027 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001028 do {
1029 ConsumeAnyToken();
1030 TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001031 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
Steve Naroff36280972008-02-08 18:01:27 +00001032 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1033 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001034 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001035 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001036}
1037
Reid Spencer5f016e22007-07-11 17:01:13 +00001038/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001039/// asm-statement:
1040/// gnu-asm-statement
1041/// ms-asm-statement
1042///
1043/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001044/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1045///
1046/// [GNU] asm-argument:
1047/// asm-string-literal
1048/// asm-string-literal ':' asm-operands[opt]
1049/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1050/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1051/// ':' asm-clobbers
1052///
1053/// [GNU] asm-clobbers:
1054/// asm-string-literal
1055/// asm-clobbers ',' asm-string-literal
1056///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001057/// [MS] ms-asm-statement:
1058/// '__asm' assembly-instruction ';'[opt]
1059/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1060///
1061/// [MS] assembly-instruction-list:
1062/// assembly-instruction ';'[opt]
1063/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1064///
Sebastian Redl9a920342008-12-11 19:48:14 +00001065Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001066 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001067 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001068
Steve Naroff5f8aa692008-02-11 23:15:56 +00001069 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001070 msAsm = true;
1071 return FuzzyParseMicrosoftAsmStatement();
1072 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 DeclSpec DS;
1074 SourceLocation Loc = Tok.getLocation();
1075 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001076
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1078 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001079 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001081 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001082
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001084 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001085 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001086 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001087 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001089 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 }
1091 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001092
Sebastian Redleffa8d12008-12-10 00:02:53 +00001093 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001094 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001095 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001096
Anders Carlssonb235fc22007-11-22 01:36:19 +00001097 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001098 ExprVector Constraints(Actions);
1099 ExprVector Exprs(Actions);
1100 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001101
Anders Carlssondfab34a2008-02-05 23:03:50 +00001102 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001103
Anders Carlssondfab34a2008-02-05 23:03:50 +00001104 SourceLocation RParenLoc;
1105 if (Tok.is(tok::r_paren)) {
1106 // We have a simple asm expression
1107 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001108
Anders Carlssondfab34a2008-02-05 23:03:50 +00001109 RParenLoc = ConsumeParen();
1110 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001111 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001112 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001113 return StmtError();
1114
Anders Carlssondfab34a2008-02-05 23:03:50 +00001115 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001116
Anders Carlssondfab34a2008-02-05 23:03:50 +00001117 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001118 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001119 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001120
Anders Carlssondfab34a2008-02-05 23:03:50 +00001121 assert(Names.size() == Constraints.size() &&
1122 Constraints.size() == Exprs.size()
1123 && "Input operand size mismatch!");
1124
1125 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001126
Anders Carlssondfab34a2008-02-05 23:03:50 +00001127 // Parse the clobbers, if present.
1128 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001129 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001130
Anders Carlssondfab34a2008-02-05 23:03:50 +00001131 // Parse the asm-string list for clobbers.
1132 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001133 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001134
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001135 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001136 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001137
1138 Clobbers.push_back(Clobber.release());
1139
Anders Carlssondfab34a2008-02-05 23:03:50 +00001140 if (Tok.isNot(tok::comma)) break;
1141 ConsumeToken();
1142 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001144
Anders Carlssondfab34a2008-02-05 23:03:50 +00001145 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001147
Sebastian Redl3037ed02009-01-18 16:53:17 +00001148 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1149 NumOutputs, NumInputs, &Names[0],
Sebastian Redlf512e822009-01-18 18:03:53 +00001150 move_arg(Constraints), move_arg(Exprs),
1151 move_arg(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001152 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153}
1154
1155/// ParseAsmOperands - Parse the asm-operands production as used by
1156/// asm-statement. We also parse a leading ':' token. If the leading colon is
1157/// not present, we do not parse anything.
1158///
1159/// [GNU] asm-operands:
1160/// asm-operand
1161/// asm-operands ',' asm-operand
1162///
1163/// [GNU] asm-operand:
1164/// asm-string-literal '(' expression ')'
1165/// '[' identifier ']' asm-string-literal '(' expression ')'
1166///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001167bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001168 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1169 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001171 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 ConsumeToken();
1173
1174 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001175 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001176 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001177
Anders Carlssonb235fc22007-11-22 01:36:19 +00001178 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001180 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 SourceLocation Loc = ConsumeBracket();
1182
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001183 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 Diag(Tok, diag::err_expected_ident);
1185 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001186 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 }
Chris Lattner69efba72007-10-29 04:06:22 +00001188
Anders Carlssonb235fc22007-11-22 01:36:19 +00001189 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001190 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001191
1192 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001194 } else
1195 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001196
Sebastian Redleffa8d12008-12-10 00:02:53 +00001197 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001198 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001199 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001200 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001201 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001202 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001203
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001204 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001205 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001207 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001209
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 // Read the parenthesized expression.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001211 OwningExprResult Res(ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001212 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001214 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001216 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001218 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 ConsumeToken();
1220 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001221
1222 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001223}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001224
1225Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1226 SourceLocation L, SourceLocation R) {
1227 // Do not enter a scope for the brace, as the arguments are in the same scope
1228 // (the function body) as the body itself. Instead, just read the statement
1229 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001230 OwningStmtResult FnBody(ParseCompoundStatementBody());
1231
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001232 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001233 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001234 FnBody = Actions.ActOnCompoundStmt(L, R, MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001235
Sebastian Redlf512e822009-01-18 18:03:53 +00001236 return Actions.ActOnFinishFunctionBody(Decl, move_arg(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001237}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001238
1239/// ParseCXXTryBlock - Parse a C++ try-block.
1240///
1241/// try-block:
1242/// 'try' compound-statement handler-seq
1243///
1244/// handler-seq:
1245/// handler handler-seq[opt]
1246///
1247Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1248 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1249
1250 SourceLocation TryLoc = ConsumeToken();
1251 if (Tok.isNot(tok::l_brace))
1252 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1253 OwningStmtResult TryBlock(ParseCompoundStatement());
1254 if (TryBlock.isInvalid())
1255 return move(TryBlock);
1256
1257 StmtVector Handlers(Actions);
1258 if (Tok.isNot(tok::kw_catch))
1259 return StmtError(Diag(Tok, diag::err_expected_catch));
1260 while (Tok.is(tok::kw_catch)) {
1261 OwningStmtResult Handler(ParseCXXCatchBlock());
1262 if (!Handler.isInvalid())
1263 Handlers.push_back(Handler.release());
1264 }
1265 // Don't bother creating the full statement if we don't have any usable
1266 // handlers.
1267 if (Handlers.empty())
1268 return StmtError();
1269
Sebastian Redlf512e822009-01-18 18:03:53 +00001270 return Actions.ActOnCXXTryBlock(TryLoc, move_arg(TryBlock),
1271 move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001272}
1273
1274/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1275///
1276/// handler:
1277/// 'catch' '(' exception-declaration ')' compound-statement
1278///
1279/// exception-declaration:
1280/// type-specifier-seq declarator
1281/// type-specifier-seq abstract-declarator
1282/// type-specifier-seq
1283/// '...'
1284///
1285Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1286 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1287
1288 SourceLocation CatchLoc = ConsumeToken();
1289
1290 SourceLocation LParenLoc = Tok.getLocation();
1291 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1292 return StmtError();
1293
1294 // C++ 3.3.2p3:
1295 // The name in a catch exception-declaration is local to the handler and
1296 // shall not be redeclared in the outermost block of the handler.
1297 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1298
1299 // exception-declaration is equivalent to '...' or a parameter-declaration
1300 // without default arguments.
1301 DeclTy *ExceptionDecl = 0;
1302 if (Tok.isNot(tok::ellipsis)) {
1303 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001304 if (ParseCXXTypeSpecifierSeq(DS))
1305 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001306 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1307 ParseDeclarator(ExDecl);
1308 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1309 } else
1310 ConsumeToken();
1311
1312 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1313 return StmtError();
1314
1315 if (Tok.isNot(tok::l_brace))
1316 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1317
1318 OwningStmtResult Block(ParseCompoundStatement());
1319 if (Block.isInvalid())
1320 return move(Block);
1321
Sebastian Redlf512e822009-01-18 18:03:53 +00001322 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move_arg(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001323}