blob: 86e18bfca8513f1a35dec0ab2159b0fafb855597 [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//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner1ff6e732008-10-20 06:51:33 +000016#include "ExtensionRAIIObject.h"
Sebastian Redl511ed552008-11-25 22:21:31 +000017#include "AstGuard.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroff4e79d342008-02-07 23:24:32 +000019#include "clang/Basic/SourceManager.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner33ad2ca2006-11-05 23:47:55 +000021#include "clang/Parse/Scope.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.8: Statements and Blocks.
26//===----------------------------------------------------------------------===//
27
28/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
29/// StatementOrDeclaration:
30/// statement
31/// declaration
32///
33/// statement:
34/// labeled-statement
35/// compound-statement
36/// expression-statement
37/// selection-statement
38/// iteration-statement
39/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000040/// [C++] declaration-statement
Fariborz Jahanian90814572007-10-04 20:19:06 +000041/// [OBC] objc-throw-statement
42/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000043/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000044/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000045/// [OMP] openmp-construct [TODO]
46///
47/// labeled-statement:
48/// identifier ':' statement
49/// 'case' constant-expression ':' statement
50/// 'default' ':' statement
51///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000052/// selection-statement:
53/// if-statement
54/// switch-statement
55///
56/// iteration-statement:
57/// while-statement
58/// do-statement
59/// for-statement
60///
Chris Lattner9075bd72006-08-10 04:59:57 +000061/// expression-statement:
62/// expression[opt] ';'
63///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000064/// jump-statement:
65/// 'goto' identifier ';'
66/// 'continue' ';'
67/// 'break' ';'
68/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000069/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000070///
Fariborz Jahanian90814572007-10-04 20:19:06 +000071/// [OBC] objc-throw-statement:
72/// [OBC] '@' 'throw' expression ';'
73/// [OBC] '@' 'throw' ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000074///
Chris Lattner30f910e2006-10-16 05:52:41 +000075Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000076 const char *SemiError = 0;
Chris Lattner30f910e2006-10-16 05:52:41 +000077 Parser::StmtResult Res;
Chris Lattner503fadc2006-08-10 05:45:44 +000078
79 // Cases in this switch statement should fall through if the parser expects
80 // the token to end in a semicolon (in which case SemiError should be set),
81 // or they directly 'return;' if not.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000082 tok::TokenKind Kind = Tok.getKind();
83 SourceLocation AtLoc;
84 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000085 case tok::at: // May be a @try or @throw statement
86 {
87 AtLoc = ConsumeToken(); // consume @
Steve Naroffe6016792008-02-05 21:27:35 +000088 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000089 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000090
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +000091 case tok::identifier:
92 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
93 // identifier ':' statement
94 return ParseLabeledStatement();
95 }
96 // PASS THROUGH.
97
Chris Lattner0ccd51e2006-08-09 05:47:47 +000098 default:
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +000099 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner2e232092008-03-13 06:29:04 +0000100 SourceLocation DeclStart = Tok.getLocation();
101 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
102 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerb943aa82008-03-13 06:29:54 +0000103 return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000104 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000105 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +0000106 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +0000107 } else {
108 // expression[opt] ';'
Fariborz Jahanian90814572007-10-04 20:19:06 +0000109 ExprResult Res = ParseExpression();
Chris Lattner89c50c62006-08-11 06:41:18 +0000110 if (Res.isInvalid) {
111 // If the expression is invalid, skip ahead to the next semicolon. Not
112 // doing this opens us up to the possibility of infinite loops if
113 // ParseExpression does not consume any tokens.
114 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000115 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000116 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000117 // Otherwise, eat the semicolon.
118 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000119 return Actions.ActOnExprStmt(Res.Val);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000120 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000121
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000122 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000123 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000124 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000125 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000126
127 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000128 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000129 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff66356bd2007-09-16 14:56:35 +0000130 return Actions.ActOnNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000131
Chris Lattner9075bd72006-08-10 04:59:57 +0000132 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000133 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000134 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000135 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000136
Chris Lattner9075bd72006-08-10 04:59:57 +0000137 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000138 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000139 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000140 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000141 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000142 break;
143 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000144 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000145
146 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000147 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000148 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000149 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000150 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000151 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000152 SemiError = "continue statement";
153 break;
154 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000155 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000156 SemiError = "break statement";
157 break;
158 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000159 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000160 SemiError = "return statement";
161 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000162
163 case tok::kw_asm:
Steve Naroffb2c80c72008-02-07 03:50:06 +0000164 bool msAsm = false;
165 Res = ParseAsmStatement(msAsm);
166 if (msAsm) return Res;
Chris Lattner0116c472006-08-15 06:03:28 +0000167 SemiError = "asm statement";
168 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000169 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000170
171 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000172 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000173 ConsumeToken();
Chris Lattner0046de12008-11-13 18:52:53 +0000174 } else if (!Res.isInvalid) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000175 Diag(Tok, diag::err_expected_semi_after) << SemiError;
Chris Lattner0046de12008-11-13 18:52:53 +0000176 // Skip until we see a } or ;, but don't eat it.
177 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000178 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000179 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000180}
181
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000182/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000183///
184/// labeled-statement:
185/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000186/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000187///
188Parser::StmtResult Parser::ParseLabeledStatement() {
189 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
190 "Not an identifier!");
191
192 Token IdentTok = Tok; // Save the whole token.
193 ConsumeToken(); // eat the identifier.
194
195 assert(Tok.is(tok::colon) && "Not a label!");
196
197 // identifier ':' statement
198 SourceLocation ColonLoc = ConsumeToken();
199
200 // Read label attributes, if present.
201 DeclTy *AttrList = 0;
202 if (Tok.is(tok::kw___attribute))
203 // TODO: save these somewhere.
204 AttrList = ParseAttributes();
205
206 StmtResult SubStmt = ParseStatement();
207
208 // Broken substmt shouldn't prevent the label from being added to the AST.
209 if (SubStmt.isInvalid)
210 SubStmt = Actions.ActOnNullStmt(ColonLoc);
211
212 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
213 IdentTok.getIdentifierInfo(),
214 ColonLoc, SubStmt.Val);
215}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000216
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000217/// ParseCaseStatement
218/// labeled-statement:
219/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000220/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000221///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000222/// Note that this does not parse the 'statement' at the end.
223///
Chris Lattner30f910e2006-10-16 05:52:41 +0000224Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000225 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000226 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000227
Chris Lattner30f910e2006-10-16 05:52:41 +0000228 ExprResult LHS = ParseConstantExpression();
229 if (LHS.isInvalid) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000230 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000231 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000232 }
Sebastian Redl511ed552008-11-25 22:21:31 +0000233 ExprGuard LHSGuard(Actions, LHS);
Chris Lattner476c3ad2006-08-13 22:09:58 +0000234
235 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000236 SourceLocation DotDotDotLoc;
237 ExprTy *RHSVal = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000238 if (Tok.is(tok::ellipsis)) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000239 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000240 DotDotDotLoc = ConsumeToken();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000241
242 ExprResult RHS = ParseConstantExpression();
243 if (RHS.isInvalid) {
244 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000245 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000246 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000247 RHSVal = RHS.Val;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000248 }
Sebastian Redl511ed552008-11-25 22:21:31 +0000249 ExprGuard RHSGuard(Actions, RHSVal);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000250
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000251 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000252 Diag(Tok, diag::err_expected_colon_after) << "'case'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000253 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000254 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000255 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000256
Chris Lattneraf635312006-10-16 06:06:51 +0000257 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000258
259 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000260 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000261 Diag(Tok, diag::err_label_end_of_compound_statement);
262 return true;
263 }
264
265 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000266
267 // Broken substmt shouldn't prevent the case from being added to the AST.
Chris Lattner30f910e2006-10-16 05:52:41 +0000268 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000269 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000270
Sebastian Redl511ed552008-11-25 22:21:31 +0000271 return Actions.ActOnCaseStmt(CaseLoc, LHSGuard.take(), DotDotDotLoc,
272 RHSGuard.take(), ColonLoc, SubStmt.Val);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000273}
274
275/// ParseDefaultStatement
276/// labeled-statement:
277/// 'default' ':' statement
278/// Note that this does not parse the 'statement' at the end.
279///
Chris Lattner30f910e2006-10-16 05:52:41 +0000280Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000281 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000282 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000283
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000284 if (Tok.isNot(tok::colon)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000285 Diag(Tok, diag::err_expected_colon_after) << "'default'";
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000286 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000287 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000288 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000289
Chris Lattneraf635312006-10-16 06:06:51 +0000290 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000291
292 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000293 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000294 Diag(Tok, diag::err_label_end_of_compound_statement);
295 return true;
296 }
297
298 StmtResult SubStmt = ParseStatement();
299 if (SubStmt.isInvalid)
300 return true;
301
Steve Naroff66356bd2007-09-16 14:56:35 +0000302 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000303}
304
305
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000306/// ParseCompoundStatement - Parse a "{}" block.
307///
308/// compound-statement: [C99 6.8.2]
309/// { block-item-list[opt] }
310/// [GNU] { label-declarations block-item-list } [TODO]
311///
312/// block-item-list:
313/// block-item
314/// block-item-list block-item
315///
316/// block-item:
317/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000318/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000319/// statement
320/// [OMP] openmp-directive [TODO]
321///
322/// [GNU] label-declarations:
323/// [GNU] label-declaration
324/// [GNU] label-declarations label-declaration
325///
326/// [GNU] label-declaration:
327/// [GNU] '__label__' identifier-list ';'
328///
329/// [OMP] openmp-directive: [TODO]
330/// [OMP] barrier-directive
331/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000332///
Chris Lattnercac27a52007-08-31 21:49:55 +0000333Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000334 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000335
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000336 // Enter a scope to hold everything within the compound stmt. Compound
337 // statements can always hold declarations.
338 EnterScope(Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000339
340 // Parse the statements in the body.
Chris Lattnercac27a52007-08-31 21:49:55 +0000341 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000342
343 ExitScope();
344 return Body;
345}
346
347
348/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000349/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000350/// consume the '}' at the end of the block. It does not manipulate the scope
351/// stack.
Chris Lattnercac27a52007-08-31 21:49:55 +0000352Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerf2978802007-01-21 06:52:16 +0000353 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
354
Chris Lattner010015a2007-05-28 07:23:54 +0000355 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000356 // only allowed at the start of a compound stmt regardless of the language.
Sebastian Redl511ed552008-11-25 22:21:31 +0000357
358 typedef StmtVector StmtsTy;
359 StmtsTy Stmts(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000360 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000361 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000362 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000363 R = ParseStatementOrDeclaration(false);
364 } else {
365 // __extension__ can start declarations and it can also be a unary
366 // operator for expressions. Consume multiple __extension__ markers here
367 // until we can determine which is which.
368 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000369 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000370 ConsumeToken();
371
Chris Lattnerfdc07482008-03-13 06:32:11 +0000372 // __extension__ silences extension warnings in the subexpression.
Chris Lattner1ff6e732008-10-20 06:51:33 +0000373 ExtensionRAIIObject O(Diags); // Use RAII to do this.
374
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000375 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000376 if (isDeclarationStatement()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000377 // FIXME: Save the __extension__ on the decl as a node somehow.
Chris Lattner2e232092008-03-13 06:29:04 +0000378 SourceLocation DeclStart = Tok.getLocation();
379 DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
380 // FIXME: Pass in the right location for the end of the declstmt.
Chris Lattnerb943aa82008-03-13 06:29:54 +0000381 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000382 } else {
383 // Otherwise this was a unary __extension__ marker. Parse the
384 // subexpression and add the __extension__ unary op.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000385 ExprResult Res = ParseCastExpression(false);
Chris Lattnerfdc07482008-03-13 06:32:11 +0000386
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000387 if (Res.isInvalid) {
388 SkipUntil(tok::semi);
389 continue;
390 }
391
392 // Add the __extension__ node to the AST.
Douglas Gregord08452f2008-11-19 15:42:04 +0000393 Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
394 Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000395 if (Res.isInvalid)
396 continue;
397
Chris Lattner1ff6e732008-10-20 06:51:33 +0000398 // Eat the semicolon at the end of stmt and convert the expr into a
399 // statement.
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000400 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000401 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000402 }
403 }
404
Chris Lattner30f910e2006-10-16 05:52:41 +0000405 if (!R.isInvalid && R.Val)
406 Stmts.push_back(R.Val);
407 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000408
409 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000410 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000411 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffe97c4ab2008-01-31 18:29:10 +0000412 return true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000413 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000414
Chris Lattner04132372006-10-16 06:12:55 +0000415 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff66356bd2007-09-16 14:56:35 +0000416 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +0000417 Stmts.take(), Stmts.size(), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000418}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000419
420/// ParseIfStatement
421/// if-statement: [C99 6.8.4.1]
422/// 'if' '(' expression ')' statement
423/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000424/// [C++] 'if' '(' condition ')' statement
425/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000426///
427Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000428 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000429 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000430
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000431 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000432 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000433 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000434 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000435 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000436
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000437 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
438
Chris Lattner2dd1b722007-08-26 23:08:06 +0000439 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
440 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000441 //
442 // C++ 6.4p3:
443 // A name introduced by a declaration in a condition is in scope from its
444 // point of declaration until the end of the substatements controlled by the
445 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000446 // C++ 3.3.2p4:
447 // Names declared in the for-init-statement, and in the condition of if,
448 // while, for, and switch statements are local to the if, while, for, or
449 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000450 //
451 if (C99orCXX)
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000452 EnterScope(Scope::DeclScope | Scope::ControlScope);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000453
Chris Lattnerc951dae2006-08-10 04:23:57 +0000454 // Parse the condition.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000455 ExprResult CondExp;
456 if (getLang().CPlusPlus) {
457 SourceLocation LParenLoc = ConsumeParen();
458 CondExp = ParseCXXCondition();
459 MatchRHSPunctuation(tok::r_paren, LParenLoc);
460 } else {
461 CondExp = ParseSimpleParenExpression();
462 }
Sebastian Redl511ed552008-11-25 22:21:31 +0000463 ExprGuard CondGuard(Actions, CondExp);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000464
Chris Lattner30f910e2006-10-16 05:52:41 +0000465 if (CondExp.isInvalid) {
466 SkipUntil(tok::semi);
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000467 if (C99orCXX)
Chris Lattner2dd1b722007-08-26 23:08:06 +0000468 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000469 return true;
470 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000471
Chris Lattner8fb26252007-08-22 05:28:50 +0000472 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000473 // there is no compound stmt. C90 does not have this clause. We only do this
474 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000475 //
476 // C++ 6.4p1:
477 // The substatement in a selection-statement (each substatement, in the else
478 // form of the if statement) implicitly defines a local scope.
479 //
480 // For C++ we create a scope for the condition and a new scope for
481 // substatements because:
482 // -When the 'then' scope exits, we want the condition declaration to still be
483 // active for the 'else' scope too.
484 // -Sema will detect name clashes by considering declarations of a
485 // 'ControlScope' as part of its direct subscope.
486 // -If we wanted the condition and substatement to be in the same scope, we
487 // would have to notify ParseStatement not to create a new scope. It's
488 // simpler to let it create a new scope.
489 //
490 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000491 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000492
Chris Lattner5c5808a2007-10-29 05:08:52 +0000493 // Read the 'then' stmt.
494 SourceLocation ThenStmtLoc = Tok.getLocation();
495 StmtResult ThenStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000496
Chris Lattner37e54f42007-08-22 05:16:28 +0000497 // Pop the 'if' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000498 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000499
500 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000501 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000502 SourceLocation ElseStmtLoc;
Chris Lattner30f910e2006-10-16 05:52:41 +0000503 StmtResult ElseStmt(false);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000504
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000505 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000506 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000507
Chris Lattner8fb26252007-08-22 05:28:50 +0000508 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000509 // there is no compound stmt. C90 does not have this clause. We only do
510 // this if the body isn't a compound statement to avoid push/pop in common
511 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000512 //
513 // C++ 6.4p1:
514 // The substatement in a selection-statement (each substatement, in the else
515 // form of the if statement) implicitly defines a local scope.
516 //
517 NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000518 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000519
Chris Lattner5c5808a2007-10-29 05:08:52 +0000520 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000521 ElseStmt = ParseStatement();
Chris Lattner37e54f42007-08-22 05:16:28 +0000522
523 // Pop the 'else' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000524 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000525 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000526
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000527 if (C99orCXX)
Chris Lattner2dd1b722007-08-26 23:08:06 +0000528 ExitScope();
529
Chris Lattner5c5808a2007-10-29 05:08:52 +0000530 // If the then or else stmt is invalid and the other is valid (and present),
531 // make turn the invalid one into a null stmt to avoid dropping the other
532 // part. If both are invalid, return error.
533 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
534 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
535 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000536 // Both invalid, or one is invalid and other is non-present: return error.
Chris Lattner5c5808a2007-10-29 05:08:52 +0000537 return true;
538 }
539
540 // Now if either are invalid, replace with a ';'.
541 if (ThenStmt.isInvalid)
542 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
543 if (ElseStmt.isInvalid)
544 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
545
Sebastian Redl511ed552008-11-25 22:21:31 +0000546 return Actions.ActOnIfStmt(IfLoc, CondGuard.take(), ThenStmt.Val,
Chris Lattner30f910e2006-10-16 05:52:41 +0000547 ElseLoc, ElseStmt.Val);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000548}
549
Chris Lattner9075bd72006-08-10 04:59:57 +0000550/// ParseSwitchStatement
551/// switch-statement:
552/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000553/// [C++] 'switch' '(' condition ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000554Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000555 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000556 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000557
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000558 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000559 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +0000560 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000561 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000562 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000563
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000564 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
565
Chris Lattner2dd1b722007-08-26 23:08:06 +0000566 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
567 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000568 //
569 // C++ 6.4p3:
570 // A name introduced by a declaration in a condition is in scope from its
571 // point of declaration until the end of the substatements controlled by the
572 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000573 // C++ 3.3.2p4:
574 // Names declared in the for-init-statement, and in the condition of if,
575 // while, for, and switch statements are local to the if, while, for, or
576 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000577 //
578 if (C99orCXX)
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000579 EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000580 else
581 EnterScope(Scope::BreakScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000582
Chris Lattner9075bd72006-08-10 04:59:57 +0000583 // Parse the condition.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000584 ExprResult Cond;
585 if (getLang().CPlusPlus) {
586 SourceLocation LParenLoc = ConsumeParen();
587 Cond = ParseCXXCondition();
588 MatchRHSPunctuation(tok::r_paren, LParenLoc);
589 } else {
590 Cond = ParseSimpleParenExpression();
591 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000592
Anders Carlsson51873c22007-07-22 07:07:56 +0000593 if (Cond.isInvalid) {
594 ExitScope();
595 return true;
596 }
597
Steve Naroff66356bd2007-09-16 14:56:35 +0000598 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlsson51873c22007-07-22 07:07:56 +0000599
Chris Lattner8fb26252007-08-22 05:28:50 +0000600 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000601 // there is no compound stmt. C90 does not have this clause. We only do this
602 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000603 //
604 // C++ 6.4p1:
605 // The substatement in a selection-statement (each substatement, in the else
606 // form of the if statement) implicitly defines a local scope.
607 //
608 // See comments in ParseIfStatement for why we create a scope for the
609 // condition and a new scope for substatement in C++.
610 //
611 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000612 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000613
Chris Lattner9075bd72006-08-10 04:59:57 +0000614 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000615 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000616
Chris Lattner8fb26252007-08-22 05:28:50 +0000617 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000618 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000619
Anders Carlsson51873c22007-07-22 07:07:56 +0000620 if (Body.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000621 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000622 // FIXME: Remove the case statement list from the Switch statement.
623 }
624
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000625 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000626
Steve Naroff66356bd2007-09-16 14:56:35 +0000627 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000628}
629
630/// ParseWhileStatement
631/// while-statement: [C99 6.8.5.1]
632/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000633/// [C++] 'while' '(' condition ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000634Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000635 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000636 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000637 ConsumeToken(); // eat the 'while'.
638
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000639 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000640 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000641 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000642 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000643 }
644
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000645 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
646
Chris Lattner2dd1b722007-08-26 23:08:06 +0000647 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
648 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000649 //
650 // C++ 6.4p3:
651 // A name introduced by a declaration in a condition is in scope from its
652 // point of declaration until the end of the substatements controlled by the
653 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000654 // C++ 3.3.2p4:
655 // Names declared in the for-init-statement, and in the condition of if,
656 // while, for, and switch statements are local to the if, while, for, or
657 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000658 //
659 if (C99orCXX)
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000660 EnterScope(Scope::BreakScope | Scope::ContinueScope |
661 Scope::DeclScope | Scope::ControlScope);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000662 else
663 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000664
Chris Lattner9075bd72006-08-10 04:59:57 +0000665 // Parse the condition.
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000666 ExprResult Cond;
667 if (getLang().CPlusPlus) {
668 SourceLocation LParenLoc = ConsumeParen();
669 Cond = ParseCXXCondition();
670 MatchRHSPunctuation(tok::r_paren, LParenLoc);
671 } else {
672 Cond = ParseSimpleParenExpression();
673 }
Sebastian Redl511ed552008-11-25 22:21:31 +0000674 ExprGuard CondGuard(Actions, Cond);
Chris Lattner9075bd72006-08-10 04:59:57 +0000675
Chris Lattner8fb26252007-08-22 05:28:50 +0000676 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000677 // there is no compound stmt. C90 does not have this clause. We only do this
678 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000679 //
680 // C++ 6.5p2:
681 // The substatement in an iteration-statement implicitly defines a local scope
682 // which is entered and exited each time through the loop.
683 //
684 // See comments in ParseIfStatement for why we create a scope for the
685 // condition and a new scope for substatement in C++.
686 //
687 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000688 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000689
Chris Lattner9075bd72006-08-10 04:59:57 +0000690 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000691 StmtResult Body = ParseStatement();
Sebastian Redl511ed552008-11-25 22:21:31 +0000692 StmtGuard BodyGuard(Actions, Body);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000693
Chris Lattner8fb26252007-08-22 05:28:50 +0000694 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000695 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000696
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000697 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000698
699 if (Cond.isInvalid || Body.isInvalid) return true;
700
Sebastian Redl511ed552008-11-25 22:21:31 +0000701 return Actions.ActOnWhileStmt(WhileLoc, CondGuard.take(), BodyGuard.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000702}
703
704/// ParseDoStatement
705/// do-statement: [C99 6.8.5.2]
706/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000707/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000708Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000709 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000710 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000711
Chris Lattner2dd1b722007-08-26 23:08:06 +0000712 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
713 // the case for C90. Start the loop scope.
714 if (getLang().C99)
715 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
716 else
717 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000718
Chris Lattner8fb26252007-08-22 05:28:50 +0000719 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000720 // there is no compound stmt. C90 does not have this clause. We only do this
721 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +0000722 //
723 // C++ 6.5p2:
724 // The substatement in an iteration-statement implicitly defines a local scope
725 // which is entered and exited each time through the loop.
726 //
Chris Lattner0046de12008-11-13 18:52:53 +0000727 bool NeedsInnerScope =
728 (getLang().C99 || getLang().CPlusPlus) && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000729 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000730
Chris Lattner9075bd72006-08-10 04:59:57 +0000731 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000732 StmtResult Body = ParseStatement();
Sebastian Redl511ed552008-11-25 22:21:31 +0000733 StmtGuard BodyGuard(Actions, Body);
Chris Lattner9075bd72006-08-10 04:59:57 +0000734
Chris Lattner8fb26252007-08-22 05:28:50 +0000735 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000736 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000737
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000738 if (Tok.isNot(tok::kw_while)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000739 ExitScope();
Chris Lattner0046de12008-11-13 18:52:53 +0000740 if (!Body.isInvalid) {
741 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +0000742 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +0000743 SkipUntil(tok::semi, false, true);
744 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000745 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000746 }
Chris Lattneraf635312006-10-16 06:06:51 +0000747 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000748
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000749 if (Tok.isNot(tok::l_paren)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000750 ExitScope();
Chris Lattner6d29c102008-11-18 07:48:38 +0000751 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +0000752 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000753 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000754 }
755
756 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000757 ExprResult Cond = ParseSimpleParenExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +0000758 ExprGuard CondGuard(Actions, Cond);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000759
760 ExitScope();
761
Chris Lattner30f910e2006-10-16 05:52:41 +0000762 if (Cond.isInvalid || Body.isInvalid) return true;
763
Sebastian Redl511ed552008-11-25 22:21:31 +0000764 return Actions.ActOnDoStmt(DoLoc, BodyGuard.take(),
765 WhileLoc, CondGuard.take());
Chris Lattner9075bd72006-08-10 04:59:57 +0000766}
767
768/// ParseForStatement
769/// for-statement: [C99 6.8.5.3]
770/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
771/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000772/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
773/// [C++] statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000774/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
775/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000776///
777/// [C++] for-init-statement:
778/// [C++] expression-statement
779/// [C++] simple-declaration
780///
Chris Lattner30f910e2006-10-16 05:52:41 +0000781Parser::StmtResult Parser::ParseForStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000782 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000783 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000784
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000785 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000786 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +0000787 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000788 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000789 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000790
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000791 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
792
Chris Lattner2dd1b722007-08-26 23:08:06 +0000793 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
794 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000795 //
796 // C++ 6.4p3:
797 // A name introduced by a declaration in a condition is in scope from its
798 // point of declaration until the end of the substatements controlled by the
799 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000800 // C++ 3.3.2p4:
801 // Names declared in the for-init-statement, and in the condition of if,
802 // while, for, and switch statements are local to the if, while, for, or
803 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000804 // C++ 6.5.3p1:
805 // Names declared in the for-init-statement are in the same declarative-region
806 // as those declared in the condition.
807 //
808 if (C99orCXX)
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000809 EnterScope(Scope::BreakScope | Scope::ContinueScope |
810 Scope::DeclScope | Scope::ControlScope);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000811 else
812 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner9075bd72006-08-10 04:59:57 +0000813
Chris Lattner04132372006-10-16 06:12:55 +0000814 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000815 ExprResult Value;
816
Chris Lattner71e23ce2006-11-04 20:18:38 +0000817 StmtTy *FirstPart = 0;
Chris Lattnercd68f642007-06-27 01:06:29 +0000818 ExprTy *SecondPart = 0;
819 StmtTy *ThirdPart = 0;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000820 bool ForEach = false;
Sebastian Redl511ed552008-11-25 22:21:31 +0000821 StmtGuard FirstGuard(Actions), ThirdGuard(Actions);
822 ExprGuard SecondGuard(Actions);
Chris Lattner71e23ce2006-11-04 20:18:38 +0000823
Chris Lattner9075bd72006-08-10 04:59:57 +0000824 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000825 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000826 // no first part, eat the ';'.
827 ConsumeToken();
Argyrios Kyrtzidisbc28fef2008-10-05 15:50:46 +0000828 } else if (isSimpleDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000829 // Parse declaration, which eats the ';'.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000830 if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +0000831 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner2e232092008-03-13 06:29:04 +0000832
833 SourceLocation DeclStart = Tok.getLocation();
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000834 DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
Chris Lattner2e232092008-03-13 06:29:04 +0000835 // FIXME: Pass in the right location for the end of the declstmt.
836 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
Chris Lattnerb943aa82008-03-13 06:29:54 +0000837 DeclStart);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000838 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000839 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000840 ConsumeToken(); // consume 'in'
841 Value = ParseExpression();
842 if (!Value.isInvalid)
843 SecondPart = Value.Val;
844 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000845 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000846 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000847
Chris Lattnercd68f642007-06-27 01:06:29 +0000848 // Turn the expression into a stmt.
849 if (!Value.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000850 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattnercd68f642007-06-27 01:06:29 +0000851 if (!R.isInvalid)
852 FirstPart = R.Val;
853 }
Steve Naroff9992bba2007-05-30 16:27:15 +0000854
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000855 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000856 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000857 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000858 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000859 ConsumeToken(); // consume 'in'
860 Value = ParseExpression();
861 if (!Value.isInvalid)
862 SecondPart = Value.Val;
863 }
864 else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000865 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000866 SkipUntil(tok::semi);
867 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000868 }
Sebastian Redl511ed552008-11-25 22:21:31 +0000869 FirstGuard.reset(FirstPart);
870 SecondGuard.reset(SecondPart);
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000871 if (!ForEach) {
Sebastian Redl511ed552008-11-25 22:21:31 +0000872 assert(!SecondGuard.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000873 // Parse the second part of the for specifier.
874 if (Tok.is(tok::semi)) { // for (...;;
875 // no second part.
876 Value = ExprResult();
877 } else {
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000878 Value = getLang().CPlusPlus ? ParseCXXCondition()
879 : ParseExpression();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000880 if (!Value.isInvalid)
881 SecondPart = Value.Val;
882 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000883
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000884 if (Tok.is(tok::semi)) {
885 ConsumeToken();
886 } else {
887 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
888 SkipUntil(tok::semi);
889 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000890
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000891 // Parse the third part of the for specifier.
892 if (Tok.is(tok::r_paren)) { // for (...;...;)
893 // no third part.
894 Value = ExprResult();
895 } else {
896 Value = ParseExpression();
897 if (!Value.isInvalid) {
898 // Turn the expression into a stmt.
899 StmtResult R = Actions.ActOnExprStmt(Value.Val);
900 if (!R.isInvalid)
901 ThirdPart = R.Val;
902 }
Chris Lattnercd68f642007-06-27 01:06:29 +0000903 }
Sebastian Redl511ed552008-11-25 22:21:31 +0000904 SecondGuard.reset(SecondPart);
905 ThirdGuard.reset(ThirdPart);
Chris Lattner9075bd72006-08-10 04:59:57 +0000906 }
Chris Lattner4564bc12006-08-10 23:14:52 +0000907 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000908 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000909
Chris Lattner8fb26252007-08-22 05:28:50 +0000910 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000911 // there is no compound stmt. C90 does not have this clause. We only do this
912 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000913 //
914 // C++ 6.5p2:
915 // The substatement in an iteration-statement implicitly defines a local scope
916 // which is entered and exited each time through the loop.
917 //
918 // See comments in ParseIfStatement for why we create a scope for
919 // for-init-statement/condition and a new scope for substatement in C++.
920 //
921 bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000922 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000923
Chris Lattner9075bd72006-08-10 04:59:57 +0000924 // Read the body statement.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000925 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000926
Chris Lattner8fb26252007-08-22 05:28:50 +0000927 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000928 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000929
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000930 // Leave the for-scope.
931 ExitScope();
932
Chris Lattner71e23ce2006-11-04 20:18:38 +0000933 if (Body.isInvalid)
934 return Body;
Chris Lattner30f910e2006-10-16 05:52:41 +0000935
Sebastian Redl511ed552008-11-25 22:21:31 +0000936 // Release all the guards.
937 FirstGuard.take();
938 SecondGuard.take();
939 ThirdGuard.take();
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000940 if (!ForEach)
941 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
942 SecondPart, ThirdPart, RParenLoc, Body.Val);
943 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000944 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000945 SecondPart, RParenLoc, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000946}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000947
Chris Lattner503fadc2006-08-10 05:45:44 +0000948/// ParseGotoStatement
949/// jump-statement:
950/// 'goto' identifier ';'
951/// [GNU] 'goto' '*' expression ';'
952///
953/// Note: this lets the caller parse the end ';'.
954///
Chris Lattner30f910e2006-10-16 05:52:41 +0000955Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000956 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000957 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000958
Chris Lattner30f910e2006-10-16 05:52:41 +0000959 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000960 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000961 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000962 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000963 ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000964 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000965 // GNU indirect goto extension.
966 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000967 SourceLocation StarLoc = ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000968 ExprResult R = ParseExpression();
Chris Lattner30f910e2006-10-16 05:52:41 +0000969 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000970 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000971 return true;
972 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000973 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattnere34b2c22007-07-22 04:13:33 +0000974 } else {
975 Diag(Tok, diag::err_expected_ident);
976 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000977 }
Chris Lattnere34b2c22007-07-22 04:13:33 +0000978
Chris Lattner30f910e2006-10-16 05:52:41 +0000979 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000980}
981
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000982/// ParseContinueStatement
983/// jump-statement:
984/// 'continue' ';'
985///
986/// Note: this lets the caller parse the end ';'.
987///
988Parser::StmtResult Parser::ParseContinueStatement() {
989 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000990 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000991}
992
993/// ParseBreakStatement
994/// jump-statement:
995/// 'break' ';'
996///
997/// Note: this lets the caller parse the end ';'.
998///
999Parser::StmtResult Parser::ParseBreakStatement() {
1000 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff66356bd2007-09-16 14:56:35 +00001001 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001002}
1003
Chris Lattner503fadc2006-08-10 05:45:44 +00001004/// ParseReturnStatement
1005/// jump-statement:
1006/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +00001007Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001008 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001009 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +00001010
Chris Lattner30f910e2006-10-16 05:52:41 +00001011 ExprResult R(0);
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001012 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +00001013 R = ParseExpression();
1014 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001015 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +00001016 return true;
1017 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001018 }
Steve Naroff66356bd2007-09-16 14:56:35 +00001019 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Chris Lattner503fadc2006-08-10 05:45:44 +00001020}
Chris Lattner0116c472006-08-15 06:03:28 +00001021
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001022/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1023/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Steve Naroffb2c80c72008-02-07 03:50:06 +00001024Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
Steve Naroff4e79d342008-02-07 23:24:32 +00001025 if (Tok.is(tok::l_brace)) {
1026 unsigned short savedBraceCount = BraceCount;
1027 do {
1028 ConsumeAnyToken();
1029 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
1030 } else {
1031 // From the MS website: If used without braces, the __asm keyword means
1032 // that the rest of the line is an assembly-language statement.
1033 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroffdb5f7d72008-02-08 03:36:19 +00001034 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff8c099c32008-02-08 18:01:27 +00001035 unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
1036 do {
1037 ConsumeAnyToken();
1038 TokLoc = Tok.getLocation();
1039 } while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
1040 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
1041 Tok.isNot(tok::eof));
Steve Naroff4e79d342008-02-07 23:24:32 +00001042 }
Steve Narofffba39422008-04-07 21:06:54 +00001043 return Actions.ActOnNullStmt(Tok.getLocation());
Steve Naroffb2c80c72008-02-07 03:50:06 +00001044}
1045
Chris Lattner0116c472006-08-15 06:03:28 +00001046/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001047/// asm-statement:
1048/// gnu-asm-statement
1049/// ms-asm-statement
1050///
1051/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001052/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1053///
1054/// [GNU] asm-argument:
1055/// asm-string-literal
1056/// asm-string-literal ':' asm-operands[opt]
1057/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1058/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1059/// ':' asm-clobbers
1060///
1061/// [GNU] asm-clobbers:
1062/// asm-string-literal
1063/// asm-clobbers ',' asm-string-literal
1064///
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001065/// [MS] ms-asm-statement:
1066/// '__asm' assembly-instruction ';'[opt]
1067/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1068///
1069/// [MS] assembly-instruction-list:
1070/// assembly-instruction ';'[opt]
1071/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1072///
Steve Naroffb2c80c72008-02-07 03:50:06 +00001073Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001074 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001075 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner0116c472006-08-15 06:03:28 +00001076
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001077 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001078 msAsm = true;
1079 return FuzzyParseMicrosoftAsmStatement();
1080 }
Chris Lattner0116c472006-08-15 06:03:28 +00001081 DeclSpec DS;
1082 SourceLocation Loc = Tok.getLocation();
1083 ParseTypeQualifierListOpt(DS);
1084
1085 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001086 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001087 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001088 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001089 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Chris Lattner0116c472006-08-15 06:03:28 +00001090
1091 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001092 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001093 bool isSimple = false;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001094 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001095 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001096 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +00001097 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001098 }
Chris Lattner04132372006-10-16 06:12:55 +00001099 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +00001100
Anders Carlsson81a5a692007-11-20 19:21:03 +00001101 ExprResult AsmString = ParseAsmStringLiteral();
1102 if (AsmString.isInvalid)
1103 return true;
Sebastian Redl511ed552008-11-25 22:21:31 +00001104 ExprGuard AsmGuard(Actions, AsmString);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001105
1106 llvm::SmallVector<std::string, 4> Names;
Sebastian Redl511ed552008-11-25 22:21:31 +00001107 ExprVector Constraints(Actions);
1108 ExprVector Exprs(Actions);
1109 ExprVector Clobbers(Actions);
Chris Lattner0116c472006-08-15 06:03:28 +00001110
Anders Carlsson19fe1162008-02-05 23:03:50 +00001111 unsigned NumInputs = 0, NumOutputs = 0;
1112
1113 SourceLocation RParenLoc;
1114 if (Tok.is(tok::r_paren)) {
1115 // We have a simple asm expression
1116 isSimple = true;
1117
1118 RParenLoc = ConsumeParen();
1119 } else {
Sebastian Redl511ed552008-11-25 22:21:31 +00001120 // Parse Outputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001121 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1122 return true;
Anders Carlsson19fe1162008-02-05 23:03:50 +00001123
1124 NumOutputs = Names.size();
1125
1126 // Parse Inputs, if present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001127 if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
1128 return true;
1129
Anders Carlsson19fe1162008-02-05 23:03:50 +00001130 assert(Names.size() == Constraints.size() &&
1131 Constraints.size() == Exprs.size()
1132 && "Input operand size mismatch!");
1133
1134 NumInputs = Names.size() - NumOutputs;
1135
1136 // Parse the clobbers, if present.
1137 if (Tok.is(tok::colon)) {
Anders Carlsson091a0592007-11-21 23:27:34 +00001138 ConsumeToken();
Anders Carlsson19fe1162008-02-05 23:03:50 +00001139
1140 // Parse the asm-string list for clobbers.
1141 while (1) {
1142 ExprResult Clobber = ParseAsmStringLiteral();
1143
1144 if (Clobber.isInvalid)
1145 break;
1146
1147 Clobbers.push_back(Clobber.Val);
1148
1149 if (Tok.isNot(tok::comma)) break;
1150 ConsumeToken();
1151 }
Chris Lattner0116c472006-08-15 06:03:28 +00001152 }
Anders Carlsson19fe1162008-02-05 23:03:50 +00001153
1154 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner0116c472006-08-15 06:03:28 +00001155 }
1156
Anders Carlsson19fe1162008-02-05 23:03:50 +00001157 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1158 NumOutputs, NumInputs,
Sebastian Redl511ed552008-11-25 22:21:31 +00001159 &Names[0], Constraints.take(),
1160 Exprs.take(), AsmGuard.take(),
1161 Clobbers.size(), Clobbers.take(),
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001162 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001163}
1164
1165/// ParseAsmOperands - Parse the asm-operands production as used by
1166/// asm-statement. We also parse a leading ':' token. If the leading colon is
1167/// not present, we do not parse anything.
1168///
1169/// [GNU] asm-operands:
1170/// asm-operand
1171/// asm-operands ',' asm-operand
1172///
1173/// [GNU] asm-operand:
1174/// asm-string-literal '(' expression ')'
1175/// '[' identifier ']' asm-string-literal '(' expression ')'
1176///
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001177bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001178 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1179 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001180 // Only do anything if this operand is present.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001181 if (Tok.isNot(tok::colon)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001182 ConsumeToken();
1183
1184 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001185 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001186 return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001187
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001188 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001189 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001190 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001191 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +00001192
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001193 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001194 Diag(Tok, diag::err_expected_ident);
1195 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001196 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001197 }
Chris Lattner645ff3f2007-10-29 04:06:22 +00001198
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001199 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001200 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001201
1202 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner0116c472006-08-15 06:03:28 +00001203 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001204 } else
1205 Names.push_back(std::string());
Chris Lattner0116c472006-08-15 06:03:28 +00001206
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001207 ExprResult Constraint = ParseAsmStringLiteral();
1208 if (Constraint.isInvalid) {
1209 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001210 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001211 }
1212 Constraints.push_back(Constraint.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001213
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001214 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001215 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001216 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001217 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001218 }
1219
1220 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001221 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +00001222 if (Res.isInvalid) {
1223 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001224 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001225 }
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001226 Exprs.push_back(Res.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001227 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001228 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001229 ConsumeToken();
1230 }
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001231
1232 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001233}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001234
1235Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1236 SourceLocation L, SourceLocation R) {
1237 // Do not enter a scope for the brace, as the arguments are in the same scope
1238 // (the function body) as the body itself. Instead, just read the statement
1239 // list and put it into a CompoundStmt for safe keeping.
1240 StmtResult FnBody = ParseCompoundStatementBody();
1241
1242 // If the function body could not be parsed, make a bogus compoundstmt.
1243 if (FnBody.isInvalid)
1244 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1245
1246 // Leave the function body scope.
1247 ExitScope();
1248
Steve Naroffb313fc32007-11-11 23:20:51 +00001249 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001250}