blob: a074463f3cf59a733a08b36753752aec9bfdb45b [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"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Parse/Scope.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.8: Statements and Blocks.
23//===----------------------------------------------------------------------===//
24
25/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
26/// StatementOrDeclaration:
27/// statement
28/// declaration
29///
30/// statement:
31/// labeled-statement
32/// compound-statement
33/// expression-statement
34/// selection-statement
35/// iteration-statement
36/// jump-statement
Fariborz Jahanianb384d322007-10-04 20:19:06 +000037/// [OBC] objc-throw-statement
38/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000039/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000040/// [GNU] asm-statement
41/// [OMP] openmp-construct [TODO]
42///
43/// labeled-statement:
44/// identifier ':' statement
45/// 'case' constant-expression ':' statement
46/// 'default' ':' statement
47///
48/// selection-statement:
49/// if-statement
50/// switch-statement
51///
52/// iteration-statement:
53/// while-statement
54/// do-statement
55/// for-statement
56///
57/// expression-statement:
58/// expression[opt] ';'
59///
60/// jump-statement:
61/// 'goto' identifier ';'
62/// 'continue' ';'
63/// 'break' ';'
64/// 'return' expression[opt] ';'
65/// [GNU] 'goto' '*' expression ';'
66///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000067/// [OBC] objc-throw-statement:
68/// [OBC] '@' 'throw' expression ';'
69/// [OBC] '@' 'throw' ';'
Reid Spencer5f016e22007-07-11 17:01:13 +000070///
71Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
72 const char *SemiError = 0;
73 Parser::StmtResult Res;
74
75 // Cases in this switch statement should fall through if the parser expects
76 // the token to end in a semicolon (in which case SemiError should be set),
77 // or they directly 'return;' if not.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000078 tok::TokenKind Kind = Tok.getKind();
79 SourceLocation AtLoc;
80 switch (Kind) {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 case tok::identifier: // C99 6.8.1: labeled-statement
82 // identifier ':' statement
83 // declaration (if !OnlyStatement)
84 // expression[opt] ';'
85 return ParseIdentifierStatement(OnlyStatement);
86
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
Steve Naroff64515f32008-02-05 21:27:35 +000090 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000091 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092
Reid Spencer5f016e22007-07-11 17:01:13 +000093 default:
Fariborz Jahanianb384d322007-10-04 20:19:06 +000094 if (!OnlyStatement && isDeclarationSpecifier()) {
Steve Naroff1b273c42007-09-16 14:56:35 +000095 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner4e1d99a2007-10-09 17:41:39 +000096 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 Diag(Tok, diag::err_expected_statement);
98 return true;
99 } else {
100 // expression[opt] ';'
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000101 ExprResult Res = ParseExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 if (Res.isInvalid) {
103 // If the expression is invalid, skip ahead to the next semicolon. Not
104 // doing this opens us up to the possibility of infinite loops if
105 // ParseExpression does not consume any tokens.
106 SkipUntil(tok::semi);
107 return true;
108 }
109 // Otherwise, eat the semicolon.
110 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000111 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113
114 case tok::kw_case: // C99 6.8.1: labeled-statement
115 return ParseCaseStatement();
116 case tok::kw_default: // C99 6.8.1: labeled-statement
117 return ParseDefaultStatement();
118
119 case tok::l_brace: // C99 6.8.2: compound-statement
120 return ParseCompoundStatement();
121 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff1b273c42007-09-16 14:56:35 +0000122 return Actions.ActOnNullStmt(ConsumeToken());
Reid Spencer5f016e22007-07-11 17:01:13 +0000123
124 case tok::kw_if: // C99 6.8.4.1: if-statement
125 return ParseIfStatement();
126 case tok::kw_switch: // C99 6.8.4.2: switch-statement
127 return ParseSwitchStatement();
128
129 case tok::kw_while: // C99 6.8.5.1: while-statement
130 return ParseWhileStatement();
131 case tok::kw_do: // C99 6.8.5.2: do-statement
132 Res = ParseDoStatement();
133 SemiError = "do/while loop";
134 break;
135 case tok::kw_for: // C99 6.8.5.3: for-statement
136 return ParseForStatement();
137
138 case tok::kw_goto: // C99 6.8.6.1: goto-statement
139 Res = ParseGotoStatement();
140 SemiError = "goto statement";
141 break;
142 case tok::kw_continue: // C99 6.8.6.2: continue-statement
143 Res = ParseContinueStatement();
144 SemiError = "continue statement";
145 break;
146 case tok::kw_break: // C99 6.8.6.3: break-statement
147 Res = ParseBreakStatement();
148 SemiError = "break statement";
149 break;
150 case tok::kw_return: // C99 6.8.6.4: return-statement
151 Res = ParseReturnStatement();
152 SemiError = "return statement";
153 break;
154
155 case tok::kw_asm:
156 Res = ParseAsmStatement();
157 SemiError = "asm statement";
158 break;
159 }
160
161 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000162 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ConsumeToken();
164 } else {
165 Diag(Tok, diag::err_expected_semi_after, SemiError);
166 SkipUntil(tok::semi);
167 }
168 return Res;
169}
170
171/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
172/// have a bit of a quandry here. Reading the identifier is necessary to see if
173/// there is a ':' after it. If there is, this is a label, regardless of what
174/// else the identifier can mean. If not, this is either part of a declaration
175/// (if the identifier is a type-name) or part of an expression.
176///
177/// labeled-statement:
178/// identifier ':' statement
179/// [GNU] identifier ':' attributes[opt] statement
180/// declaration (if !OnlyStatement)
181/// expression[opt] ';'
182///
183Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000184 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 "Not an identifier!");
186
Chris Lattnerd2177732007-07-20 16:59:19 +0000187 Token IdentTok = Tok; // Save the whole token.
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 ConsumeToken(); // eat the identifier.
189
190 // identifier ':' statement
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000191 if (Tok.is(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 SourceLocation ColonLoc = ConsumeToken();
193
194 // Read label attributes, if present.
195 DeclTy *AttrList = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000196 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // TODO: save these somewhere.
198 AttrList = ParseAttributes();
199
200 StmtResult SubStmt = ParseStatement();
201
202 // Broken substmt shouldn't prevent the label from being added to the AST.
203 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000204 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205
Steve Naroff1b273c42007-09-16 14:56:35 +0000206 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 IdentTok.getIdentifierInfo(),
208 ColonLoc, SubStmt.Val);
209 }
210
211 // Check to see if this is a declaration.
212 void *TypeRep;
213 if (!OnlyStatement &&
214 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
215 // Handle this. Warn/disable if in middle of block and !C99.
216 DeclSpec DS;
217
218 // Add the typedef name to the start of the decl-specs.
219 const char *PrevSpec = 0;
220 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
221 IdentTok.getLocation(), PrevSpec,
222 TypeRep);
223 assert(!isInvalid && "First declspec can't be invalid!");
Steve Narofff908a872007-10-30 02:23:23 +0000224 SourceLocation endProtoLoc;
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000225 if (Tok.is(tok::less)) {
226 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000227 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000228 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
229 new llvm::SmallVector<DeclTy *, 8>;
230 DS.setProtocolQualifiers(ProtocolDecl);
231 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
232 &ProtocolRefs[0], ProtocolRefs.size(),
233 *ProtocolDecl);
234 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000235
236 // ParseDeclarationSpecifiers will continue from there.
237 ParseDeclarationSpecifiers(DS);
238
239 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
240 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000241 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 // TODO: emit error on 'int;' or 'const enum foo;'.
243 // if (!DS.isMissingDeclaratorOk()) Diag(...);
244
245 ConsumeToken();
246 // FIXME: Return this as a type decl.
247 return 0;
248 }
249
250 // Parse all the declarators.
251 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
252 ParseDeclarator(DeclaratorInfo);
253
254 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff1b273c42007-09-16 14:56:35 +0000255 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 }
257
258 // Otherwise, this is an expression. Seed it with II and parse it.
259 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
260 if (Res.isInvalid) {
261 SkipUntil(tok::semi);
262 return true;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000263 } else if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 Diag(Tok, diag::err_expected_semi_after, "expression");
265 SkipUntil(tok::semi);
266 return true;
267 } else {
268 ConsumeToken();
269 // Convert expr to a stmt.
Steve Naroff1b273c42007-09-16 14:56:35 +0000270 return Actions.ActOnExprStmt(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 }
272}
273
274/// ParseCaseStatement
275/// labeled-statement:
276/// 'case' constant-expression ':' statement
277/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
278///
279/// Note that this does not parse the 'statement' at the end.
280///
281Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000282 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
284
285 ExprResult LHS = ParseConstantExpression();
286 if (LHS.isInvalid) {
287 SkipUntil(tok::colon);
288 return true;
289 }
290
291 // GNU case range extension.
292 SourceLocation DotDotDotLoc;
293 ExprTy *RHSVal = 0;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000294 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 Diag(Tok, diag::ext_gnu_case_range);
296 DotDotDotLoc = ConsumeToken();
297
298 ExprResult RHS = ParseConstantExpression();
299 if (RHS.isInvalid) {
300 SkipUntil(tok::colon);
301 return true;
302 }
303 RHSVal = RHS.Val;
304 }
305
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000306 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 Diag(Tok, diag::err_expected_colon_after, "'case'");
308 SkipUntil(tok::colon);
309 return true;
310 }
311
312 SourceLocation ColonLoc = ConsumeToken();
313
314 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000315 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 Diag(Tok, diag::err_label_end_of_compound_statement);
317 return true;
318 }
319
320 StmtResult SubStmt = ParseStatement();
321
322 // Broken substmt shouldn't prevent the case from being added to the AST.
323 if (SubStmt.isInvalid)
Steve Naroff1b273c42007-09-16 14:56:35 +0000324 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000325
Steve Naroff1b273c42007-09-16 14:56:35 +0000326 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 SubStmt.Val);
328}
329
330/// ParseDefaultStatement
331/// labeled-statement:
332/// 'default' ':' statement
333/// Note that this does not parse the 'statement' at the end.
334///
335Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000336 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
338
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000339 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 Diag(Tok, diag::err_expected_colon_after, "'default'");
341 SkipUntil(tok::colon);
342 return true;
343 }
344
345 SourceLocation ColonLoc = ConsumeToken();
346
347 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000348 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 Diag(Tok, diag::err_label_end_of_compound_statement);
350 return true;
351 }
352
353 StmtResult SubStmt = ParseStatement();
354 if (SubStmt.isInvalid)
355 return true;
356
Steve Naroff1b273c42007-09-16 14:56:35 +0000357 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358}
359
360
361/// ParseCompoundStatement - Parse a "{}" block.
362///
363/// compound-statement: [C99 6.8.2]
364/// { block-item-list[opt] }
365/// [GNU] { label-declarations block-item-list } [TODO]
366///
367/// block-item-list:
368/// block-item
369/// block-item-list block-item
370///
371/// block-item:
372/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000373/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000374/// statement
375/// [OMP] openmp-directive [TODO]
376///
377/// [GNU] label-declarations:
378/// [GNU] label-declaration
379/// [GNU] label-declarations label-declaration
380///
381/// [GNU] label-declaration:
382/// [GNU] '__label__' identifier-list ';'
383///
384/// [OMP] openmp-directive: [TODO]
385/// [OMP] barrier-directive
386/// [OMP] flush-directive
387///
Chris Lattner98414c12007-08-31 21:49:55 +0000388Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000389 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000390
Chris Lattner31e05722007-08-26 06:24:45 +0000391 // Enter a scope to hold everything within the compound stmt. Compound
392 // statements can always hold declarations.
393 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000394
395 // Parse the statements in the body.
Chris Lattner98414c12007-08-31 21:49:55 +0000396 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397
398 ExitScope();
399 return Body;
400}
401
402
403/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000404/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000405/// consume the '}' at the end of the block. It does not manipulate the scope
406/// stack.
Chris Lattner98414c12007-08-31 21:49:55 +0000407Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
409
410 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattner45a566c2007-08-27 01:01:57 +0000411 // only allowed at the start of a compound stmt regardless of the language.
Reid Spencer5f016e22007-07-11 17:01:13 +0000412
413 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000414 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000415 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000416 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000417 R = ParseStatementOrDeclaration(false);
418 } else {
419 // __extension__ can start declarations and it can also be a unary
420 // operator for expressions. Consume multiple __extension__ markers here
421 // until we can determine which is which.
422 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000423 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000424 ConsumeToken();
425
426 // If this is the start of a declaration, parse it as such.
427 if (isDeclarationSpecifier()) {
428 // FIXME: Save the __extension__ on the decl as a node somehow.
429 // FIXME: disable extwarns.
Steve Naroff1b273c42007-09-16 14:56:35 +0000430 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattner45a566c2007-08-27 01:01:57 +0000431 } else {
432 // Otherwise this was a unary __extension__ marker. Parse the
433 // subexpression and add the __extension__ unary op.
434 // FIXME: disable extwarns.
435 ExprResult Res = ParseCastExpression(false);
436 if (Res.isInvalid) {
437 SkipUntil(tok::semi);
438 continue;
439 }
440
441 // Add the __extension__ node to the AST.
Steve Narofff69936d2007-09-16 03:34:24 +0000442 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000443 if (Res.isInvalid)
444 continue;
445
446 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
447 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff1b273c42007-09-16 14:56:35 +0000448 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattner45a566c2007-08-27 01:01:57 +0000449 }
450 }
451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 if (!R.isInvalid && R.Val)
453 Stmts.push_back(R.Val);
454 }
455
456 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000457 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffd1a7cf82008-01-31 18:29:10 +0000459 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 }
461
462 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff1b273c42007-09-16 14:56:35 +0000463 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattner98414c12007-08-31 21:49:55 +0000464 &Stmts[0], Stmts.size(), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
467/// ParseIfStatement
468/// if-statement: [C99 6.8.4.1]
469/// 'if' '(' expression ')' statement
470/// 'if' '(' expression ')' statement 'else' statement
471///
472Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000473 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
475
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000476 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 Diag(Tok, diag::err_expected_lparen_after, "if");
478 SkipUntil(tok::semi);
479 return true;
480 }
481
Chris Lattner22153252007-08-26 23:08:06 +0000482 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
483 // the case for C90.
484 if (getLang().C99)
485 EnterScope(Scope::DeclScope);
486
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 // Parse the condition.
488 ExprResult CondExp = ParseSimpleParenExpression();
489 if (CondExp.isInvalid) {
490 SkipUntil(tok::semi);
Chris Lattner22153252007-08-26 23:08:06 +0000491 if (getLang().C99)
492 ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 return true;
494 }
495
Chris Lattner0ecea032007-08-22 05:28:50 +0000496 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000497 // there is no compound stmt. C90 does not have this clause. We only do this
498 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000499 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000500 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000501
Chris Lattnerb96728d2007-10-29 05:08:52 +0000502 // Read the 'then' stmt.
503 SourceLocation ThenStmtLoc = Tok.getLocation();
504 StmtResult ThenStmt = ParseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000505
Chris Lattnera36ce712007-08-22 05:16:28 +0000506 // Pop the 'if' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000507 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000508
509 // If it has an else, parse it.
510 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000511 SourceLocation ElseStmtLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 StmtResult ElseStmt(false);
Chris Lattnerb96728d2007-10-29 05:08:52 +0000513
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000514 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 ElseLoc = ConsumeToken();
Chris Lattnera36ce712007-08-22 05:16:28 +0000516
Chris Lattner0ecea032007-08-22 05:28:50 +0000517 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000518 // there is no compound stmt. C90 does not have this clause. We only do
519 // this if the body isn't a compound statement to avoid push/pop in common
520 // cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000521 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000522 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattnera36ce712007-08-22 05:16:28 +0000523
Chris Lattnerb96728d2007-10-29 05:08:52 +0000524 ElseStmtLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 ElseStmt = ParseStatement();
Chris Lattnera36ce712007-08-22 05:16:28 +0000526
527 // Pop the 'else' scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000528 if (NeedsInnerScope) ExitScope();
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 }
530
Chris Lattner22153252007-08-26 23:08:06 +0000531 if (getLang().C99)
532 ExitScope();
533
Chris Lattnerb96728d2007-10-29 05:08:52 +0000534 // If the then or else stmt is invalid and the other is valid (and present),
535 // make turn the invalid one into a null stmt to avoid dropping the other
536 // part. If both are invalid, return error.
537 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
538 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
539 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
540 // Both invalid, or one is invalid and other is non-present: delete cond and
541 // return error.
542 Actions.DeleteExpr(CondExp.Val);
543 return true;
544 }
545
546 // Now if either are invalid, replace with a ';'.
547 if (ThenStmt.isInvalid)
548 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
549 if (ElseStmt.isInvalid)
550 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
551
Chris Lattnerb96728d2007-10-29 05:08:52 +0000552 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 ElseLoc, ElseStmt.Val);
554}
555
556/// ParseSwitchStatement
557/// switch-statement:
558/// 'switch' '(' expression ')' statement
559Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000560 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
562
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000563 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 Diag(Tok, diag::err_expected_lparen_after, "switch");
565 SkipUntil(tok::semi);
566 return true;
567 }
Chris Lattner22153252007-08-26 23:08:06 +0000568
569 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
570 // not the case for C90. Start the switch scope.
571 if (getLang().C99)
572 EnterScope(Scope::BreakScope|Scope::DeclScope);
573 else
574 EnterScope(Scope::BreakScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000575
576 // Parse the condition.
577 ExprResult Cond = ParseSimpleParenExpression();
578
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000579 if (Cond.isInvalid) {
580 ExitScope();
581 return true;
582 }
583
Steve Naroff1b273c42007-09-16 14:56:35 +0000584 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000585
Chris Lattner0ecea032007-08-22 05:28:50 +0000586 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000587 // there is no compound stmt. C90 does not have this clause. We only do this
588 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000589 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000590 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 // Read the body statement.
593 StmtResult Body = ParseStatement();
594
Chris Lattner0ecea032007-08-22 05:28:50 +0000595 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000596 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000597
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000598 if (Body.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000599 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000600 // FIXME: Remove the case statement list from the Switch statement.
601 }
602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 ExitScope();
604
Steve Naroff1b273c42007-09-16 14:56:35 +0000605 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606}
607
608/// ParseWhileStatement
609/// while-statement: [C99 6.8.5.1]
610/// 'while' '(' expression ')' statement
611Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000612 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 SourceLocation WhileLoc = Tok.getLocation();
614 ConsumeToken(); // eat the 'while'.
615
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000616 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 Diag(Tok, diag::err_expected_lparen_after, "while");
618 SkipUntil(tok::semi);
619 return true;
620 }
621
Chris Lattner22153252007-08-26 23:08:06 +0000622 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
623 // the case for C90. Start the loop scope.
624 if (getLang().C99)
625 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
626 else
627 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000628
629 // Parse the condition.
630 ExprResult Cond = ParseSimpleParenExpression();
631
Chris Lattner0ecea032007-08-22 05:28:50 +0000632 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000633 // there is no compound stmt. C90 does not have this clause. We only do this
634 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000635 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000636 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000637
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 // Read the body statement.
639 StmtResult Body = ParseStatement();
640
Chris Lattner0ecea032007-08-22 05:28:50 +0000641 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000642 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 ExitScope();
645
646 if (Cond.isInvalid || Body.isInvalid) return true;
647
Steve Naroff1b273c42007-09-16 14:56:35 +0000648 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649}
650
651/// ParseDoStatement
652/// do-statement: [C99 6.8.5.2]
653/// 'do' statement 'while' '(' expression ')' ';'
654/// Note: this lets the caller parse the end ';'.
655Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000656 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
658
Chris Lattner22153252007-08-26 23:08:06 +0000659 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
660 // the case for C90. Start the loop scope.
661 if (getLang().C99)
662 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
663 else
664 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000665
Chris Lattner0ecea032007-08-22 05:28:50 +0000666 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000667 // there is no compound stmt. C90 does not have this clause. We only do this
668 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000669 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000670 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 // Read the body statement.
673 StmtResult Body = ParseStatement();
674
Chris Lattner0ecea032007-08-22 05:28:50 +0000675 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000676 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000677
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000678 if (Tok.isNot(tok::kw_while)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 ExitScope();
680 Diag(Tok, diag::err_expected_while);
681 Diag(DoLoc, diag::err_matching, "do");
682 SkipUntil(tok::semi);
683 return true;
684 }
685 SourceLocation WhileLoc = ConsumeToken();
686
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000687 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 ExitScope();
689 Diag(Tok, diag::err_expected_lparen_after, "do/while");
690 SkipUntil(tok::semi);
691 return true;
692 }
693
694 // Parse the condition.
695 ExprResult Cond = ParseSimpleParenExpression();
696
697 ExitScope();
698
699 if (Cond.isInvalid || Body.isInvalid) return true;
700
Steve Naroff1b273c42007-09-16 14:56:35 +0000701 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000702}
703
704/// ParseForStatement
705/// for-statement: [C99 6.8.5.3]
706/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
707/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000708/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
709/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000710Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000711 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
713
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000714 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 Diag(Tok, diag::err_expected_lparen_after, "for");
716 SkipUntil(tok::semi);
717 return true;
718 }
719
Chris Lattner22153252007-08-26 23:08:06 +0000720 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
721 // the case for C90. Start the loop scope.
722 if (getLang().C99)
723 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
724 else
725 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000726
727 SourceLocation LParenLoc = ConsumeParen();
728 ExprResult Value;
729
730 StmtTy *FirstPart = 0;
731 ExprTy *SecondPart = 0;
732 StmtTy *ThirdPart = 0;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000733 bool ForEach = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000734
735 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000736 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 // no first part, eat the ';'.
738 ConsumeToken();
739 } else if (isDeclarationSpecifier()) { // for (int X = 4;
740 // Parse declaration, which eats the ';'.
741 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
742 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
743 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff1b273c42007-09-16 14:56:35 +0000744 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000746 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000747 ConsumeToken(); // consume 'in'
748 Value = ParseExpression();
749 if (!Value.isInvalid)
750 SecondPart = Value.Val;
751 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 } else {
753 Value = ParseExpression();
754
755 // Turn the expression into a stmt.
756 if (!Value.isInvalid) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000757 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 if (!R.isInvalid)
759 FirstPart = R.Val;
760 }
761
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000762 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000764 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000765 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000766 ConsumeToken(); // consume 'in'
767 Value = ParseExpression();
768 if (!Value.isInvalid)
769 SecondPart = Value.Val;
770 }
771 else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
773 SkipUntil(tok::semi);
774 }
775 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000776 if (!ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000777 // Parse the second part of the for specifier.
778 if (Tok.is(tok::semi)) { // for (...;;
779 // no second part.
780 Value = ExprResult();
781 } else {
782 Value = ParseExpression();
783 if (!Value.isInvalid)
784 SecondPart = Value.Val;
785 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000786
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000787 if (Tok.is(tok::semi)) {
788 ConsumeToken();
789 } else {
790 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
791 SkipUntil(tok::semi);
792 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000793
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000794 // Parse the third part of the for specifier.
795 if (Tok.is(tok::r_paren)) { // for (...;...;)
796 // no third part.
797 Value = ExprResult();
798 } else {
799 Value = ParseExpression();
800 if (!Value.isInvalid) {
801 // Turn the expression into a stmt.
802 StmtResult R = Actions.ActOnExprStmt(Value.Val);
803 if (!R.isInvalid)
804 ThirdPart = R.Val;
805 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 }
807 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 // Match the ')'.
809 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
810
Chris Lattner0ecea032007-08-22 05:28:50 +0000811 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000812 // there is no compound stmt. C90 does not have this clause. We only do this
813 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000814 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner31e05722007-08-26 06:24:45 +0000815 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner0ecea032007-08-22 05:28:50 +0000816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 // Read the body statement.
818 StmtResult Body = ParseStatement();
819
Chris Lattner0ecea032007-08-22 05:28:50 +0000820 // Pop the body scope if needed.
Chris Lattner38484402007-08-22 05:33:11 +0000821 if (NeedsInnerScope) ExitScope();
Chris Lattner0ecea032007-08-22 05:28:50 +0000822
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 // Leave the for-scope.
824 ExitScope();
825
826 if (Body.isInvalid)
827 return Body;
828
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000829 if (!ForEach)
830 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
831 SecondPart, ThirdPart, RParenLoc, Body.Val);
832 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000833 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000834 SecondPart, RParenLoc, Body.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835}
836
837/// ParseGotoStatement
838/// jump-statement:
839/// 'goto' identifier ';'
840/// [GNU] 'goto' '*' expression ';'
841///
842/// Note: this lets the caller parse the end ';'.
843///
844Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000845 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
847
848 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000849 if (Tok.is(tok::identifier)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000850 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 Tok.getIdentifierInfo());
852 ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000853 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 // GNU indirect goto extension.
855 Diag(Tok, diag::ext_gnu_indirect_goto);
856 SourceLocation StarLoc = ConsumeToken();
857 ExprResult R = ParseExpression();
858 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
859 SkipUntil(tok::semi, false, true);
860 return true;
861 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000862 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattner95cfb852007-07-22 04:13:33 +0000863 } else {
864 Diag(Tok, diag::err_expected_ident);
865 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 }
Chris Lattner95cfb852007-07-22 04:13:33 +0000867
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 return Res;
869}
870
871/// ParseContinueStatement
872/// jump-statement:
873/// 'continue' ';'
874///
875/// Note: this lets the caller parse the end ';'.
876///
877Parser::StmtResult Parser::ParseContinueStatement() {
878 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000879 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000880}
881
882/// ParseBreakStatement
883/// jump-statement:
884/// 'break' ';'
885///
886/// Note: this lets the caller parse the end ';'.
887///
888Parser::StmtResult Parser::ParseBreakStatement() {
889 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff1b273c42007-09-16 14:56:35 +0000890 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Reid Spencer5f016e22007-07-11 17:01:13 +0000891}
892
893/// ParseReturnStatement
894/// jump-statement:
895/// 'return' expression[opt] ';'
896Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000897 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
899
900 ExprResult R(0);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000901 if (Tok.isNot(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 R = ParseExpression();
903 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
904 SkipUntil(tok::semi, false, true);
905 return true;
906 }
907 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000908 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000909}
910
911/// ParseAsmStatement - Parse a GNU extended asm statement.
912/// [GNU] asm-statement:
913/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
914///
915/// [GNU] asm-argument:
916/// asm-string-literal
917/// asm-string-literal ':' asm-operands[opt]
918/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
919/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
920/// ':' asm-clobbers
921///
922/// [GNU] asm-clobbers:
923/// asm-string-literal
924/// asm-clobbers ',' asm-string-literal
925///
926Parser::StmtResult Parser::ParseAsmStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000927 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +0000928 SourceLocation AsmLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000929
930 DeclSpec DS;
931 SourceLocation Loc = Tok.getLocation();
932 ParseTypeQualifierListOpt(DS);
933
934 // GNU asms accept, but warn, about type-qualifiers other than volatile.
935 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
936 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
937 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
938 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
939
940 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +0000941 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlssondfab34a2008-02-05 23:03:50 +0000942 bool isSimple = false;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000943 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 Diag(Tok, diag::err_expected_lparen_after, "asm");
945 SkipUntil(tok::r_paren);
946 return true;
947 }
948 Loc = ConsumeParen();
949
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000950 ExprResult AsmString = ParseAsmStringLiteral();
951 if (AsmString.isInvalid)
952 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +0000953
954 llvm::SmallVector<std::string, 4> Names;
955 llvm::SmallVector<ExprTy*, 4> Constraints;
956 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlssonb235fc22007-11-22 01:36:19 +0000957 llvm::SmallVector<ExprTy*, 4> Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +0000958
Anders Carlssondfab34a2008-02-05 23:03:50 +0000959 unsigned NumInputs = 0, NumOutputs = 0;
960
961 SourceLocation RParenLoc;
962 if (Tok.is(tok::r_paren)) {
963 // We have a simple asm expression
964 isSimple = true;
965
966 RParenLoc = ConsumeParen();
967 } else {
968 // Parse Outputs, if present.
969 ParseAsmOperandsOpt(Names, Constraints, Exprs);
970
971 NumOutputs = Names.size();
972
973 // Parse Inputs, if present.
974 ParseAsmOperandsOpt(Names, Constraints, Exprs);
975 assert(Names.size() == Constraints.size() &&
976 Constraints.size() == Exprs.size()
977 && "Input operand size mismatch!");
978
979 NumInputs = Names.size() - NumOutputs;
980
981 // Parse the clobbers, if present.
982 if (Tok.is(tok::colon)) {
Anders Carlssoneecf8472007-11-21 23:27:34 +0000983 ConsumeToken();
Anders Carlssondfab34a2008-02-05 23:03:50 +0000984
985 // Parse the asm-string list for clobbers.
986 while (1) {
987 ExprResult Clobber = ParseAsmStringLiteral();
988
989 if (Clobber.isInvalid)
990 break;
991
992 Clobbers.push_back(Clobber.Val);
993
994 if (Tok.isNot(tok::comma)) break;
995 ConsumeToken();
996 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 }
Anders Carlssondfab34a2008-02-05 23:03:50 +0000998
999 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 }
1001
Anders Carlssondfab34a2008-02-05 23:03:50 +00001002 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1003 NumOutputs, NumInputs,
Anders Carlssonb235fc22007-11-22 01:36:19 +00001004 &Names[0], &Constraints[0], &Exprs[0],
1005 AsmString.Val,
1006 Clobbers.size(), &Clobbers[0],
1007 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001008}
1009
1010/// ParseAsmOperands - Parse the asm-operands production as used by
1011/// asm-statement. We also parse a leading ':' token. If the leading colon is
1012/// not present, we do not parse anything.
1013///
1014/// [GNU] asm-operands:
1015/// asm-operand
1016/// asm-operands ',' asm-operand
1017///
1018/// [GNU] asm-operand:
1019/// asm-string-literal '(' expression ')'
1020/// '[' identifier ']' asm-string-literal '(' expression ')'
1021///
Anders Carlssonb235fc22007-11-22 01:36:19 +00001022void Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
1023 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1024 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 // Only do anything if this operand is present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001026 if (Tok.isNot(tok::colon)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 ConsumeToken();
1028
1029 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001030 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 return;
1032
Anders Carlssonb235fc22007-11-22 01:36:19 +00001033 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001035 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 SourceLocation Loc = ConsumeBracket();
1037
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001038 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 Diag(Tok, diag::err_expected_ident);
1040 SkipUntil(tok::r_paren);
1041 return;
1042 }
Chris Lattner69efba72007-10-29 04:06:22 +00001043
Anders Carlssonb235fc22007-11-22 01:36:19 +00001044 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001045 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001046
1047 Names.push_back(std::string(II->getName(), II->getLength()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001049 } else
1050 Names.push_back(std::string());
Reid Spencer5f016e22007-07-11 17:01:13 +00001051
Anders Carlssonb235fc22007-11-22 01:36:19 +00001052 ExprResult Constraint = ParseAsmStringLiteral();
1053 if (Constraint.isInvalid) {
1054 SkipUntil(tok::r_paren);
1055 return;
1056 }
1057 Constraints.push_back(Constraint.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001059 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1061 SkipUntil(tok::r_paren);
1062 return;
1063 }
1064
1065 // Read the parenthesized expression.
1066 ExprResult Res = ParseSimpleParenExpression();
1067 if (Res.isInvalid) {
1068 SkipUntil(tok::r_paren);
1069 return;
1070 }
Anders Carlssonb235fc22007-11-22 01:36:19 +00001071 Exprs.push_back(Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 // Eat the comma and continue parsing if it exists.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001073 if (Tok.isNot(tok::comma)) return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 ConsumeToken();
1075 }
1076}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001077
1078Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1079 SourceLocation L, SourceLocation R) {
1080 // Do not enter a scope for the brace, as the arguments are in the same scope
1081 // (the function body) as the body itself. Instead, just read the statement
1082 // list and put it into a CompoundStmt for safe keeping.
1083 StmtResult FnBody = ParseCompoundStatementBody();
1084
1085 // If the function body could not be parsed, make a bogus compoundstmt.
1086 if (FnBody.isInvalid)
1087 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1088
1089 // Leave the function body scope.
1090 ExitScope();
1091
Steve Naroffd6d054d2007-11-11 23:20:51 +00001092 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001093}