blob: 7637382ac032dc25175af40254f0066104cc19b2 [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;
John McCalld8ac0572009-11-03 19:26:08 +0000941 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, DeclEnd);
Chris Lattnercd147752009-03-29 17:27:48 +0000942 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Chris Lattnercd147752009-03-29 17:27:48 +0000944 if (Tok.is(tok::semi)) { // for (int x = 4;
945 ConsumeToken();
946 } else if ((ForEach = isTokIdentifier_in())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000947 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000948 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000949 SecondPart = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +0000950 } else {
951 Diag(Tok, diag::err_expected_semi_for);
952 SkipUntil(tok::semi);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000953 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 } else {
955 Value = ParseExpression();
956
957 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000958 if (!Value.isInvalid())
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000959 FirstPart = Actions.ActOnExprStmt(Actions.FullExpr(Value));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000960
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000961 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000963 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000964 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000965 SecondPart = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +0000966 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000967 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 SkipUntil(tok::semi);
969 }
970 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000971 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000972 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000973 // Parse the second part of the for specifier.
974 if (Tok.is(tok::semi)) { // for (...;;
975 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000976 } else {
Chris Lattner15ff1112008-12-12 06:31:07 +0000977 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000978 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000979
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000980 if (Tok.is(tok::semi)) {
981 ConsumeToken();
982 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000983 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000984 SkipUntil(tok::semi);
985 }
Sebastian Redl9a920342008-12-11 19:48:14 +0000986
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000987 // Parse the third part of the for specifier.
Chris Lattner4dca69b2009-03-29 17:29:28 +0000988 if (Tok.isNot(tok::r_paren)) // for (...;...;)
Sebastian Redlf05b1522009-01-16 23:28:06 +0000989 ThirdPart = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 // Match the ')'.
992 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000993
Chris Lattner0ecea032007-08-22 05:28:50 +0000994 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000995 // there is no compound stmt. C90 does not have this clause. We only do this
996 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000997 //
998 // C++ 6.5p2:
999 // The substatement in an iteration-statement implicitly defines a local scope
1000 // which is entered and exited each time through the loop.
1001 //
1002 // See comments in ParseIfStatement for why we create a scope for
1003 // for-init-statement/condition and a new scope for substatement in C++.
1004 //
Mike Stump1eb44332009-09-09 15:08:12 +00001005 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001006 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001007
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 // Read the body statement.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001009 OwningStmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001010
Chris Lattner0ecea032007-08-22 05:28:50 +00001011 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001012 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001013
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001015 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001016
1017 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001018 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001019
1020 if (!ForEach)
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001021 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart),
1022 move(SecondPart), move(ThirdPart),
1023 RParenLoc, move(Body));
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattner682bf922009-03-29 16:50:03 +00001025 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1026 move(FirstPart),
1027 move(SecondPart),
1028 RParenLoc, move(Body));
Reid Spencer5f016e22007-07-11 17:01:13 +00001029}
1030
1031/// ParseGotoStatement
1032/// jump-statement:
1033/// 'goto' identifier ';'
1034/// [GNU] 'goto' '*' expression ';'
1035///
1036/// Note: this lets the caller parse the end ';'.
1037///
Sebastian Redl9a920342008-12-11 19:48:14 +00001038Parser::OwningStmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001039 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001041
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001042 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001043 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +00001044 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 Tok.getIdentifierInfo());
1046 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001047 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // GNU indirect goto extension.
1049 Diag(Tok, diag::ext_gnu_indirect_goto);
1050 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001051 OwningExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001052 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001054 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001056 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R));
Chris Lattner95cfb852007-07-22 04:13:33 +00001057 } else {
1058 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001059 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001061
Sebastian Redl9a920342008-12-11 19:48:14 +00001062 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001063}
1064
1065/// ParseContinueStatement
1066/// jump-statement:
1067/// 'continue' ';'
1068///
1069/// Note: this lets the caller parse the end ';'.
1070///
Sebastian Redl9a920342008-12-11 19:48:14 +00001071Parser::OwningStmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001073 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001074}
1075
1076/// ParseBreakStatement
1077/// jump-statement:
1078/// 'break' ';'
1079///
1080/// Note: this lets the caller parse the end ';'.
1081///
Sebastian Redl9a920342008-12-11 19:48:14 +00001082Parser::OwningStmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Sebastian Redl4cffe2f2009-01-18 13:19:59 +00001084 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001085}
1086
1087/// ParseReturnStatement
1088/// jump-statement:
1089/// 'return' expression[opt] ';'
Sebastian Redl9a920342008-12-11 19:48:14 +00001090Parser::OwningStmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001091 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001093
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001094 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001095 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001097 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001099 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 }
1101 }
Anders Carlssonf53b4432009-08-18 16:11:00 +00001102 return Actions.ActOnReturnStmt(ReturnLoc, move(R));
Reid Spencer5f016e22007-07-11 17:01:13 +00001103}
1104
Steve Naroff5f8aa692008-02-11 23:15:56 +00001105/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1106/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Sebastian Redl9a920342008-12-11 19:48:14 +00001107Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001108 if (Tok.is(tok::l_brace)) {
1109 unsigned short savedBraceCount = BraceCount;
1110 do {
1111 ConsumeAnyToken();
1112 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001113 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001114 // From the MS website: If used without braces, the __asm keyword means
1115 // that the rest of the line is an assembly-language statement.
1116 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001117 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001118 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001119 do {
1120 ConsumeAnyToken();
1121 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001122 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1123 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001124 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001125 }
Sebastian Redla60528c2008-12-21 12:04:03 +00001126 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001127}
1128
Reid Spencer5f016e22007-07-11 17:01:13 +00001129/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001130/// asm-statement:
1131/// gnu-asm-statement
1132/// ms-asm-statement
1133///
1134/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001135/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1136///
1137/// [GNU] asm-argument:
1138/// asm-string-literal
1139/// asm-string-literal ':' asm-operands[opt]
1140/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1141/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1142/// ':' asm-clobbers
1143///
1144/// [GNU] asm-clobbers:
1145/// asm-string-literal
1146/// asm-clobbers ',' asm-string-literal
1147///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001148/// [MS] ms-asm-statement:
1149/// '__asm' assembly-instruction ';'[opt]
1150/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1151///
1152/// [MS] assembly-instruction-list:
1153/// assembly-instruction ';'[opt]
1154/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1155///
Sebastian Redl9a920342008-12-11 19:48:14 +00001156Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001157 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001158 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001159
Steve Naroff5f8aa692008-02-11 23:15:56 +00001160 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001161 msAsm = true;
1162 return FuzzyParseMicrosoftAsmStatement();
1163 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 DeclSpec DS;
1165 SourceLocation Loc = Tok.getLocation();
1166 ParseTypeQualifierListOpt(DS);
Sebastian Redl9a920342008-12-11 19:48:14 +00001167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1169 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001170 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001172 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001173
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001175 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001176 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001177 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001178 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001180 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 }
1182 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001183
Sebastian Redleffa8d12008-12-10 00:02:53 +00001184 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001185 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001186 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001187
Anders Carlssonb235fc22007-11-22 01:36:19 +00001188 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001189 ExprVector Constraints(Actions);
1190 ExprVector Exprs(Actions);
1191 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001192
Anders Carlssondfab34a2008-02-05 23:03:50 +00001193 unsigned NumInputs = 0, NumOutputs = 0;
Sebastian Redl9a920342008-12-11 19:48:14 +00001194
Anders Carlssondfab34a2008-02-05 23:03:50 +00001195 SourceLocation RParenLoc;
1196 if (Tok.is(tok::r_paren)) {
1197 // We have a simple asm expression
1198 isSimple = true;
Sebastian Redl9a920342008-12-11 19:48:14 +00001199
Anders Carlssondfab34a2008-02-05 23:03:50 +00001200 RParenLoc = ConsumeParen();
1201 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001202 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001203 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001204 return StmtError();
1205
Anders Carlssondfab34a2008-02-05 23:03:50 +00001206 NumOutputs = Names.size();
Sebastian Redl9a920342008-12-11 19:48:14 +00001207
Anders Carlssondfab34a2008-02-05 23:03:50 +00001208 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001209 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
Sebastian Redl9a920342008-12-11 19:48:14 +00001210 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001211
Anders Carlssondfab34a2008-02-05 23:03:50 +00001212 assert(Names.size() == Constraints.size() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001213 Constraints.size() == Exprs.size()
Anders Carlssondfab34a2008-02-05 23:03:50 +00001214 && "Input operand size mismatch!");
1215
1216 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001217
Anders Carlssondfab34a2008-02-05 23:03:50 +00001218 // Parse the clobbers, if present.
1219 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001220 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001221
Anders Carlssondfab34a2008-02-05 23:03:50 +00001222 // Parse the asm-string list for clobbers.
1223 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001224 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001225
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001226 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001227 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001228
1229 Clobbers.push_back(Clobber.release());
1230
Anders Carlssondfab34a2008-02-05 23:03:50 +00001231 if (Tok.isNot(tok::comma)) break;
1232 ConsumeToken();
1233 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001235
Anders Carlssondfab34a2008-02-05 23:03:50 +00001236 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001238
Sebastian Redl3037ed02009-01-18 16:53:17 +00001239 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001240 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001241 move_arg(Constraints), move_arg(Exprs),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001242 move(AsmString), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001243 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001244}
1245
1246/// ParseAsmOperands - Parse the asm-operands production as used by
1247/// asm-statement. We also parse a leading ':' token. If the leading colon is
1248/// not present, we do not parse anything.
1249///
1250/// [GNU] asm-operands:
1251/// asm-operand
1252/// asm-operands ',' asm-operand
1253///
1254/// [GNU] asm-operand:
1255/// asm-string-literal '(' expression ')'
1256/// '[' identifier ']' asm-string-literal '(' expression ')'
1257///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001258//
1259// FIXME: Avoid unnecessary std::string trashing.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001260bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001261 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1262 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001264 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001268 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001269 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
1271 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001273 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001276 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 Diag(Tok, diag::err_expected_ident);
1278 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001279 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Anders Carlssonb235fc22007-11-22 01:36:19 +00001282 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001283 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001284
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001285 Names.push_back(II->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001287 } else
1288 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001289
Sebastian Redleffa8d12008-12-10 00:02:53 +00001290 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001291 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001292 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001293 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001294 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001295 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001296
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001297 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001298 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001300 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001302
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001304 SourceLocation OpenLoc = ConsumeParen();
1305 OwningExprResult Res(ParseExpression());
1306 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001307 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001309 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001311 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001313 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 ConsumeToken();
1315 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001316
1317 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001318}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001319
Chris Lattnerb28317a2009-03-28 19:18:32 +00001320Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001321 assert(Tok.is(tok::l_brace));
1322 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001323
Chris Lattner49f28ca2009-03-05 08:00:35 +00001324 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions,
1325 PP.getSourceManager(),
1326 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001328 // Do not enter a scope for the brace, as the arguments are in the same scope
1329 // (the function body) as the body itself. Instead, just read the statement
1330 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001331 OwningStmtResult FnBody(ParseCompoundStatementBody());
1332
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001333 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001334 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001335 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001336 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001337
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001338 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001339}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001340
Sebastian Redld3a413d2009-04-26 20:35:05 +00001341/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1342///
1343/// function-try-block:
1344/// 'try' ctor-initializer[opt] compound-statement handler-seq
1345///
1346Parser::DeclPtrTy Parser::ParseFunctionTryBlock(DeclPtrTy Decl) {
1347 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1348 SourceLocation TryLoc = ConsumeToken();
1349
1350 PrettyStackTraceActionsDecl CrashInfo(Decl, TryLoc, Actions,
1351 PP.getSourceManager(),
1352 "parsing function try block");
1353
1354 // Constructor initializer list?
1355 if (Tok.is(tok::colon))
1356 ParseConstructorInitializer(Decl);
1357
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001358 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001359 OwningStmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
1360 // If we failed to parse the try-catch, we just give the function an empty
1361 // compound statement as the body.
1362 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001363 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001364 MultiStmtArg(Actions), false);
1365
1366 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody));
1367}
1368
Sebastian Redla0fd8652008-12-21 16:41:36 +00001369/// ParseCXXTryBlock - Parse a C++ try-block.
1370///
1371/// try-block:
1372/// 'try' compound-statement handler-seq
1373///
Sebastian Redla0fd8652008-12-21 16:41:36 +00001374Parser::OwningStmtResult Parser::ParseCXXTryBlock() {
1375 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1376
1377 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001378 return ParseCXXTryBlockCommon(TryLoc);
1379}
1380
1381/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1382/// function-try-block.
1383///
1384/// try-block:
1385/// 'try' compound-statement handler-seq
1386///
1387/// function-try-block:
1388/// 'try' ctor-initializer[opt] compound-statement handler-seq
1389///
1390/// handler-seq:
1391/// handler handler-seq[opt]
1392///
1393Parser::OwningStmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001394 if (Tok.isNot(tok::l_brace))
1395 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1396 OwningStmtResult TryBlock(ParseCompoundStatement());
1397 if (TryBlock.isInvalid())
1398 return move(TryBlock);
1399
1400 StmtVector Handlers(Actions);
1401 if (Tok.isNot(tok::kw_catch))
1402 return StmtError(Diag(Tok, diag::err_expected_catch));
1403 while (Tok.is(tok::kw_catch)) {
1404 OwningStmtResult Handler(ParseCXXCatchBlock());
1405 if (!Handler.isInvalid())
1406 Handlers.push_back(Handler.release());
1407 }
1408 // Don't bother creating the full statement if we don't have any usable
1409 // handlers.
1410 if (Handlers.empty())
1411 return StmtError();
1412
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001413 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001414}
1415
1416/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1417///
1418/// handler:
1419/// 'catch' '(' exception-declaration ')' compound-statement
1420///
1421/// exception-declaration:
1422/// type-specifier-seq declarator
1423/// type-specifier-seq abstract-declarator
1424/// type-specifier-seq
1425/// '...'
1426///
1427Parser::OwningStmtResult Parser::ParseCXXCatchBlock() {
1428 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1429
1430 SourceLocation CatchLoc = ConsumeToken();
1431
1432 SourceLocation LParenLoc = Tok.getLocation();
1433 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1434 return StmtError();
1435
1436 // C++ 3.3.2p3:
1437 // The name in a catch exception-declaration is local to the handler and
1438 // shall not be redeclared in the outermost block of the handler.
1439 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1440
1441 // exception-declaration is equivalent to '...' or a parameter-declaration
1442 // without default arguments.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001443 DeclPtrTy ExceptionDecl;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001444 if (Tok.isNot(tok::ellipsis)) {
1445 DeclSpec DS;
Sebastian Redl4b07b292008-12-22 19:15:10 +00001446 if (ParseCXXTypeSpecifierSeq(DS))
1447 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001448 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1449 ParseDeclarator(ExDecl);
1450 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl);
1451 } else
1452 ConsumeToken();
1453
1454 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
1455 return StmtError();
1456
1457 if (Tok.isNot(tok::l_brace))
1458 return StmtError(Diag(Tok, diag::err_expected_lbrace));
1459
1460 OwningStmtResult Block(ParseCompoundStatement());
1461 if (Block.isInvalid())
1462 return move(Block);
1463
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001464 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001465}