blob: cb35740465a5e2231460ed86e70208d0581a8703 [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
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000512 bool WithinElse = CurScope->isWithinElse();
513 CurScope->setWithinElse(true);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000514 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 ElseStmt = ParseStatement();
Douglas Gregorcaaf29a2008-12-10 23:01:14 +0000516 CurScope->setWithinElse(WithinElse);
Chris Lattnera36ce712007-08-22 05:16:28 +0000517
518 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000519 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 }
521
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000522 IfScope.Exit();
Chris Lattner22153252007-08-26 23:08:06 +0000523
Chris Lattnerb96728d2007-10-29 05:08:52 +0000524 // If the then or else stmt is invalid and the other is valid (and present),
525 // make turn the invalid one into a null stmt to avoid dropping the other
526 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000527 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
528 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
529 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000530 // Both invalid, or one is invalid and other is non-present: return error.
Chris Lattnerb96728d2007-10-29 05:08:52 +0000531 return true;
532 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000533
Chris Lattnerb96728d2007-10-29 05:08:52 +0000534 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000535 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000536 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000537 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000538 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000539
Sebastian Redleffa8d12008-12-10 00:02:53 +0000540 return Actions.ActOnIfStmt(IfLoc, CondExp.release(), ThenStmt.release(),
541 ElseLoc, ElseStmt.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000542}
543
544/// ParseSwitchStatement
545/// switch-statement:
546/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000547/// [C++] 'switch' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000548Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000549 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
551
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000552 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000553 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 SkipUntil(tok::semi);
555 return true;
556 }
Chris Lattner22153252007-08-26 23:08:06 +0000557
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000558 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
559
Chris Lattner22153252007-08-26 23:08:06 +0000560 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
561 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000562 //
563 // C++ 6.4p3:
564 // A name introduced by a declaration in a condition is in scope from its
565 // point of declaration until the end of the substatements controlled by the
566 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000567 // C++ 3.3.2p4:
568 // Names declared in the for-init-statement, and in the condition of if,
569 // while, for, and switch statements are local to the if, while, for, or
570 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000571 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000572 unsigned ScopeFlags
573 = C99orCXX? Scope::BreakScope | Scope::DeclScope | Scope::ControlScope
574 : Scope::BreakScope;
575 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000576
577 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000578 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000579 if (getLang().CPlusPlus) {
580 SourceLocation LParenLoc = ConsumeParen();
581 Cond = ParseCXXCondition();
582 MatchRHSPunctuation(tok::r_paren, LParenLoc);
583 } else {
584 Cond = ParseSimpleParenExpression();
585 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000586
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000587 if (Cond.isInvalid())
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000588 return true;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000589
Sebastian Redleffa8d12008-12-10 00:02:53 +0000590 OwningStmtResult Switch(Actions,
591 Actions.ActOnStartOfSwitchStmt(Cond.release()));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000592
Chris Lattner0ecea032007-08-22 05:28:50 +0000593 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000594 // there is no compound stmt. C90 does not have this clause. We only do this
595 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000596 //
597 // C++ 6.4p1:
598 // The substatement in a selection-statement (each substatement, in the else
599 // form of the if statement) implicitly defines a local scope.
600 //
601 // See comments in ParseIfStatement for why we create a scope for the
602 // condition and a new scope for substatement in C++.
603 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000604 ParseScope InnerScope(this, Scope::DeclScope,
605 C99orCXX && Tok.isNot(tok::l_brace));
Chris Lattner0ecea032007-08-22 05:28:50 +0000606
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000608 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000609
Chris Lattner0ecea032007-08-22 05:28:50 +0000610 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000611 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000612
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000613 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000614 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000615 // FIXME: Remove the case statement list from the Switch statement.
616 }
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000617
618 SwitchScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000619
Sebastian Redleffa8d12008-12-10 00:02:53 +0000620 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.release(),
621 Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000622}
623
624/// ParseWhileStatement
625/// while-statement: [C99 6.8.5.1]
626/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000627/// [C++] 'while' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000628Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000629 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 SourceLocation WhileLoc = Tok.getLocation();
631 ConsumeToken(); // eat the 'while'.
632
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000633 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000634 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 SkipUntil(tok::semi);
636 return true;
637 }
638
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000639 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
640
Chris Lattner22153252007-08-26 23:08:06 +0000641 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
642 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000643 //
644 // C++ 6.4p3:
645 // A name introduced by a declaration in a condition is in scope from its
646 // point of declaration until the end of the substatements controlled by the
647 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000648 // C++ 3.3.2p4:
649 // Names declared in the for-init-statement, and in the condition of if,
650 // while, for, and switch statements are local to the if, while, for, or
651 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000652 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000653 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000654 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000655 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
656 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000657 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000658 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
659 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000660
661 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000662 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000663 if (getLang().CPlusPlus) {
664 SourceLocation LParenLoc = ConsumeParen();
665 Cond = ParseCXXCondition();
666 MatchRHSPunctuation(tok::r_paren, LParenLoc);
667 } else {
668 Cond = ParseSimpleParenExpression();
669 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000670
Chris Lattner0ecea032007-08-22 05:28:50 +0000671 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000672 // there is no compound stmt. C90 does not have this clause. We only do this
673 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000674 //
675 // C++ 6.5p2:
676 // The substatement in an iteration-statement implicitly defines a local scope
677 // which is entered and exited each time through the loop.
678 //
679 // See comments in ParseIfStatement for why we create a scope for the
680 // condition and a new scope for substatement in C++.
681 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000682 ParseScope InnerScope(this, Scope::DeclScope,
683 C99orCXX && Tok.isNot(tok::l_brace));
Chris Lattner0ecea032007-08-22 05:28:50 +0000684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000686 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000687
Chris Lattner0ecea032007-08-22 05:28:50 +0000688 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000689 InnerScope.Exit();
690 WhileScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000691
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000692 if (Cond.isInvalid() || Body.isInvalid()) return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000693
Sebastian Redleffa8d12008-12-10 00:02:53 +0000694 return Actions.ActOnWhileStmt(WhileLoc, Cond.release(), Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000695}
696
697/// ParseDoStatement
698/// do-statement: [C99 6.8.5.2]
699/// 'do' statement 'while' '(' expression ')' ';'
700/// Note: this lets the caller parse the end ';'.
701Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000702 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
704
Chris Lattner22153252007-08-26 23:08:06 +0000705 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
706 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000707 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +0000708 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000709 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +0000710 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000711 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
712
713 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000714
Chris Lattner0ecea032007-08-22 05:28:50 +0000715 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000716 // there is no compound stmt. C90 does not have this clause. We only do this
717 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000718 //
719 // C++ 6.5p2:
720 // The substatement in an iteration-statement implicitly defines a local scope
721 // which is entered and exited each time through the loop.
722 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000723 ParseScope InnerScope(this, Scope::DeclScope,
724 (getLang().C99 || getLang().CPlusPlus) &&
725 Tok.isNot(tok::l_brace));
Chris Lattner0ecea032007-08-22 05:28:50 +0000726
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000728 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000729
Chris Lattner0ecea032007-08-22 05:28:50 +0000730 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000731 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000732
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000733 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000734 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000735 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000736 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000737 SkipUntil(tok::semi, false, true);
738 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 return true;
740 }
741 SourceLocation WhileLoc = ConsumeToken();
742
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000743 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000744 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000745 SkipUntil(tok::semi, false, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 return true;
747 }
748
749 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000750 OwningExprResult Cond(Actions, ParseSimpleParenExpression());
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000751 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000752
753 if (Cond.isInvalid() || Body.isInvalid()) return true;
754
Sebastian Redleffa8d12008-12-10 00:02:53 +0000755 return Actions.ActOnDoStmt(DoLoc, Body.release(), WhileLoc, Cond.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000756}
757
758/// ParseForStatement
759/// for-statement: [C99 6.8.5.3]
760/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
761/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000762/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
763/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000764/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
765/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000766///
767/// [C++] for-init-statement:
768/// [C++] expression-statement
769/// [C++] simple-declaration
770///
Reid Spencer5f016e22007-07-11 17:01:13 +0000771Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000772 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
774
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000775 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000776 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 SkipUntil(tok::semi);
778 return true;
779 }
780
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000781 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
782
Chris Lattner22153252007-08-26 23:08:06 +0000783 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
784 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000785 //
786 // C++ 6.4p3:
787 // A name introduced by a declaration in a condition is in scope from its
788 // point of declaration until the end of the substatements controlled by the
789 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000790 // C++ 3.3.2p4:
791 // Names declared in the for-init-statement, and in the condition of if,
792 // while, for, and switch statements are local to the if, while, for, or
793 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000794 // C++ 6.5.3p1:
795 // Names declared in the for-init-statement are in the same declarative-region
796 // as those declared in the condition.
797 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000798 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000799 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000800 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
801 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +0000802 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000803 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
804
805 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000806
807 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000808 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000809
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000810 bool ForEach = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000811 OwningStmtResult FirstPart(Actions), ThirdPart(Actions);
812 OwningExprResult SecondPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000815 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // no first part, eat the ';'.
817 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000818 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000820 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000822
823 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000824 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000825 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000826 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
827 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000828 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000829 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000830 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000831 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 } else {
833 Value = ParseExpression();
834
835 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000836 if (!Value.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000837 FirstPart = Actions.ActOnExprStmt(Value.release());
838
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000839 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000841 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000842 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000843 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000844 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000845 }
846 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000847 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 SkipUntil(tok::semi);
849 }
850 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000851 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000852 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000853 // Parse the second part of the for specifier.
854 if (Tok.is(tok::semi)) { // for (...;;
855 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000856 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000857 SecondPart = getLang().CPlusPlus ? ParseCXXCondition()
858 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000859 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000860
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000861 if (Tok.is(tok::semi)) {
862 ConsumeToken();
863 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000864 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000865 SkipUntil(tok::semi);
866 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000867
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000868 // Parse the third part of the for specifier.
869 if (Tok.is(tok::r_paren)) { // for (...;...;)
870 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000871 } else {
872 Value = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000873 if (!Value.isInvalid()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000874 // Turn the expression into a stmt.
Sebastian Redleffa8d12008-12-10 00:02:53 +0000875 ThirdPart = Actions.ActOnExprStmt(Value.release());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000876 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 }
878 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 // Match the ')'.
880 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000881
Chris Lattner0ecea032007-08-22 05:28:50 +0000882 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000883 // there is no compound stmt. C90 does not have this clause. We only do this
884 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000885 //
886 // C++ 6.5p2:
887 // The substatement in an iteration-statement implicitly defines a local scope
888 // which is entered and exited each time through the loop.
889 //
890 // See comments in ParseIfStatement for why we create a scope for
891 // for-init-statement/condition and a new scope for substatement in C++.
892 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000893 ParseScope InnerScope(this, Scope::DeclScope,
894 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000895
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000897 OwningStmtResult Body(Actions, ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000898
Chris Lattner0ecea032007-08-22 05:28:50 +0000899 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000900 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +0000901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000903 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000904
905 if (Body.isInvalid())
906 return true;
Sebastian Redleffa8d12008-12-10 00:02:53 +0000907
908 if (!ForEach)
909 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.release(),
910 SecondPart.release(), ThirdPart.release(),
911 RParenLoc, Body.release());
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000912 else
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000913 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000914 FirstPart.release(),
915 SecondPart.release(),
916 RParenLoc, Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000917}
918
919/// ParseGotoStatement
920/// jump-statement:
921/// 'goto' identifier ';'
922/// [GNU] 'goto' '*' expression ';'
923///
924/// Note: this lets the caller parse the end ';'.
925///
926Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000927 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
929
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000930 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000931 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000932 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 Tok.getIdentifierInfo());
934 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000935 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 // GNU indirect goto extension.
937 Diag(Tok, diag::ext_gnu_indirect_goto);
938 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000939 OwningExprResult R(Actions, ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000940 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 SkipUntil(tok::semi, false, true);
942 return true;
943 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000944 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.release());
Chris Lattner95cfb852007-07-22 04:13:33 +0000945 } else {
946 Diag(Tok, diag::err_expected_ident);
947 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000949
Sebastian Redleffa8d12008-12-10 00:02:53 +0000950 return Res.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
952
953/// ParseContinueStatement
954/// jump-statement:
955/// 'continue' ';'
956///
957/// Note: this lets the caller parse the end ';'.
958///
959Parser::StmtResult Parser::ParseContinueStatement() {
960 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000961 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000962}
963
964/// ParseBreakStatement
965/// jump-statement:
966/// 'break' ';'
967///
968/// Note: this lets the caller parse the end ';'.
969///
970Parser::StmtResult Parser::ParseBreakStatement() {
971 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000972 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000973}
974
975/// ParseReturnStatement
976/// jump-statement:
977/// 'return' expression[opt] ';'
978Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000979 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
981
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000982 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000983 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000985 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 SkipUntil(tok::semi, false, true);
987 return true;
988 }
989 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000990 return Actions.ActOnReturnStmt(ReturnLoc, R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000991}
992
Steve Naroff5f8aa692008-02-11 23:15:56 +0000993/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
994/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +0000995Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +0000996 if (Tok.is(tok::l_brace)) {
997 unsigned short savedBraceCount = BraceCount;
998 do {
999 ConsumeAnyToken();
1000 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1001 } else {
1002 // From the MS website: If used without braces, the __asm keyword means
1003 // that the rest of the line is an assembly-language statement.
1004 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001005 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001006 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1007 do {
1008 ConsumeAnyToken();
1009 TokLoc = Tok.getLocation();
1010 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1011 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1012 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001013 }
Steve Naroffd77bc282008-04-07 21:06:54 +00001014 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001015}
1016
Reid Spencer5f016e22007-07-11 17:01:13 +00001017/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001018/// asm-statement:
1019/// gnu-asm-statement
1020/// ms-asm-statement
1021///
1022/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001023/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1024///
1025/// [GNU] asm-argument:
1026/// asm-string-literal
1027/// asm-string-literal ':' asm-operands[opt]
1028/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1029/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1030/// ':' asm-clobbers
1031///
1032/// [GNU] asm-clobbers:
1033/// asm-string-literal
1034/// asm-clobbers ',' asm-string-literal
1035///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001036/// [MS] ms-asm-statement:
1037/// '__asm' assembly-instruction ';'[opt]
1038/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1039///
1040/// [MS] assembly-instruction-list:
1041/// assembly-instruction ';'[opt]
1042/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1043///
Steve Naroffd62701b2008-02-07 03:50:06 +00001044Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001045 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001046 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001047
Steve Naroff5f8aa692008-02-11 23:15:56 +00001048 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001049 msAsm = true;
1050 return FuzzyParseMicrosoftAsmStatement();
1051 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 DeclSpec DS;
1053 SourceLocation Loc = Tok.getLocation();
1054 ParseTypeQualifierListOpt(DS);
1055
1056 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1057 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001058 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001060 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Reid Spencer5f016e22007-07-11 17:01:13 +00001061
1062 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001063 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001064 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001065 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001066 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 SkipUntil(tok::r_paren);
1068 return true;
1069 }
1070 Loc = ConsumeParen();
1071
Sebastian Redleffa8d12008-12-10 00:02:53 +00001072 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001073 if (AsmString.isInvalid())
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001074 return true;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001075
Anders Carlssonb235fc22007-11-22 01:36:19 +00001076 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001077 ExprVector Constraints(Actions);
1078 ExprVector Exprs(Actions);
1079 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001080
Anders Carlssondfab34a2008-02-05 23:03:50 +00001081 unsigned NumInputs = 0, NumOutputs = 0;
1082
1083 SourceLocation RParenLoc;
1084 if (Tok.is(tok::r_paren)) {
1085 // We have a simple asm expression
1086 isSimple = true;
1087
1088 RParenLoc = ConsumeParen();
1089 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001090 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001091 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1092 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001093
1094 NumOutputs = Names.size();
1095
1096 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001097 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1098 return true;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001099
Anders Carlssondfab34a2008-02-05 23:03:50 +00001100 assert(Names.size() == Constraints.size() &&
1101 Constraints.size() == Exprs.size()
1102 && "Input operand size mismatch!");
1103
1104 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001105
Anders Carlssondfab34a2008-02-05 23:03:50 +00001106 // Parse the clobbers, if present.
1107 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001108 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001109
Anders Carlssondfab34a2008-02-05 23:03:50 +00001110 // Parse the asm-string list for clobbers.
1111 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001112 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001113
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001114 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001115 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001116
1117 Clobbers.push_back(Clobber.release());
1118
Anders Carlssondfab34a2008-02-05 23:03:50 +00001119 if (Tok.isNot(tok::comma)) break;
1120 ConsumeToken();
1121 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001123
Anders Carlssondfab34a2008-02-05 23:03:50 +00001124 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001126
Anders Carlssondfab34a2008-02-05 23:03:50 +00001127 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1128 NumOutputs, NumInputs,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001129 &Names[0], Constraints.take(),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001130 Exprs.take(), AsmString.release(),
Sebastian Redla55e52c2008-11-25 22:21:31 +00001131 Clobbers.size(), Clobbers.take(),
Anders Carlssonb235fc22007-11-22 01:36:19 +00001132 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001133}
1134
1135/// ParseAsmOperands - Parse the asm-operands production as used by
1136/// asm-statement. We also parse a leading ':' token. If the leading colon is
1137/// not present, we do not parse anything.
1138///
1139/// [GNU] asm-operands:
1140/// asm-operand
1141/// asm-operands ',' asm-operand
1142///
1143/// [GNU] asm-operand:
1144/// asm-string-literal '(' expression ')'
1145/// '[' identifier ']' asm-string-literal '(' expression ')'
1146///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001147bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001148 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1149 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001151 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 ConsumeToken();
1153
1154 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001155 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001156 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001157
Anders Carlssonb235fc22007-11-22 01:36:19 +00001158 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001160 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 SourceLocation Loc = ConsumeBracket();
1162
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001163 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 Diag(Tok, diag::err_expected_ident);
1165 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001166 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 }
Chris Lattner69efba72007-10-29 04:06:22 +00001168
Anders Carlssonb235fc22007-11-22 01:36:19 +00001169 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001170 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001171
1172 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001174 } else
1175 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001176
Sebastian Redleffa8d12008-12-10 00:02:53 +00001177 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001178 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001179 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001180 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001181 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001182 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001183
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001184 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001185 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001187 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 // Read the parenthesized expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001191 OwningExprResult Res(Actions, ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001192 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001194 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001196 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001198 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 ConsumeToken();
1200 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001201
1202 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001203}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001204
1205Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1206 SourceLocation L, SourceLocation R) {
1207 // Do not enter a scope for the brace, as the arguments are in the same scope
1208 // (the function body) as the body itself. Instead, just read the statement
1209 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001210 OwningStmtResult FnBody(Actions, ParseCompoundStatementBody());
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001211
1212 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001213 if (FnBody.isInvalid())
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001214 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1215
Sebastian Redleffa8d12008-12-10 00:02:53 +00001216 return Actions.ActOnFinishFunctionBody(Decl, FnBody.release());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001217}