blob: 95008cdc8034c0df9249ccc9ca8401eb5d030738 [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()) {
Steve Naroff2a8ad182007-05-29 22:59:26 +000087 return Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerf8afb622006-08-10 18:26:31 +000088 } else if (Tok.getKind() == tok::r_brace) {
89 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +000090 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +000091 } else {
92 // expression[opt] ';'
Chris Lattner89c50c62006-08-11 06:41:18 +000093 ExprResult Res = ParseExpression();
94 if (Res.isInvalid) {
95 // If the expression is invalid, skip ahead to the next semicolon. Not
96 // doing this opens us up to the possibility of infinite loops if
97 // ParseExpression does not consume any tokens.
98 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +000099 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000100 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000101 // Otherwise, eat the semicolon.
102 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Chris Lattnercd68f642007-06-27 01:06:29 +0000103 return Actions.ParseExprStmt(Res.Val);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000104 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000105
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000106 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000107 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000108 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000109 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000110
111 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000112 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000113 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
114 return Actions.ParseNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000115
Chris Lattner9075bd72006-08-10 04:59:57 +0000116 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000117 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000118 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000119 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000120
Chris Lattner9075bd72006-08-10 04:59:57 +0000121 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000122 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000123 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000124 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000125 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000126 break;
127 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000128 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000129
130 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000131 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000132 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000133 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000134 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000135 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000136 SemiError = "continue statement";
137 break;
138 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000139 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000140 SemiError = "break statement";
141 break;
142 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000143 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000144 SemiError = "return statement";
145 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000146
147 case tok::kw_asm:
Chris Lattner30f910e2006-10-16 05:52:41 +0000148 Res = ParseAsmStatement();
Chris Lattner0116c472006-08-15 06:03:28 +0000149 SemiError = "asm statement";
150 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000151 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000152
153 // If we reached this code, the statement must end in a semicolon.
154 if (Tok.getKind() == tok::semi) {
155 ConsumeToken();
156 } else {
157 Diag(Tok, diag::err_expected_semi_after, SemiError);
158 SkipUntil(tok::semi);
159 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000160 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000161}
162
Chris Lattnerf8afb622006-08-10 18:26:31 +0000163/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
164/// have a bit of a quandry here. Reading the identifier is necessary to see if
165/// there is a ':' after it. If there is, this is a label, regardless of what
166/// else the identifier can mean. If not, this is either part of a declaration
167/// (if the identifier is a type-name) or part of an expression.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000168///
169/// labeled-statement:
170/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000171/// [GNU] identifier ':' attributes[opt] statement
Chris Lattner6dfd9782006-08-10 18:31:37 +0000172/// declaration (if !OnlyStatement)
173/// expression[opt] ';'
174///
Chris Lattner30f910e2006-10-16 05:52:41 +0000175Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner0663d2a2006-11-05 18:39:59 +0000176 assert(Tok.getKind() == tok::identifier && Tok.getIdentifierInfo() &&
177 "Not an identifier!");
Chris Lattner6dfd9782006-08-10 18:31:37 +0000178
Chris Lattner146762e2007-07-20 16:59:19 +0000179 Token IdentTok = Tok; // Save the whole token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000180 ConsumeToken(); // eat the identifier.
181
182 // identifier ':' statement
183 if (Tok.getKind() == tok::colon) {
Chris Lattneraf635312006-10-16 06:06:51 +0000184 SourceLocation ColonLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000185
186 // Read label attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000188 if (Tok.getKind() == tok::kw___attribute)
Chris Lattner30f910e2006-10-16 05:52:41 +0000189 // TODO: save these somewhere.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +0000191
Chris Lattner30f910e2006-10-16 05:52:41 +0000192 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000193
194 // Broken substmt shouldn't prevent the label from being added to the AST.
195 if (SubStmt.isInvalid)
196 SubStmt = Actions.ParseNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000197
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000198 return Actions.ParseLabelStmt(IdentTok.getLocation(),
199 IdentTok.getIdentifierInfo(),
200 ColonLoc, SubStmt.Val);
Chris Lattner6dfd9782006-08-10 18:31:37 +0000201 }
202
Chris Lattner30f910e2006-10-16 05:52:41 +0000203 // Check to see if this is a declaration.
Chris Lattner49252eb2007-01-27 19:04:39 +0000204 void *TypeRep;
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000205 if (!OnlyStatement &&
Chris Lattner49252eb2007-01-27 19:04:39 +0000206 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000207 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000208 DeclSpec DS;
209
Chris Lattner49252eb2007-01-27 19:04:39 +0000210 // Add the typedef name to the start of the decl-specs.
211 const char *PrevSpec = 0;
212 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
213 IdentTok.getLocation(), PrevSpec,
214 TypeRep);
215 assert(!isInvalid && "First declspec can't be invalid!");
Chris Lattner0663d2a2006-11-05 18:39:59 +0000216
Chris Lattner2f9980e2006-08-10 18:39:24 +0000217 // ParseDeclarationSpecifiers will continue from there.
218 ParseDeclarationSpecifiers(DS);
219
Chris Lattner0e894622006-08-13 19:58:17 +0000220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
222 if (Tok.getKind() == tok::semi) {
223 // TODO: emit error on 'int;' or 'const enum foo;'.
224 // if (!DS.isMissingDeclaratorOk()) Diag(...);
225
226 ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000227 // FIXME: Return this as a type decl.
228 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000229 }
230
Chris Lattner2f9980e2006-08-10 18:39:24 +0000231 // Parse all the declarators.
232 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
233 ParseDeclarator(DeclaratorInfo);
234
Chris Lattner436806a2007-07-10 05:03:31 +0000235 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
236 return Decl ? Actions.ParseDeclStmt(Decl) : 0;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000237 }
238
Chris Lattner0c6c0342006-08-12 18:12:45 +0000239 // Otherwise, this is an expression. Seed it with II and parse it.
240 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
Chris Lattner30f910e2006-10-16 05:52:41 +0000241 if (Res.isInvalid) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000242 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000243 return true;
244 } else if (Tok.getKind() != tok::semi) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000245 Diag(Tok, diag::err_expected_semi_after, "expression");
246 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000247 return true;
248 } else {
249 ConsumeToken();
Chris Lattnercd68f642007-06-27 01:06:29 +0000250 // Convert expr to a stmt.
251 return Actions.ParseExprStmt(Res.Val);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000252 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000253}
254
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000255/// ParseCaseStatement
256/// labeled-statement:
257/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000258/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000259///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000260/// Note that this does not parse the 'statement' at the end.
261///
Chris Lattner30f910e2006-10-16 05:52:41 +0000262Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000263 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000264 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000265
Chris Lattner30f910e2006-10-16 05:52:41 +0000266 ExprResult LHS = ParseConstantExpression();
267 if (LHS.isInvalid) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000268 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000269 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000270 }
271
272 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000273 SourceLocation DotDotDotLoc;
274 ExprTy *RHSVal = 0;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000275 if (Tok.getKind() == tok::ellipsis) {
276 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000277 DotDotDotLoc = ConsumeToken();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000278
279 ExprResult RHS = ParseConstantExpression();
280 if (RHS.isInvalid) {
281 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000282 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000283 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000284 RHSVal = RHS.Val;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000285 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000286
Chris Lattner30f910e2006-10-16 05:52:41 +0000287 if (Tok.getKind() != tok::colon) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000288 Diag(Tok, diag::err_expected_colon_after, "'case'");
289 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000290 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000291 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000292
Chris Lattneraf635312006-10-16 06:06:51 +0000293 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000294
295 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
296 if (Tok.getKind() == tok::r_brace) {
297 Diag(Tok, diag::err_label_end_of_compound_statement);
298 return true;
299 }
300
301 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000302
303 // Broken substmt shouldn't prevent the case from being added to the AST.
Chris Lattner30f910e2006-10-16 05:52:41 +0000304 if (SubStmt.isInvalid)
Chris Lattnerac4471c2007-05-28 05:38:24 +0000305 SubStmt = Actions.ParseNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000306
Chris Lattner30f910e2006-10-16 05:52:41 +0000307 return Actions.ParseCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
308 SubStmt.Val);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000309}
310
311/// ParseDefaultStatement
312/// labeled-statement:
313/// 'default' ':' statement
314/// Note that this does not parse the 'statement' at the end.
315///
Chris Lattner30f910e2006-10-16 05:52:41 +0000316Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000317 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000318 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000319
Chris Lattner30f910e2006-10-16 05:52:41 +0000320 if (Tok.getKind() != tok::colon) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000321 Diag(Tok, diag::err_expected_colon_after, "'default'");
322 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000323 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000324 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000325
Chris Lattneraf635312006-10-16 06:06:51 +0000326 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000327
328 // Diagnose the common error "switch (X) {... default: }", which is not valid.
329 if (Tok.getKind() == tok::r_brace) {
330 Diag(Tok, diag::err_label_end_of_compound_statement);
331 return true;
332 }
333
334 StmtResult SubStmt = ParseStatement();
335 if (SubStmt.isInvalid)
336 return true;
337
Chris Lattner46eeb222007-07-18 02:28:47 +0000338 return Actions.ParseDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000339}
340
341
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000342/// ParseCompoundStatement - Parse a "{}" block.
343///
344/// compound-statement: [C99 6.8.2]
345/// { block-item-list[opt] }
346/// [GNU] { label-declarations block-item-list } [TODO]
347///
348/// block-item-list:
349/// block-item
350/// block-item-list block-item
351///
352/// block-item:
353/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000354/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000355/// statement
356/// [OMP] openmp-directive [TODO]
357///
358/// [GNU] label-declarations:
359/// [GNU] label-declaration
360/// [GNU] label-declarations label-declaration
361///
362/// [GNU] label-declaration:
363/// [GNU] '__label__' identifier-list ';'
364///
365/// [OMP] openmp-directive: [TODO]
366/// [OMP] barrier-directive
367/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000368///
Chris Lattnercac27a52007-08-31 21:49:55 +0000369Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000370 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000371
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000372 // Enter a scope to hold everything within the compound stmt. Compound
373 // statements can always hold declarations.
374 EnterScope(Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000375
376 // Parse the statements in the body.
Chris Lattnercac27a52007-08-31 21:49:55 +0000377 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000378
379 ExitScope();
380 return Body;
381}
382
383
384/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
385/// ParseCompoundStmt action. This expects the '{' to be the current token, and
386/// consume the '}' at the end of the block. It does not manipulate the scope
387/// stack.
Chris Lattnercac27a52007-08-31 21:49:55 +0000388Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerf2978802007-01-21 06:52:16 +0000389 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
390
Chris Lattner010015a2007-05-28 07:23:54 +0000391 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000392 // only allowed at the start of a compound stmt regardless of the language.
Chris Lattner010015a2007-05-28 07:23:54 +0000393
Chris Lattner23b7eb62007-06-15 23:05:46 +0000394 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattner30f910e2006-10-16 05:52:41 +0000395 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000396 StmtResult R;
397 if (Tok.getKind() != tok::kw___extension__) {
398 R = ParseStatementOrDeclaration(false);
399 } else {
400 // __extension__ can start declarations and it can also be a unary
401 // operator for expressions. Consume multiple __extension__ markers here
402 // until we can determine which is which.
403 SourceLocation ExtLoc = ConsumeToken();
404 while (Tok.getKind() == tok::kw___extension__)
405 ConsumeToken();
406
407 // If this is the start of a declaration, parse it as such.
408 if (isDeclarationSpecifier()) {
409 // FIXME: Save the __extension__ on the decl as a node somehow.
410 // FIXME: disable extwarns.
411 R = Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext));
412 } else {
413 // Otherwise this was a unary __extension__ marker. Parse the
414 // subexpression and add the __extension__ unary op.
415 // FIXME: disable extwarns.
416 ExprResult Res = ParseCastExpression(false);
417 if (Res.isInvalid) {
418 SkipUntil(tok::semi);
419 continue;
420 }
421
422 // Add the __extension__ node to the AST.
Steve Naroff83895f72007-09-16 03:34:24 +0000423 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000424 if (Res.isInvalid)
425 continue;
426
427 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
428 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
429 R = Actions.ParseExprStmt(Res.Val);
430 }
431 }
432
Chris Lattner30f910e2006-10-16 05:52:41 +0000433 if (!R.isInvalid && R.Val)
434 Stmts.push_back(R.Val);
435 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000436
437 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner30f910e2006-10-16 05:52:41 +0000438 if (Tok.getKind() != tok::r_brace) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000439 Diag(Tok, diag::err_expected_rbrace);
Chris Lattner30f910e2006-10-16 05:52:41 +0000440 return 0;
441 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000442
Chris Lattner04132372006-10-16 06:12:55 +0000443 SourceLocation RBraceLoc = ConsumeBrace();
Chris Lattner30f910e2006-10-16 05:52:41 +0000444 return Actions.ParseCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattnercac27a52007-08-31 21:49:55 +0000445 &Stmts[0], Stmts.size(), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000446}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000447
448/// ParseIfStatement
449/// if-statement: [C99 6.8.4.1]
450/// 'if' '(' expression ')' statement
451/// 'if' '(' expression ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000452///
453Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000454 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000455 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000456
457 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000458 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000459 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000460 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000461 }
462
Chris Lattner2dd1b722007-08-26 23:08:06 +0000463 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
464 // the case for C90.
465 if (getLang().C99)
466 EnterScope(Scope::DeclScope);
467
Chris Lattnerc951dae2006-08-10 04:23:57 +0000468 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000469 ExprResult CondExp = ParseSimpleParenExpression();
470 if (CondExp.isInvalid) {
471 SkipUntil(tok::semi);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000472 if (getLang().C99)
473 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000474 return true;
475 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000476
Chris Lattner8fb26252007-08-22 05:28:50 +0000477 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000478 // there is no compound stmt. C90 does not have this clause. We only do this
479 // if the body isn't a compound statement to avoid push/pop in common cases.
480 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000481 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000482
Chris Lattnerc951dae2006-08-10 04:23:57 +0000483 // Read the if condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000484 StmtResult CondStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000485
486 // Broken substmt shouldn't prevent the label from being added to the AST.
487 if (CondStmt.isInvalid)
488 CondStmt = Actions.ParseNullStmt(Tok.getLocation());
489
Chris Lattner37e54f42007-08-22 05:16:28 +0000490 // Pop the 'if' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000491 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000492
493 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000494 SourceLocation ElseLoc;
495 StmtResult ElseStmt(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000496 if (Tok.getKind() == tok::kw_else) {
Chris Lattneraf635312006-10-16 06:06:51 +0000497 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000498
Chris Lattner8fb26252007-08-22 05:28:50 +0000499 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000500 // there is no compound stmt. C90 does not have this clause. We only do
501 // this if the body isn't a compound statement to avoid push/pop in common
502 // cases.
503 NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000504 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000505
Chris Lattner30f910e2006-10-16 05:52:41 +0000506 ElseStmt = ParseStatement();
Chris Lattner37e54f42007-08-22 05:16:28 +0000507
508 // Pop the 'else' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000509 if (NeedsInnerScope) ExitScope();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000510
511 if (ElseStmt.isInvalid)
512 ElseStmt = Actions.ParseNullStmt(ElseLoc);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000513 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000514
Chris Lattner2dd1b722007-08-26 23:08:06 +0000515 if (getLang().C99)
516 ExitScope();
517
Chris Lattner30f910e2006-10-16 05:52:41 +0000518 return Actions.ParseIfStmt(IfLoc, CondExp.Val, CondStmt.Val,
519 ElseLoc, ElseStmt.Val);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000520}
521
Chris Lattner9075bd72006-08-10 04:59:57 +0000522/// ParseSwitchStatement
523/// switch-statement:
524/// 'switch' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000525Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000526 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000527 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000528
529 if (Tok.getKind() != tok::l_paren) {
530 Diag(Tok, diag::err_expected_lparen_after, "switch");
531 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000532 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000533 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000534
535 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
536 // not the case for C90. Start the switch scope.
537 if (getLang().C99)
538 EnterScope(Scope::BreakScope|Scope::DeclScope);
539 else
540 EnterScope(Scope::BreakScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000541
Chris Lattner9075bd72006-08-10 04:59:57 +0000542 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000543 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000544
Anders Carlsson51873c22007-07-22 07:07:56 +0000545 if (Cond.isInvalid) {
546 ExitScope();
547 return true;
548 }
549
550 StmtResult Switch = Actions.StartSwitchStmt(Cond.Val);
551
Chris Lattner8fb26252007-08-22 05:28:50 +0000552 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000553 // there is no compound stmt. C90 does not have this clause. We only do this
554 // if the body isn't a compound statement to avoid push/pop in common cases.
555 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000556 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000557
Chris Lattner9075bd72006-08-10 04:59:57 +0000558 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000559 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000560
Chris Lattner8fb26252007-08-22 05:28:50 +0000561 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000562 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000563
Anders Carlsson51873c22007-07-22 07:07:56 +0000564 if (Body.isInvalid) {
565 Body = Actions.ParseNullStmt(Tok.getLocation());
566 // FIXME: Remove the case statement list from the Switch statement.
567 }
568
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000569 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000570
Anders Carlsson51873c22007-07-22 07:07:56 +0000571 return Actions.FinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000572}
573
574/// ParseWhileStatement
575/// while-statement: [C99 6.8.5.1]
576/// 'while' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000577Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000578 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000579 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000580 ConsumeToken(); // eat the 'while'.
581
582 if (Tok.getKind() != tok::l_paren) {
583 Diag(Tok, diag::err_expected_lparen_after, "while");
584 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000585 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000586 }
587
Chris Lattner2dd1b722007-08-26 23:08:06 +0000588 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
589 // the case for C90. Start the loop scope.
590 if (getLang().C99)
591 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
592 else
593 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000594
Chris Lattner9075bd72006-08-10 04:59:57 +0000595 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000596 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000597
Chris Lattner8fb26252007-08-22 05:28:50 +0000598 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000599 // there is no compound stmt. C90 does not have this clause. We only do this
600 // if the body isn't a compound statement to avoid push/pop in common cases.
601 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000602 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000603
Chris Lattner9075bd72006-08-10 04:59:57 +0000604 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000605 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000606
Chris Lattner8fb26252007-08-22 05:28:50 +0000607 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000608 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000609
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000610 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000611
612 if (Cond.isInvalid || Body.isInvalid) return true;
613
614 return Actions.ParseWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000615}
616
617/// ParseDoStatement
618/// do-statement: [C99 6.8.5.2]
619/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000620/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000621Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000622 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000623 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000624
Chris Lattner2dd1b722007-08-26 23:08:06 +0000625 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
626 // the case for C90. Start the loop scope.
627 if (getLang().C99)
628 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
629 else
630 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000631
Chris Lattner8fb26252007-08-22 05:28:50 +0000632 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-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.
635 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000636 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000637
Chris Lattner9075bd72006-08-10 04:59:57 +0000638 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000639 StmtResult Body = ParseStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000640
Chris Lattner8fb26252007-08-22 05:28:50 +0000641 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000642 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000643
Chris Lattner9075bd72006-08-10 04:59:57 +0000644 if (Tok.getKind() != tok::kw_while) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000645 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000646 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000647 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000648 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000649 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000650 }
Chris Lattneraf635312006-10-16 06:06:51 +0000651 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000652
653 if (Tok.getKind() != tok::l_paren) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000654 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000655 Diag(Tok, diag::err_expected_lparen_after, "do/while");
656 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000657 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000658 }
659
660 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000661 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000662
663 ExitScope();
664
Chris Lattner30f910e2006-10-16 05:52:41 +0000665 if (Cond.isInvalid || Body.isInvalid) return true;
666
667 return Actions.ParseDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000668}
669
670/// ParseForStatement
671/// for-statement: [C99 6.8.5.3]
672/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
673/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000674Parser::StmtResult Parser::ParseForStatement() {
Chris Lattner9075bd72006-08-10 04:59:57 +0000675 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000676 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000677
678 if (Tok.getKind() != tok::l_paren) {
679 Diag(Tok, diag::err_expected_lparen_after, "for");
680 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000681 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000682 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000683
Chris Lattner2dd1b722007-08-26 23:08:06 +0000684 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
685 // the case for C90. Start the loop scope.
686 if (getLang().C99)
687 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
688 else
689 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner9075bd72006-08-10 04:59:57 +0000690
Chris Lattner04132372006-10-16 06:12:55 +0000691 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000692 ExprResult Value;
693
Chris Lattner71e23ce2006-11-04 20:18:38 +0000694 StmtTy *FirstPart = 0;
Chris Lattnercd68f642007-06-27 01:06:29 +0000695 ExprTy *SecondPart = 0;
696 StmtTy *ThirdPart = 0;
Chris Lattner71e23ce2006-11-04 20:18:38 +0000697
Chris Lattner9075bd72006-08-10 04:59:57 +0000698 // Parse the first part of the for specifier.
699 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000700 // no first part, eat the ';'.
701 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000702 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000703 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000704 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
705 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000706 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
707 StmtResult stmtResult = Actions.ParseDeclStmt(aBlockVarDecl);
708 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Chris Lattner9075bd72006-08-10 04:59:57 +0000709 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000710 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000711
Chris Lattnercd68f642007-06-27 01:06:29 +0000712 // Turn the expression into a stmt.
713 if (!Value.isInvalid) {
714 StmtResult R = Actions.ParseExprStmt(Value.Val);
715 if (!R.isInvalid)
716 FirstPart = R.Val;
717 }
Steve Naroff9992bba2007-05-30 16:27:15 +0000718
Chris Lattner53361ac2006-08-10 05:19:57 +0000719 if (Tok.getKind() == tok::semi) {
720 ConsumeToken();
721 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000722 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000723 SkipUntil(tok::semi);
724 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000725 }
726
727 // Parse the second part of the for specifier.
728 if (Tok.getKind() == tok::semi) { // for (...;;
729 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000730 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000731 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000732 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000733 if (!Value.isInvalid)
734 SecondPart = Value.Val;
Chris Lattner9075bd72006-08-10 04:59:57 +0000735 }
736
737 if (Tok.getKind() == tok::semi) {
738 ConsumeToken();
739 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000740 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000741 SkipUntil(tok::semi);
742 }
743
744 // Parse the third part of the for specifier.
745 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
746 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000747 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000748 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000749 Value = ParseExpression();
Chris Lattnercd68f642007-06-27 01:06:29 +0000750 if (!Value.isInvalid) {
751 // Turn the expression into a stmt.
752 StmtResult R = Actions.ParseExprStmt(Value.Val);
753 if (!R.isInvalid)
754 ThirdPart = R.Val;
755 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000756 }
757
Chris Lattner4564bc12006-08-10 23:14:52 +0000758 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000759 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000760
Chris Lattner8fb26252007-08-22 05:28:50 +0000761 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000762 // there is no compound stmt. C90 does not have this clause. We only do this
763 // if the body isn't a compound statement to avoid push/pop in common cases.
764 bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace;
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000765 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000766
Chris Lattner9075bd72006-08-10 04:59:57 +0000767 // Read the body statement.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000768 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000769
Chris Lattner8fb26252007-08-22 05:28:50 +0000770 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000771 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000772
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000773 // Leave the for-scope.
774 ExitScope();
775
Chris Lattner71e23ce2006-11-04 20:18:38 +0000776 if (Body.isInvalid)
777 return Body;
Chris Lattner30f910e2006-10-16 05:52:41 +0000778
Chris Lattner71e23ce2006-11-04 20:18:38 +0000779 return Actions.ParseForStmt(ForLoc, LParenLoc, FirstPart, SecondPart,
780 ThirdPart, RParenLoc, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000781}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000782
Chris Lattner503fadc2006-08-10 05:45:44 +0000783/// ParseGotoStatement
784/// jump-statement:
785/// 'goto' identifier ';'
786/// [GNU] 'goto' '*' expression ';'
787///
788/// Note: this lets the caller parse the end ';'.
789///
Chris Lattner30f910e2006-10-16 05:52:41 +0000790Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattner503fadc2006-08-10 05:45:44 +0000791 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000792 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000793
Chris Lattner30f910e2006-10-16 05:52:41 +0000794 StmtResult Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000795 if (Tok.getKind() == tok::identifier) {
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000796 Res = Actions.ParseGotoStmt(GotoLoc, Tok.getLocation(),
797 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000798 ConsumeToken();
799 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
800 // GNU indirect goto extension.
801 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000802 SourceLocation StarLoc = ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000803 ExprResult R = ParseExpression();
Chris Lattner30f910e2006-10-16 05:52:41 +0000804 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000805 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000806 return true;
807 }
808 Res = Actions.ParseIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattnere34b2c22007-07-22 04:13:33 +0000809 } else {
810 Diag(Tok, diag::err_expected_ident);
811 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000812 }
Chris Lattnere34b2c22007-07-22 04:13:33 +0000813
Chris Lattner30f910e2006-10-16 05:52:41 +0000814 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000815}
816
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000817/// ParseContinueStatement
818/// jump-statement:
819/// 'continue' ';'
820///
821/// Note: this lets the caller parse the end ';'.
822///
823Parser::StmtResult Parser::ParseContinueStatement() {
824 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Chris Lattnereaafe1222006-11-10 05:17:58 +0000825 return Actions.ParseContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000826}
827
828/// ParseBreakStatement
829/// jump-statement:
830/// 'break' ';'
831///
832/// Note: this lets the caller parse the end ';'.
833///
834Parser::StmtResult Parser::ParseBreakStatement() {
835 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Chris Lattnereaafe1222006-11-10 05:17:58 +0000836 return Actions.ParseBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000837}
838
Chris Lattner503fadc2006-08-10 05:45:44 +0000839/// ParseReturnStatement
840/// jump-statement:
841/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +0000842Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattner503fadc2006-08-10 05:45:44 +0000843 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000844 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000845
Chris Lattner30f910e2006-10-16 05:52:41 +0000846 ExprResult R(0);
Chris Lattnera0927ce2006-08-12 16:59:03 +0000847 if (Tok.getKind() != tok::semi) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000848 R = ParseExpression();
849 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000850 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000851 return true;
852 }
Chris Lattnera0927ce2006-08-12 16:59:03 +0000853 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000854 return Actions.ParseReturnStmt(ReturnLoc, R.Val);
Chris Lattner503fadc2006-08-10 05:45:44 +0000855}
Chris Lattner0116c472006-08-15 06:03:28 +0000856
857/// ParseAsmStatement - Parse a GNU extended asm statement.
858/// [GNU] asm-statement:
859/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
860///
861/// [GNU] asm-argument:
862/// asm-string-literal
863/// asm-string-literal ':' asm-operands[opt]
864/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
865/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
866/// ':' asm-clobbers
867///
868/// [GNU] asm-clobbers:
869/// asm-string-literal
870/// asm-clobbers ',' asm-string-literal
871///
Chris Lattner30f910e2006-10-16 05:52:41 +0000872Parser::StmtResult Parser::ParseAsmStatement() {
Chris Lattner0116c472006-08-15 06:03:28 +0000873 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
874 ConsumeToken();
875
876 DeclSpec DS;
877 SourceLocation Loc = Tok.getLocation();
878 ParseTypeQualifierListOpt(DS);
879
880 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +0000881 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner0116c472006-08-15 06:03:28 +0000882 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
Chris Lattnera925dc62006-11-28 04:33:46 +0000883 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner0116c472006-08-15 06:03:28 +0000884 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
885
886 // Remember if this was a volatile asm.
Chris Lattner1f496802006-10-18 04:02:28 +0000887 //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
Chris Lattner0116c472006-08-15 06:03:28 +0000888
889 if (Tok.getKind() != tok::l_paren) {
890 Diag(Tok, diag::err_expected_lparen_after, "asm");
891 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +0000892 return true;
Chris Lattner0116c472006-08-15 06:03:28 +0000893 }
Chris Lattner04132372006-10-16 06:12:55 +0000894 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +0000895
896 ParseAsmStringLiteral();
897
898 // Parse Outputs, if present.
899 ParseAsmOperandsOpt();
900
901 // Parse Inputs, if present.
902 ParseAsmOperandsOpt();
903
904 // Parse the clobbers, if present.
905 if (Tok.getKind() == tok::colon) {
906 ConsumeToken();
907
Chris Lattnerd3e98952006-10-06 05:22:26 +0000908 if (isTokenStringLiteral()) {
Chris Lattner0116c472006-08-15 06:03:28 +0000909 // Parse the asm-string list for clobbers.
910 while (1) {
911 ParseAsmStringLiteral();
912
913 if (Tok.getKind() != tok::comma) break;
914 ConsumeToken();
915 }
916 }
917 }
918
919 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000920
921 // FIXME: Implement action for asm parsing.
922 return false;
Chris Lattner0116c472006-08-15 06:03:28 +0000923}
924
925/// ParseAsmOperands - Parse the asm-operands production as used by
926/// asm-statement. We also parse a leading ':' token. If the leading colon is
927/// not present, we do not parse anything.
928///
929/// [GNU] asm-operands:
930/// asm-operand
931/// asm-operands ',' asm-operand
932///
933/// [GNU] asm-operand:
934/// asm-string-literal '(' expression ')'
935/// '[' identifier ']' asm-string-literal '(' expression ')'
936///
937void Parser::ParseAsmOperandsOpt() {
938 // Only do anything if this operand is present.
939 if (Tok.getKind() != tok::colon) return;
940 ConsumeToken();
941
942 // 'asm-operands' isn't present?
Chris Lattnerd3e98952006-10-06 05:22:26 +0000943 if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square)
Chris Lattner0116c472006-08-15 06:03:28 +0000944 return;
945
946 while (1) {
947 // Read the [id] if present.
948 if (Tok.getKind() == tok::l_square) {
Chris Lattner04132372006-10-16 06:12:55 +0000949 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +0000950
951 if (Tok.getKind() != tok::identifier) {
952 Diag(Tok, diag::err_expected_ident);
953 SkipUntil(tok::r_paren);
954 return;
955 }
956 MatchRHSPunctuation(tok::r_square, Loc);
957 }
958
959 ParseAsmStringLiteral();
960
961 if (Tok.getKind() != tok::l_paren) {
962 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
963 SkipUntil(tok::r_paren);
964 return;
965 }
966
967 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000968 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +0000969 if (Res.isInvalid) {
970 SkipUntil(tok::r_paren);
971 return;
972 }
973
974 // Eat the comma and continue parsing if it exists.
975 if (Tok.getKind() != tok::comma) return;
976 ConsumeToken();
977 }
978}