blob: a074463f3cf59a733a08b36753752aec9bfdb45b [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"
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
Fariborz Jahanian90814572007-10-04 20:19:06 +000037/// [OBC] objc-throw-statement
38/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000039/// [OBC] objc-synchronized-statement
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///
Fariborz Jahanian90814572007-10-04 20:19:06 +000067/// [OBC] objc-throw-statement:
68/// [OBC] '@' 'throw' expression ';'
69/// [OBC] '@' 'throw' ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000070///
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.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000078 tok::TokenKind Kind = Tok.getKind();
79 SourceLocation AtLoc;
80 switch (Kind) {
Chris Lattnerf8afb622006-08-10 18:26:31 +000081 case tok::identifier: // C99 6.8.1: labeled-statement
82 // identifier ':' statement
83 // declaration (if !OnlyStatement)
84 // expression[opt] ';'
85 return ParseIdentifierStatement(OnlyStatement);
86
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000087 case tok::at: // May be a @try or @throw statement
88 {
89 AtLoc = ConsumeToken(); // consume @
Steve Naroffe6016792008-02-05 21:27:35 +000090 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000091 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +000092
Chris Lattner0ccd51e2006-08-09 05:47:47 +000093 default:
Fariborz Jahanian90814572007-10-04 20:19:06 +000094 if (!OnlyStatement && isDeclarationSpecifier()) {
Steve Naroff66356bd2007-09-16 14:56:35 +000095 return Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerfeb00b62007-10-09 17:41:39 +000096 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +000097 Diag(Tok, diag::err_expected_statement);
Chris Lattner30f910e2006-10-16 05:52:41 +000098 return true;
Chris Lattnerf8afb622006-08-10 18:26:31 +000099 } else {
100 // expression[opt] ';'
Fariborz Jahanian90814572007-10-04 20:19:06 +0000101 ExprResult Res = ParseExpression();
Chris Lattner89c50c62006-08-11 06:41:18 +0000102 if (Res.isInvalid) {
103 // If the expression is invalid, skip ahead to the next semicolon. Not
104 // doing this opens us up to the possibility of infinite loops if
105 // ParseExpression does not consume any tokens.
106 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000107 return true;
Chris Lattner89c50c62006-08-11 06:41:18 +0000108 }
Chris Lattner2e550fe2007-06-06 05:26:32 +0000109 // Otherwise, eat the semicolon.
110 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000111 return Actions.ActOnExprStmt(Res.Val);
Chris Lattnerf8afb622006-08-10 18:26:31 +0000112 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000113
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000114 case tok::kw_case: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000115 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000116 case tok::kw_default: // C99 6.8.1: labeled-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000117 return ParseDefaultStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000118
119 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000120 return ParseCompoundStatement();
Chris Lattner0f203a72007-05-28 01:45:28 +0000121 case tok::semi: // C99 6.8.3p3: expression[opt] ';'
Steve Naroff66356bd2007-09-16 14:56:35 +0000122 return Actions.ActOnNullStmt(ConsumeToken());
Chris Lattner503fadc2006-08-10 05:45:44 +0000123
Chris Lattner9075bd72006-08-10 04:59:57 +0000124 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000125 return ParseIfStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000126 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000127 return ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000128
Chris Lattner9075bd72006-08-10 04:59:57 +0000129 case tok::kw_while: // C99 6.8.5.1: while-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000130 return ParseWhileStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000131 case tok::kw_do: // C99 6.8.5.2: do-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000132 Res = ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000133 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000134 break;
135 case tok::kw_for: // C99 6.8.5.3: for-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000136 return ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000137
138 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000139 Res = ParseGotoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000140 SemiError = "goto statement";
Chris Lattner9075bd72006-08-10 04:59:57 +0000141 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000142 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000143 Res = ParseContinueStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000144 SemiError = "continue statement";
145 break;
146 case tok::kw_break: // C99 6.8.6.3: break-statement
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000147 Res = ParseBreakStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000148 SemiError = "break statement";
149 break;
150 case tok::kw_return: // C99 6.8.6.4: return-statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000151 Res = ParseReturnStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000152 SemiError = "return statement";
153 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000154
155 case tok::kw_asm:
Chris Lattner30f910e2006-10-16 05:52:41 +0000156 Res = ParseAsmStatement();
Chris Lattner0116c472006-08-15 06:03:28 +0000157 SemiError = "asm statement";
158 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000159 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000160
161 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000162 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000163 ConsumeToken();
164 } else {
165 Diag(Tok, diag::err_expected_semi_after, SemiError);
166 SkipUntil(tok::semi);
167 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000168 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000169}
170
Chris Lattnerf8afb622006-08-10 18:26:31 +0000171/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
172/// have a bit of a quandry here. Reading the identifier is necessary to see if
173/// there is a ':' after it. If there is, this is a label, regardless of what
174/// else the identifier can mean. If not, this is either part of a declaration
175/// (if the identifier is a type-name) or part of an expression.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000176///
177/// labeled-statement:
178/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000179/// [GNU] identifier ':' attributes[opt] statement
Chris Lattner6dfd9782006-08-10 18:31:37 +0000180/// declaration (if !OnlyStatement)
181/// expression[opt] ';'
182///
Chris Lattner30f910e2006-10-16 05:52:41 +0000183Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000184 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
Chris Lattner0663d2a2006-11-05 18:39:59 +0000185 "Not an identifier!");
Chris Lattner6dfd9782006-08-10 18:31:37 +0000186
Chris Lattner146762e2007-07-20 16:59:19 +0000187 Token IdentTok = Tok; // Save the whole token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000188 ConsumeToken(); // eat the identifier.
189
190 // identifier ':' statement
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000191 if (Tok.is(tok::colon)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000192 SourceLocation ColonLoc = ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000193
194 // Read label attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000195 DeclTy *AttrList = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000196 if (Tok.is(tok::kw___attribute))
Chris Lattner30f910e2006-10-16 05:52:41 +0000197 // TODO: save these somewhere.
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +0000199
Chris Lattner30f910e2006-10-16 05:52:41 +0000200 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000201
202 // Broken substmt shouldn't prevent the label from being added to the AST.
203 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000204 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000205
Steve Naroff66356bd2007-09-16 14:56:35 +0000206 return Actions.ActOnLabelStmt(IdentTok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000207 IdentTok.getIdentifierInfo(),
208 ColonLoc, SubStmt.Val);
Chris Lattner6dfd9782006-08-10 18:31:37 +0000209 }
210
Chris Lattner30f910e2006-10-16 05:52:41 +0000211 // Check to see if this is a declaration.
Chris Lattner49252eb2007-01-27 19:04:39 +0000212 void *TypeRep;
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000213 if (!OnlyStatement &&
Chris Lattner49252eb2007-01-27 19:04:39 +0000214 (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000215 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000216 DeclSpec DS;
217
Chris Lattner49252eb2007-01-27 19:04:39 +0000218 // Add the typedef name to the start of the decl-specs.
219 const char *PrevSpec = 0;
220 int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef,
221 IdentTok.getLocation(), PrevSpec,
222 TypeRep);
223 assert(!isInvalid && "First declspec can't be invalid!");
Steve Naroffc5484042007-10-30 02:23:23 +0000224 SourceLocation endProtoLoc;
Fariborz Jahaniand7971132007-10-11 18:08:47 +0000225 if (Tok.is(tok::less)) {
226 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffc5484042007-10-30 02:23:23 +0000227 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahaniand7971132007-10-11 18:08:47 +0000228 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
229 new llvm::SmallVector<DeclTy *, 8>;
230 DS.setProtocolQualifiers(ProtocolDecl);
231 Actions.FindProtocolDeclaration(IdentTok.getLocation(),
232 &ProtocolRefs[0], ProtocolRefs.size(),
233 *ProtocolDecl);
234 }
Chris Lattner0663d2a2006-11-05 18:39:59 +0000235
Chris Lattner2f9980e2006-08-10 18:39:24 +0000236 // ParseDeclarationSpecifiers will continue from there.
237 ParseDeclarationSpecifiers(DS);
238
Chris Lattner0e894622006-08-13 19:58:17 +0000239 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
240 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000241 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000242 // TODO: emit error on 'int;' or 'const enum foo;'.
243 // if (!DS.isMissingDeclaratorOk()) Diag(...);
244
245 ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000246 // FIXME: Return this as a type decl.
247 return 0;
Chris Lattner0e894622006-08-13 19:58:17 +0000248 }
249
Chris Lattner2f9980e2006-08-10 18:39:24 +0000250 // Parse all the declarators.
251 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
252 ParseDeclarator(DeclaratorInfo);
253
Chris Lattner436806a2007-07-10 05:03:31 +0000254 DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Steve Naroff66356bd2007-09-16 14:56:35 +0000255 return Decl ? Actions.ActOnDeclStmt(Decl) : 0;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000256 }
257
Chris Lattner0c6c0342006-08-12 18:12:45 +0000258 // Otherwise, this is an expression. Seed it with II and parse it.
259 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
Chris Lattner30f910e2006-10-16 05:52:41 +0000260 if (Res.isInvalid) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000261 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000262 return true;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000263 } else if (Tok.isNot(tok::semi)) {
Chris Lattner0c6c0342006-08-12 18:12:45 +0000264 Diag(Tok, diag::err_expected_semi_after, "expression");
265 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000266 return true;
267 } else {
268 ConsumeToken();
Chris Lattnercd68f642007-06-27 01:06:29 +0000269 // Convert expr to a stmt.
Steve Naroff66356bd2007-09-16 14:56:35 +0000270 return Actions.ActOnExprStmt(Res.Val);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000271 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000272}
273
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000274/// ParseCaseStatement
275/// labeled-statement:
276/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000277/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000278///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000279/// Note that this does not parse the 'statement' at the end.
280///
Chris Lattner30f910e2006-10-16 05:52:41 +0000281Parser::StmtResult Parser::ParseCaseStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000282 assert(Tok.is(tok::kw_case) && "Not a case stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000283 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000284
Chris Lattner30f910e2006-10-16 05:52:41 +0000285 ExprResult LHS = ParseConstantExpression();
286 if (LHS.isInvalid) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000287 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000288 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000289 }
290
291 // GNU case range extension.
Chris Lattner30f910e2006-10-16 05:52:41 +0000292 SourceLocation DotDotDotLoc;
293 ExprTy *RHSVal = 0;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000294 if (Tok.is(tok::ellipsis)) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000295 Diag(Tok, diag::ext_gnu_case_range);
Chris Lattneraf635312006-10-16 06:06:51 +0000296 DotDotDotLoc = ConsumeToken();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000297
298 ExprResult RHS = ParseConstantExpression();
299 if (RHS.isInvalid) {
300 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000301 return true;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000302 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000303 RHSVal = RHS.Val;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000304 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000305
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000306 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000307 Diag(Tok, diag::err_expected_colon_after, "'case'");
308 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000309 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000310 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000311
Chris Lattneraf635312006-10-16 06:06:51 +0000312 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000313
314 // Diagnose the common error "switch (X) { case 4: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000315 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000316 Diag(Tok, diag::err_label_end_of_compound_statement);
317 return true;
318 }
319
320 StmtResult SubStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000321
322 // Broken substmt shouldn't prevent the case from being added to the AST.
Chris Lattner30f910e2006-10-16 05:52:41 +0000323 if (SubStmt.isInvalid)
Steve Naroff66356bd2007-09-16 14:56:35 +0000324 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner30f910e2006-10-16 05:52:41 +0000325
Steve Naroff66356bd2007-09-16 14:56:35 +0000326 return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
Chris Lattner30f910e2006-10-16 05:52:41 +0000327 SubStmt.Val);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000328}
329
330/// ParseDefaultStatement
331/// labeled-statement:
332/// 'default' ':' statement
333/// Note that this does not parse the 'statement' at the end.
334///
Chris Lattner30f910e2006-10-16 05:52:41 +0000335Parser::StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000336 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000337 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000338
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000339 if (Tok.isNot(tok::colon)) {
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000340 Diag(Tok, diag::err_expected_colon_after, "'default'");
341 SkipUntil(tok::colon);
Chris Lattner30f910e2006-10-16 05:52:41 +0000342 return true;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000343 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000344
Chris Lattneraf635312006-10-16 06:06:51 +0000345 SourceLocation ColonLoc = ConsumeToken();
Chris Lattner30f910e2006-10-16 05:52:41 +0000346
347 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000348 if (Tok.is(tok::r_brace)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000349 Diag(Tok, diag::err_label_end_of_compound_statement);
350 return true;
351 }
352
353 StmtResult SubStmt = ParseStatement();
354 if (SubStmt.isInvalid)
355 return true;
356
Steve Naroff66356bd2007-09-16 14:56:35 +0000357 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000358}
359
360
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000361/// ParseCompoundStatement - Parse a "{}" block.
362///
363/// compound-statement: [C99 6.8.2]
364/// { block-item-list[opt] }
365/// [GNU] { label-declarations block-item-list } [TODO]
366///
367/// block-item-list:
368/// block-item
369/// block-item-list block-item
370///
371/// block-item:
372/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000373/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000374/// statement
375/// [OMP] openmp-directive [TODO]
376///
377/// [GNU] label-declarations:
378/// [GNU] label-declaration
379/// [GNU] label-declarations label-declaration
380///
381/// [GNU] label-declaration:
382/// [GNU] '__label__' identifier-list ';'
383///
384/// [OMP] openmp-directive: [TODO]
385/// [OMP] barrier-directive
386/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000387///
Chris Lattnercac27a52007-08-31 21:49:55 +0000388Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000389 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000390
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000391 // Enter a scope to hold everything within the compound stmt. Compound
392 // statements can always hold declarations.
393 EnterScope(Scope::DeclScope);
Chris Lattnerf2978802007-01-21 06:52:16 +0000394
395 // Parse the statements in the body.
Chris Lattnercac27a52007-08-31 21:49:55 +0000396 StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000397
398 ExitScope();
399 return Body;
400}
401
402
403/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000404/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000405/// consume the '}' at the end of the block. It does not manipulate the scope
406/// stack.
Chris Lattnercac27a52007-08-31 21:49:55 +0000407Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Chris Lattnerf2978802007-01-21 06:52:16 +0000408 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
409
Chris Lattner010015a2007-05-28 07:23:54 +0000410 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000411 // only allowed at the start of a compound stmt regardless of the language.
Chris Lattner010015a2007-05-28 07:23:54 +0000412
Chris Lattner23b7eb62007-06-15 23:05:46 +0000413 llvm::SmallVector<StmtTy*, 32> Stmts;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000414 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000415 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000416 if (Tok.isNot(tok::kw___extension__)) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000417 R = ParseStatementOrDeclaration(false);
418 } else {
419 // __extension__ can start declarations and it can also be a unary
420 // operator for expressions. Consume multiple __extension__ markers here
421 // until we can determine which is which.
422 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000423 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000424 ConsumeToken();
425
426 // If this is the start of a declaration, parse it as such.
427 if (isDeclarationSpecifier()) {
428 // FIXME: Save the __extension__ on the decl as a node somehow.
429 // FIXME: disable extwarns.
Steve Naroff66356bd2007-09-16 14:56:35 +0000430 R = Actions.ActOnDeclStmt(ParseDeclaration(Declarator::BlockContext));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000431 } else {
432 // Otherwise this was a unary __extension__ marker. Parse the
433 // subexpression and add the __extension__ unary op.
434 // FIXME: disable extwarns.
435 ExprResult Res = ParseCastExpression(false);
436 if (Res.isInvalid) {
437 SkipUntil(tok::semi);
438 continue;
439 }
440
441 // Add the __extension__ node to the AST.
Steve Naroff83895f72007-09-16 03:34:24 +0000442 Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000443 if (Res.isInvalid)
444 continue;
445
446 // Eat the semicolon at the end of stmt and convert the expr into a stmt.
447 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Steve Naroff66356bd2007-09-16 14:56:35 +0000448 R = Actions.ActOnExprStmt(Res.Val);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000449 }
450 }
451
Chris Lattner30f910e2006-10-16 05:52:41 +0000452 if (!R.isInvalid && R.Val)
453 Stmts.push_back(R.Val);
454 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000455
456 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000457 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000458 Diag(Tok, diag::err_expected_rbrace);
Steve Naroffe97c4ab2008-01-31 18:29:10 +0000459 return true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000460 }
Chris Lattnerf2978802007-01-21 06:52:16 +0000461
Chris Lattner04132372006-10-16 06:12:55 +0000462 SourceLocation RBraceLoc = ConsumeBrace();
Steve Naroff66356bd2007-09-16 14:56:35 +0000463 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
Chris Lattnercac27a52007-08-31 21:49:55 +0000464 &Stmts[0], Stmts.size(), isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000465}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000466
467/// ParseIfStatement
468/// if-statement: [C99 6.8.4.1]
469/// 'if' '(' expression ')' statement
470/// 'if' '(' expression ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000471///
472Parser::StmtResult Parser::ParseIfStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000473 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000474 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000475
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000476 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000477 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000478 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000479 return true;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000480 }
481
Chris Lattner2dd1b722007-08-26 23:08:06 +0000482 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
483 // the case for C90.
484 if (getLang().C99)
485 EnterScope(Scope::DeclScope);
486
Chris Lattnerc951dae2006-08-10 04:23:57 +0000487 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000488 ExprResult CondExp = ParseSimpleParenExpression();
489 if (CondExp.isInvalid) {
490 SkipUntil(tok::semi);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000491 if (getLang().C99)
492 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000493 return true;
494 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000495
Chris Lattner8fb26252007-08-22 05:28:50 +0000496 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000497 // there is no compound stmt. C90 does not have this clause. We only do this
498 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000499 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000500 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000501
Chris Lattner5c5808a2007-10-29 05:08:52 +0000502 // Read the 'then' stmt.
503 SourceLocation ThenStmtLoc = Tok.getLocation();
504 StmtResult ThenStmt = ParseStatement();
Chris Lattnerac4471c2007-05-28 05:38:24 +0000505
Chris Lattner37e54f42007-08-22 05:16:28 +0000506 // Pop the 'if' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000507 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000508
509 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000510 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000511 SourceLocation ElseStmtLoc;
Chris Lattner30f910e2006-10-16 05:52:41 +0000512 StmtResult ElseStmt(false);
Chris Lattner5c5808a2007-10-29 05:08:52 +0000513
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000514 if (Tok.is(tok::kw_else)) {
Chris Lattneraf635312006-10-16 06:06:51 +0000515 ElseLoc = ConsumeToken();
Chris Lattner37e54f42007-08-22 05:16:28 +0000516
Chris Lattner8fb26252007-08-22 05:28:50 +0000517 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000518 // there is no compound stmt. C90 does not have this clause. We only do
519 // this if the body isn't a compound statement to avoid push/pop in common
520 // cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000521 NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000522 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner37e54f42007-08-22 05:16:28 +0000523
Chris Lattner5c5808a2007-10-29 05:08:52 +0000524 ElseStmtLoc = Tok.getLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000525 ElseStmt = ParseStatement();
Chris Lattner37e54f42007-08-22 05:16:28 +0000526
527 // Pop the 'else' scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000528 if (NeedsInnerScope) ExitScope();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000529 }
Chris Lattner30f910e2006-10-16 05:52:41 +0000530
Chris Lattner2dd1b722007-08-26 23:08:06 +0000531 if (getLang().C99)
532 ExitScope();
533
Chris Lattner5c5808a2007-10-29 05:08:52 +0000534 // If the then or else stmt is invalid and the other is valid (and present),
535 // make turn the invalid one into a null stmt to avoid dropping the other
536 // part. If both are invalid, return error.
537 if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
538 (ThenStmt.isInvalid && ElseStmt.Val == 0) ||
539 (ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
540 // Both invalid, or one is invalid and other is non-present: delete cond and
541 // return error.
542 Actions.DeleteExpr(CondExp.Val);
543 return true;
544 }
545
546 // Now if either are invalid, replace with a ';'.
547 if (ThenStmt.isInvalid)
548 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
549 if (ElseStmt.isInvalid)
550 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
551
Chris Lattner5c5808a2007-10-29 05:08:52 +0000552 return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
Chris Lattner30f910e2006-10-16 05:52:41 +0000553 ElseLoc, ElseStmt.Val);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000554}
555
Chris Lattner9075bd72006-08-10 04:59:57 +0000556/// ParseSwitchStatement
557/// switch-statement:
558/// 'switch' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000559Parser::StmtResult Parser::ParseSwitchStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000560 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000561 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000562
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000563 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000564 Diag(Tok, diag::err_expected_lparen_after, "switch");
565 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000566 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000567 }
Chris Lattner2dd1b722007-08-26 23:08:06 +0000568
569 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
570 // not the case for C90. Start the switch scope.
571 if (getLang().C99)
572 EnterScope(Scope::BreakScope|Scope::DeclScope);
573 else
574 EnterScope(Scope::BreakScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000575
Chris Lattner9075bd72006-08-10 04:59:57 +0000576 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000577 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000578
Anders Carlsson51873c22007-07-22 07:07:56 +0000579 if (Cond.isInvalid) {
580 ExitScope();
581 return true;
582 }
583
Steve Naroff66356bd2007-09-16 14:56:35 +0000584 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
Anders Carlsson51873c22007-07-22 07:07:56 +0000585
Chris Lattner8fb26252007-08-22 05:28:50 +0000586 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000587 // there is no compound stmt. C90 does not have this clause. We only do this
588 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000589 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000590 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000591
Chris Lattner9075bd72006-08-10 04:59:57 +0000592 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000593 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000594
Chris Lattner8fb26252007-08-22 05:28:50 +0000595 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000596 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000597
Anders Carlsson51873c22007-07-22 07:07:56 +0000598 if (Body.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000599 Body = Actions.ActOnNullStmt(Tok.getLocation());
Anders Carlsson51873c22007-07-22 07:07:56 +0000600 // FIXME: Remove the case statement list from the Switch statement.
601 }
602
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000603 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000604
Steve Naroff66356bd2007-09-16 14:56:35 +0000605 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000606}
607
608/// ParseWhileStatement
609/// while-statement: [C99 6.8.5.1]
610/// 'while' '(' expression ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000611Parser::StmtResult Parser::ParseWhileStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000612 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +0000613 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +0000614 ConsumeToken(); // eat the 'while'.
615
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000616 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000617 Diag(Tok, diag::err_expected_lparen_after, "while");
618 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000619 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000620 }
621
Chris Lattner2dd1b722007-08-26 23:08:06 +0000622 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
623 // the case for C90. Start the loop scope.
624 if (getLang().C99)
625 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
626 else
627 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000628
Chris Lattner9075bd72006-08-10 04:59:57 +0000629 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000630 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +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.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000635 bool NeedsInnerScope = getLang().C99 && Tok.isNot(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 Lattner33ad2ca2006-11-05 23:47:55 +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 Lattner33ad2ca2006-11-05 23:47:55 +0000644 ExitScope();
Chris Lattner30f910e2006-10-16 05:52:41 +0000645
646 if (Cond.isInvalid || Body.isInvalid) return true;
647
Steve Naroff66356bd2007-09-16 14:56:35 +0000648 return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000649}
650
651/// ParseDoStatement
652/// do-statement: [C99 6.8.5.2]
653/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000654/// Note: this lets the caller parse the end ';'.
Chris Lattner30f910e2006-10-16 05:52:41 +0000655Parser::StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000656 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000657 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000658
Chris Lattner2dd1b722007-08-26 23:08:06 +0000659 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
660 // the case for C90. Start the loop scope.
661 if (getLang().C99)
662 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
663 else
664 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000665
Chris Lattner8fb26252007-08-22 05:28:50 +0000666 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000667 // there is no compound stmt. C90 does not have this clause. We only do this
668 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000669 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000670 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000671
Chris Lattner9075bd72006-08-10 04:59:57 +0000672 // Read the body statement.
Chris Lattner30f910e2006-10-16 05:52:41 +0000673 StmtResult Body = ParseStatement();
Chris Lattner9075bd72006-08-10 04:59:57 +0000674
Chris Lattner8fb26252007-08-22 05:28:50 +0000675 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000676 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000677
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000678 if (Tok.isNot(tok::kw_while)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000679 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000680 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000681 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000682 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000683 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000684 }
Chris Lattneraf635312006-10-16 06:06:51 +0000685 SourceLocation WhileLoc = ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000686
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000687 if (Tok.isNot(tok::l_paren)) {
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000688 ExitScope();
Chris Lattner9075bd72006-08-10 04:59:57 +0000689 Diag(Tok, diag::err_expected_lparen_after, "do/while");
690 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000691 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000692 }
693
694 // Parse the condition.
Chris Lattner30f910e2006-10-16 05:52:41 +0000695 ExprResult Cond = ParseSimpleParenExpression();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000696
697 ExitScope();
698
Chris Lattner30f910e2006-10-16 05:52:41 +0000699 if (Cond.isInvalid || Body.isInvalid) return true;
700
Steve Naroff66356bd2007-09-16 14:56:35 +0000701 return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000702}
703
704/// ParseForStatement
705/// for-statement: [C99 6.8.5.3]
706/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
707/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000708/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
709/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000710Parser::StmtResult Parser::ParseForStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000711 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000712 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000713
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000714 if (Tok.isNot(tok::l_paren)) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000715 Diag(Tok, diag::err_expected_lparen_after, "for");
716 SkipUntil(tok::semi);
Chris Lattner30f910e2006-10-16 05:52:41 +0000717 return true;
Chris Lattner9075bd72006-08-10 04:59:57 +0000718 }
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000719
Chris Lattner2dd1b722007-08-26 23:08:06 +0000720 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
721 // the case for C90. Start the loop scope.
722 if (getLang().C99)
723 EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
724 else
725 EnterScope(Scope::BreakScope | Scope::ContinueScope);
Chris Lattner9075bd72006-08-10 04:59:57 +0000726
Chris Lattner04132372006-10-16 06:12:55 +0000727 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000728 ExprResult Value;
729
Chris Lattner71e23ce2006-11-04 20:18:38 +0000730 StmtTy *FirstPart = 0;
Chris Lattnercd68f642007-06-27 01:06:29 +0000731 ExprTy *SecondPart = 0;
732 StmtTy *ThirdPart = 0;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000733 bool ForEach = false;
Chris Lattner71e23ce2006-11-04 20:18:38 +0000734
Chris Lattner9075bd72006-08-10 04:59:57 +0000735 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000736 if (Tok.is(tok::semi)) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000737 // no first part, eat the ';'.
738 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000739 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000740 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000741 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
742 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000743 DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
Steve Naroff66356bd2007-09-16 14:56:35 +0000744 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000745 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000746 if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000747 ConsumeToken(); // consume 'in'
748 Value = ParseExpression();
749 if (!Value.isInvalid)
750 SecondPart = Value.Val;
751 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000752 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000753 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +0000754
Chris Lattnercd68f642007-06-27 01:06:29 +0000755 // Turn the expression into a stmt.
756 if (!Value.isInvalid) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000757 StmtResult R = Actions.ActOnExprStmt(Value.Val);
Chris Lattnercd68f642007-06-27 01:06:29 +0000758 if (!R.isInvalid)
759 FirstPart = R.Val;
760 }
Steve Naroff9992bba2007-05-30 16:27:15 +0000761
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000762 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000763 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000764 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000765 else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000766 ConsumeToken(); // consume 'in'
767 Value = ParseExpression();
768 if (!Value.isInvalid)
769 SecondPart = Value.Val;
770 }
771 else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000772 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000773 SkipUntil(tok::semi);
774 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000775 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000776 if (!ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000777 // Parse the second part of the for specifier.
778 if (Tok.is(tok::semi)) { // for (...;;
779 // no second part.
780 Value = ExprResult();
781 } else {
782 Value = ParseExpression();
783 if (!Value.isInvalid)
784 SecondPart = Value.Val;
785 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000786
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000787 if (Tok.is(tok::semi)) {
788 ConsumeToken();
789 } else {
790 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
791 SkipUntil(tok::semi);
792 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000793
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000794 // Parse the third part of the for specifier.
795 if (Tok.is(tok::r_paren)) { // for (...;...;)
796 // no third part.
797 Value = ExprResult();
798 } else {
799 Value = ParseExpression();
800 if (!Value.isInvalid) {
801 // Turn the expression into a stmt.
802 StmtResult R = Actions.ActOnExprStmt(Value.Val);
803 if (!R.isInvalid)
804 ThirdPart = R.Val;
805 }
Chris Lattnercd68f642007-06-27 01:06:29 +0000806 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000807 }
Chris Lattner4564bc12006-08-10 23:14:52 +0000808 // Match the ')'.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000809 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000810
Chris Lattner8fb26252007-08-22 05:28:50 +0000811 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000812 // there is no compound stmt. C90 does not have this clause. We only do this
813 // if the body isn't a compound statement to avoid push/pop in common cases.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000814 bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000815 if (NeedsInnerScope) EnterScope(Scope::DeclScope);
Chris Lattner8fb26252007-08-22 05:28:50 +0000816
Chris Lattner9075bd72006-08-10 04:59:57 +0000817 // Read the body statement.
Chris Lattner71e23ce2006-11-04 20:18:38 +0000818 StmtResult Body = ParseStatement();
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000819
Chris Lattner8fb26252007-08-22 05:28:50 +0000820 // Pop the body scope if needed.
Chris Lattner8f44d202007-08-22 05:33:11 +0000821 if (NeedsInnerScope) ExitScope();
Chris Lattner8fb26252007-08-22 05:28:50 +0000822
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000823 // Leave the for-scope.
824 ExitScope();
825
Chris Lattner71e23ce2006-11-04 20:18:38 +0000826 if (Body.isInvalid)
827 return Body;
Chris Lattner30f910e2006-10-16 05:52:41 +0000828
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000829 if (!ForEach)
830 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
831 SecondPart, ThirdPart, RParenLoc, Body.Val);
832 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000833 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000834 SecondPart, RParenLoc, Body.Val);
Chris Lattner9075bd72006-08-10 04:59:57 +0000835}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000836
Chris Lattner503fadc2006-08-10 05:45:44 +0000837/// ParseGotoStatement
838/// jump-statement:
839/// 'goto' identifier ';'
840/// [GNU] 'goto' '*' expression ';'
841///
842/// Note: this lets the caller parse the end ';'.
843///
Chris Lattner30f910e2006-10-16 05:52:41 +0000844Parser::StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000845 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000846 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000847
Chris Lattner30f910e2006-10-16 05:52:41 +0000848 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000849 if (Tok.is(tok::identifier)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000850 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000851 Tok.getIdentifierInfo());
Chris Lattner503fadc2006-08-10 05:45:44 +0000852 ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000853 } else if (Tok.is(tok::star) && !getLang().NoExtensions) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000854 // GNU indirect goto extension.
855 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +0000856 SourceLocation StarLoc = ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000857 ExprResult R = ParseExpression();
Chris Lattner30f910e2006-10-16 05:52:41 +0000858 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000859 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000860 return true;
861 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000862 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
Chris Lattnere34b2c22007-07-22 04:13:33 +0000863 } else {
864 Diag(Tok, diag::err_expected_ident);
865 return true;
Chris Lattner503fadc2006-08-10 05:45:44 +0000866 }
Chris Lattnere34b2c22007-07-22 04:13:33 +0000867
Chris Lattner30f910e2006-10-16 05:52:41 +0000868 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +0000869}
870
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000871/// ParseContinueStatement
872/// jump-statement:
873/// 'continue' ';'
874///
875/// Note: this lets the caller parse the end ';'.
876///
877Parser::StmtResult Parser::ParseContinueStatement() {
878 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000879 return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000880}
881
882/// ParseBreakStatement
883/// jump-statement:
884/// 'break' ';'
885///
886/// Note: this lets the caller parse the end ';'.
887///
888Parser::StmtResult Parser::ParseBreakStatement() {
889 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Steve Naroff66356bd2007-09-16 14:56:35 +0000890 return Actions.ActOnBreakStmt(BreakLoc, CurScope);
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000891}
892
Chris Lattner503fadc2006-08-10 05:45:44 +0000893/// ParseReturnStatement
894/// jump-statement:
895/// 'return' expression[opt] ';'
Chris Lattner30f910e2006-10-16 05:52:41 +0000896Parser::StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000897 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000898 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Chris Lattner503fadc2006-08-10 05:45:44 +0000899
Chris Lattner30f910e2006-10-16 05:52:41 +0000900 ExprResult R(0);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000901 if (Tok.isNot(tok::semi)) {
Chris Lattner30f910e2006-10-16 05:52:41 +0000902 R = ParseExpression();
903 if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +0000904 SkipUntil(tok::semi, false, true);
Chris Lattner30f910e2006-10-16 05:52:41 +0000905 return true;
906 }
Chris Lattnera0927ce2006-08-12 16:59:03 +0000907 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000908 return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
Chris Lattner503fadc2006-08-10 05:45:44 +0000909}
Chris Lattner0116c472006-08-15 06:03:28 +0000910
911/// ParseAsmStatement - Parse a GNU extended asm statement.
912/// [GNU] asm-statement:
913/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
914///
915/// [GNU] asm-argument:
916/// asm-string-literal
917/// asm-string-literal ':' asm-operands[opt]
918/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
919/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
920/// ':' asm-clobbers
921///
922/// [GNU] asm-clobbers:
923/// asm-string-literal
924/// asm-clobbers ',' asm-string-literal
925///
Chris Lattner30f910e2006-10-16 05:52:41 +0000926Parser::StmtResult Parser::ParseAsmStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000927 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +0000928 SourceLocation AsmLoc = ConsumeToken();
Chris Lattner0116c472006-08-15 06:03:28 +0000929
930 DeclSpec DS;
931 SourceLocation Loc = Tok.getLocation();
932 ParseTypeQualifierListOpt(DS);
933
934 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +0000935 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner0116c472006-08-15 06:03:28 +0000936 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
Chris Lattnera925dc62006-11-28 04:33:46 +0000937 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner0116c472006-08-15 06:03:28 +0000938 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
939
940 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +0000941 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Anders Carlsson19fe1162008-02-05 23:03:50 +0000942 bool isSimple = false;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000943 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +0000944 Diag(Tok, diag::err_expected_lparen_after, "asm");
945 SkipUntil(tok::r_paren);
Chris Lattner30f910e2006-10-16 05:52:41 +0000946 return true;
Chris Lattner0116c472006-08-15 06:03:28 +0000947 }
Chris Lattner04132372006-10-16 06:12:55 +0000948 Loc = ConsumeParen();
Chris Lattner0116c472006-08-15 06:03:28 +0000949
Anders Carlsson81a5a692007-11-20 19:21:03 +0000950 ExprResult AsmString = ParseAsmStringLiteral();
951 if (AsmString.isInvalid)
952 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000953
954 llvm::SmallVector<std::string, 4> Names;
955 llvm::SmallVector<ExprTy*, 4> Constraints;
956 llvm::SmallVector<ExprTy*, 4> Exprs;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000957 llvm::SmallVector<ExprTy*, 4> Clobbers;
Chris Lattner0116c472006-08-15 06:03:28 +0000958
Anders Carlsson19fe1162008-02-05 23:03:50 +0000959 unsigned NumInputs = 0, NumOutputs = 0;
960
961 SourceLocation RParenLoc;
962 if (Tok.is(tok::r_paren)) {
963 // We have a simple asm expression
964 isSimple = true;
965
966 RParenLoc = ConsumeParen();
967 } else {
968 // Parse Outputs, if present.
969 ParseAsmOperandsOpt(Names, Constraints, Exprs);
970
971 NumOutputs = Names.size();
972
973 // Parse Inputs, if present.
974 ParseAsmOperandsOpt(Names, Constraints, Exprs);
975 assert(Names.size() == Constraints.size() &&
976 Constraints.size() == Exprs.size()
977 && "Input operand size mismatch!");
978
979 NumInputs = Names.size() - NumOutputs;
980
981 // Parse the clobbers, if present.
982 if (Tok.is(tok::colon)) {
Anders Carlsson091a0592007-11-21 23:27:34 +0000983 ConsumeToken();
Anders Carlsson19fe1162008-02-05 23:03:50 +0000984
985 // Parse the asm-string list for clobbers.
986 while (1) {
987 ExprResult Clobber = ParseAsmStringLiteral();
988
989 if (Clobber.isInvalid)
990 break;
991
992 Clobbers.push_back(Clobber.Val);
993
994 if (Tok.isNot(tok::comma)) break;
995 ConsumeToken();
996 }
Chris Lattner0116c472006-08-15 06:03:28 +0000997 }
Anders Carlsson19fe1162008-02-05 23:03:50 +0000998
999 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner0116c472006-08-15 06:03:28 +00001000 }
1001
Anders Carlsson19fe1162008-02-05 23:03:50 +00001002 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
1003 NumOutputs, NumInputs,
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001004 &Names[0], &Constraints[0], &Exprs[0],
1005 AsmString.Val,
1006 Clobbers.size(), &Clobbers[0],
1007 RParenLoc);
Chris Lattner0116c472006-08-15 06:03:28 +00001008}
1009
1010/// ParseAsmOperands - Parse the asm-operands production as used by
1011/// asm-statement. We also parse a leading ':' token. If the leading colon is
1012/// not present, we do not parse anything.
1013///
1014/// [GNU] asm-operands:
1015/// asm-operand
1016/// asm-operands ',' asm-operand
1017///
1018/// [GNU] asm-operand:
1019/// asm-string-literal '(' expression ')'
1020/// '[' identifier ']' asm-string-literal '(' expression ')'
1021///
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001022void Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
1023 llvm::SmallVectorImpl<ExprTy*> &Constraints,
1024 llvm::SmallVectorImpl<ExprTy*> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001025 // Only do anything if this operand is present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001026 if (Tok.isNot(tok::colon)) return;
Chris Lattner0116c472006-08-15 06:03:28 +00001027 ConsumeToken();
1028
1029 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001030 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Chris Lattner0116c472006-08-15 06:03:28 +00001031 return;
1032
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001033 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001034 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001035 if (Tok.is(tok::l_square)) {
Chris Lattner04132372006-10-16 06:12:55 +00001036 SourceLocation Loc = ConsumeBracket();
Chris Lattner0116c472006-08-15 06:03:28 +00001037
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001038 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001039 Diag(Tok, diag::err_expected_ident);
1040 SkipUntil(tok::r_paren);
1041 return;
1042 }
Chris Lattner645ff3f2007-10-29 04:06:22 +00001043
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001044 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001045 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001046
1047 Names.push_back(std::string(II->getName(), II->getLength()));
Chris Lattner0116c472006-08-15 06:03:28 +00001048 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001049 } else
1050 Names.push_back(std::string());
Chris Lattner0116c472006-08-15 06:03:28 +00001051
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001052 ExprResult Constraint = ParseAsmStringLiteral();
1053 if (Constraint.isInvalid) {
1054 SkipUntil(tok::r_paren);
1055 return;
1056 }
1057 Constraints.push_back(Constraint.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001058
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001059 if (Tok.isNot(tok::l_paren)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001060 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
1061 SkipUntil(tok::r_paren);
1062 return;
1063 }
1064
1065 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001066 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +00001067 if (Res.isInvalid) {
1068 SkipUntil(tok::r_paren);
1069 return;
1070 }
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001071 Exprs.push_back(Res.Val);
Chris Lattner0116c472006-08-15 06:03:28 +00001072 // Eat the comma and continue parsing if it exists.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001073 if (Tok.isNot(tok::comma)) return;
Chris Lattner0116c472006-08-15 06:03:28 +00001074 ConsumeToken();
1075 }
1076}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001077
1078Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
1079 SourceLocation L, SourceLocation R) {
1080 // Do not enter a scope for the brace, as the arguments are in the same scope
1081 // (the function body) as the body itself. Instead, just read the statement
1082 // list and put it into a CompoundStmt for safe keeping.
1083 StmtResult FnBody = ParseCompoundStatementBody();
1084
1085 // If the function body could not be parsed, make a bogus compoundstmt.
1086 if (FnBody.isInvalid)
1087 FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
1088
1089 // Leave the function body scope.
1090 ExitScope();
1091
Steve Naroffb313fc32007-11-11 23:20:51 +00001092 return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001093}