blob: 8b5277bc365360a4bcb29d721c3acf5b404c9084 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner39146d62008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroffb746ce82008-02-07 23:24:32 +000019#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Parse/DeclSpec.h"
21#include "clang/Parse/Scope.h"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000040/// [C++] declaration-statement
Fariborz Jahanianb384d322007-10-04 20:19:06 +000041/// [OBC] objc-throw-statement
42/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000043/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000044/// [GNU] asm-statement
45/// [OMP] openmp-construct [TODO]
46///
47/// labeled-statement:
48/// identifier ':' statement
49/// 'case' constant-expression ':' statement
50/// 'default' ':' statement
51///
52/// selection-statement:
53/// if-statement
54/// switch-statement
55///
56/// iteration-statement:
57/// while-statement
58/// do-statement
59/// for-statement
60///
61/// expression-statement:
62/// expression[opt] ';'
63///
64/// jump-statement:
65/// 'goto' identifier ';'
66/// 'continue' ';'
67/// 'break' ';'
68/// 'return' expression[opt] ';'
69/// [GNU] 'goto' '*' expression ';'
70///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000071/// [OBC] objc-throw-statement:
72/// [OBC] '@' 'throw' expression ';'
73/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000074///
75Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
76 const char *SemiError = 0;
Sebastian Redl15faa7f2008-12-09 20:22:58 +000077 OwningStmtResult Res(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000078
Reid Spencer5f016e22007-07-11 17:01:13 +000079 // Cases in this switch statement should fall through if the parser expects
80 // the token to end in a semicolon (in which case SemiError should be set),
81 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000082 tok::TokenKind Kind = Tok.getKind();
83 SourceLocation AtLoc;
84 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000085 case tok::at: // May be a @try or @throw statement
86 {
87 AtLoc = ConsumeToken(); // consume @
Steve Naroff64515f32008-02-05 21:27:35 +000088 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000089 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000090
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +000091 case tok::identifier:
92 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
93 // identifier ':' statement
94 return ParseLabeledStatement();
95 }
96 // PASS THROUGH.
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098 default:
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +000099 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000100 SourceLocation DeclStart = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000101 DeclTy *Decl = ParseDeclaration(Declarator::BlockContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000102 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000103 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000104 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 Diag(Tok, diag::err_expected_statement);
106 return true;
107 } else {
108 // expression[opt] ';'
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000109 OwningExprResult Expr(Actions, ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000110 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 // If the expression is invalid, skip ahead to the next semicolon. Not
112 // doing this opens us up to the possibility of infinite loops if
113 // ParseExpression does not consume any tokens.
114 SkipUntil(tok::semi);
115 return true;
116 }
117 // Otherwise, eat the semicolon.
118 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000119 return Actions.ActOnExprStmt(Expr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121
122 case tok::kw_case: // C99 6.8.1: labeled-statement
123 return ParseCaseStatement();
124 case tok::kw_default: // C99 6.8.1: labeled-statement
125 return ParseDefaultStatement();
126
127 case tok::l_brace: // C99 6.8.2: compound-statement
128 return ParseCompoundStatement();
129 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000130 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
132 case tok::kw_if: // C99 6.8.4.1: if-statement
133 return ParseIfStatement();
134 case tok::kw_switch: // C99 6.8.4.2: switch-statement
135 return ParseSwitchStatement();
136
137 case tok::kw_while: // C99 6.8.5.1: while-statement
138 return ParseWhileStatement();
139 case tok::kw_do: // C99 6.8.5.2: do-statement
140 Res = ParseDoStatement();
141 SemiError = "do/while loop";
142 break;
143 case tok::kw_for: // C99 6.8.5.3: for-statement
144 return ParseForStatement();
145
146 case tok::kw_goto: // C99 6.8.6.1: goto-statement
147 Res = ParseGotoStatement();
148 SemiError = "goto statement";
149 break;
150 case tok::kw_continue: // C99 6.8.6.2: continue-statement
151 Res = ParseContinueStatement();
152 SemiError = "continue statement";
153 break;
154 case tok::kw_break: // C99 6.8.6.3: break-statement
155 Res = ParseBreakStatement();
156 SemiError = "break statement";
157 break;
158 case tok::kw_return: // C99 6.8.6.4: return-statement
159 Res = ParseReturnStatement();
160 SemiError = "return statement";
161 break;
162
163 case tok::kw_asm:
Steve Naroffd62701b2008-02-07 03:50:06 +0000164 bool msAsm = false;
165 Res = ParseAsmStatement(msAsm);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000166 if (msAsm) return Res.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 SemiError = "asm statement";
168 break;
169 }
170
171 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000172 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000174 } else if (!Res.isInvalid()) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000175 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner19504402008-11-13 18:52:53 +0000176 // Skip until we see a } or ;, but don't eat it.
177 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000179 return Res.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000182/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000183///
184/// labeled-statement:
185/// identifier ':' statement
186/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000187///
188Parser::StmtResult Parser::ParseLabeledStatement() {
189 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
190 "Not an identifier!");
191
192 Token IdentTok = Tok; // Save the whole token.
193 ConsumeToken(); // eat the identifier.
194
195 assert(Tok.is(tok::colon) && "Not a label!");
196
197 // identifier ':' statement
198 SourceLocation ColonLoc = ConsumeToken();
199
200 // Read label attributes, if present.
201 DeclTy *AttrList = 0;
202 if (Tok.is(tok::kw___attribute))
203 // TODO: save these somewhere.
204 AttrList = ParseAttributes();
205
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000206 OwningStmtResult SubStmt(Actions, ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000207
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000208 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000209 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000210 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000211
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000212 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
213 IdentTok.getIdentifierInfo(),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000214 ColonLoc, SubStmt.release());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000215}
Reid Spencer5f016e22007-07-11 17:01:13 +0000216
217/// ParseCaseStatement
218/// labeled-statement:
219/// 'case' constant-expression ':' statement
220/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
221///
222/// Note that this does not parse the 'statement' at the end.
223///
224Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000225 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
227
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000228 OwningExprResult LHS(Actions, ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000229 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 SkipUntil(tok::colon);
231 return true;
232 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 // GNU case range extension.
235 SourceLocation DotDotDotLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000236 OwningExprResult RHS(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000237 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 Diag(Tok, diag::ext_gnu_case_range);
239 DotDotDotLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000240
241 RHS = ParseConstantExpression();
242 if (RHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 SkipUntil(tok::colon);
244 return true;
245 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000247
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000248 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000249 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 SkipUntil(tok::colon);
251 return true;
252 }
253
254 SourceLocation ColonLoc = ConsumeToken();
255
256 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000257 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 Diag(Tok, diag::err_label_end_of_compound_statement);
259 return true;
260 }
261
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000262 OwningStmtResult SubStmt(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000263
264 // Broken substmt shouldn't prevent the case from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000265 if (SubStmt.isInvalid())
Steve Naroff1b273c42007-09-16 14:56:35 +0000266 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267
Sebastian Redleffa8d12008-12-10 00:02:53 +0000268 return Actions.ActOnCaseStmt(CaseLoc, LHS.release(), DotDotDotLoc,
269 RHS.release(), ColonLoc, SubStmt.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000270}
271
272/// ParseDefaultStatement
273/// labeled-statement:
274/// 'default' ':' statement
275/// Note that this does not parse the 'statement' at the end.
276///
277Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000278 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
280
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000281 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000282 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 SkipUntil(tok::colon);
284 return true;
285 }
286
287 SourceLocation ColonLoc = ConsumeToken();
288
289 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000290 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 Diag(Tok, diag::err_label_end_of_compound_statement);
292 return true;
293 }
294
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000295 OwningStmtResult SubStmt(Actions, ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000296 if (SubStmt.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 return true;
298
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000299 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000300 SubStmt.release(), CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000301}
302
303
304/// ParseCompoundStatement - Parse a "{}" block.
305///
306/// compound-statement: [C99 6.8.2]
307/// { block-item-list[opt] }
308/// [GNU] { label-declarations block-item-list } [TODO]
309///
310/// block-item-list:
311/// block-item
312/// block-item-list block-item
313///
314/// block-item:
315/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000316/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000317/// statement
318/// [OMP] openmp-directive [TODO]
319///
320/// [GNU] label-declarations:
321/// [GNU] label-declaration
322/// [GNU] label-declarations label-declaration
323///
324/// [GNU] label-declaration:
325/// [GNU] '__label__' identifier-list ';'
326///
327/// [OMP] openmp-directive: [TODO]
328/// [OMP] barrier-directive
329/// [OMP] flush-directive
330///
Chris Lattner98414c12007-08-31 21:49:55 +0000331Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000332 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000333
Chris Lattner31e05722007-08-26 06:24:45 +0000334 // Enter a scope to hold everything within the compound stmt. Compound
335 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000336 ParseScope CompoundScope(this, Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000337
338 // Parse the statements in the body.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000339 OwningStmtResult Body(Actions, ParseCompoundStatementBody(isStmtExpr));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000340 return Body.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000341}
342
343
344/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000345/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000346/// consume the '}' at the end of the block. It does not manipulate the scope
347/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000348Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
350
351 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000352 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000353
354 typedef StmtVector StmtsTy;
355 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000356 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000357 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000358 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000359 R = ParseStatementOrDeclaration(false);
360 } else {
361 // __extension__ can start declarations and it can also be a unary
362 // operator for expressions. Consume multiple __extension__ markers here
363 // until we can determine which is which.
364 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000365 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000366 ConsumeToken();
367
Chris Lattner043a0b52008-03-13 06:32:11 +0000368 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000369 ExtensionRAIIObject O(Diags); // Use RAII to do this.
370
Chris Lattner45a566c2007-08-27 01:01:57 +0000371 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000372 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000373 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000374 SourceLocation DeclStart = Tok.getLocation();
375 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
376 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000377 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000378 } else {
379 // Otherwise this was a unary __extension__ marker. Parse the
380 // subexpression and add the __extension__ unary op.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000381 OwningExprResult Res(Actions, ParseCastExpression(false));
Chris Lattner043a0b52008-03-13 06:32:11 +0000382
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000383 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000384 SkipUntil(tok::semi);
385 continue;
386 }
387
388 // Add the __extension__ node to the AST.
Douglas Gregor74253732008-11-19 15:42:04 +0000389 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000390 Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000391 if (Res.isInvalid())
Chris Lattner45a566c2007-08-27 01:01:57 +0000392 continue;
393
Chris Lattner39146d62008-10-20 06:51:33 +0000394 // Eat the semicolon at the end of stmt and convert the expr into a
395 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000396 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000397 R = Actions.ActOnExprStmt(Res.release());
Chris Lattner45a566c2007-08-27 01:01:57 +0000398 }
399 }
400
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000401 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000402 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 }
404
405 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000406 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000408 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 }
410
411 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000412 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000413 Stmts.take(), Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414}
415
416/// ParseIfStatement
417/// if-statement: [C99 6.8.4.1]
418/// 'if' '(' expression ')' statement
419/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000420/// [C++] 'if' '(' condition ')' statement
421/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000422///
423Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000424 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
426
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000427 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000428 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 SkipUntil(tok::semi);
430 return true;
431 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000432
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000433 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
434
Chris Lattner22153252007-08-26 23:08:06 +0000435 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
436 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000437 //
438 // C++ 6.4p3:
439 // A name introduced by a declaration in a condition is in scope from its
440 // point of declaration until the end of the substatements controlled by the
441 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000442 // C++ 3.3.2p4:
443 // Names declared in the for-init-statement, and in the condition of if,
444 // while, for, and switch statements are local to the if, while, for, or
445 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000446 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000447 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000448
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000450 OwningExprResult CondExp(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000451 if (getLang().CPlusPlus) {
452 SourceLocation LParenLoc = ConsumeParen();
453 CondExp = ParseCXXCondition();
454 MatchRHSPunctuation(tok::r_paren, LParenLoc);
455 } else {
456 CondExp = ParseSimpleParenExpression();
457 }
458
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000459 if (CondExp.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 SkipUntil(tok::semi);
461 return true;
462 }
463
Chris Lattner0ecea032007-08-22 05:28:50 +0000464 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000465 // there is no compound stmt. C90 does not have this clause. We only do this
466 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000467 //
468 // C++ 6.4p1:
469 // The substatement in a selection-statement (each substatement, in the else
470 // form of the if statement) implicitly defines a local scope.
471 //
472 // For C++ we create a scope for the condition and a new scope for
473 // substatements because:
474 // -When the 'then' scope exits, we want the condition declaration to still be
475 // active for the 'else' scope too.
476 // -Sema will detect name clashes by considering declarations of a
477 // 'ControlScope' as part of its direct subscope.
478 // -If we wanted the condition and substatement to be in the same scope, we
479 // would have to notify ParseStatement not to create a new scope. It's
480 // simpler to let it create a new scope.
481 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000482 ParseScope InnerScope(this, Scope::DeclScope,
483 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000484
Chris Lattnerb96728d2007-10-29 05:08:52 +0000485 // Read the 'then' stmt.
486 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000487 OwningStmtResult ThenStmt(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000488
Chris Lattnera36ce712007-08-22 05:16:28 +0000489 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000490 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000491
492 // If it has an else, parse it.
493 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000494 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000495 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000496
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000497 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000499
Chris Lattner0ecea032007-08-22 05:28:50 +0000500 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000501 // there is no compound stmt. C90 does not have this clause. We only do
502 // this if the body isn't a compound statement to avoid push/pop in common
503 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000504 //
505 // C++ 6.4p1:
506 // The substatement in a selection-statement (each substatement, in the else
507 // form of the if statement) implicitly defines a local scope.
508 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000509 ParseScope InnerScope(this, Scope::DeclScope,
510 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000511
Chris Lattnerb96728d2007-10-29 05:08:52 +0000512 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000514
515 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000516 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 }
518
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000519 IfScope.Exit();
Chris Lattner22153252007-08-26 23:08:06 +0000520
Chris Lattnerb96728d2007-10-29 05:08:52 +0000521 // If the then or else stmt is invalid and the other is valid (and present),
522 // make turn the invalid one into a null stmt to avoid dropping the other
523 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000524 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
525 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
526 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000527 // Both invalid, or one is invalid and other is non-present: return error.
Chris Lattnerb96728d2007-10-29 05:08:52 +0000528 return true;
529 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000530
Chris Lattnerb96728d2007-10-29 05:08:52 +0000531 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000532 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000533 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000534 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000535 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000536
Sebastian Redleffa8d12008-12-10 00:02:53 +0000537 return Actions.ActOnIfStmt(IfLoc, CondExp.release(), ThenStmt.release(),
538 ElseLoc, ElseStmt.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000539}
540
541/// ParseSwitchStatement
542/// switch-statement:
543/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000544/// [C++] 'switch' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000545Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000546 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
548
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000549 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000550 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 SkipUntil(tok::semi);
552 return true;
553 }
Chris Lattner22153252007-08-26 23:08:06 +0000554
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000555 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
556
Chris Lattner22153252007-08-26 23:08:06 +0000557 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
558 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000559 //
560 // C++ 6.4p3:
561 // A name introduced by a declaration in a condition is in scope from its
562 // point of declaration until the end of the substatements controlled by the
563 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000564 // C++ 3.3.2p4:
565 // Names declared in the for-init-statement, and in the condition of if,
566 // while, for, and switch statements are local to the if, while, for, or
567 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000568 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000569 unsigned ScopeFlags
570 = C99orCXX? Scope::BreakScope | Scope::DeclScope | Scope::ControlScope
571 : Scope::BreakScope;
572 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000573
574 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000575 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000576 if (getLang().CPlusPlus) {
577 SourceLocation LParenLoc = ConsumeParen();
578 Cond = ParseCXXCondition();
579 MatchRHSPunctuation(tok::r_paren, LParenLoc);
580 } else {
581 Cond = ParseSimpleParenExpression();
582 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000583
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000584 if (Cond.isInvalid())
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000585 return true;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000586
Sebastian Redleffa8d12008-12-10 00:02:53 +0000587 OwningStmtResult Switch(Actions,
588 Actions.ActOnStartOfSwitchStmt(Cond.release()));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000589
Chris Lattner0ecea032007-08-22 05:28:50 +0000590 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000591 // there is no compound stmt. C90 does not have this clause. We only do this
592 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000593 //
594 // C++ 6.4p1:
595 // The substatement in a selection-statement (each substatement, in the else
596 // form of the if statement) implicitly defines a local scope.
597 //
598 // See comments in ParseIfStatement for why we create a scope for the
599 // condition and a new scope for substatement in C++.
600 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000601 ParseScope InnerScope(this, Scope::DeclScope,
602 C99orCXX && Tok.isNot(tok::l_brace));
Chris Lattner0ecea032007-08-22 05:28:50 +0000603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000605 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000606
Chris Lattner0ecea032007-08-22 05:28:50 +0000607 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000608 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000609
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000610 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000611 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000612 // FIXME: Remove the case statement list from the Switch statement.
613 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000614
615 SwitchScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000616
Sebastian Redleffa8d12008-12-10 00:02:53 +0000617 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.release(),
618 Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000619}
620
621/// ParseWhileStatement
622/// while-statement: [C99 6.8.5.1]
623/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000624/// [C++] 'while' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000625Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000626 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 SourceLocation WhileLoc = Tok.getLocation();
628 ConsumeToken(); // eat the 'while'.
629
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000630 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000631 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 SkipUntil(tok::semi);
633 return true;
634 }
635
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000636 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
637
Chris Lattner22153252007-08-26 23:08:06 +0000638 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
639 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000640 //
641 // C++ 6.4p3:
642 // A name introduced by a declaration in a condition is in scope from its
643 // point of declaration until the end of the substatements controlled by the
644 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000645 // C++ 3.3.2p4:
646 // Names declared in the for-init-statement, and in the condition of if,
647 // while, for, and switch statements are local to the if, while, for, or
648 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000649 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000650 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000651 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000652 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
653 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000654 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000655 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
656 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000657
658 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000659 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000660 if (getLang().CPlusPlus) {
661 SourceLocation LParenLoc = ConsumeParen();
662 Cond = ParseCXXCondition();
663 MatchRHSPunctuation(tok::r_paren, LParenLoc);
664 } else {
665 Cond = ParseSimpleParenExpression();
666 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000667
Chris Lattner0ecea032007-08-22 05:28:50 +0000668 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000669 // there is no compound stmt. C90 does not have this clause. We only do this
670 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000671 //
672 // C++ 6.5p2:
673 // The substatement in an iteration-statement implicitly defines a local scope
674 // which is entered and exited each time through the loop.
675 //
676 // See comments in ParseIfStatement for why we create a scope for the
677 // condition and a new scope for substatement in C++.
678 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000679 ParseScope InnerScope(this, Scope::DeclScope,
680 C99orCXX && Tok.isNot(tok::l_brace));
Chris Lattner0ecea032007-08-22 05:28:50 +0000681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000683 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000684
Chris Lattner0ecea032007-08-22 05:28:50 +0000685 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000686 InnerScope.Exit();
687 WhileScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000688
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000689 if (Cond.isInvalid() || Body.isInvalid()) return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
Sebastian Redleffa8d12008-12-10 00:02:53 +0000691 return Actions.ActOnWhileStmt(WhileLoc, Cond.release(), Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000692}
693
694/// ParseDoStatement
695/// do-statement: [C99 6.8.5.2]
696/// 'do' statement 'while' '(' expression ')' ';'
697/// Note: this lets the caller parse the end ';'.
698Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000699 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
701
Chris Lattner22153252007-08-26 23:08:06 +0000702 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
703 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000704 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000705 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000706 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000707 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000708 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
709
710 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000711
Chris Lattner0ecea032007-08-22 05:28:50 +0000712 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000713 // there is no compound stmt. C90 does not have this clause. We only do this
714 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000715 //
716 // C++ 6.5p2:
717 // The substatement in an iteration-statement implicitly defines a local scope
718 // which is entered and exited each time through the loop.
719 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000720 ParseScope InnerScope(this, Scope::DeclScope,
721 (getLang().C99 || getLang().CPlusPlus) &&
722 Tok.isNot(tok::l_brace));
Chris Lattner0ecea032007-08-22 05:28:50 +0000723
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000725 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000726
Chris Lattner0ecea032007-08-22 05:28:50 +0000727 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000728 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000729
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000730 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000731 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000732 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000733 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000734 SkipUntil(tok::semi, false, true);
735 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 return true;
737 }
738 SourceLocation WhileLoc = ConsumeToken();
739
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000740 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000741 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000742 SkipUntil(tok::semi, false, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 return true;
744 }
745
746 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000747 OwningExprResult Cond(Actions, ParseSimpleParenExpression());
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000748 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000749
750 if (Cond.isInvalid() || Body.isInvalid()) return true;
751
Sebastian Redleffa8d12008-12-10 00:02:53 +0000752 return Actions.ActOnDoStmt(DoLoc, Body.release(), WhileLoc, Cond.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000753}
754
755/// ParseForStatement
756/// for-statement: [C99 6.8.5.3]
757/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
758/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000759/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
760/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000761/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
762/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000763///
764/// [C++] for-init-statement:
765/// [C++] expression-statement
766/// [C++] simple-declaration
767///
Reid Spencer5f016e22007-07-11 17:01:13 +0000768Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000769 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
771
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000772 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000773 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 SkipUntil(tok::semi);
775 return true;
776 }
777
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000778 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
779
Chris Lattner22153252007-08-26 23:08:06 +0000780 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
781 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000782 //
783 // C++ 6.4p3:
784 // A name introduced by a declaration in a condition is in scope from its
785 // point of declaration until the end of the substatements controlled by the
786 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000787 // C++ 3.3.2p4:
788 // Names declared in the for-init-statement, and in the condition of if,
789 // while, for, and switch statements are local to the if, while, for, or
790 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000791 // C++ 6.5.3p1:
792 // Names declared in the for-init-statement are in the same declarative-region
793 // as those declared in the condition.
794 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000795 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000796 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000797 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
798 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000799 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000800 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
801
802 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000803
804 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000805 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000806
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000807 bool ForEach = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000808 OwningStmtResult FirstPart(Actions), ThirdPart(Actions);
809 OwningExprResult SecondPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000812 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 // no first part, eat the ';'.
814 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000815 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000817 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000819
820 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000821 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000822 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000823 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
824 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000825 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000826 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000827 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000828 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 } else {
830 Value = ParseExpression();
831
832 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000833 if (!Value.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000834 FirstPart = Actions.ActOnExprStmt(Value.release());
835
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000836 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000838 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000839 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000840 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000841 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000842 }
843 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000844 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 SkipUntil(tok::semi);
846 }
847 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000848 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000849 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000850 // Parse the second part of the for specifier.
851 if (Tok.is(tok::semi)) { // for (...;;
852 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000853 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000854 SecondPart = getLang().CPlusPlus ? ParseCXXCondition()
855 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000856 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000857
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000858 if (Tok.is(tok::semi)) {
859 ConsumeToken();
860 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000861 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000862 SkipUntil(tok::semi);
863 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000864
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000865 // Parse the third part of the for specifier.
866 if (Tok.is(tok::r_paren)) { // for (...;...;)
867 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000868 } else {
869 Value = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000870 if (!Value.isInvalid()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000871 // Turn the expression into a stmt.
Sebastian Redleffa8d12008-12-10 00:02:53 +0000872 ThirdPart = Actions.ActOnExprStmt(Value.release());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000873 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 }
875 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 // Match the ')'.
877 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000878
Chris Lattner0ecea032007-08-22 05:28:50 +0000879 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000880 // there is no compound stmt. C90 does not have this clause. We only do this
881 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000882 //
883 // C++ 6.5p2:
884 // The substatement in an iteration-statement implicitly defines a local scope
885 // which is entered and exited each time through the loop.
886 //
887 // See comments in ParseIfStatement for why we create a scope for
888 // for-init-statement/condition and a new scope for substatement in C++.
889 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000890 ParseScope InnerScope(this, Scope::DeclScope,
891 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000892
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000894 OwningStmtResult Body(Actions, ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000895
Chris Lattner0ecea032007-08-22 05:28:50 +0000896 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000897 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000900 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000901
902 if (Body.isInvalid())
903 return true;
Sebastian Redleffa8d12008-12-10 00:02:53 +0000904
905 if (!ForEach)
906 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.release(),
907 SecondPart.release(), ThirdPart.release(),
908 RParenLoc, Body.release());
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000909 else
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000910 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000911 FirstPart.release(),
912 SecondPart.release(),
913 RParenLoc, Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000914}
915
916/// ParseGotoStatement
917/// jump-statement:
918/// 'goto' identifier ';'
919/// [GNU] 'goto' '*' expression ';'
920///
921/// Note: this lets the caller parse the end ';'.
922///
923Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000924 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
926
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000927 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000928 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000929 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 Tok.getIdentifierInfo());
931 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000932 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 // GNU indirect goto extension.
934 Diag(Tok, diag::ext_gnu_indirect_goto);
935 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000936 OwningExprResult R(Actions, ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000937 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 SkipUntil(tok::semi, false, true);
939 return true;
940 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000941 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.release());
Chris Lattner95cfb852007-07-22 04:13:33 +0000942 } else {
943 Diag(Tok, diag::err_expected_ident);
944 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000946
Sebastian Redleffa8d12008-12-10 00:02:53 +0000947 return Res.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000948}
949
950/// ParseContinueStatement
951/// jump-statement:
952/// 'continue' ';'
953///
954/// Note: this lets the caller parse the end ';'.
955///
956Parser::StmtResult Parser::ParseContinueStatement() {
957 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000958 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000959}
960
961/// ParseBreakStatement
962/// jump-statement:
963/// 'break' ';'
964///
965/// Note: this lets the caller parse the end ';'.
966///
967Parser::StmtResult Parser::ParseBreakStatement() {
968 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000969 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000970}
971
972/// ParseReturnStatement
973/// jump-statement:
974/// 'return' expression[opt] ';'
975Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000976 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
978
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000979 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000980 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000982 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 SkipUntil(tok::semi, false, true);
984 return true;
985 }
986 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000987 return Actions.ActOnReturnStmt(ReturnLoc, R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000988}
989
Steve Naroff5f8aa692008-02-11 23:15:56 +0000990/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
991/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +0000992Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +0000993 if (Tok.is(tok::l_brace)) {
994 unsigned short savedBraceCount = BraceCount;
995 do {
996 ConsumeAnyToken();
997 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
998 } else {
999 // From the MS website: If used without braces, the __asm keyword means
1000 // that the rest of the line is an assembly-language statement.
1001 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001002 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001003 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1004 do {
1005 ConsumeAnyToken();
1006 TokLoc = Tok.getLocation();
1007 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1008 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1009 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001010 }
Steve Naroffd77bc282008-04-07 21:06:54 +00001011 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001012}
1013
Reid Spencer5f016e22007-07-11 17:01:13 +00001014/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001015/// asm-statement:
1016/// gnu-asm-statement
1017/// ms-asm-statement
1018///
1019/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001020/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1021///
1022/// [GNU] asm-argument:
1023/// asm-string-literal
1024/// asm-string-literal ':' asm-operands[opt]
1025/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1026/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1027/// ':' asm-clobbers
1028///
1029/// [GNU] asm-clobbers:
1030/// asm-string-literal
1031/// asm-clobbers ',' asm-string-literal
1032///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001033/// [MS] ms-asm-statement:
1034/// '__asm' assembly-instruction ';'[opt]
1035/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1036///
1037/// [MS] assembly-instruction-list:
1038/// assembly-instruction ';'[opt]
1039/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1040///
Steve Naroffd62701b2008-02-07 03:50:06 +00001041Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001042 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001043 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001044
Steve Naroff5f8aa692008-02-11 23:15:56 +00001045 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001046 msAsm = true;
1047 return FuzzyParseMicrosoftAsmStatement();
1048 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 DeclSpec DS;
1050 SourceLocation Loc = Tok.getLocation();
1051 ParseTypeQualifierListOpt(DS);
1052
1053 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1054 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001055 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001057 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Reid Spencer5f016e22007-07-11 17:01:13 +00001058
1059 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001060 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001061 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001062 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001063 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 SkipUntil(tok::r_paren);
1065 return true;
1066 }
1067 Loc = ConsumeParen();
1068
Sebastian Redleffa8d12008-12-10 00:02:53 +00001069 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001070 if (AsmString.isInvalid())
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001071 return true;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001072
Anders Carlssonb235fc22007-11-22 01:36:19 +00001073 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001074 ExprVector Constraints(Actions);
1075 ExprVector Exprs(Actions);
1076 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001077
Anders Carlssondfab34a2008-02-05 23:03:50 +00001078 unsigned NumInputs = 0, NumOutputs = 0;
1079
1080 SourceLocation RParenLoc;
1081 if (Tok.is(tok::r_paren)) {
1082 // We have a simple asm expression
1083 isSimple = true;
1084
1085 RParenLoc = ConsumeParen();
1086 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001087 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001088 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1089 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001090
1091 NumOutputs = Names.size();
1092
1093 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001094 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1095 return true;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001096
Anders Carlssondfab34a2008-02-05 23:03:50 +00001097 assert(Names.size() == Constraints.size() &&
1098 Constraints.size() == Exprs.size()
1099 && "Input operand size mismatch!");
1100
1101 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001102
Anders Carlssondfab34a2008-02-05 23:03:50 +00001103 // Parse the clobbers, if present.
1104 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001105 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001106
Anders Carlssondfab34a2008-02-05 23:03:50 +00001107 // Parse the asm-string list for clobbers.
1108 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001109 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001110
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001111 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001112 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001113
1114 Clobbers.push_back(Clobber.release());
1115
Anders Carlssondfab34a2008-02-05 23:03:50 +00001116 if (Tok.isNot(tok::comma)) break;
1117 ConsumeToken();
1118 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001120
Anders Carlssondfab34a2008-02-05 23:03:50 +00001121 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001123
Anders Carlssondfab34a2008-02-05 23:03:50 +00001124 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1125 NumOutputs, NumInputs,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001126 &Names[0], Constraints.take(),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001127 Exprs.take(), AsmString.release(),
Sebastian Redla55e52c2008-11-25 22:21:31 +00001128 Clobbers.size(), Clobbers.take(),
Anders Carlssonb235fc22007-11-22 01:36:19 +00001129 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001130}
1131
1132/// ParseAsmOperands - Parse the asm-operands production as used by
1133/// asm-statement. We also parse a leading ':' token. If the leading colon is
1134/// not present, we do not parse anything.
1135///
1136/// [GNU] asm-operands:
1137/// asm-operand
1138/// asm-operands ',' asm-operand
1139///
1140/// [GNU] asm-operand:
1141/// asm-string-literal '(' expression ')'
1142/// '[' identifier ']' asm-string-literal '(' expression ')'
1143///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001144bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001145 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1146 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001148 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 ConsumeToken();
1150
1151 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001152 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001153 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001154
Anders Carlssonb235fc22007-11-22 01:36:19 +00001155 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001157 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 SourceLocation Loc = ConsumeBracket();
1159
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001160 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 Diag(Tok, diag::err_expected_ident);
1162 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001163 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 }
Chris Lattner69efba72007-10-29 04:06:22 +00001165
Anders Carlssonb235fc22007-11-22 01:36:19 +00001166 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001167 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001168
1169 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001171 } else
1172 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001173
Sebastian Redleffa8d12008-12-10 00:02:53 +00001174 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001175 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001176 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001177 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001178 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001179 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001180
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001181 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001182 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001184 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001186
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 // Read the parenthesized expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001188 OwningExprResult Res(Actions, ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001189 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001191 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001193 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001195 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 ConsumeToken();
1197 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001198
1199 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001200}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001201
1202Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1203 SourceLocation L, SourceLocation R) {
1204 // Do not enter a scope for the brace, as the arguments are in the same scope
1205 // (the function body) as the body itself. Instead, just read the statement
1206 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001207 OwningStmtResult FnBody(Actions, ParseCompoundStatementBody());
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001208
1209 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001210 if (FnBody.isInvalid())
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001211 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1212
Sebastian Redleffa8d12008-12-10 00:02:53 +00001213 return Actions.ActOnFinishFunctionBody(Decl, FnBody.release());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001214}