blob: 86e18bfca8513f1a35dec0ab2159b0fafb855597 [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;
77 Parser::StmtResult Res;
78
79 // 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();
101 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
102 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000103 return Actions.ActOnDeclStmt(Res, 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] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000109 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 if (Res.isInvalid) {
111 // 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);
Steve Naroff1b273c42007-09-16 14:56:35 +0000119 return Actions.ActOnExprStmt(Res.Val);
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);
166 if (msAsm) return Res;
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();
Chris Lattner19504402008-11-13 18:52:53 +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 }
179 return Res;
180}
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
206 StmtResult SubStmt = ParseStatement();
207
208 // Broken substmt shouldn't prevent the label from being added to the AST.
209 if (SubStmt.isInvalid)
210 SubStmt = Actions.ActOnNullStmt(ColonLoc);
211
212 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
213 IdentTok.getIdentifierInfo(),
214 ColonLoc, SubStmt.Val);
215}
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
228 ExprResult LHS = ParseConstantExpression();
229 if (LHS.isInvalid) {
230 SkipUntil(tok::colon);
231 return true;
232 }
Sebastian Redla55e52c2008-11-25 22:21:31 +0000233 ExprGuard LHSGuard(Actions, LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234
235 // GNU case range extension.
236 SourceLocation DotDotDotLoc;
237 ExprTy *RHSVal = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000238 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 Diag(Tok, diag::ext_gnu_case_range);
240 DotDotDotLoc = ConsumeToken();
241
242 ExprResult RHS = ParseConstantExpression();
243 if (RHS.isInvalid) {
244 SkipUntil(tok::colon);
245 return true;
246 }
247 RHSVal = RHS.Val;
248 }
Sebastian Redla55e52c2008-11-25 22:21:31 +0000249 ExprGuard RHSGuard(Actions, RHSVal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000251 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000252 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 SkipUntil(tok::colon);
254 return true;
255 }
256
257 SourceLocation ColonLoc = ConsumeToken();
258
259 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000260 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 Diag(Tok, diag::err_label_end_of_compound_statement);
262 return true;
263 }
264
265 StmtResult SubStmt = ParseStatement();
266
267 // Broken substmt shouldn't prevent the case from being added to the AST.
268 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000269 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000270
Sebastian Redla55e52c2008-11-25 22:21:31 +0000271 return Actions.ActOnCaseStmt(CaseLoc, LHSGuard.take(), DotDotDotLoc,
272 RHSGuard.take(), ColonLoc, SubStmt.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
275/// ParseDefaultStatement
276/// labeled-statement:
277/// 'default' ':' statement
278/// Note that this does not parse the 'statement' at the end.
279///
280Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000281 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
283
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000284 if (Tok.isNot(tok::colon)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000285 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 SkipUntil(tok::colon);
287 return true;
288 }
289
290 SourceLocation ColonLoc = ConsumeToken();
291
292 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000293 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 Diag(Tok, diag::err_label_end_of_compound_statement);
295 return true;
296 }
297
298 StmtResult SubStmt = ParseStatement();
299 if (SubStmt.isInvalid)
300 return true;
301
Steve Naroff1b273c42007-09-16 14:56:35 +0000302 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
305
306/// ParseCompoundStatement - Parse a "{}" block.
307///
308/// compound-statement: [C99 6.8.2]
309/// { block-item-list[opt] }
310/// [GNU] { label-declarations block-item-list } [TODO]
311///
312/// block-item-list:
313/// block-item
314/// block-item-list block-item
315///
316/// block-item:
317/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000318/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000319/// statement
320/// [OMP] openmp-directive [TODO]
321///
322/// [GNU] label-declarations:
323/// [GNU] label-declaration
324/// [GNU] label-declarations label-declaration
325///
326/// [GNU] label-declaration:
327/// [GNU] '__label__' identifier-list ';'
328///
329/// [OMP] openmp-directive: [TODO]
330/// [OMP] barrier-directive
331/// [OMP] flush-directive
332///
Chris Lattner98414c12007-08-31 21:49:55 +0000333Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000334 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000335
Chris Lattner31e05722007-08-26 06:24:45 +0000336 // Enter a scope to hold everything within the compound stmt. Compound
337 // statements can always hold declarations.
338 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
340 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000341 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342
343 ExitScope();
344 return Body;
345}
346
347
348/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000349/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000350/// consume the '}' at the end of the block. It does not manipulate the scope
351/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000352Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
354
355 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000356 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000357
358 typedef StmtVector StmtsTy;
359 StmtsTy Stmts(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000360 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000361 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000362 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000363 R = ParseStatementOrDeclaration(false);
364 } else {
365 // __extension__ can start declarations and it can also be a unary
366 // operator for expressions. Consume multiple __extension__ markers here
367 // until we can determine which is which.
368 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000369 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000370 ConsumeToken();
371
Chris Lattner043a0b52008-03-13 06:32:11 +0000372 // __extension__ silences extension warnings in the subexpression.
Chris Lattner39146d62008-10-20 06:51:33 +0000373 ExtensionRAIIObject O(Diags); // Use RAII to do this.
374
Chris Lattner45a566c2007-08-27 01:01:57 +0000375 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000376 if (isDeclarationStatement()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000377 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner81c018d2008-03-13 06:29:04 +0000378 SourceLocation DeclStart = Tok.getLocation();
379 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
380 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattner691a38b2008-03-13 06:29:54 +0000381 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattner45a566c2007-08-27 01:01:57 +0000382 } else {
383 // Otherwise this was a unary __extension__ marker. Parse the
384 // subexpression and add the __extension__ unary op.
Chris Lattner45a566c2007-08-27 01:01:57 +0000385 ExprResult Res = ParseCastExpression(false);
Chris Lattner043a0b52008-03-13 06:32:11 +0000386
Chris Lattner45a566c2007-08-27 01:01:57 +0000387 if (Res.isInvalid) {
388 SkipUntil(tok::semi);
389 continue;
390 }
391
392 // Add the __extension__ node to the AST.
Douglas Gregor74253732008-11-19 15:42:04 +0000393 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
394 Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000395 if (Res.isInvalid)
396 continue;
397
Chris Lattner39146d62008-10-20 06:51:33 +0000398 // Eat the semicolon at the end of stmt and convert the expr into a
399 // statement.
Chris Lattner45a566c2007-08-27 01:01:57 +0000400 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000401 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000402 }
403 }
404
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 if (!R.isInvalid && R.Val)
406 Stmts.push_back(R.Val);
407 }
408
409 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000410 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000412 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 }
414
415 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000416 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000417 Stmts.take(), Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000418}
419
420/// ParseIfStatement
421/// if-statement: [C99 6.8.4.1]
422/// 'if' '(' expression ')' statement
423/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000424/// [C++] 'if' '(' condition ')' statement
425/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000426///
427Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000428 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
430
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000431 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000432 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 SkipUntil(tok::semi);
434 return true;
435 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000436
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000437 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
438
Chris Lattner22153252007-08-26 23:08:06 +0000439 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
440 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000441 //
442 // C++ 6.4p3:
443 // A name introduced by a declaration in a condition is in scope from its
444 // point of declaration until the end of the substatements controlled by the
445 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000446 // C++ 3.3.2p4:
447 // Names declared in the for-init-statement, and in the condition of if,
448 // while, for, and switch statements are local to the if, while, for, or
449 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000450 //
451 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000452 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000453
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000455 ExprResult CondExp;
456 if (getLang().CPlusPlus) {
457 SourceLocation LParenLoc = ConsumeParen();
458 CondExp = ParseCXXCondition();
459 MatchRHSPunctuation(tok::r_paren, LParenLoc);
460 } else {
461 CondExp = ParseSimpleParenExpression();
462 }
Sebastian Redla55e52c2008-11-25 22:21:31 +0000463 ExprGuard CondGuard(Actions, CondExp);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000464
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 if (CondExp.isInvalid) {
466 SkipUntil(tok::semi);
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000467 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000468 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 return true;
470 }
471
Chris Lattner0ecea032007-08-22 05:28:50 +0000472 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000473 // there is no compound stmt. C90 does not have this clause. We only do this
474 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000475 //
476 // C++ 6.4p1:
477 // The substatement in a selection-statement (each substatement, in the else
478 // form of the if statement) implicitly defines a local scope.
479 //
480 // For C++ we create a scope for the condition and a new scope for
481 // substatements because:
482 // -When the 'then' scope exits, we want the condition declaration to still be
483 // active for the 'else' scope too.
484 // -Sema will detect name clashes by considering declarations of a
485 // 'ControlScope' as part of its direct subscope.
486 // -If we wanted the condition and substatement to be in the same scope, we
487 // would have to notify ParseStatement not to create a new scope. It's
488 // simpler to let it create a new scope.
489 //
490 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000491 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000492
Chris Lattnerb96728d2007-10-29 05:08:52 +0000493 // Read the 'then' stmt.
494 SourceLocation ThenStmtLoc = Tok.getLocation();
495 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000496
Chris Lattnera36ce712007-08-22 05:16:28 +0000497 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000498 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000499
500 // If it has an else, parse it.
501 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000502 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000504
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000505 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000507
Chris Lattner0ecea032007-08-22 05:28:50 +0000508 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000509 // there is no compound stmt. C90 does not have this clause. We only do
510 // this if the body isn't a compound statement to avoid push/pop in common
511 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000512 //
513 // C++ 6.4p1:
514 // The substatement in a selection-statement (each substatement, in the else
515 // form of the if statement) implicitly defines a local scope.
516 //
517 NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000518 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000519
Chris Lattnerb96728d2007-10-29 05:08:52 +0000520 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000522
523 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000524 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
526
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000527 if (C99orCXX)
Chris Lattner22153252007-08-26 23:08:06 +0000528 ExitScope();
529
Chris Lattnerb96728d2007-10-29 05:08:52 +0000530 // If the then or else stmt is invalid and the other is valid (and present),
531 // make turn the invalid one into a null stmt to avoid dropping the other
532 // part. If both are invalid, return error.
533 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
534 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
535 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000536 // Both invalid, or one is invalid and other is non-present: return error.
Chris Lattnerb96728d2007-10-29 05:08:52 +0000537 return true;
538 }
539
540 // Now if either are invalid, replace with a ';'.
541 if (ThenStmt.isInvalid)
542 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
543 if (ElseStmt.isInvalid)
544 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
545
Sebastian Redla55e52c2008-11-25 22:21:31 +0000546 return Actions.ActOnIfStmt(IfLoc, CondGuard.take(), ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 ElseLoc, ElseStmt.Val);
548}
549
550/// ParseSwitchStatement
551/// switch-statement:
552/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000553/// [C++] 'switch' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000554Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000555 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
557
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000558 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000559 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 SkipUntil(tok::semi);
561 return true;
562 }
Chris Lattner22153252007-08-26 23:08:06 +0000563
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000564 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
565
Chris Lattner22153252007-08-26 23:08:06 +0000566 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
567 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000568 //
569 // C++ 6.4p3:
570 // A name introduced by a declaration in a condition is in scope from its
571 // point of declaration until the end of the substatements controlled by the
572 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000573 // C++ 3.3.2p4:
574 // Names declared in the for-init-statement, and in the condition of if,
575 // while, for, and switch statements are local to the if, while, for, or
576 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000577 //
578 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000579 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000580 else
581 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000582
583 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000584 ExprResult Cond;
585 if (getLang().CPlusPlus) {
586 SourceLocation LParenLoc = ConsumeParen();
587 Cond = ParseCXXCondition();
588 MatchRHSPunctuation(tok::r_paren, LParenLoc);
589 } else {
590 Cond = ParseSimpleParenExpression();
591 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000592
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000593 if (Cond.isInvalid) {
594 ExitScope();
595 return true;
596 }
597
Steve Naroff1b273c42007-09-16 14:56:35 +0000598 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000599
Chris Lattner0ecea032007-08-22 05:28:50 +0000600 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000601 // there is no compound stmt. C90 does not have this clause. We only do this
602 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000603 //
604 // C++ 6.4p1:
605 // The substatement in a selection-statement (each substatement, in the else
606 // form of the if statement) implicitly defines a local scope.
607 //
608 // See comments in ParseIfStatement for why we create a scope for the
609 // condition and a new scope for substatement in C++.
610 //
611 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000612 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 // Read the body statement.
615 StmtResult Body = ParseStatement();
616
Chris Lattner0ecea032007-08-22 05:28:50 +0000617 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000618 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000619
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000620 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000621 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000622 // FIXME: Remove the case statement list from the Switch statement.
623 }
624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 ExitScope();
626
Steve Naroff1b273c42007-09-16 14:56:35 +0000627 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000628}
629
630/// ParseWhileStatement
631/// while-statement: [C99 6.8.5.1]
632/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000633/// [C++] 'while' '(' condition ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000634Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000635 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 SourceLocation WhileLoc = Tok.getLocation();
637 ConsumeToken(); // eat the 'while'.
638
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000639 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000640 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 SkipUntil(tok::semi);
642 return true;
643 }
644
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000645 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
646
Chris Lattner22153252007-08-26 23:08:06 +0000647 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
648 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000649 //
650 // C++ 6.4p3:
651 // A name introduced by a declaration in a condition is in scope from its
652 // point of declaration until the end of the substatements controlled by the
653 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000654 // C++ 3.3.2p4:
655 // Names declared in the for-init-statement, and in the condition of if,
656 // while, for, and switch statements are local to the if, while, for, or
657 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000658 //
659 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000660 EnterScope(Scope::BreakScope | Scope::ContinueScope |
661 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000662 else
663 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000664
665 // Parse the condition.
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000666 ExprResult Cond;
667 if (getLang().CPlusPlus) {
668 SourceLocation LParenLoc = ConsumeParen();
669 Cond = ParseCXXCondition();
670 MatchRHSPunctuation(tok::r_paren, LParenLoc);
671 } else {
672 Cond = ParseSimpleParenExpression();
673 }
Sebastian Redla55e52c2008-11-25 22:21:31 +0000674 ExprGuard CondGuard(Actions, Cond);
Reid Spencer5f016e22007-07-11 17:01:13 +0000675
Chris Lattner0ecea032007-08-22 05:28:50 +0000676 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000677 // there is no compound stmt. C90 does not have this clause. We only do this
678 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000679 //
680 // C++ 6.5p2:
681 // The substatement in an iteration-statement implicitly defines a local scope
682 // which is entered and exited each time through the loop.
683 //
684 // See comments in ParseIfStatement for why we create a scope for the
685 // condition and a new scope for substatement in C++.
686 //
687 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000688 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 // Read the body statement.
691 StmtResult Body = ParseStatement();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000692 StmtGuard BodyGuard(Actions, Body);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693
Chris Lattner0ecea032007-08-22 05:28:50 +0000694 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000695 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 ExitScope();
698
699 if (Cond.isInvalid || Body.isInvalid) return true;
700
Sebastian Redla55e52c2008-11-25 22:21:31 +0000701 return Actions.ActOnWhileStmt(WhileLoc, CondGuard.take(), BodyGuard.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000702}
703
704/// ParseDoStatement
705/// do-statement: [C99 6.8.5.2]
706/// 'do' statement 'while' '(' expression ')' ';'
707/// Note: this lets the caller parse the end ';'.
708Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000709 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
711
Chris Lattner22153252007-08-26 23:08:06 +0000712 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
713 // the case for C90. Start the loop scope.
714 if (getLang().C99)
715 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
716 else
717 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000718
Chris Lattner0ecea032007-08-22 05:28:50 +0000719 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000720 // there is no compound stmt. C90 does not have this clause. We only do this
721 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +0000722 //
723 // C++ 6.5p2:
724 // The substatement in an iteration-statement implicitly defines a local scope
725 // which is entered and exited each time through the loop.
726 //
Chris Lattner19504402008-11-13 18:52:53 +0000727 bool NeedsInnerScope =
728 (getLang().C99 || getLang().CPlusPlus) && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000729 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000730
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 // Read the body statement.
732 StmtResult Body = ParseStatement();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000733 StmtGuard BodyGuard(Actions, Body);
Reid Spencer5f016e22007-07-11 17:01:13 +0000734
Chris Lattner0ecea032007-08-22 05:28:50 +0000735 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000736 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000737
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000738 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 ExitScope();
Chris Lattner19504402008-11-13 18:52:53 +0000740 if (!Body.isInvalid) {
741 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000742 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +0000743 SkipUntil(tok::semi, false, true);
744 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 return true;
746 }
747 SourceLocation WhileLoc = ConsumeToken();
748
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000749 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 ExitScope();
Chris Lattner1ab3b962008-11-18 07:48:38 +0000751 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +0000752 SkipUntil(tok::semi, false, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 return true;
754 }
755
756 // Parse the condition.
757 ExprResult Cond = ParseSimpleParenExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000758 ExprGuard CondGuard(Actions, Cond);
Reid Spencer5f016e22007-07-11 17:01:13 +0000759
760 ExitScope();
761
762 if (Cond.isInvalid || Body.isInvalid) return true;
763
Sebastian Redla55e52c2008-11-25 22:21:31 +0000764 return Actions.ActOnDoStmt(DoLoc, BodyGuard.take(),
765 WhileLoc, CondGuard.take());
Reid Spencer5f016e22007-07-11 17:01:13 +0000766}
767
768/// ParseForStatement
769/// for-statement: [C99 6.8.5.3]
770/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
771/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000772/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
773/// [C++] statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000774/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
775/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000776///
777/// [C++] for-init-statement:
778/// [C++] expression-statement
779/// [C++] simple-declaration
780///
Reid Spencer5f016e22007-07-11 17:01:13 +0000781Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000782 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
784
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000785 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000786 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 SkipUntil(tok::semi);
788 return true;
789 }
790
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000791 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
792
Chris Lattner22153252007-08-26 23:08:06 +0000793 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
794 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000795 //
796 // C++ 6.4p3:
797 // A name introduced by a declaration in a condition is in scope from its
798 // point of declaration until the end of the substatements controlled by the
799 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000800 // C++ 3.3.2p4:
801 // Names declared in the for-init-statement, and in the condition of if,
802 // while, for, and switch statements are local to the if, while, for, or
803 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000804 // C++ 6.5.3p1:
805 // Names declared in the for-init-statement are in the same declarative-region
806 // as those declared in the condition.
807 //
808 if (C99orCXX)
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000809 EnterScope(Scope::BreakScope | Scope::ContinueScope |
810 Scope::DeclScope | Scope::ControlScope);
Chris Lattner22153252007-08-26 23:08:06 +0000811 else
812 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000813
814 SourceLocation LParenLoc = ConsumeParen();
815 ExprResult Value;
816
817 StmtTy *FirstPart = 0;
818 ExprTy *SecondPart = 0;
819 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000820 bool ForEach = false;
Sebastian Redla55e52c2008-11-25 22:21:31 +0000821 StmtGuard FirstGuard(Actions), ThirdGuard(Actions);
822 ExprGuard SecondGuard(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000823
824 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000825 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 // no first part, eat the ';'.
827 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +0000828 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000830 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner81c018d2008-03-13 06:29:04 +0000832
833 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000834 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner81c018d2008-03-13 06:29:04 +0000835 // FIXME: Pass in the right location for the end of the declstmt.
836 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattner691a38b2008-03-13 06:29:54 +0000837 DeclStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000839 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000840 ConsumeToken(); // consume 'in'
841 Value = ParseExpression();
842 if (!Value.isInvalid)
843 SecondPart = Value.Val;
844 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 } else {
846 Value = ParseExpression();
847
848 // Turn the expression into a stmt.
849 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000850 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 if (!R.isInvalid)
852 FirstPart = R.Val;
853 }
854
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000855 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000857 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000858 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000859 ConsumeToken(); // consume 'in'
860 Value = ParseExpression();
861 if (!Value.isInvalid)
862 SecondPart = Value.Val;
863 }
864 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
866 SkipUntil(tok::semi);
867 }
868 }
Sebastian Redla55e52c2008-11-25 22:21:31 +0000869 FirstGuard.reset(FirstPart);
870 SecondGuard.reset(SecondPart);
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000871 if (!ForEach) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000872 assert(!SecondGuard.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000873 // Parse the second part of the for specifier.
874 if (Tok.is(tok::semi)) { // for (...;;
875 // no second part.
876 Value = ExprResult();
877 } else {
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000878 Value = getLang().CPlusPlus ? ParseCXXCondition()
879 : ParseExpression();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000880 if (!Value.isInvalid)
881 SecondPart = Value.Val;
882 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000883
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000884 if (Tok.is(tok::semi)) {
885 ConsumeToken();
886 } else {
887 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
888 SkipUntil(tok::semi);
889 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000890
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000891 // Parse the third part of the for specifier.
892 if (Tok.is(tok::r_paren)) { // for (...;...;)
893 // no third part.
894 Value = ExprResult();
895 } else {
896 Value = ParseExpression();
897 if (!Value.isInvalid) {
898 // Turn the expression into a stmt.
899 StmtResult R = Actions.ActOnExprStmt(Value.Val);
900 if (!R.isInvalid)
901 ThirdPart = R.Val;
902 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 }
Sebastian Redla55e52c2008-11-25 22:21:31 +0000904 SecondGuard.reset(SecondPart);
905 ThirdGuard.reset(ThirdPart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 // Match the ')'.
908 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
909
Chris Lattner0ecea032007-08-22 05:28:50 +0000910 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000911 // there is no compound stmt. C90 does not have this clause. We only do this
912 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000913 //
914 // C++ 6.5p2:
915 // The substatement in an iteration-statement implicitly defines a local scope
916 // which is entered and exited each time through the loop.
917 //
918 // See comments in ParseIfStatement for why we create a scope for
919 // for-init-statement/condition and a new scope for substatement in C++.
920 //
921 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000922 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000923
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 // Read the body statement.
925 StmtResult Body = ParseStatement();
926
Chris Lattner0ecea032007-08-22 05:28:50 +0000927 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000928 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000929
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 // Leave the for-scope.
931 ExitScope();
932
933 if (Body.isInvalid)
934 return Body;
935
Sebastian Redla55e52c2008-11-25 22:21:31 +0000936 // Release all the guards.
937 FirstGuard.take();
938 SecondGuard.take();
939 ThirdGuard.take();
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000940 if (!ForEach)
941 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
942 SecondPart, ThirdPart, RParenLoc, Body.Val);
943 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000944 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000945 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946}
947
948/// ParseGotoStatement
949/// jump-statement:
950/// 'goto' identifier ';'
951/// [GNU] 'goto' '*' expression ';'
952///
953/// Note: this lets the caller parse the end ';'.
954///
955Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000956 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
958
959 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000960 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000961 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 Tok.getIdentifierInfo());
963 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000964 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 // GNU indirect goto extension.
966 Diag(Tok, diag::ext_gnu_indirect_goto);
967 SourceLocation StarLoc = ConsumeToken();
968 ExprResult R = ParseExpression();
969 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
970 SkipUntil(tok::semi, false, true);
971 return true;
972 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000973 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000974 } else {
975 Diag(Tok, diag::err_expected_ident);
976 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000978
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 return Res;
980}
981
982/// ParseContinueStatement
983/// jump-statement:
984/// 'continue' ';'
985///
986/// Note: this lets the caller parse the end ';'.
987///
988Parser::StmtResult Parser::ParseContinueStatement() {
989 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000990 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000991}
992
993/// ParseBreakStatement
994/// jump-statement:
995/// 'break' ';'
996///
997/// Note: this lets the caller parse the end ';'.
998///
999Parser::StmtResult Parser::ParseBreakStatement() {
1000 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +00001001 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001002}
1003
1004/// ParseReturnStatement
1005/// jump-statement:
1006/// 'return' expression[opt] ';'
1007Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001008 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
1010
1011 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001012 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 R = ParseExpression();
1014 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
1015 SkipUntil(tok::semi, false, true);
1016 return true;
1017 }
1018 }
Steve Naroff1b273c42007-09-16 14:56:35 +00001019 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001020}
1021
Steve Naroff5f8aa692008-02-11 23:15:56 +00001022/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1023/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffd62701b2008-02-07 03:50:06 +00001024Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroffb746ce82008-02-07 23:24:32 +00001025 if (Tok.is(tok::l_brace)) {
1026 unsigned short savedBraceCount = BraceCount;
1027 do {
1028 ConsumeAnyToken();
1029 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1030 } else {
1031 // From the MS website: If used without braces, the __asm keyword means
1032 // that the rest of the line is an assembly-language statement.
1033 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001034 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001035 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1036 do {
1037 ConsumeAnyToken();
1038 TokLoc = Tok.getLocation();
1039 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1040 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1041 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001042 }
Steve Naroffd77bc282008-04-07 21:06:54 +00001043 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffd62701b2008-02-07 03:50:06 +00001044}
1045
Reid Spencer5f016e22007-07-11 17:01:13 +00001046/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001047/// asm-statement:
1048/// gnu-asm-statement
1049/// ms-asm-statement
1050///
1051/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001052/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1053///
1054/// [GNU] asm-argument:
1055/// asm-string-literal
1056/// asm-string-literal ':' asm-operands[opt]
1057/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1058/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1059/// ':' asm-clobbers
1060///
1061/// [GNU] asm-clobbers:
1062/// asm-string-literal
1063/// asm-clobbers ',' asm-string-literal
1064///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001065/// [MS] ms-asm-statement:
1066/// '__asm' assembly-instruction ';'[opt]
1067/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1068///
1069/// [MS] assembly-instruction-list:
1070/// assembly-instruction ';'[opt]
1071/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1072///
Steve Naroffd62701b2008-02-07 03:50:06 +00001073Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001074 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001075 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001076
Steve Naroff5f8aa692008-02-11 23:15:56 +00001077 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001078 msAsm = true;
1079 return FuzzyParseMicrosoftAsmStatement();
1080 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 DeclSpec DS;
1082 SourceLocation Loc = Tok.getLocation();
1083 ParseTypeQualifierListOpt(DS);
1084
1085 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1086 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001087 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001089 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Reid Spencer5f016e22007-07-11 17:01:13 +00001090
1091 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001092 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001093 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001094 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001095 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 SkipUntil(tok::r_paren);
1097 return true;
1098 }
1099 Loc = ConsumeParen();
1100
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +00001101 ExprResult AsmString = ParseAsmStringLiteral();
1102 if (AsmString.isInvalid)
1103 return true;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001104 ExprGuard AsmGuard(Actions, AsmString);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001105
1106 llvm::SmallVector<std::string, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001107 ExprVector Constraints(Actions);
1108 ExprVector Exprs(Actions);
1109 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001110
Anders Carlssondfab34a2008-02-05 23:03:50 +00001111 unsigned NumInputs = 0, NumOutputs = 0;
1112
1113 SourceLocation RParenLoc;
1114 if (Tok.is(tok::r_paren)) {
1115 // We have a simple asm expression
1116 isSimple = true;
1117
1118 RParenLoc = ConsumeParen();
1119 } else {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001120 // Parse Outputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001121 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1122 return true;
Anders Carlssondfab34a2008-02-05 23:03:50 +00001123
1124 NumOutputs = Names.size();
1125
1126 // Parse Inputs, if present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001127 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1128 return true;
1129
Anders Carlssondfab34a2008-02-05 23:03:50 +00001130 assert(Names.size() == Constraints.size() &&
1131 Constraints.size() == Exprs.size()
1132 && "Input operand size mismatch!");
1133
1134 NumInputs = Names.size() - NumOutputs;
1135
1136 // Parse the clobbers, if present.
1137 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +00001138 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +00001139
1140 // Parse the asm-string list for clobbers.
1141 while (1) {
1142 ExprResult Clobber = ParseAsmStringLiteral();
1143
1144 if (Clobber.isInvalid)
1145 break;
1146
1147 Clobbers.push_back(Clobber.Val);
1148
1149 if (Tok.isNot(tok::comma)) break;
1150 ConsumeToken();
1151 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 }
Anders Carlssondfab34a2008-02-05 23:03:50 +00001153
1154 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 }
1156
Anders Carlssondfab34a2008-02-05 23:03:50 +00001157 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1158 NumOutputs, NumInputs,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001159 &Names[0], Constraints.take(),
1160 Exprs.take(), AsmGuard.take(),
1161 Clobbers.size(), Clobbers.take(),
Anders Carlssonb235fc22007-11-22 01:36:19 +00001162 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163}
1164
1165/// ParseAsmOperands - Parse the asm-operands production as used by
1166/// asm-statement. We also parse a leading ':' token. If the leading colon is
1167/// not present, we do not parse anything.
1168///
1169/// [GNU] asm-operands:
1170/// asm-operand
1171/// asm-operands ',' asm-operand
1172///
1173/// [GNU] asm-operand:
1174/// asm-string-literal '(' expression ')'
1175/// '[' identifier ']' asm-string-literal '(' expression ')'
1176///
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001177bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001178 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1179 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // Only do anything if this operand is present.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001181 if (Tok.isNot(tok::colon)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 ConsumeToken();
1183
1184 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001185 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001186 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001187
Anders Carlssonb235fc22007-11-22 01:36:19 +00001188 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001190 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 SourceLocation Loc = ConsumeBracket();
1192
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001193 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 Diag(Tok, diag::err_expected_ident);
1195 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001196 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 }
Chris Lattner69efba72007-10-29 04:06:22 +00001198
Anders Carlssonb235fc22007-11-22 01:36:19 +00001199 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001200 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001201
1202 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001204 } else
1205 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001206
Anders Carlssonb235fc22007-11-22 01:36:19 +00001207 ExprResult Constraint = ParseAsmStringLiteral();
1208 if (Constraint.isInvalid) {
1209 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001210 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001211 }
1212 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001213
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001214 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001215 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001217 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 }
1219
1220 // Read the parenthesized expression.
1221 ExprResult Res = ParseSimpleParenExpression();
1222 if (Res.isInvalid) {
1223 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001224 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001226 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001228 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 ConsumeToken();
1230 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001231
1232 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001233}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001234
1235Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1236 SourceLocation L, SourceLocation R) {
1237 // Do not enter a scope for the brace, as the arguments are in the same scope
1238 // (the function body) as the body itself. Instead, just read the statement
1239 // list and put it into a CompoundStmt for safe keeping.
1240 StmtResult FnBody = ParseCompoundStatementBody();
1241
1242 // If the function body could not be parsed, make a bogus compoundstmt.
1243 if (FnBody.isInvalid)
1244 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1245
1246 // Leave the function body scope.
1247 ExitScope();
1248
Steve Naroffd6d054d2007-11-11 23:20:51 +00001249 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001250}