blob: f244e4ba98caa89f68f106a8636149b6672490f9 [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.
336 EnterScope(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));
Reid Spencer5f016e22007-07-11 17:01:13 +0000340
341 ExitScope();
Sebastian Redleffa8d12008-12-10 00:02:53 +0000342 return Body.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000343}
344
345
346/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000347/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000348/// consume the '}' at the end of the block. It does not manipulate the scope
349/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000350Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
352
353 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000354 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000355
356 typedef StmtVector StmtsTy;
357 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000358 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000359 OwningStmtResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000360 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000361 R = ParseStatementOrDeclaration(false);
362 } else {
363 // __extension__ can start declarations and it can also be a unary
364 // operator for expressions. Consume multiple __extension__ markers here
365 // until we can determine which is which.
366 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000367 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000368 ConsumeToken();
369
Chris Lattner043a0b52008-03-13 06:32:11 +0000370 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000371 ExtensionRAIIObject O(Diags); // Use RAII to do this.
372
Chris Lattner45a566c2007-08-27 01:01:57 +0000373 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000374 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000375 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000376 SourceLocation DeclStart = Tok.getLocation();
377 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
378 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000379 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000380 } else {
381 // Otherwise this was a unary __extension__ marker. Parse the
382 // subexpression and add the __extension__ unary op.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000383 OwningExprResult Res(Actions, ParseCastExpression(false));
Chris Lattner043a0b52008-03-13 06:32:11 +0000384
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000385 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000386 SkipUntil(tok::semi);
387 continue;
388 }
389
390 // Add the __extension__ node to the AST.
Douglas Gregor74253732008-11-19 15:42:04 +0000391 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000392 Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000393 if (Res.isInvalid())
Chris Lattner45a566c2007-08-27 01:01:57 +0000394 continue;
395
Chris Lattner39146d62008-10-20 06:51:33 +0000396 // Eat the semicolon at the end of stmt and convert the expr into a
397 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000398 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000399 R = Actions.ActOnExprStmt(Res.release());
Chris Lattner45a566c2007-08-27 01:01:57 +0000400 }
401 }
402
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000403 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000404 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 }
406
407 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000408 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000410 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 }
412
413 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000414 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000415 Stmts.take(), Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000416}
417
418/// ParseIfStatement
419/// if-statement: [C99 6.8.4.1]
420/// 'if' '(' expression ')' statement
421/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000422/// [C++] 'if' '(' condition ')' statement
423/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000424///
425Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000426 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
428
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000429 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000430 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 SkipUntil(tok::semi);
432 return true;
433 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000434
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000435 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
436
Chris Lattner22153252007-08-26 23:08:06 +0000437 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
438 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000439 //
440 // C++ 6.4p3:
441 // A name introduced by a declaration in a condition is in scope from its
442 // point of declaration until the end of the substatements controlled by the
443 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000444 // C++ 3.3.2p4:
445 // Names declared in the for-init-statement, and in the condition of if,
446 // while, for, and switch statements are local to the if, while, for, or
447 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000448 //
449 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000450 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000453 OwningExprResult CondExp(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000454 if (getLang().CPlusPlus) {
455 SourceLocation LParenLoc = ConsumeParen();
456 CondExp = ParseCXXCondition();
457 MatchRHSPunctuation(tok::r_paren, LParenLoc);
458 } else {
459 CondExp = ParseSimpleParenExpression();
460 }
461
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000462 if (CondExp.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 SkipUntil(tok::semi);
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000464 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000465 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 return true;
467 }
468
Chris Lattner0ecea032007-08-22 05:28:50 +0000469 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000470 // there is no compound stmt. C90 does not have this clause. We only do this
471 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000472 //
473 // C++ 6.4p1:
474 // The substatement in a selection-statement (each substatement, in the else
475 // form of the if statement) implicitly defines a local scope.
476 //
477 // For C++ we create a scope for the condition and a new scope for
478 // substatements because:
479 // -When the 'then' scope exits, we want the condition declaration to still be
480 // active for the 'else' scope too.
481 // -Sema will detect name clashes by considering declarations of a
482 // 'ControlScope' as part of its direct subscope.
483 // -If we wanted the condition and substatement to be in the same scope, we
484 // would have to notify ParseStatement not to create a new scope. It's
485 // simpler to let it create a new scope.
486 //
487 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000488 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000489
Chris Lattnerb96728d2007-10-29 05:08:52 +0000490 // Read the 'then' stmt.
491 SourceLocation ThenStmtLoc = Tok.getLocation();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000492 OwningStmtResult ThenStmt(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000493
Chris Lattnera36ce712007-08-22 05:16:28 +0000494 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000495 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000496
497 // If it has an else, parse it.
498 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000499 SourceLocation ElseStmtLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000500 OwningStmtResult ElseStmt(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000501
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000502 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000504
Chris Lattner0ecea032007-08-22 05:28:50 +0000505 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000506 // there is no compound stmt. C90 does not have this clause. We only do
507 // this if the body isn't a compound statement to avoid push/pop in common
508 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000509 //
510 // C++ 6.4p1:
511 // The substatement in a selection-statement (each substatement, in the else
512 // form of the if statement) implicitly defines a local scope.
513 //
514 NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000515 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000516
Chris Lattnerb96728d2007-10-29 05:08:52 +0000517 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000519
520 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000521 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 }
523
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000524 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000525 ExitScope();
526
Chris Lattnerb96728d2007-10-29 05:08:52 +0000527 // If the then or else stmt is invalid and the other is valid (and present),
528 // make turn the invalid one into a null stmt to avoid dropping the other
529 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000530 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
531 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
532 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000533 // Both invalid, or one is invalid and other is non-present: return error.
Chris Lattnerb96728d2007-10-29 05:08:52 +0000534 return true;
535 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000536
Chris Lattnerb96728d2007-10-29 05:08:52 +0000537 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000538 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000539 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000540 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000541 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000542
Sebastian Redleffa8d12008-12-10 00:02:53 +0000543 return Actions.ActOnIfStmt(IfLoc, CondExp.release(), ThenStmt.release(),
544 ElseLoc, ElseStmt.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000545}
546
547/// ParseSwitchStatement
548/// switch-statement:
549/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000550/// [C++] 'switch' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000551Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000552 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
554
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000555 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000556 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 SkipUntil(tok::semi);
558 return true;
559 }
Chris Lattner22153252007-08-26 23:08:06 +0000560
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000561 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
562
Chris Lattner22153252007-08-26 23:08:06 +0000563 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
564 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000565 //
566 // C++ 6.4p3:
567 // A name introduced by a declaration in a condition is in scope from its
568 // point of declaration until the end of the substatements controlled by the
569 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000570 // C++ 3.3.2p4:
571 // Names declared in the for-init-statement, and in the condition of if,
572 // while, for, and switch statements are local to the if, while, for, or
573 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000574 //
575 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000576 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000577 else
578 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579
580 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000581 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000582 if (getLang().CPlusPlus) {
583 SourceLocation LParenLoc = ConsumeParen();
584 Cond = ParseCXXCondition();
585 MatchRHSPunctuation(tok::r_paren, LParenLoc);
586 } else {
587 Cond = ParseSimpleParenExpression();
588 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000589
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000590 if (Cond.isInvalid()) {
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000591 ExitScope();
592 return true;
593 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000594
Sebastian Redleffa8d12008-12-10 00:02:53 +0000595 OwningStmtResult Switch(Actions,
596 Actions.ActOnStartOfSwitchStmt(Cond.release()));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000597
Chris Lattner0ecea032007-08-22 05:28:50 +0000598 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000599 // there is no compound stmt. C90 does not have this clause. We only do this
600 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000601 //
602 // C++ 6.4p1:
603 // The substatement in a selection-statement (each substatement, in the else
604 // form of the if statement) implicitly defines a local scope.
605 //
606 // See comments in ParseIfStatement for why we create a scope for the
607 // condition and a new scope for substatement in C++.
608 //
609 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000610 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000613 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000614
Chris Lattner0ecea032007-08-22 05:28:50 +0000615 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000616 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000617
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000618 if (Body.isInvalid()) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000619 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000620 // FIXME: Remove the case statement list from the Switch statement.
621 }
622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 ExitScope();
624
Sebastian Redleffa8d12008-12-10 00:02:53 +0000625 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.release(),
626 Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000627}
628
629/// ParseWhileStatement
630/// while-statement: [C99 6.8.5.1]
631/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000632/// [C++] 'while' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000633Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000634 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 SourceLocation WhileLoc = Tok.getLocation();
636 ConsumeToken(); // eat the 'while'.
637
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000638 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000639 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 SkipUntil(tok::semi);
641 return true;
642 }
643
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000644 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
645
Chris Lattner22153252007-08-26 23:08:06 +0000646 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
647 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000648 //
649 // C++ 6.4p3:
650 // A name introduced by a declaration in a condition is in scope from its
651 // point of declaration until the end of the substatements controlled by the
652 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000653 // C++ 3.3.2p4:
654 // Names declared in the for-init-statement, and in the condition of if,
655 // while, for, and switch statements are local to the if, while, for, or
656 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000657 //
658 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000659 EnterScope(Scope::BreakScope | Scope::ContinueScope |
660 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000661 else
662 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000663
664 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000665 OwningExprResult Cond(Actions);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000666 if (getLang().CPlusPlus) {
667 SourceLocation LParenLoc = ConsumeParen();
668 Cond = ParseCXXCondition();
669 MatchRHSPunctuation(tok::r_paren, LParenLoc);
670 } else {
671 Cond = ParseSimpleParenExpression();
672 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000673
Chris Lattner0ecea032007-08-22 05:28:50 +0000674 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000675 // there is no compound stmt. C90 does not have this clause. We only do this
676 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000677 //
678 // C++ 6.5p2:
679 // The substatement in an iteration-statement implicitly defines a local scope
680 // which is entered and exited each time through the loop.
681 //
682 // See comments in ParseIfStatement for why we create a scope for the
683 // condition and a new scope for substatement in C++.
684 //
685 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000686 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000689 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
Chris Lattner0ecea032007-08-22 05:28:50 +0000691 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000692 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 ExitScope();
695
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000696 if (Cond.isInvalid() || Body.isInvalid()) return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000697
Sebastian Redleffa8d12008-12-10 00:02:53 +0000698 return Actions.ActOnWhileStmt(WhileLoc, Cond.release(), Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000699}
700
701/// ParseDoStatement
702/// do-statement: [C99 6.8.5.2]
703/// 'do' statement 'while' '(' expression ')' ';'
704/// Note: this lets the caller parse the end ';'.
705Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000706 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
708
Chris Lattner22153252007-08-26 23:08:06 +0000709 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
710 // the case for C90. Start the loop scope.
711 if (getLang().C99)
712 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
713 else
714 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000715
Chris Lattner0ecea032007-08-22 05:28:50 +0000716 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000717 // there is no compound stmt. C90 does not have this clause. We only do this
718 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000719 //
720 // C++ 6.5p2:
721 // The substatement in an iteration-statement implicitly defines a local scope
722 // which is entered and exited each time through the loop.
723 //
Chris Lattner19504402008-11-13 18:52:53 +0000724 bool NeedsInnerScope =
725 (getLang().C99 || getLang().CPlusPlus) && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000726 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000727
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000729 OwningStmtResult Body(Actions, ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000730
Chris Lattner0ecea032007-08-22 05:28:50 +0000731 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000732 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000733
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000734 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 ExitScope();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000736 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +0000737 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000738 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000739 SkipUntil(tok::semi, false, true);
740 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 return true;
742 }
743 SourceLocation WhileLoc = ConsumeToken();
744
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000745 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 ExitScope();
Chris Lattner1ab3b962008-11-18 07:48:38 +0000747 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000748 SkipUntil(tok::semi, false, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 return true;
750 }
751
752 // Parse the condition.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000753 OwningExprResult Cond(Actions, ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000754
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 ExitScope();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000756
757 if (Cond.isInvalid() || Body.isInvalid()) return true;
758
Sebastian Redleffa8d12008-12-10 00:02:53 +0000759 return Actions.ActOnDoStmt(DoLoc, Body.release(), WhileLoc, Cond.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000760}
761
762/// ParseForStatement
763/// for-statement: [C99 6.8.5.3]
764/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
765/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000766/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
767/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000768/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
769/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000770///
771/// [C++] for-init-statement:
772/// [C++] expression-statement
773/// [C++] simple-declaration
774///
Reid Spencer5f016e22007-07-11 17:01:13 +0000775Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000776 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
778
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000779 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000780 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 SkipUntil(tok::semi);
782 return true;
783 }
784
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000785 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
786
Chris Lattner22153252007-08-26 23:08:06 +0000787 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
788 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000789 //
790 // C++ 6.4p3:
791 // A name introduced by a declaration in a condition is in scope from its
792 // point of declaration until the end of the substatements controlled by the
793 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000794 // C++ 3.3.2p4:
795 // Names declared in the for-init-statement, and in the condition of if,
796 // while, for, and switch statements are local to the if, while, for, or
797 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000798 // C++ 6.5.3p1:
799 // Names declared in the for-init-statement are in the same declarative-region
800 // as those declared in the condition.
801 //
802 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000803 EnterScope(Scope::BreakScope | Scope::ContinueScope |
804 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000805 else
806 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000807
808 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000809 OwningExprResult Value(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000810
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000811 bool ForEach = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000812 OwningStmtResult FirstPart(Actions), ThirdPart(Actions);
813 OwningExprResult SecondPart(Actions);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000814
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000816 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 // no first part, eat the ';'.
818 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000819 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000821 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000823
824 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000825 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000826 // FIXME: Pass in the right location for the end of the declstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000827 FirstPart = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
828 DeclStart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000829 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000830 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000831 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000832 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 } else {
834 Value = ParseExpression();
835
836 // Turn the expression into a stmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000837 if (!Value.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000838 FirstPart = Actions.ActOnExprStmt(Value.release());
839
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000840 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000842 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000843 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000844 ConsumeToken(); // consume 'in'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000845 SecondPart = ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000846 }
847 else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000848 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 SkipUntil(tok::semi);
850 }
851 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000852 if (!ForEach) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000853 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000854 // Parse the second part of the for specifier.
855 if (Tok.is(tok::semi)) { // for (...;;
856 // no second part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000857 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000858 SecondPart = getLang().CPlusPlus ? ParseCXXCondition()
859 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000860 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000861
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000862 if (Tok.is(tok::semi)) {
863 ConsumeToken();
864 } else {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000865 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000866 SkipUntil(tok::semi);
867 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000868
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000869 // Parse the third part of the for specifier.
870 if (Tok.is(tok::r_paren)) { // for (...;...;)
871 // no third part.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000872 } else {
873 Value = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000874 if (!Value.isInvalid()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000875 // Turn the expression into a stmt.
Sebastian Redleffa8d12008-12-10 00:02:53 +0000876 ThirdPart = Actions.ActOnExprStmt(Value.release());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000877 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 }
879 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 // Match the ')'.
881 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000882
Chris Lattner0ecea032007-08-22 05:28:50 +0000883 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000884 // there is no compound stmt. C90 does not have this clause. We only do this
885 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000886 //
887 // C++ 6.5p2:
888 // The substatement in an iteration-statement implicitly defines a local scope
889 // which is entered and exited each time through the loop.
890 //
891 // See comments in ParseIfStatement for why we create a scope for
892 // for-init-statement/condition and a new scope for substatement in C++.
893 //
894 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000895 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 // Read the body statement.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000898 OwningStmtResult Body(Actions, ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000899
Chris Lattner0ecea032007-08-22 05:28:50 +0000900 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000901 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 // Leave the for-scope.
904 ExitScope();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000905
906 if (Body.isInvalid())
907 return true;
Sebastian Redleffa8d12008-12-10 00:02:53 +0000908
909 if (!ForEach)
910 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.release(),
911 SecondPart.release(), ThirdPart.release(),
912 RParenLoc, Body.release());
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000913 else
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000914 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000915 FirstPart.release(),
916 SecondPart.release(),
917 RParenLoc, Body.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000918}
919
920/// ParseGotoStatement
921/// jump-statement:
922/// 'goto' identifier ';'
923/// [GNU] 'goto' '*' expression ';'
924///
925/// Note: this lets the caller parse the end ';'.
926///
927Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000928 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
930
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000931 OwningStmtResult Res(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000932 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000933 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 Tok.getIdentifierInfo());
935 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000936 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 // GNU indirect goto extension.
938 Diag(Tok, diag::ext_gnu_indirect_goto);
939 SourceLocation StarLoc = ConsumeToken();
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000940 OwningExprResult R(Actions, ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000941 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 SkipUntil(tok::semi, false, true);
943 return true;
944 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000945 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.release());
Chris Lattner95cfb852007-07-22 04:13:33 +0000946 } else {
947 Diag(Tok, diag::err_expected_ident);
948 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000950
Sebastian Redleffa8d12008-12-10 00:02:53 +0000951 return Res.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000952}
953
954/// ParseContinueStatement
955/// jump-statement:
956/// 'continue' ';'
957///
958/// Note: this lets the caller parse the end ';'.
959///
960Parser::StmtResult Parser::ParseContinueStatement() {
961 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000962 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000963}
964
965/// ParseBreakStatement
966/// jump-statement:
967/// 'break' ';'
968///
969/// Note: this lets the caller parse the end ';'.
970///
971Parser::StmtResult Parser::ParseBreakStatement() {
972 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000973 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000974}
975
976/// ParseReturnStatement
977/// jump-statement:
978/// 'return' expression[opt] ';'
979Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000980 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
982
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000983 OwningExprResult R(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000984 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000986 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 SkipUntil(tok::semi, false, true);
988 return true;
989 }
990 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000991 return Actions.ActOnReturnStmt(ReturnLoc, R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000992}
993
Steve Naroff5f8aa692008-02-11 23:15:56 +0000994/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
995/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +0000996Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +0000997 if (Tok.is(tok::l_brace)) {
998 unsigned short savedBraceCount = BraceCount;
999 do {
1000 ConsumeAnyToken();
1001 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1002 } else {
1003 // From the MS website: If used without braces, the __asm keyword means
1004 // that the rest of the line is an assembly-language statement.
1005 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001006 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001007 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1008 do {
1009 ConsumeAnyToken();
1010 TokLoc = Tok.getLocation();
1011 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1012 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1013 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001014 }
Steve Naroffd77bc282008-04-07 21:06:54 +00001015 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001016}
1017
Reid Spencer5f016e22007-07-11 17:01:13 +00001018/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001019/// asm-statement:
1020/// gnu-asm-statement
1021/// ms-asm-statement
1022///
1023/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001024/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1025///
1026/// [GNU] asm-argument:
1027/// asm-string-literal
1028/// asm-string-literal ':' asm-operands[opt]
1029/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1030/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1031/// ':' asm-clobbers
1032///
1033/// [GNU] asm-clobbers:
1034/// asm-string-literal
1035/// asm-clobbers ',' asm-string-literal
1036///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001037/// [MS] ms-asm-statement:
1038/// '__asm' assembly-instruction ';'[opt]
1039/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1040///
1041/// [MS] assembly-instruction-list:
1042/// assembly-instruction ';'[opt]
1043/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1044///
Steve Naroffd62701b2008-02-07 03:50:06 +00001045Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001046 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001047 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001048
Steve Naroff5f8aa692008-02-11 23:15:56 +00001049 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001050 msAsm = true;
1051 return FuzzyParseMicrosoftAsmStatement();
1052 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 DeclSpec DS;
1054 SourceLocation Loc = Tok.getLocation();
1055 ParseTypeQualifierListOpt(DS);
1056
1057 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1058 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001059 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001061 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Reid Spencer5f016e22007-07-11 17:01:13 +00001062
1063 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001064 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001065 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001066 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001067 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 SkipUntil(tok::r_paren);
1069 return true;
1070 }
1071 Loc = ConsumeParen();
1072
Sebastian Redleffa8d12008-12-10 00:02:53 +00001073 OwningExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001074 if (AsmString.isInvalid())
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001075 return true;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001076
Anders Carlssonb235fc22007-11-22 01:36:19 +00001077 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001078 ExprVector Constraints(Actions);
1079 ExprVector Exprs(Actions);
1080 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001081
Anders Carlssondfab34a2008-02-05 23:03:50 +00001082 unsigned NumInputs = 0, NumOutputs = 0;
1083
1084 SourceLocation RParenLoc;
1085 if (Tok.is(tok::r_paren)) {
1086 // We have a simple asm expression
1087 isSimple = true;
1088
1089 RParenLoc = ConsumeParen();
1090 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001091 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001092 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1093 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001094
1095 NumOutputs = Names.size();
1096
1097 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001098 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1099 return true;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001100
Anders Carlssondfab34a2008-02-05 23:03:50 +00001101 assert(Names.size() == Constraints.size() &&
1102 Constraints.size() == Exprs.size()
1103 && "Input operand size mismatch!");
1104
1105 NumInputs = Names.size() - NumOutputs;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001106
Anders Carlssondfab34a2008-02-05 23:03:50 +00001107 // Parse the clobbers, if present.
1108 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001109 ConsumeToken();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001110
Anders Carlssondfab34a2008-02-05 23:03:50 +00001111 // Parse the asm-string list for clobbers.
1112 while (1) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001113 OwningExprResult Clobber(ParseAsmStringLiteral());
Anders Carlssondfab34a2008-02-05 23:03:50 +00001114
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001115 if (Clobber.isInvalid())
Anders Carlssondfab34a2008-02-05 23:03:50 +00001116 break;
Sebastian Redleffa8d12008-12-10 00:02:53 +00001117
1118 Clobbers.push_back(Clobber.release());
1119
Anders Carlssondfab34a2008-02-05 23:03:50 +00001120 if (Tok.isNot(tok::comma)) break;
1121 ConsumeToken();
1122 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001124
Anders Carlssondfab34a2008-02-05 23:03:50 +00001125 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001127
Anders Carlssondfab34a2008-02-05 23:03:50 +00001128 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1129 NumOutputs, NumInputs,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001130 &Names[0], Constraints.take(),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001131 Exprs.take(), AsmString.release(),
Sebastian Redla55e52c2008-11-25 22:21:31 +00001132 Clobbers.size(), Clobbers.take(),
Anders Carlssonb235fc22007-11-22 01:36:19 +00001133 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134}
1135
1136/// ParseAsmOperands - Parse the asm-operands production as used by
1137/// asm-statement. We also parse a leading ':' token. If the leading colon is
1138/// not present, we do not parse anything.
1139///
1140/// [GNU] asm-operands:
1141/// asm-operand
1142/// asm-operands ',' asm-operand
1143///
1144/// [GNU] asm-operand:
1145/// asm-string-literal '(' expression ')'
1146/// '[' identifier ']' asm-string-literal '(' expression ')'
1147///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001148bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001149 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1150 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001152 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 ConsumeToken();
1154
1155 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001156 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001157 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001158
Anders Carlssonb235fc22007-11-22 01:36:19 +00001159 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001161 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 SourceLocation Loc = ConsumeBracket();
1163
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001164 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 Diag(Tok, diag::err_expected_ident);
1166 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001167 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 }
Chris Lattner69efba72007-10-29 04:06:22 +00001169
Anders Carlssonb235fc22007-11-22 01:36:19 +00001170 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001171 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001172
1173 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001175 } else
1176 Names.push_back(std::string());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001177
Sebastian Redleffa8d12008-12-10 00:02:53 +00001178 OwningExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001179 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001180 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001181 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001182 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001183 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001184
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001185 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001186 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001188 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // Read the parenthesized expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001192 OwningExprResult Res(Actions, ParseSimpleParenExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001193 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001195 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001197 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001199 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 ConsumeToken();
1201 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001202
1203 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001205
1206Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1207 SourceLocation L, SourceLocation R) {
1208 // Do not enter a scope for the brace, as the arguments are in the same scope
1209 // (the function body) as the body itself. Instead, just read the statement
1210 // list and put it into a CompoundStmt for safe keeping.
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001211 OwningStmtResult FnBody(Actions, ParseCompoundStatementBody());
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001212
1213 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001214 if (FnBody.isInvalid())
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001215 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1216
1217 // Leave the function body scope.
1218 ExitScope();
1219
Sebastian Redleffa8d12008-12-10 00:02:53 +00001220 return Actions.ActOnFinishFunctionBody(Decl, FnBody.release());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001221}