blob: 01cdbbcb4676c9c92a82844400202699f07d2aec [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Chris Lattner288e86ff12006-11-11 23:03:42 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner33ad2ca2006-11-05 23:47:55 +000018#include "clang/Parse/Scope.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000019using 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
37/// [OBC] objc-throw-statement [TODO]
38/// [OBC] objc-try-catch-statement [TODO]
39/// [OBC] objc-synchronized-statement [TODO]
Chris Lattner0116c472006-08-15 06:03:28 +000040/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000041/// [OMP] openmp-construct [TODO]
42///
43/// labeled-statement:
44/// identifier ':' statement
45/// 'case' constant-expression ':' statement
46/// 'default' ':' statement
47///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// selection-statement:
49/// if-statement
50/// switch-statement
51///
52/// iteration-statement:
53/// while-statement
54/// do-statement
55/// for-statement
56///
Chris Lattner9075bd72006-08-10 04:59:57 +000057/// expression-statement:
58/// expression[opt] ';'
59///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000060/// jump-statement:
61/// 'goto' identifier ';'
62/// 'continue' ';'
63/// 'break' ';'
64/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000065/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000066///
67/// [OBC] objc-throw-statement: [TODO]
68/// [OBC] '@' 'throw' expression ';' [TODO]
69/// [OBC] '@' 'throw' ';' [TODO]
70///
Chris Lattner30f910e2006-10-16 05:52:41 +000071Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000072 const char *SemiError = 0;
Chris Lattner30f910e2006-10-16 05:52:41 +000073 Parser::StmtResult Res;
Chris Lattner503fadc2006-08-10 05:45:44 +000074
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.
Chris Lattner0ccd51e2006-08-09 05:47:47 +000078 switch (Tok.getKind()) {
Chris Lattnerf8afb622006-08-10 18:26:31 +000079 case tok::identifier: // C99 6.8.1: labeled-statement
80 // identifier ':' statement
81 // declaration (if !OnlyStatement)
82 // expression[opt] ';'
83 return ParseIdentifierStatement(OnlyStatement);
84
Chris Lattner0ccd51e2006-08-09 05:47:47 +000085 default:
Chris Lattnerf8afb622006-08-10 18:26:31 +000086 if (!OnlyStatement && isDeclarationSpecifier()) {
87 // TODO: warn/disable if declaration is in the middle of a block and !C99.
Steve Naroff2a8ad182007-05-29 22:59:26 +000088 return Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerf8afb622006-08-10 18:26:31 +000089 } else if (Tok.getKind() == tok::r_brace) {
90 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +000091 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +000092 } else {
93 // expression[opt] ';'
Chris Lattner89c50c62006-08-11 06:41:18 +000094 ExprResult Res = ParseExpression();
95 if (Res.isInvalid) {
96 // If the expression is invalid, skip ahead to the next semicolon. Not
97 // doing this opens us up to the possibility of infinite loops if
98 // ParseExpression does not consume any tokens.
99 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000100 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000101 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000102 // Otherwise, eat the semicolon.
103 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Chris Lattnercd68f642007-06-27 01:06:29 +0000104 return Actions.ParseExprStmt(Res.Val);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000105 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000106
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000107 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000108 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000109 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000110 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000111
112 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000113 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000114 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
115 return Actions.ParseNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000116
Chris Lattner9075bd72006-08-10 04:59:57 +0000117 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000118 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000119 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000120 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000121
Chris Lattner9075bd72006-08-10 04:59:57 +0000122 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000123 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000124 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000125 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000126 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000127 break;
128 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000129 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000130
131 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000132 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000133 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000134 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000135 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000136 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000137 SemiError = "continue statement";
138 break;
139 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000140 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000141 SemiError = "break statement";
142 break;
143 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000144 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000145 SemiError = "return statement";
146 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000147
148 case tok::kw_asm:
Chris Lattner30f910e2006-10-16 05:52:41 +0000149 Res = ParseAsmStatement();
Chris Lattner0116c472006-08-15 06:03:28 +0000150 SemiError = "asm statement";
151 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000152 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000153
154 // If we reached this code, the statement must end in a semicolon.
155 if (Tok.getKind() == tok::semi) {
156 ConsumeToken();
157 } else {
158 Diag(Tok, diag::err_expected_semi_after, SemiError);
159 SkipUntil(tok::semi);
160 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000161 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000162}
163
Chris Lattnerf8afb622006-08-10 18:26:31 +0000164/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
165/// have a bit of a quandry here. Reading the identifier is necessary to see if
166/// there is a ':' after it. If there is, this is a label, regardless of what
167/// else the identifier can mean. If not, this is either part of a declaration
168/// (if the identifier is a type-name) or part of an expression.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000169///
170/// labeled-statement:
171/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000172/// [GNU] identifier ':' attributes[opt] statement
Chris Lattner6dfd9782006-08-10 18:31:37 +0000173/// declaration (if !OnlyStatement)
174/// expression[opt] ';'
175///
Chris Lattner30f910e2006-10-16 05:52:41 +0000176Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner0663d2a2006-11-05 18:39:59 +0000177 assert(Tok.getKind() == tok::identifier && Tok.getIdentifierInfo() &&
178 "Not an identifier!");
Chris Lattner6dfd9782006-08-10 18:31:37 +0000179
Chris Lattner146762e2007-07-20 16:59:19 +0000180 Token IdentTok = Tok; // Save the whole token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000181 ConsumeToken(); // eat the identifier.
182
183 // identifier ':' statement
184 if (Tok.getKind() == tok::colon) {
Chris Lattneraf635312006-10-16 06:06:51 +0000185 SourceLocation ColonLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000186
187 // Read label attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000189 if (Tok.getKind() == tok::kw___attribute)
Chris Lattner30f910e2006-10-16 05:52:41 +0000190 // TODO: save these somewhere.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +0000192
Chris Lattner30f910e2006-10-16 05:52:41 +0000193 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000194
195 // Broken substmt shouldn't prevent the label from being added to the AST.
196 if (SubStmt.isInvalid)
197 SubStmt = Actions.ParseNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000198
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000199 return Actions.ParseLabelStmt(IdentTok.getLocation(),
200 IdentTok.getIdentifierInfo(),
201 ColonLoc, SubStmt.Val);
Chris Lattner6dfd9782006-08-10 18:31:37 +0000202 }
203
Chris Lattner30f910e2006-10-16 05:52:41 +0000204 // Check to see if this is a declaration.
Chris Lattner49252eb2007-01-27 19:04:39 +0000205 void *TypeRep;
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000206 if (!OnlyStatement &&
Chris Lattner49252eb2007-01-27 19:04:39 +0000207 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000208 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000209 DeclSpec DS;
210
Chris Lattner49252eb2007-01-27 19:04:39 +0000211 // Add the typedef name to the start of the decl-specs.
212 const char *PrevSpec = 0;
213 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
214 IdentTok.getLocation(), PrevSpec,
215 TypeRep);
216 assert(!isInvalid && "First declspec can't be invalid!");
Chris Lattner0663d2a2006-11-05 18:39:59 +0000217
Chris Lattner2f9980e2006-08-10 18:39:24 +0000218 // ParseDeclarationSpecifiers will continue from there.
219 ParseDeclarationSpecifiers(DS);
220
Chris Lattner0e894622006-08-13 19:58:17 +0000221 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
222 // declaration-specifiers init-declarator-list[opt] ';'
223 if (Tok.getKind() == tok::semi) {
224 // TODO: emit error on 'int;' or 'const enum foo;'.
225 // if (!DS.isMissingDeclaratorOk()) Diag(...);
226
227 ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000228 // FIXME: Return this as a type decl.
229 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000230 }
231
Chris Lattner2f9980e2006-08-10 18:39:24 +0000232 // Parse all the declarators.
233 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
234 ParseDeclarator(DeclaratorInfo);
235
Chris Lattner436806a2007-07-10 05:03:31 +0000236 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
237 return Decl ? Actions.ParseDeclStmt(Decl) : 0;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000238 }
239
Chris Lattner0c6c0342006-08-12 18:12:45 +0000240 // Otherwise, this is an expression. Seed it with II and parse it.
241 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
Chris Lattner30f910e2006-10-16 05:52:41 +0000242 if (Res.isInvalid) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000243 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000244 return true;
245 } else if (Tok.getKind() != tok::semi) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000246 Diag(Tok, diag::err_expected_semi_after, "expression");
247 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000248 return true;
249 } else {
250 ConsumeToken();
Chris Lattnercd68f642007-06-27 01:06:29 +0000251 // Convert expr to a stmt.
252 return Actions.ParseExprStmt(Res.Val);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000253 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000254}
255
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000256/// ParseCaseStatement
257/// labeled-statement:
258/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000259/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000260///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000261/// Note that this does not parse the 'statement' at the end.
262///
Chris Lattner30f910e2006-10-16 05:52:41 +0000263Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000264 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000265 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000266
Chris Lattner30f910e2006-10-16 05:52:41 +0000267 ExprResult LHS = ParseConstantExpression();
268 if (LHS.isInvalid) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000269 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000270 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000271 }
272
273 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000274 SourceLocation DotDotDotLoc;
275 ExprTy *RHSVal = 0;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000276 if (Tok.getKind() == tok::ellipsis) {
277 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000278 DotDotDotLoc = ConsumeToken();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000279
280 ExprResult RHS = ParseConstantExpression();
281 if (RHS.isInvalid) {
282 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000283 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000284 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000285 RHSVal = RHS.Val;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000286 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000287
Chris Lattner30f910e2006-10-16 05:52:41 +0000288 if (Tok.getKind() != tok::colon) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000289 Diag(Tok, diag::err_expected_colon_after, "'case'");
290 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000291 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000292 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000293
Chris Lattneraf635312006-10-16 06:06:51 +0000294 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000295
296 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
297 if (Tok.getKind() == tok::r_brace) {
298 Diag(Tok, diag::err_label_end_of_compound_statement);
299 return true;
300 }
301
302 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000303
304 // Broken substmt shouldn't prevent the case from being added to the AST.
Chris Lattner30f910e2006-10-16 05:52:41 +0000305 if (SubStmt.isInvalid)
Chris Lattnerac4471c2007-05-28 05:38:24 +0000306 SubStmt = Actions.ParseNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000307
308 // TODO: look up enclosing switch stmt.
309 return Actions.ParseCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
310 SubStmt.Val);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000311}
312
313/// ParseDefaultStatement
314/// labeled-statement:
315/// 'default' ':' statement
316/// Note that this does not parse the 'statement' at the end.
317///
Chris Lattner30f910e2006-10-16 05:52:41 +0000318Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000319 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000320 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000321
Chris Lattner30f910e2006-10-16 05:52:41 +0000322 if (Tok.getKind() != tok::colon) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000323 Diag(Tok, diag::err_expected_colon_after, "'default'");
324 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000325 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000326 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000327
Chris Lattneraf635312006-10-16 06:06:51 +0000328 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000329
330 // Diagnose the common error "switch (X) {... default: }", which is not valid.
331 if (Tok.getKind() == tok::r_brace) {
332 Diag(Tok, diag::err_label_end_of_compound_statement);
333 return true;
334 }
335
336 StmtResult SubStmt = ParseStatement();
337 if (SubStmt.isInvalid)
338 return true;
339
340 // TODO: look up enclosing switch stmt.
Chris Lattner46eeb222007-07-18 02:28:47 +0000341 return Actions.ParseDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000342}
343
344
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000345/// ParseCompoundStatement - Parse a "{}" block.
346///
347/// compound-statement: [C99 6.8.2]
348/// { block-item-list[opt] }
349/// [GNU] { label-declarations block-item-list } [TODO]
350///
351/// block-item-list:
352/// block-item
353/// block-item-list block-item
354///
355/// block-item:
356/// declaration
357/// [GNU] '__extension__' declaration [TODO]
358/// statement
359/// [OMP] openmp-directive [TODO]
360///
361/// [GNU] label-declarations:
362/// [GNU] label-declaration
363/// [GNU] label-declarations label-declaration
364///
365/// [GNU] label-declaration:
366/// [GNU] '__label__' identifier-list ';'
367///
368/// [OMP] openmp-directive: [TODO]
369/// [OMP] barrier-directive
370/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000371///
372Parser::StmtResult Parser::ParseCompoundStatement() {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000373 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000374
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000375 // Enter a scope to hold everything within the compound stmt. Compound
376 // statements can always hold declarations.
377 EnterScope(Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000378
379 // Parse the statements in the body.
380 StmtResult Body = ParseCompoundStatementBody();
381
382 ExitScope();
383 return Body;
384}
385
386
387/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
388/// ParseCompoundStmt action. This expects the '{' to be the current token, and
389/// consume the '}' at the end of the block. It does not manipulate the scope
390/// stack.
391Parser::StmtResult Parser::ParseCompoundStatementBody() {
392 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
393
Chris Lattner010015a2007-05-28 07:23:54 +0000394 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
395 // only allowed at the start of a compound stmt.
396
Chris Lattner23b7eb62007-06-15 23:05:46 +0000397 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner30f910e2006-10-16 05:52:41 +0000398 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) {
399 StmtResult R = ParseStatementOrDeclaration(false);
400 if (!R.isInvalid && R.Val)
401 Stmts.push_back(R.Val);
402 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000403
404 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner30f910e2006-10-16 05:52:41 +0000405 if (Tok.getKind() != tok::r_brace) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000406 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner30f910e2006-10-16 05:52:41 +0000407 return 0;
408 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000409
Chris Lattner04132372006-10-16 06:12:55 +0000410 SourceLocation RBraceLoc = ConsumeBrace();
Chris Lattner30f910e2006-10-16 05:52:41 +0000411 return Actions.ParseCompoundStmt(LBraceLoc, RBraceLoc,
412 &Stmts[0], Stmts.size());
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000413}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000414
415/// ParseIfStatement
416/// if-statement: [C99 6.8.4.1]
417/// 'if' '(' expression ')' statement
418/// 'if' '(' expression ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000419///
420Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000421 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000422 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000423
424 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000425 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000426 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000427 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000428 }
429
Chris Lattner2dd1b722007-08-26 23:08:06 +0000430 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
431 // the case for C90.
432 if (getLang().C99)
433 EnterScope(Scope::DeclScope);
434
Chris Lattnerc951dae2006-08-10 04:23:57 +0000435 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000436 ExprResult CondExp = ParseSimpleParenExpression();
437 if (CondExp.isInvalid) {
438 SkipUntil(tok::semi);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000439 if (getLang().C99)
440 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000441 return true;
442 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000443
Chris Lattner8fb26252007-08-22 05:28:50 +0000444 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000445 // there is no compound stmt. C90 does not have this clause. We only do this
446 // if the body isn't a compound statement to avoid push/pop in common cases.
447 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000448 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000449
Chris Lattnerc951dae2006-08-10 04:23:57 +0000450 // Read the if condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000451 StmtResult CondStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000452
453 // Broken substmt shouldn't prevent the label from being added to the AST.
454 if (CondStmt.isInvalid)
455 CondStmt = Actions.ParseNullStmt(Tok.getLocation());
456
Chris Lattner37e54f42007-08-22 05:16:28 +0000457 // Pop the 'if' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000458 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000459
460 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000461 SourceLocation ElseLoc;
462 StmtResult ElseStmt(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000463 if (Tok.getKind() == tok::kw_else) {
Chris Lattneraf635312006-10-16 06:06:51 +0000464 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000465
Chris Lattner8fb26252007-08-22 05:28:50 +0000466 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000467 // there is no compound stmt. C90 does not have this clause. We only do
468 // this if the body isn't a compound statement to avoid push/pop in common
469 // cases.
470 NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000471 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000472
Chris Lattner30f910e2006-10-16 05:52:41 +0000473 ElseStmt = ParseStatement();
Chris Lattner37e54f42007-08-22 05:16:28 +0000474
475 // Pop the 'else' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000476 if (NeedsInnerScope) ExitScope();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000477
478 if (ElseStmt.isInvalid)
479 ElseStmt = Actions.ParseNullStmt(ElseLoc);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000480 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000481
Chris Lattner2dd1b722007-08-26 23:08:06 +0000482 if (getLang().C99)
483 ExitScope();
484
Chris Lattner30f910e2006-10-16 05:52:41 +0000485 return Actions.ParseIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
486 ElseLoc, ElseStmt.Val);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000487}
488
Chris Lattner9075bd72006-08-10 04:59:57 +0000489/// ParseSwitchStatement
490/// switch-statement:
491/// 'switch' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000492Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000493 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000494 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000495
496 if (Tok.getKind() != tok::l_paren) {
497 Diag(Tok, diag::err_expected_lparen_after, "switch");
498 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000499 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000500 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000501
502 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
503 // not the case for C90. Start the switch scope.
504 if (getLang().C99)
505 EnterScope(Scope::BreakScope|Scope::DeclScope);
506 else
507 EnterScope(Scope::BreakScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000508
Chris Lattner9075bd72006-08-10 04:59:57 +0000509 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000510 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000511
Anders Carlsson51873c22007-07-22 07:07:56 +0000512 if (Cond.isInvalid) {
513 ExitScope();
514 return true;
515 }
516
517 StmtResult Switch = Actions.StartSwitchStmt(Cond.Val);
518
Chris Lattner8fb26252007-08-22 05:28:50 +0000519 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000520 // there is no compound stmt. C90 does not have this clause. We only do this
521 // if the body isn't a compound statement to avoid push/pop in common cases.
522 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000523 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000524
Chris Lattner9075bd72006-08-10 04:59:57 +0000525 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000526 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000527
Chris Lattner8fb26252007-08-22 05:28:50 +0000528 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000529 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000530
Anders Carlsson51873c22007-07-22 07:07:56 +0000531 if (Body.isInvalid) {
532 Body = Actions.ParseNullStmt(Tok.getLocation());
533 // FIXME: Remove the case statement list from the Switch statement.
534 }
535
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000536 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000537
Anders Carlsson51873c22007-07-22 07:07:56 +0000538 return Actions.FinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000539}
540
541/// ParseWhileStatement
542/// while-statement: [C99 6.8.5.1]
543/// 'while' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000544Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000545 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000546 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000547 ConsumeToken(); // eat the 'while'.
548
549 if (Tok.getKind() != tok::l_paren) {
550 Diag(Tok, diag::err_expected_lparen_after, "while");
551 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000552 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000553 }
554
Chris Lattner2dd1b722007-08-26 23:08:06 +0000555 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
556 // the case for C90. Start the loop scope.
557 if (getLang().C99)
558 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
559 else
560 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000561
Chris Lattner9075bd72006-08-10 04:59:57 +0000562 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000563 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000564
Chris Lattner8fb26252007-08-22 05:28:50 +0000565 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000566 // there is no compound stmt. C90 does not have this clause. We only do this
567 // if the body isn't a compound statement to avoid push/pop in common cases.
568 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000569 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000570
Chris Lattner9075bd72006-08-10 04:59:57 +0000571 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000572 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000573
Chris Lattner8fb26252007-08-22 05:28:50 +0000574 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000575 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000576
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000577 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000578
579 if (Cond.isInvalid || Body.isInvalid) return true;
580
581 return Actions.ParseWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000582}
583
584/// ParseDoStatement
585/// do-statement: [C99 6.8.5.2]
586/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000587/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000588Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000589 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000590 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000591
Chris Lattner2dd1b722007-08-26 23:08:06 +0000592 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
593 // the case for C90. Start the loop scope.
594 if (getLang().C99)
595 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
596 else
597 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000598
Chris Lattner8fb26252007-08-22 05:28:50 +0000599 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000600 // there is no compound stmt. C90 does not have this clause. We only do this
601 // if the body isn't a compound statement to avoid push/pop in common cases.
602 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000603 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000604
Chris Lattner9075bd72006-08-10 04:59:57 +0000605 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000606 StmtResult Body = ParseStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000607
Chris Lattner8fb26252007-08-22 05:28:50 +0000608 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000609 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000610
Chris Lattner9075bd72006-08-10 04:59:57 +0000611 if (Tok.getKind() != tok::kw_while) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000612 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000613 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000614 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000615 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000616 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000617 }
Chris Lattneraf635312006-10-16 06:06:51 +0000618 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000619
620 if (Tok.getKind() != tok::l_paren) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000621 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000622 Diag(Tok, diag::err_expected_lparen_after, "do/while");
623 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000624 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000625 }
626
627 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000628 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000629
630 ExitScope();
631
Chris Lattner30f910e2006-10-16 05:52:41 +0000632 if (Cond.isInvalid || Body.isInvalid) return true;
633
634 return Actions.ParseDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000635}
636
637/// ParseForStatement
638/// for-statement: [C99 6.8.5.3]
639/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
640/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000641Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000642 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000643 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000644
645 if (Tok.getKind() != tok::l_paren) {
646 Diag(Tok, diag::err_expected_lparen_after, "for");
647 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000648 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000649 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000650
Chris Lattner2dd1b722007-08-26 23:08:06 +0000651 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
652 // the case for C90. Start the loop scope.
653 if (getLang().C99)
654 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
655 else
656 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner9075bd72006-08-10 04:59:57 +0000657
Chris Lattner04132372006-10-16 06:12:55 +0000658 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000659 ExprResult Value;
660
Chris Lattner71e23ce2006-11-04 20:18:38 +0000661 StmtTy *FirstPart = 0;
Chris Lattnercd68f642007-06-27 01:06:29 +0000662 ExprTy *SecondPart = 0;
663 StmtTy *ThirdPart = 0;
Chris Lattner71e23ce2006-11-04 20:18:38 +0000664
Chris Lattner9075bd72006-08-10 04:59:57 +0000665 // Parse the first part of the for specifier.
666 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000667 // no first part, eat the ';'.
668 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000669 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000670 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000671 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
672 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000673 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
674 StmtResult stmtResult = Actions.ParseDeclStmt(aBlockVarDecl);
675 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Chris Lattner9075bd72006-08-10 04:59:57 +0000676 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000677 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000678
Chris Lattnercd68f642007-06-27 01:06:29 +0000679 // Turn the expression into a stmt.
680 if (!Value.isInvalid) {
681 StmtResult R = Actions.ParseExprStmt(Value.Val);
682 if (!R.isInvalid)
683 FirstPart = R.Val;
684 }
Steve Naroff9992bba2007-05-30 16:27:15 +0000685
Chris Lattner53361ac2006-08-10 05:19:57 +0000686 if (Tok.getKind() == tok::semi) {
687 ConsumeToken();
688 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000689 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000690 SkipUntil(tok::semi);
691 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000692 }
693
694 // Parse the second part of the for specifier.
695 if (Tok.getKind() == tok::semi) { // for (...;;
696 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000697 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000698 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000699 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000700 if (!Value.isInvalid)
701 SecondPart = Value.Val;
Chris Lattner9075bd72006-08-10 04:59:57 +0000702 }
703
704 if (Tok.getKind() == tok::semi) {
705 ConsumeToken();
706 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000707 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000708 SkipUntil(tok::semi);
709 }
710
711 // Parse the third part of the for specifier.
712 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
713 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000714 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000715 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000716 Value = ParseExpression();
Chris Lattnercd68f642007-06-27 01:06:29 +0000717 if (!Value.isInvalid) {
718 // Turn the expression into a stmt.
719 StmtResult R = Actions.ParseExprStmt(Value.Val);
720 if (!R.isInvalid)
721 ThirdPart = R.Val;
722 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000723 }
724
Chris Lattner4564bc12006-08-10 23:14:52 +0000725 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000726 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000727
Chris Lattner8fb26252007-08-22 05:28:50 +0000728 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000729 // there is no compound stmt. C90 does not have this clause. We only do this
730 // if the body isn't a compound statement to avoid push/pop in common cases.
731 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000732 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000733
Chris Lattner9075bd72006-08-10 04:59:57 +0000734 // Read the body statement.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000735 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000736
Chris Lattner8fb26252007-08-22 05:28:50 +0000737 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000738 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000739
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000740 // Leave the for-scope.
741 ExitScope();
742
Chris Lattner71e23ce2006-11-04 20:18:38 +0000743 if (Body.isInvalid)
744 return Body;
Chris Lattner30f910e2006-10-16 05:52:41 +0000745
Chris Lattner71e23ce2006-11-04 20:18:38 +0000746 return Actions.ParseForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
747 ThirdPart, RParenLoc, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000748}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000749
Chris Lattner503fadc2006-08-10 05:45:44 +0000750/// ParseGotoStatement
751/// jump-statement:
752/// 'goto' identifier ';'
753/// [GNU] 'goto' '*' expression ';'
754///
755/// Note: this lets the caller parse the end ';'.
756///
Chris Lattner30f910e2006-10-16 05:52:41 +0000757Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner503fadc2006-08-10 05:45:44 +0000758 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000759 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000760
Chris Lattner30f910e2006-10-16 05:52:41 +0000761 StmtResult Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000762 if (Tok.getKind() == tok::identifier) {
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000763 Res = Actions.ParseGotoStmt(GotoLoc, Tok.getLocation(),
764 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000765 ConsumeToken();
766 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
767 // GNU indirect goto extension.
768 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000769 SourceLocation StarLoc = ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000770 ExprResult R = ParseExpression();
Chris Lattner30f910e2006-10-16 05:52:41 +0000771 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000772 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000773 return true;
774 }
775 Res = Actions.ParseIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattnere34b2c22007-07-22 04:13:33 +0000776 } else {
777 Diag(Tok, diag::err_expected_ident);
778 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000779 }
Chris Lattnere34b2c22007-07-22 04:13:33 +0000780
Chris Lattner30f910e2006-10-16 05:52:41 +0000781 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000782}
783
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000784/// ParseContinueStatement
785/// jump-statement:
786/// 'continue' ';'
787///
788/// Note: this lets the caller parse the end ';'.
789///
790Parser::StmtResult Parser::ParseContinueStatement() {
791 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Chris Lattnereaafe1222006-11-10 05:17:58 +0000792 return Actions.ParseContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000793}
794
795/// ParseBreakStatement
796/// jump-statement:
797/// 'break' ';'
798///
799/// Note: this lets the caller parse the end ';'.
800///
801Parser::StmtResult Parser::ParseBreakStatement() {
802 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Chris Lattnereaafe1222006-11-10 05:17:58 +0000803 return Actions.ParseBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000804}
805
Chris Lattner503fadc2006-08-10 05:45:44 +0000806/// ParseReturnStatement
807/// jump-statement:
808/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +0000809Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner503fadc2006-08-10 05:45:44 +0000810 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000811 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000812
Chris Lattner30f910e2006-10-16 05:52:41 +0000813 ExprResult R(0);
Chris Lattnera0927ce2006-08-12 16:59:03 +0000814 if (Tok.getKind() != tok::semi) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000815 R = ParseExpression();
816 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000817 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000818 return true;
819 }
Chris Lattnera0927ce2006-08-12 16:59:03 +0000820 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000821 return Actions.ParseReturnStmt(ReturnLoc, R.Val);
Chris Lattner503fadc2006-08-10 05:45:44 +0000822}
Chris Lattner0116c472006-08-15 06:03:28 +0000823
824/// ParseAsmStatement - Parse a GNU extended asm statement.
825/// [GNU] asm-statement:
826/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
827///
828/// [GNU] asm-argument:
829/// asm-string-literal
830/// asm-string-literal ':' asm-operands[opt]
831/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
832/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
833/// ':' asm-clobbers
834///
835/// [GNU] asm-clobbers:
836/// asm-string-literal
837/// asm-clobbers ',' asm-string-literal
838///
Chris Lattner30f910e2006-10-16 05:52:41 +0000839Parser::StmtResult Parser::ParseAsmStatement() {
Chris Lattner0116c472006-08-15 06:03:28 +0000840 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
841 ConsumeToken();
842
843 DeclSpec DS;
844 SourceLocation Loc = Tok.getLocation();
845 ParseTypeQualifierListOpt(DS);
846
847 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +0000848 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner0116c472006-08-15 06:03:28 +0000849 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
Chris Lattnera925dc62006-11-28 04:33:46 +0000850 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner0116c472006-08-15 06:03:28 +0000851 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
852
853 // Remember if this was a volatile asm.
Chris Lattner1f496802006-10-18 04:02:28 +0000854 //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
Chris Lattner0116c472006-08-15 06:03:28 +0000855
856 if (Tok.getKind() != tok::l_paren) {
857 Diag(Tok, diag::err_expected_lparen_after, "asm");
858 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +0000859 return true;
Chris Lattner0116c472006-08-15 06:03:28 +0000860 }
Chris Lattner04132372006-10-16 06:12:55 +0000861 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +0000862
863 ParseAsmStringLiteral();
864
865 // Parse Outputs, if present.
866 ParseAsmOperandsOpt();
867
868 // Parse Inputs, if present.
869 ParseAsmOperandsOpt();
870
871 // Parse the clobbers, if present.
872 if (Tok.getKind() == tok::colon) {
873 ConsumeToken();
874
Chris Lattnerd3e98952006-10-06 05:22:26 +0000875 if (isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000876 // Parse the asm-string list for clobbers.
877 while (1) {
878 ParseAsmStringLiteral();
879
880 if (Tok.getKind() != tok::comma) break;
881 ConsumeToken();
882 }
883 }
884 }
885
886 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000887
888 // FIXME: Implement action for asm parsing.
889 return false;
Chris Lattner0116c472006-08-15 06:03:28 +0000890}
891
892/// ParseAsmOperands - Parse the asm-operands production as used by
893/// asm-statement. We also parse a leading ':' token. If the leading colon is
894/// not present, we do not parse anything.
895///
896/// [GNU] asm-operands:
897/// asm-operand
898/// asm-operands ',' asm-operand
899///
900/// [GNU] asm-operand:
901/// asm-string-literal '(' expression ')'
902/// '[' identifier ']' asm-string-literal '(' expression ')'
903///
904void Parser::ParseAsmOperandsOpt() {
905 // Only do anything if this operand is present.
906 if (Tok.getKind() != tok::colon) return;
907 ConsumeToken();
908
909 // 'asm-operands' isn't present?
Chris Lattnerd3e98952006-10-06 05:22:26 +0000910 if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square)
Chris Lattner0116c472006-08-15 06:03:28 +0000911 return;
912
913 while (1) {
914 // Read the [id] if present.
915 if (Tok.getKind() == tok::l_square) {
Chris Lattner04132372006-10-16 06:12:55 +0000916 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +0000917
918 if (Tok.getKind() != tok::identifier) {
919 Diag(Tok, diag::err_expected_ident);
920 SkipUntil(tok::r_paren);
921 return;
922 }
923 MatchRHSPunctuation(tok::r_square, Loc);
924 }
925
926 ParseAsmStringLiteral();
927
928 if (Tok.getKind() != tok::l_paren) {
929 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
930 SkipUntil(tok::r_paren);
931 return;
932 }
933
934 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000935 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +0000936 if (Res.isInvalid) {
937 SkipUntil(tok::r_paren);
938 return;
939 }
940
941 // Eat the comma and continue parsing if it exists.
942 if (Tok.getKind() != tok::comma) return;
943 ConsumeToken();
944 }
945}