blob: 272be2f660173c882f2ff77f636b2af8b9e07a5a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner39146d62008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
21#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000041/// [C++] try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000042/// [OBC] objc-throw-statement
43/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000044/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000045/// [GNU] asm-statement
46/// [OMP] openmp-construct [TODO]
47///
48/// labeled-statement:
49/// identifier ':' statement
50/// 'case' constant-expression ':' statement
51/// 'default' ':' statement
52///
53/// selection-statement:
54/// if-statement
55/// switch-statement
56///
57/// iteration-statement:
58/// while-statement
59/// do-statement
60/// for-statement
61///
62/// expression-statement:
63/// expression[opt] ';'
64///
65/// jump-statement:
66/// 'goto' identifier ';'
67/// 'continue' ';'
68/// 'break' ';'
69/// 'return' expression[opt] ';'
70/// [GNU] 'goto' '*' expression ';'
71///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000072/// [OBC] objc-throw-statement:
73/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000074/// [OBC] '@' 'throw' ';'
75///
Sebastian Redl61364dd2008-12-11 19:30:53 +000076Parser::OwningStmtResult
77Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000078 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000079 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000080
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
Douglas Gregor791215b2009-09-21 20:51:25 +000093 case tok::code_completion:
94 Actions.CodeCompleteOrdinaryName(CurScope);
95 ConsumeToken();
96 return ParseStatementOrDeclaration(OnlyStatement);
97
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +000098 case tok::identifier:
99 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
100 // identifier ':' statement
101 return ParseLabeledStatement();
102 }
103 // PASS THROUGH.
104
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000105 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000106 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000107 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
108 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext, DeclEnd);
109 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000110 }
111
112 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000114 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000117 // expression[opt] ';'
118 OwningExprResult Expr(ParseExpression());
119 if (Expr.isInvalid()) {
120 // If the expression is invalid, skip ahead to the next semicolon. Not
121 // doing this opens us up to the possibility of infinite loops if
122 // ParseExpression does not consume any tokens.
123 SkipUntil(tok::semi);
124 return StmtError();
125 }
126 // Otherwise, eat the semicolon.
127 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000128 return Actions.ActOnExprStmt(Actions.FullExpr(Expr));
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000129 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 case tok::kw_case: // C99 6.8.1: labeled-statement
132 return ParseCaseStatement();
133 case tok::kw_default: // C99 6.8.1: labeled-statement
134 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000135
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 case tok::l_brace: // C99 6.8.2: compound-statement
137 return ParseCompoundStatement();
138 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Sebastian Redla60528c2008-12-21 12:04:03 +0000139 return Actions.ActOnNullStmt(ConsumeToken());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case tok::kw_if: // C99 6.8.4.1: if-statement
142 return ParseIfStatement();
143 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000144 return ParseSwitchStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 case tok::kw_while: // C99 6.8.5.1: while-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000147 return ParseWhileStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 case tok::kw_do: // C99 6.8.5.2: do-statement
149 Res = ParseDoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000150 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 break;
152 case tok::kw_for: // C99 6.8.5.3: for-statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000153 return ParseForStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000154
155 case tok::kw_goto: // C99 6.8.6.1: goto-statement
156 Res = ParseGotoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000157 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 break;
159 case tok::kw_continue: // C99 6.8.6.2: continue-statement
160 Res = ParseContinueStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000161 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 break;
163 case tok::kw_break: // C99 6.8.6.3: break-statement
164 Res = ParseBreakStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000165 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 break;
167 case tok::kw_return: // C99 6.8.6.4: return-statement
168 Res = ParseReturnStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000169 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000171
Sebastian Redla0fd8652008-12-21 16:41:36 +0000172 case tok::kw_asm: {
Steve Naroffd62701b2008-02-07 03:50:06 +0000173 bool msAsm = false;
174 Res = ParseAsmStatement(msAsm);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000175 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000176 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 break;
178 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000179
Sebastian Redla0fd8652008-12-21 16:41:36 +0000180 case tok::kw_try: // C++ 15: try-block
181 return ParseCXXTryBlock();
182 }
183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000185 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000187 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000188 // If the result was valid, then we do want to diagnose this. Use
189 // ExpectAndConsume to emit the diagnostic, even though we know it won't
190 // succeed.
191 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000192 // Skip until we see a } or ;, but don't eat it.
193 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Sebastian Redl61364dd2008-12-11 19:30:53 +0000196 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000199/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000200///
201/// labeled-statement:
202/// identifier ':' statement
203/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000204///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000205Parser::OwningStmtResult Parser::ParseLabeledStatement() {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000206 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
207 "Not an identifier!");
208
209 Token IdentTok = Tok; // Save the whole token.
210 ConsumeToken(); // eat the identifier.
211
212 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000213
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000214 // identifier ':' statement
215 SourceLocation ColonLoc = ConsumeToken();
216
217 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000218 Action::AttrTy *AttrList = 0;
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000219 if (Tok.is(tok::kw___attribute))
220 // TODO: save these somewhere.
221 AttrList = ParseAttributes();
222
Sebastian Redl61364dd2008-12-11 19:30:53 +0000223 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000224
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000225 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000226 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000227 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000228
Sebastian Redlde307472009-01-11 00:38:46 +0000229 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
230 IdentTok.getIdentifierInfo(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000231 ColonLoc, move(SubStmt));
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000232}
Reid Spencer5f016e22007-07-11 17:01:13 +0000233
234/// ParseCaseStatement
235/// labeled-statement:
236/// 'case' constant-expression ':' statement
237/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
238///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000239Parser::OwningStmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000240 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner24e1e702009-03-04 04:23:07 +0000242 // It is very very common for code to contain many case statements recursively
243 // nested, as in (but usually without indentation):
244 // case 1:
245 // case 2:
246 // case 3:
247 // case 4:
248 // case 5: etc.
249 //
250 // Parsing this naively works, but is both inefficient and can cause us to run
251 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000252 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000253 // but all the grossness is constrained to ParseCaseStatement (and some
254 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattner24e1e702009-03-04 04:23:07 +0000256 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
257 // example above.
258 OwningStmtResult TopLevelCase(Actions, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Chris Lattner24e1e702009-03-04 04:23:07 +0000260 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
261 // gets updated each time a new case is parsed, and whose body is unset so
262 // far. When parsing 'case 4', this is the 'case 3' node.
263 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattner24e1e702009-03-04 04:23:07 +0000265 // While we have case statements, eat and stack them.
266 do {
267 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000269 if (Tok.is(tok::code_completion)) {
270 Actions.CodeCompleteCase(CurScope);
271 ConsumeToken();
272 }
273
Chris Lattner24e1e702009-03-04 04:23:07 +0000274 OwningExprResult LHS(ParseConstantExpression());
275 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000277 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000279
Chris Lattner24e1e702009-03-04 04:23:07 +0000280 // GNU case range extension.
281 SourceLocation DotDotDotLoc;
282 OwningExprResult RHS(Actions);
283 if (Tok.is(tok::ellipsis)) {
284 Diag(Tok, diag::ext_gnu_case_range);
285 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000286
Chris Lattner24e1e702009-03-04 04:23:07 +0000287 RHS = ParseConstantExpression();
288 if (RHS.isInvalid()) {
289 SkipUntil(tok::colon);
290 return StmtError();
291 }
292 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000293
Chris Lattner24e1e702009-03-04 04:23:07 +0000294 if (Tok.isNot(tok::colon)) {
295 Diag(Tok, diag::err_expected_colon_after) << "'case'";
296 SkipUntil(tok::colon);
297 return StmtError();
298 }
299
300 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner24e1e702009-03-04 04:23:07 +0000302 OwningStmtResult Case =
303 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc,
304 move(RHS), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner24e1e702009-03-04 04:23:07 +0000306 // If we had a sema error parsing this case, then just ignore it and
307 // continue parsing the sub-stmt.
308 if (Case.isInvalid()) {
309 if (TopLevelCase.isInvalid()) // No parsed case stmts.
310 return ParseStatement();
311 // Otherwise, just don't add it as a nested case.
312 } else {
313 // If this is the first case statement we parsed, it becomes TopLevelCase.
314 // Otherwise we link it into the current chain.
315 StmtTy *NextDeepest = Case.get();
316 if (TopLevelCase.isInvalid())
317 TopLevelCase = move(Case);
318 else
319 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case));
320 DeepestParsedCaseStmt = NextDeepest;
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Chris Lattner24e1e702009-03-04 04:23:07 +0000323 // Handle all case statements.
324 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner24e1e702009-03-04 04:23:07 +0000326 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner24e1e702009-03-04 04:23:07 +0000328 // If we found a non-case statement, start by parsing it.
329 OwningStmtResult SubStmt(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Chris Lattner24e1e702009-03-04 04:23:07 +0000331 if (Tok.isNot(tok::r_brace)) {
332 SubStmt = ParseStatement();
333 } else {
334 // Nicely diagnose the common error "switch (X) { case 4: }", which is
335 // not valid.
336 // FIXME: add insertion hint.
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 Diag(Tok, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000338 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattner24e1e702009-03-04 04:23:07 +0000341 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000342 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000343 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattner24e1e702009-03-04 04:23:07 +0000345 // Install the body into the most deeply-nested case.
346 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000347
Chris Lattner24e1e702009-03-04 04:23:07 +0000348 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000349 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350}
351
352/// ParseDefaultStatement
353/// labeled-statement:
354/// 'default' ':' statement
355/// Note that this does not parse the 'statement' at the end.
356///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000357Parser::OwningStmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000358 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
360
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000361 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000362 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000364 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 SourceLocation ColonLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000370 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 Diag(Tok, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000372 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 }
374
Sebastian Redl61364dd2008-12-11 19:30:53 +0000375 OwningStmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000376 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000377 return StmtError();
378
Sebastian Redl117054a2008-12-28 16:13:43 +0000379 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000380 move(SubStmt), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381}
382
383
384/// ParseCompoundStatement - Parse a "{}" block.
385///
386/// compound-statement: [C99 6.8.2]
387/// { block-item-list[opt] }
388/// [GNU] { label-declarations block-item-list } [TODO]
389///
390/// block-item-list:
391/// block-item
392/// block-item-list block-item
393///
394/// block-item:
395/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000396/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000397/// statement
398/// [OMP] openmp-directive [TODO]
399///
400/// [GNU] label-declarations:
401/// [GNU] label-declaration
402/// [GNU] label-declarations label-declaration
403///
404/// [GNU] label-declaration:
405/// [GNU] '__label__' identifier-list ';'
406///
407/// [OMP] openmp-directive: [TODO]
408/// [OMP] barrier-directive
409/// [OMP] flush-directive
410///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000411Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000412 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000413
Chris Lattner31e05722007-08-26 06:24:45 +0000414 // Enter a scope to hold everything within the compound stmt. Compound
415 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000416 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000417
418 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000419 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000420}
421
422
423/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000424/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000425/// consume the '}' at the end of the block. It does not manipulate the scope
426/// stack.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000427Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000428 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000429 Tok.getLocation(),
430 "in compound statement ('{}')");
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
433
434 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000435 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000436
437 typedef StmtVector StmtsTy;
438 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000439 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000440 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000441 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000442 R = ParseStatementOrDeclaration(false);
443 } else {
444 // __extension__ can start declarations and it can also be a unary
445 // operator for expressions. Consume multiple __extension__ markers here
446 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000447 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000448 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000449 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000450 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000451
Chris Lattner45a566c2007-08-27 01:01:57 +0000452 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000453 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000454 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000455 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000456 ExtensionRAIIObject O(Diags);
457
Chris Lattner97144fc2009-04-02 04:16:50 +0000458 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
459 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext,DeclEnd);
460 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000461 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000462 // Otherwise this was a unary __extension__ marker.
463 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000464
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000465 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000466 SkipUntil(tok::semi);
467 continue;
468 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000469
Chris Lattner39146d62008-10-20 06:51:33 +0000470 // Eat the semicolon at the end of stmt and convert the expr into a
471 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000472 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000473 R = Actions.ActOnExprStmt(Actions.FullExpr(Res));
Chris Lattner45a566c2007-08-27 01:01:57 +0000474 }
475 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000476
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000477 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000478 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000480
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000482 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 Diag(Tok, diag::err_expected_rbrace);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000484 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000486
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000488 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000489 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000490}
491
Chris Lattner15ff1112008-12-12 06:31:07 +0000492/// ParseParenExprOrCondition:
493/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000494/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000495///
496/// This function parses and performs error recovery on the specified condition
497/// or expression (depending on whether we're in C++ or C mode). This function
498/// goes out of its way to recover well. It returns true if there was a parser
499/// error (the right paren couldn't be found), which indicates that the caller
500/// should try to recover harder. It returns false if the condition is
501/// successfully parsed. Note that a successful parse can still have semantic
502/// errors in the condition.
Chris Lattnerff871fb2008-12-12 06:35:28 +0000503bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp,
Chris Lattner98913592009-06-12 23:04:47 +0000504 bool OnlyAllowCondition,
505 SourceLocation *LParenLocPtr,
506 SourceLocation *RParenLocPtr) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000507 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner98913592009-06-12 23:04:47 +0000508 if (LParenLocPtr) *LParenLocPtr = LParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattner15ff1112008-12-12 06:31:07 +0000510 if (getLang().CPlusPlus)
511 CondExp = ParseCXXCondition();
512 else
513 CondExp = ParseExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner15ff1112008-12-12 06:31:07 +0000515 // If the parser was confused by the condition and we don't have a ')', try to
516 // recover by skipping ahead to a semi and bailing out. If condexp is
517 // semantically invalid but we have well formed code, keep going.
518 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) {
519 SkipUntil(tok::semi);
520 // Skipping may have stopped if it found the containing ')'. If so, we can
521 // continue parsing the if statement.
522 if (Tok.isNot(tok::r_paren))
523 return true;
524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Chris Lattner15ff1112008-12-12 06:31:07 +0000526 // Otherwise the condition is valid or the rparen is present.
Chris Lattner98913592009-06-12 23:04:47 +0000527 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
528 if (RParenLocPtr) *RParenLocPtr = RPLoc;
Chris Lattner15ff1112008-12-12 06:31:07 +0000529 return false;
530}
531
532
Reid Spencer5f016e22007-07-11 17:01:13 +0000533/// ParseIfStatement
534/// if-statement: [C99 6.8.4.1]
535/// 'if' '(' expression ')' statement
536/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000537/// [C++] 'if' '(' condition ')' statement
538/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000539///
Sebastian Redl61364dd2008-12-11 19:30:53 +0000540Parser::OwningStmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000541 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
543
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000544 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000545 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000547 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000549
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000550 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
551
Chris Lattner22153252007-08-26 23:08:06 +0000552 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
553 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000554 //
555 // C++ 6.4p3:
556 // A name introduced by a declaration in a condition is in scope from its
557 // point of declaration until the end of the substatements controlled by the
558 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000559 // C++ 3.3.2p4:
560 // Names declared in the for-init-statement, and in the condition of if,
561 // while, for, and switch statements are local to the if, while, for, or
562 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000563 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000564 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000567 OwningExprResult CondExp(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000568 if (ParseParenExprOrCondition(CondExp))
569 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000570
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000571 FullExprArg FullCondExp(Actions.FullExpr(CondExp));
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattner0ecea032007-08-22 05:28:50 +0000573 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000574 // there is no compound stmt. C90 does not have this clause. We only do this
575 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000576 //
577 // C++ 6.4p1:
578 // The substatement in a selection-statement (each substatement, in the else
579 // form of the if statement) implicitly defines a local scope.
580 //
581 // For C++ we create a scope for the condition and a new scope for
582 // substatements because:
583 // -When the 'then' scope exits, we want the condition declaration to still be
584 // active for the 'else' scope too.
585 // -Sema will detect name clashes by considering declarations of a
586 // 'ControlScope' as part of its direct subscope.
587 // -If we wanted the condition and substatement to be in the same scope, we
588 // would have to notify ParseStatement not to create a new scope. It's
589 // simpler to let it create a new scope.
590 //
Mike Stump1eb44332009-09-09 15:08:12 +0000591 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000592 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000593
Chris Lattnerb96728d2007-10-29 05:08:52 +0000594 // Read the 'then' stmt.
595 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000596 OwningStmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000597
Chris Lattnera36ce712007-08-22 05:16:28 +0000598 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000599 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000600
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 // If it has an else, parse it.
602 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000603 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000604 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000605
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000606 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 ElseLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000608
Chris Lattner0ecea032007-08-22 05:28:50 +0000609 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000610 // there is no compound stmt. C90 does not have this clause. We only do
611 // this if the body isn't a compound statement to avoid push/pop in common
612 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000613 //
614 // C++ 6.4p1:
615 // The substatement in a selection-statement (each substatement, in the else
616 // form of the if statement) implicitly defines a local scope.
617 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000618 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000619 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000620
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000621 bool WithinElse = CurScope->isWithinElse();
622 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000623 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000625 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000626
627 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000628 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000630
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000631 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Chris Lattner18914bc2008-12-12 06:19:11 +0000633 // If the condition was invalid, discard the if statement. We could recover
634 // better by replacing it with a valid expr, but don't do that yet.
635 if (CondExp.isInvalid())
636 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000637
Chris Lattnerb96728d2007-10-29 05:08:52 +0000638 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000639 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000640 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000641 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
642 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
643 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000644 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000645 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000646 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000647
Chris Lattnerb96728d2007-10-29 05:08:52 +0000648 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000649 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000650 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000651 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000652 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000653
Mike Stump1eb44332009-09-09 15:08:12 +0000654 return Actions.ActOnIfStmt(IfLoc, FullCondExp, move(ThenStmt),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000655 ElseLoc, move(ElseStmt));
Reid Spencer5f016e22007-07-11 17:01:13 +0000656}
657
658/// ParseSwitchStatement
659/// switch-statement:
660/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000661/// [C++] 'switch' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000662Parser::OwningStmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000663 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
665
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000666 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000667 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000669 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 }
Chris Lattner22153252007-08-26 23:08:06 +0000671
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000672 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
673
Chris Lattner22153252007-08-26 23:08:06 +0000674 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
675 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000676 //
677 // C++ 6.4p3:
678 // A name introduced by a declaration in a condition is in scope from its
679 // point of declaration until the end of the substatements controlled by the
680 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000681 // C++ 3.3.2p4:
682 // Names declared in the for-init-statement, and in the condition of if,
683 // while, for, and switch statements are local to the if, while, for, or
684 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000685 //
Chris Lattner15ff1112008-12-12 06:31:07 +0000686 unsigned ScopeFlags = Scope::BreakScope;
687 if (C99orCXX)
688 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000689 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
691 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000692 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000693 if (ParseParenExprOrCondition(Cond))
Sebastian Redl9a920342008-12-11 19:48:14 +0000694 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +0000695
696 OwningStmtResult Switch(Actions);
697 if (!Cond.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000698 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000699
Chris Lattner0ecea032007-08-22 05:28:50 +0000700 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000701 // there is no compound stmt. C90 does not have this clause. We only do this
702 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000703 //
704 // C++ 6.4p1:
705 // The substatement in a selection-statement (each substatement, in the else
706 // form of the if statement) implicitly defines a local scope.
707 //
708 // See comments in ParseIfStatement for why we create a scope for the
709 // condition and a new scope for substatement in C++.
710 //
Mike Stump1eb44332009-09-09 15:08:12 +0000711 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000712 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +0000713
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000715 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000716
Chris Lattner0ecea032007-08-22 05:28:50 +0000717 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000718 InnerScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000719
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000720 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000721 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000722 // FIXME: Remove the case statement list from the Switch statement.
723 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000724
725 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000726
Chris Lattner15ff1112008-12-12 06:31:07 +0000727 if (Cond.isInvalid())
728 return StmtError();
Sebastian Redlde307472009-01-11 00:38:46 +0000729
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000730 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000731}
732
733/// ParseWhileStatement
734/// while-statement: [C99 6.8.5.1]
735/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000736/// [C++] 'while' '(' condition ')' statement
Sebastian Redl9a920342008-12-11 19:48:14 +0000737Parser::OwningStmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000738 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 SourceLocation WhileLoc = Tok.getLocation();
740 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000741
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000742 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000743 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000745 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000747
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000748 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
749
Chris Lattner22153252007-08-26 23:08:06 +0000750 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
751 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000752 //
753 // C++ 6.4p3:
754 // A name introduced by a declaration in a condition is in scope from its
755 // point of declaration until the end of the substatements controlled by the
756 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000757 // C++ 3.3.2p4:
758 // Names declared in the for-init-statement, and in the condition of if,
759 // while, for, and switch statements are local to the if, while, for, or
760 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000761 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000762 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000763 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000764 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
765 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000766 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000767 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
768 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000769
770 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000771 OwningExprResult Cond(Actions);
Chris Lattner15ff1112008-12-12 06:31:07 +0000772 if (ParseParenExprOrCondition(Cond))
773 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000774
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000775 FullExprArg FullCond(Actions.FullExpr(Cond));
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattner0ecea032007-08-22 05:28:50 +0000777 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000778 // there is no compound stmt. C90 does not have this clause. We only do this
779 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000780 //
781 // C++ 6.5p2:
782 // The substatement in an iteration-statement implicitly defines a local scope
783 // which is entered and exited each time through the loop.
784 //
785 // See comments in ParseIfStatement for why we create a scope for the
786 // condition and a new scope for substatement in C++.
787 //
Mike Stump1eb44332009-09-09 15:08:12 +0000788 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000789 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000790
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000792 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000793
Chris Lattner0ecea032007-08-22 05:28:50 +0000794 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000795 InnerScope.Exit();
796 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +0000797
798 if (Cond.isInvalid() || Body.isInvalid())
799 return StmtError();
800
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000801 return Actions.ActOnWhileStmt(WhileLoc, FullCond, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +0000802}
803
804/// ParseDoStatement
805/// do-statement: [C99 6.8.5.2]
806/// 'do' statement 'while' '(' expression ')' ';'
807/// Note: this lets the caller parse the end ';'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000808Parser::OwningStmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000809 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000811
Chris Lattner22153252007-08-26 23:08:06 +0000812 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
813 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000814 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000815 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000816 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000817 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000818 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +0000819
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000820 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000821
Chris Lattner0ecea032007-08-22 05:28:50 +0000822 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000823 // there is no compound stmt. C90 does not have this clause. We only do this
824 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000825 //
826 // C++ 6.5p2:
827 // The substatement in an iteration-statement implicitly defines a local scope
828 // which is entered and exited each time through the loop.
829 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000830 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +0000831 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000832 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +0000833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000835 OwningStmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000836
Chris Lattner0ecea032007-08-22 05:28:50 +0000837 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000838 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000839
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000840 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000841 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000842 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000843 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000844 SkipUntil(tok::semi, false, true);
845 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000846 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 }
848 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +0000849
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000850 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000851 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000852 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +0000853 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000855
Chris Lattnerff871fb2008-12-12 06:35:28 +0000856 // Parse the parenthesized condition.
857 OwningExprResult Cond(Actions);
Chris Lattner98913592009-06-12 23:04:47 +0000858 SourceLocation LPLoc, RPLoc;
859 ParseParenExprOrCondition(Cond, true, &LPLoc, &RPLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000861 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000862
Sebastian Redl9a920342008-12-11 19:48:14 +0000863 if (Cond.isInvalid() || Body.isInvalid())
864 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000865
Chris Lattner98913592009-06-12 23:04:47 +0000866 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, LPLoc,
867 move(Cond), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000868}
869
870/// ParseForStatement
871/// for-statement: [C99 6.8.5.3]
872/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
873/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000874/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
875/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000876/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
877/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000878///
879/// [C++] for-init-statement:
880/// [C++] expression-statement
881/// [C++] simple-declaration
882///
Sebastian Redl9a920342008-12-11 19:48:14 +0000883Parser::OwningStmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000884 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +0000886
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000887 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000888 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000890 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000892
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000893 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000894
Chris Lattner22153252007-08-26 23:08:06 +0000895 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
896 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000897 //
898 // C++ 6.4p3:
899 // A name introduced by a declaration in a condition is in scope from its
900 // point of declaration until the end of the substatements controlled by the
901 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000902 // C++ 3.3.2p4:
903 // Names declared in the for-init-statement, and in the condition of if,
904 // while, for, and switch statements are local to the if, while, for, or
905 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000906 // C++ 6.5.3p1:
907 // Names declared in the for-init-statement are in the same declarative-region
908 // as those declared in the condition.
909 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000910 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000911 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000912 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
913 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000914 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000915 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
916
917 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000918
919 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000920 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000921
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000922 bool ForEach = false;
Sebastian Redlf05b1522009-01-16 23:28:06 +0000923 OwningStmtResult FirstPart(Actions);
924 OwningExprResult SecondPart(Actions), ThirdPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000925
Douglas Gregor791215b2009-09-21 20:51:25 +0000926 if (Tok.is(tok::code_completion)) {
927 Actions.CodeCompleteOrdinaryName(CurScope);
928 ConsumeToken();
929 }
930
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000932 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 // no first part, eat the ';'.
934 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000935 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +0000937 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +0000939
Chris Lattner97144fc2009-04-02 04:16:50 +0000940 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
941 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd,
942 false);
Chris Lattnercd147752009-03-29 17:27:48 +0000943 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattnercd147752009-03-29 17:27:48 +0000945 if (Tok.is(tok::semi)) { // for (int x = 4;
946 ConsumeToken();
947 } else if ((ForEach = isTokIdentifier_in())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000948 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000949 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000950 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000951 } else {
952 Diag(Tok, diag::err_expected_semi_for);
953 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000954 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 } else {
956 Value = ParseExpression();
957
958 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000959 if (!Value.isInvalid())
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000960 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000961
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000962 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000964 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000965 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000966 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000967 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000968 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 SkipUntil(tok::semi);
970 }
971 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000972 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000973 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000974 // Parse the second part of the for specifier.
975 if (Tok.is(tok::semi)) { // for (...;;
976 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000977 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000978 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000979 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000980
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000981 if (Tok.is(tok::semi)) {
982 ConsumeToken();
983 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000984 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000985 SkipUntil(tok::semi);
986 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000987
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000988 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000989 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000990 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 // Match the ')'.
993 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000994
Chris Lattner0ecea032007-08-22 05:28:50 +0000995 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000996 // there is no compound stmt. C90 does not have this clause. We only do this
997 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000998 //
999 // C++ 6.5p2:
1000 // The substatement in an iteration-statement implicitly defines a local scope
1001 // which is entered and exited each time through the loop.
1002 //
1003 // See comments in ParseIfStatement for why we create a scope for
1004 // for-init-statement/condition and a new scope for substatement in C++.
1005 //
Mike Stump1eb44332009-09-09 15:08:12 +00001006 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001007 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001008
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001010 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001011
Chris Lattner0ecea032007-08-22 05:28:50 +00001012 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001013 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001014
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001016 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001017
1018 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001019 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001020
1021 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001022 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
1023 move(SecondPart), move(ThirdPart),
1024 RParenLoc, move(Body));
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattner682bf922009-03-29 16:50:03 +00001026 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1027 move(FirstPart),
1028 move(SecondPart),
1029 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001030}
1031
1032/// ParseGotoStatement
1033/// jump-statement:
1034/// 'goto' identifier ';'
1035/// [GNU] 'goto' '*' expression ';'
1036///
1037/// Note: this lets the caller parse the end ';'.
1038///
Sebastian Redl9a920342008-12-11 19:48:14 +00001039Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001040 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001042
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001043 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001044 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001045 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 Tok.getIdentifierInfo());
1047 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001048 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 // GNU indirect goto extension.
1050 Diag(Tok, diag::ext_gnu_indirect_goto);
1051 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001052 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001053 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001055 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001057 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001058 } else {
1059 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001060 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001062
Sebastian Redl9a920342008-12-11 19:48:14 +00001063 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001064}
1065
1066/// ParseContinueStatement
1067/// jump-statement:
1068/// 'continue' ';'
1069///
1070/// Note: this lets the caller parse the end ';'.
1071///
Sebastian Redl9a920342008-12-11 19:48:14 +00001072Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001074 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001075}
1076
1077/// ParseBreakStatement
1078/// jump-statement:
1079/// 'break' ';'
1080///
1081/// Note: this lets the caller parse the end ';'.
1082///
Sebastian Redl9a920342008-12-11 19:48:14 +00001083Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001085 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001086}
1087
1088/// ParseReturnStatement
1089/// jump-statement:
1090/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001091Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001092 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001094
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001095 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001096 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001098 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001100 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 }
1102 }
Anders Carlssonf53b4432009-08-18 16:11:00 +00001103 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001104}
1105
Steve Naroff5f8aa692008-02-11 23:15:56 +00001106/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1107/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001108Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001109 if (Tok.is(tok::l_brace)) {
1110 unsigned short savedBraceCount = BraceCount;
1111 do {
1112 ConsumeAnyToken();
1113 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001114 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001115 // From the MS website: If used without braces, the __asm keyword means
1116 // that the rest of the line is an assembly-language statement.
1117 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001118 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001119 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001120 do {
1121 ConsumeAnyToken();
1122 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001123 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1124 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001125 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001126 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001127 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001128}
1129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001131/// asm-statement:
1132/// gnu-asm-statement
1133/// ms-asm-statement
1134///
1135/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001136/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1137///
1138/// [GNU] asm-argument:
1139/// asm-string-literal
1140/// asm-string-literal ':' asm-operands[opt]
1141/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1142/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1143/// ':' asm-clobbers
1144///
1145/// [GNU] asm-clobbers:
1146/// asm-string-literal
1147/// asm-clobbers ',' asm-string-literal
1148///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001149/// [MS] ms-asm-statement:
1150/// '__asm' assembly-instruction ';'[opt]
1151/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1152///
1153/// [MS] assembly-instruction-list:
1154/// assembly-instruction ';'[opt]
1155/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1156///
Sebastian Redl9a920342008-12-11 19:48:14 +00001157Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001158 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001159 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001160
Steve Naroff5f8aa692008-02-11 23:15:56 +00001161 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001162 msAsm = true;
1163 return FuzzyParseMicrosoftAsmStatement();
1164 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 DeclSpec DS;
1166 SourceLocation Loc = Tok.getLocation();
1167 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1170 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001171 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001173 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001176 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001177 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001178 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001179 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001181 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 }
1183 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001184
Sebastian Redleffa8d12008-12-10 00:02:53 +00001185 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001186 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001187 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001188
Anders Carlssonb235fc22007-11-22 01:36:19 +00001189 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001190 ExprVector Constraints(Actions);
1191 ExprVector Exprs(Actions);
1192 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193
Anders Carlssondfab34a2008-02-05 23:03:50 +00001194 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001195
Anders Carlssondfab34a2008-02-05 23:03:50 +00001196 SourceLocation RParenLoc;
1197 if (Tok.is(tok::r_paren)) {
1198 // We have a simple asm expression
1199 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001200
Anders Carlssondfab34a2008-02-05 23:03:50 +00001201 RParenLoc = ConsumeParen();
1202 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001203 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001204 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001205 return StmtError();
1206
Anders Carlssondfab34a2008-02-05 23:03:50 +00001207 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001208
Anders Carlssondfab34a2008-02-05 23:03:50 +00001209 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001210 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001211 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001212
Anders Carlssondfab34a2008-02-05 23:03:50 +00001213 assert(Names.size() == Constraints.size() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001214 Constraints.size() == Exprs.size()
Anders Carlssondfab34a2008-02-05 23:03:50 +00001215 && "Input operand size mismatch!");
1216
1217 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001218
Anders Carlssondfab34a2008-02-05 23:03:50 +00001219 // Parse the clobbers, if present.
1220 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001221 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001222
Anders Carlssondfab34a2008-02-05 23:03:50 +00001223 // Parse the asm-string list for clobbers.
1224 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001225 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001226
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001227 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001228 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001229
1230 Clobbers.push_back(Clobber.release());
1231
Anders Carlssondfab34a2008-02-05 23:03:50 +00001232 if (Tok.isNot(tok::comma)) break;
1233 ConsumeToken();
1234 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001236
Anders Carlssondfab34a2008-02-05 23:03:50 +00001237 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001239
Sebastian Redl3037ed02009-01-18 16:53:17 +00001240 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001241 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001242 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001243 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001244 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001245}
1246
1247/// ParseAsmOperands - Parse the asm-operands production as used by
1248/// asm-statement. We also parse a leading ':' token. If the leading colon is
1249/// not present, we do not parse anything.
1250///
1251/// [GNU] asm-operands:
1252/// asm-operand
1253/// asm-operands ',' asm-operand
1254///
1255/// [GNU] asm-operand:
1256/// asm-string-literal '(' expression ')'
1257/// '[' identifier ']' asm-string-literal '(' expression ')'
1258///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001259//
1260// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001261bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001262 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1263 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001265 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001269 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001270 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001271
1272 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001274 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001277 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 Diag(Tok, diag::err_expected_ident);
1279 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001280 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Anders Carlssonb235fc22007-11-22 01:36:19 +00001283 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001284 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001285
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001286 Names.push_back(II->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001288 } else
1289 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001290
Sebastian Redleffa8d12008-12-10 00:02:53 +00001291 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001292 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001293 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001294 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001295 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001296 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001297
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001298 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001299 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001301 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001305 SourceLocation OpenLoc = ConsumeParen();
1306 OwningExprResult Res(ParseExpression());
1307 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001308 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001310 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001312 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001314 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 ConsumeToken();
1316 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001317
1318 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001319}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001320
Chris Lattnerb28317a2009-03-28 19:18:32 +00001321Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001322 assert(Tok.is(tok::l_brace));
1323 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001324
Chris Lattner49f28ca2009-03-05 08:00:35 +00001325 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1326 PP.getSourceManager(),
1327 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001329 // Do not enter a scope for the brace, as the arguments are in the same scope
1330 // (the function body) as the body itself. Instead, just read the statement
1331 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001332 OwningStmtResult FnBody(ParseCompoundStatementBody());
1333
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001334 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001335 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001336 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001337 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001338
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001339 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001340}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001341
Sebastian Redld3a413d2009-04-26 20:35:05 +00001342/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1343///
1344/// function-try-block:
1345/// 'try' ctor-initializer[opt] compound-statement handler-seq
1346///
1347Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1348 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1349 SourceLocation TryLoc = ConsumeToken();
1350
1351 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1352 PP.getSourceManager(),
1353 "parsing function try block");
1354
1355 // Constructor initializer list?
1356 if (Tok.is(tok::colon))
1357 ParseConstructorInitializer(Decl);
1358
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001359 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001360 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1361 // If we failed to parse the try-catch, we just give the function an empty
1362 // compound statement as the body.
1363 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001364 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001365 MultiStmtArg(Actions), false);
1366
1367 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1368}
1369
Sebastian Redla0fd8652008-12-21 16:41:36 +00001370/// ParseCXXTryBlock - Parse a C++ try-block.
1371///
1372/// try-block:
1373/// 'try' compound-statement handler-seq
1374///
Sebastian Redla0fd8652008-12-21 16:41:36 +00001375Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1376 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1377
1378 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001379 return ParseCXXTryBlockCommon(TryLoc);
1380}
1381
1382/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1383/// function-try-block.
1384///
1385/// try-block:
1386/// 'try' compound-statement handler-seq
1387///
1388/// function-try-block:
1389/// 'try' ctor-initializer[opt] compound-statement handler-seq
1390///
1391/// handler-seq:
1392/// handler handler-seq[opt]
1393///
1394Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001395 if (Tok.isNot(tok::l_brace))
1396 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1397 OwningStmtResult TryBlock(ParseCompoundStatement());
1398 if (TryBlock.isInvalid())
1399 return move(TryBlock);
1400
1401 StmtVector Handlers(Actions);
1402 if (Tok.isNot(tok::kw_catch))
1403 return StmtError(Diag(Tok, diag::err_expected_catch));
1404 while (Tok.is(tok::kw_catch)) {
1405 OwningStmtResult Handler(ParseCXXCatchBlock());
1406 if (!Handler.isInvalid())
1407 Handlers.push_back(Handler.release());
1408 }
1409 // Don't bother creating the full statement if we don't have any usable
1410 // handlers.
1411 if (Handlers.empty())
1412 return StmtError();
1413
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001414 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001415}
1416
1417/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1418///
1419/// handler:
1420/// 'catch' '(' exception-declaration ')' compound-statement
1421///
1422/// exception-declaration:
1423/// type-specifier-seq declarator
1424/// type-specifier-seq abstract-declarator
1425/// type-specifier-seq
1426/// '...'
1427///
1428Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1429 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1430
1431 SourceLocation CatchLoc = ConsumeToken();
1432
1433 SourceLocation LParenLoc = Tok.getLocation();
1434 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1435 return StmtError();
1436
1437 // C++ 3.3.2p3:
1438 // The name in a catch exception-declaration is local to the handler and
1439 // shall not be redeclared in the outermost block of the handler.
1440 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1441
1442 // exception-declaration is equivalent to '...' or a parameter-declaration
1443 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001444 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001445 if (Tok.isNot(tok::ellipsis)) {
1446 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001447 if (ParseCXXTypeSpecifierSeq(DS))
1448 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001449 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1450 ParseDeclarator(ExDecl);
1451 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1452 } else
1453 ConsumeToken();
1454
1455 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1456 return StmtError();
1457
1458 if (Tok.isNot(tok::l_brace))
1459 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1460
1461 OwningStmtResult Block(ParseCompoundStatement());
1462 if (Block.isInvalid())
1463 return move(Block);
1464
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001465 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001466}