blob: 293f9cc72b1eeb15a559a8b9ea7fd3099093c4a3 [file] [log] [blame]
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001//===--- Statement.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
16#include "clang/Basic/Diagnostic.h"
Chris Lattner53361ac2006-08-10 05:19:57 +000017#include "clang/Parse/Declarations.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000018using namespace llvm;
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.8: Statements and Blocks.
23//===----------------------------------------------------------------------===//
24
25/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
26/// StatementOrDeclaration:
27/// statement
28/// declaration
29///
30/// statement:
31/// labeled-statement
32/// compound-statement
33/// expression-statement
34/// selection-statement
35/// iteration-statement
36/// jump-statement
37/// [OBC] objc-throw-statement [TODO]
38/// [OBC] objc-try-catch-statement [TODO]
39/// [OBC] objc-synchronized-statement [TODO]
Chris Lattner0116c472006-08-15 06:03:28 +000040/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000041/// [OMP] openmp-construct [TODO]
42///
43/// labeled-statement:
44/// identifier ':' statement
45/// 'case' constant-expression ':' statement
46/// 'default' ':' statement
47///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// selection-statement:
49/// if-statement
50/// switch-statement
51///
52/// iteration-statement:
53/// while-statement
54/// do-statement
55/// for-statement
56///
Chris Lattner9075bd72006-08-10 04:59:57 +000057/// expression-statement:
58/// expression[opt] ';'
59///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000060/// jump-statement:
61/// 'goto' identifier ';'
62/// 'continue' ';'
63/// 'break' ';'
64/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000065/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000066///
67/// [OBC] objc-throw-statement: [TODO]
68/// [OBC] '@' 'throw' expression ';' [TODO]
69/// [OBC] '@' 'throw' ';' [TODO]
70///
Chris Lattnerc951dae2006-08-10 04:23:57 +000071void Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
Chris Lattner503fadc2006-08-10 05:45:44 +000072 const char *SemiError = 0;
73
Chris Lattnerd2685cf2006-08-10 05:59:48 +000074ParseNextStatement:
Chris Lattner503fadc2006-08-10 05:45:44 +000075 // Cases in this switch statement should fall through if the parser expects
76 // the token to end in a semicolon (in which case SemiError should be set),
77 // or they directly 'return;' if not.
Chris Lattner0ccd51e2006-08-09 05:47:47 +000078 switch (Tok.getKind()) {
Chris Lattnerf8afb622006-08-10 18:26:31 +000079 case tok::identifier: // C99 6.8.1: labeled-statement
80 // identifier ':' statement
81 // declaration (if !OnlyStatement)
82 // expression[opt] ';'
83 return ParseIdentifierStatement(OnlyStatement);
84
Chris Lattner0ccd51e2006-08-09 05:47:47 +000085 default:
Chris Lattnerf8afb622006-08-10 18:26:31 +000086 if (!OnlyStatement && isDeclarationSpecifier()) {
87 // TODO: warn/disable if declaration is in the middle of a block and !C99.
88 ParseDeclaration(Declarator::BlockContext);
89 return;
90 } else if (Tok.getKind() == tok::r_brace) {
91 Diag(Tok, diag::err_expected_statement);
92 } else {
93 // expression[opt] ';'
Chris Lattner89c50c62006-08-11 06:41:18 +000094 ExprResult Res = ParseExpression();
95 if (Res.isInvalid) {
96 // If the expression is invalid, skip ahead to the next semicolon. Not
97 // doing this opens us up to the possibility of infinite loops if
98 // ParseExpression does not consume any tokens.
99 SkipUntil(tok::semi);
100 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000101 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000102 return;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000103
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000104 case tok::kw_case: // C99 6.8.1: labeled-statement
105 ParseCaseStatement();
106 if (Tok.getKind() == tok::r_brace) {
107 Diag(Tok, diag::err_label_end_of_compound_statement);
108 return;
109 }
110 OnlyStatement = true;
111 goto ParseNextStatement;
112 case tok::kw_default: // C99 6.8.1: labeled-statement
113 ParseDefaultStatement();
114 if (Tok.getKind() == tok::r_brace) {
115 Diag(Tok, diag::err_label_end_of_compound_statement);
116 return;
117 }
118 OnlyStatement = true;
119 goto ParseNextStatement;
Chris Lattner9075bd72006-08-10 04:59:57 +0000120
121 case tok::l_brace: // C99 6.8.2: compound-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000122 ParseCompoundStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000123 return;
Chris Lattner9075bd72006-08-10 04:59:57 +0000124 case tok::semi: // C99 6.8.3: expression[opt] ';'
Chris Lattnerc951dae2006-08-10 04:23:57 +0000125 ConsumeToken();
Chris Lattner503fadc2006-08-10 05:45:44 +0000126 return;
127
Chris Lattner9075bd72006-08-10 04:59:57 +0000128 case tok::kw_if: // C99 6.8.4.1: if-statement
Chris Lattnerc951dae2006-08-10 04:23:57 +0000129 ParseIfStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000130 return;
Chris Lattner9075bd72006-08-10 04:59:57 +0000131 case tok::kw_switch: // C99 6.8.4.2: switch-statement
132 ParseSwitchStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000133 return;
134
Chris Lattner9075bd72006-08-10 04:59:57 +0000135 case tok::kw_while: // C99 6.8.5.1: while-statement
136 ParseWhileStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000137 return;
Chris Lattner9075bd72006-08-10 04:59:57 +0000138 case tok::kw_do: // C99 6.8.5.2: do-statement
139 ParseDoStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000140 SemiError = "do/while loop";
Chris Lattner9075bd72006-08-10 04:59:57 +0000141 break;
142 case tok::kw_for: // C99 6.8.5.3: for-statement
143 ParseForStatement();
Chris Lattner503fadc2006-08-10 05:45:44 +0000144 return;
145
146 case tok::kw_goto: // C99 6.8.6.1: goto-statement
147 ParseGotoStatement();
148 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
151 ConsumeToken(); // eat the 'continue'.
152 SemiError = "continue statement";
153 break;
154 case tok::kw_break: // C99 6.8.6.3: break-statement
155 ConsumeToken(); // eat the 'break'.
156 SemiError = "break statement";
157 break;
158 case tok::kw_return: // C99 6.8.6.4: return-statement
159 ParseReturnStatement();
160 SemiError = "return statement";
161 break;
Chris Lattner0116c472006-08-15 06:03:28 +0000162
163 case tok::kw_asm:
164 ParseAsmStatement();
165 SemiError = "asm statement";
166 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000167 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000168
169 // If we reached this code, the statement must end in a semicolon.
170 if (Tok.getKind() == tok::semi) {
171 ConsumeToken();
172 } else {
173 Diag(Tok, diag::err_expected_semi_after, SemiError);
174 SkipUntil(tok::semi);
175 }
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000176}
177
Chris Lattnerf8afb622006-08-10 18:26:31 +0000178/// ParseIdentifierStatement - Because we don't have two-token lookahead, we
179/// have a bit of a quandry here. Reading the identifier is necessary to see if
180/// there is a ':' after it. If there is, this is a label, regardless of what
181/// else the identifier can mean. If not, this is either part of a declaration
182/// (if the identifier is a type-name) or part of an expression.
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
Chris Lattner6dfd9782006-08-10 18:31:37 +0000187/// declaration (if !OnlyStatement)
188/// expression[opt] ';'
189///
Chris Lattnerf8afb622006-08-10 18:26:31 +0000190void Parser::ParseIdentifierStatement(bool OnlyStatement) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000191 IdentifierInfo *II = Tok.getIdentifierInfo();
192 assert(Tok.getKind() == tok::identifier && II && "Not an identifier!");
193
Chris Lattner0c6c0342006-08-12 18:12:45 +0000194 LexerToken IdentTok = Tok; // Save the token.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000195 ConsumeToken(); // eat the identifier.
196
197 // identifier ':' statement
198 if (Tok.getKind() == tok::colon) {
199 ConsumeToken();
Chris Lattnere37e2332006-08-15 04:50:22 +0000200
201 // Read label attributes, if present.
202 if (Tok.getKind() == tok::kw___attribute)
203 ParseAttributes();
204
Chris Lattner6dfd9782006-08-10 18:31:37 +0000205 ParseStatement();
206 return;
207 }
208
209 // declaration
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000210 if (!OnlyStatement &&
211 Actions.isTypedefName(*IdentTok.getIdentifierInfo(), CurScope)) {
Chris Lattner6dfd9782006-08-10 18:31:37 +0000212 // Handle this. Warn/disable if in middle of block and !C99.
Chris Lattner2f9980e2006-08-10 18:39:24 +0000213 DeclSpec DS;
214
215 // FIXME: Add the typedef name to the start of the decl-specs.
216 // ParseDeclarationSpecifiers will continue from there.
217 ParseDeclarationSpecifiers(DS);
218
Chris Lattner0e894622006-08-13 19:58:17 +0000219 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
220 // declaration-specifiers init-declarator-list[opt] ';'
221 if (Tok.getKind() == tok::semi) {
222 // TODO: emit error on 'int;' or 'const enum foo;'.
223 // if (!DS.isMissingDeclaratorOk()) Diag(...);
224
225 ConsumeToken();
226 return;
227 }
228
Chris Lattner2f9980e2006-08-10 18:39:24 +0000229 // Parse all the declarators.
230 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
231 ParseDeclarator(DeclaratorInfo);
232
233 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
234 return;
Chris Lattner6dfd9782006-08-10 18:31:37 +0000235 }
236
Chris Lattner0c6c0342006-08-12 18:12:45 +0000237 // Otherwise, this is an expression. Seed it with II and parse it.
238 ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok);
239 if (Res.isInvalid)
240 SkipUntil(tok::semi);
241 else if (Tok.getKind() == tok::semi)
242 ConsumeToken();
243 else {
244 Diag(Tok, diag::err_expected_semi_after, "expression");
245 SkipUntil(tok::semi);
246 }
Chris Lattnerf8afb622006-08-10 18:26:31 +0000247}
248
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000249/// ParseCaseStatement
250/// labeled-statement:
251/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000252/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000253///
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000254/// Note that this does not parse the 'statement' at the end.
255///
256void Parser::ParseCaseStatement() {
257 assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
258 ConsumeToken(); // eat the 'case'.
259
Chris Lattner476c3ad2006-08-13 22:09:58 +0000260 ExprResult Res = ParseConstantExpression();
261 if (Res.isInvalid) {
262 SkipUntil(tok::colon);
263 return;
264 }
265
266 // GNU case range extension.
267 if (Tok.getKind() == tok::ellipsis) {
268 Diag(Tok, diag::ext_gnu_case_range);
269 ConsumeToken();
270
271 ExprResult RHS = ParseConstantExpression();
272 if (RHS.isInvalid) {
273 SkipUntil(tok::colon);
274 return;
275 }
276 }
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000277
278 if (Tok.getKind() == tok::colon) {
279 ConsumeToken();
280 } else {
281 Diag(Tok, diag::err_expected_colon_after, "'case'");
282 SkipUntil(tok::colon);
283 }
284}
285
286/// ParseDefaultStatement
287/// labeled-statement:
288/// 'default' ':' statement
289/// Note that this does not parse the 'statement' at the end.
290///
291void Parser::ParseDefaultStatement() {
292 assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
293 ConsumeToken(); // eat the 'default'.
294
295 if (Tok.getKind() == tok::colon) {
296 ConsumeToken();
297 } else {
298 Diag(Tok, diag::err_expected_colon_after, "'default'");
299 SkipUntil(tok::colon);
300 }
301}
302
303
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000304/// ParseCompoundStatement - Parse a "{}" block.
305///
306/// compound-statement: [C99 6.8.2]
307/// { block-item-list[opt] }
308/// [GNU] { label-declarations block-item-list } [TODO]
309///
310/// block-item-list:
311/// block-item
312/// block-item-list block-item
313///
314/// block-item:
315/// declaration
316/// [GNU] '__extension__' declaration [TODO]
317/// statement
318/// [OMP] openmp-directive [TODO]
319///
320/// [GNU] label-declarations:
321/// [GNU] label-declaration
322/// [GNU] label-declarations label-declaration
323///
324/// [GNU] label-declaration:
325/// [GNU] '__label__' identifier-list ';'
326///
327/// [OMP] openmp-directive: [TODO]
328/// [OMP] barrier-directive
329/// [OMP] flush-directive
330void Parser::ParseCompoundStatement() {
331 assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
332 ConsumeBrace(); // eat the '{'.
333
334 while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof)
Chris Lattnerc951dae2006-08-10 04:23:57 +0000335 ParseStatementOrDeclaration(false);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000336
337 // We broke out of the while loop because we found a '}' or EOF.
338 if (Tok.getKind() == tok::r_brace)
339 ConsumeBrace();
340 else
341 Diag(Tok, diag::err_expected_rbrace);
342}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000343
344/// ParseIfStatement
345/// if-statement: [C99 6.8.4.1]
346/// 'if' '(' expression ')' statement
347/// 'if' '(' expression ')' statement 'else' statement
348void Parser::ParseIfStatement() {
349 assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
350 ConsumeToken(); // eat the 'if'.
351
352 if (Tok.getKind() != tok::l_paren) {
Chris Lattner9075bd72006-08-10 04:59:57 +0000353 Diag(Tok, diag::err_expected_lparen_after, "if");
Chris Lattnerc951dae2006-08-10 04:23:57 +0000354 SkipUntil(tok::semi);
355 return;
356 }
357
358 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000359 ParenParseOption ParenExprType = SimpleExpr;
360 ParseParenExpression(ParenExprType);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000361
362 // Read the if condition.
363 ParseStatement();
364
365 // If it has an else, parse it.
366 if (Tok.getKind() == tok::kw_else) {
367 ConsumeToken();
368 ParseStatement();
369 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000370}
371
Chris Lattner9075bd72006-08-10 04:59:57 +0000372/// ParseSwitchStatement
373/// switch-statement:
374/// 'switch' '(' expression ')' statement
375void Parser::ParseSwitchStatement() {
376 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
377 ConsumeToken(); // eat the 'switch'.
378
379 if (Tok.getKind() != tok::l_paren) {
380 Diag(Tok, diag::err_expected_lparen_after, "switch");
381 SkipUntil(tok::semi);
382 return;
383 }
384
385 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000386 ParenParseOption ParenExprType = SimpleExpr;
387 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000388
389 // Read the body statement.
390 ParseStatement();
391}
392
393/// ParseWhileStatement
394/// while-statement: [C99 6.8.5.1]
395/// 'while' '(' expression ')' statement
396void Parser::ParseWhileStatement() {
397 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
398 ConsumeToken(); // eat the 'while'.
399
400 if (Tok.getKind() != tok::l_paren) {
401 Diag(Tok, diag::err_expected_lparen_after, "while");
402 SkipUntil(tok::semi);
403 return;
404 }
405
406 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000407 ParenParseOption ParenExprType = SimpleExpr;
408 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000409
410 // Read the body statement.
411 ParseStatement();
412}
413
414/// ParseDoStatement
415/// do-statement: [C99 6.8.5.2]
416/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000417/// Note: this lets the caller parse the end ';'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000418void Parser::ParseDoStatement() {
419 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
420 SourceLocation DoLoc = Tok.getLocation();
421 ConsumeToken(); // eat the 'do'.
422
423 // Read the body statement.
424 ParseStatement();
425
426 if (Tok.getKind() != tok::kw_while) {
427 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000428 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000429 SkipUntil(tok::semi);
430 return;
431 }
432 ConsumeToken();
433
434 if (Tok.getKind() != tok::l_paren) {
435 Diag(Tok, diag::err_expected_lparen_after, "do/while");
436 SkipUntil(tok::semi);
437 return;
438 }
439
440 // Parse the condition.
Chris Lattner4add4e62006-08-11 01:33:00 +0000441 ParenParseOption ParenExprType = SimpleExpr;
442 ParseParenExpression(ParenExprType);
Chris Lattner9075bd72006-08-10 04:59:57 +0000443}
444
445/// ParseForStatement
446/// for-statement: [C99 6.8.5.3]
447/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
448/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
449void Parser::ParseForStatement() {
450 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
451 SourceLocation ForLoc = Tok.getLocation();
452 ConsumeToken(); // eat the 'for'.
453
454 if (Tok.getKind() != tok::l_paren) {
455 Diag(Tok, diag::err_expected_lparen_after, "for");
456 SkipUntil(tok::semi);
457 return;
458 }
459
460 SourceLocation LParenLoc = Tok.getLocation();
461 ConsumeParen();
462
Chris Lattner89c50c62006-08-11 06:41:18 +0000463 ExprResult Value;
464
Chris Lattner9075bd72006-08-10 04:59:57 +0000465 // Parse the first part of the for specifier.
466 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000467 // no first part, eat the ';'.
468 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000469 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000470 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000471 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
472 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner53361ac2006-08-10 05:19:57 +0000473 ParseDeclaration(Declarator::ForContext);
Chris Lattner9075bd72006-08-10 04:59:57 +0000474 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000475 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000476
Chris Lattner53361ac2006-08-10 05:19:57 +0000477 if (Tok.getKind() == tok::semi) {
478 ConsumeToken();
479 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000480 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000481 SkipUntil(tok::semi);
482 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000483 }
484
485 // Parse the second part of the for specifier.
486 if (Tok.getKind() == tok::semi) { // for (...;;
487 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000488 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000489 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000490 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000491 }
492
493 if (Tok.getKind() == tok::semi) {
494 ConsumeToken();
495 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000496 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000497 SkipUntil(tok::semi);
498 }
499
500 // Parse the third part of the for specifier.
501 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
502 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000503 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000504 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000505 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000506 }
507
Chris Lattner4564bc12006-08-10 23:14:52 +0000508 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000509 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000510
511 // Read the body statement.
512 ParseStatement();
513}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000514
Chris Lattner503fadc2006-08-10 05:45:44 +0000515/// ParseGotoStatement
516/// jump-statement:
517/// 'goto' identifier ';'
518/// [GNU] 'goto' '*' expression ';'
519///
520/// Note: this lets the caller parse the end ';'.
521///
522void Parser::ParseGotoStatement() {
523 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
524 ConsumeToken(); // eat the 'goto'.
525
526 if (Tok.getKind() == tok::identifier) {
527 ConsumeToken();
528 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
529 // GNU indirect goto extension.
530 Diag(Tok, diag::ext_gnu_indirect_goto);
531 ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000532 ExprResult R = ParseExpression();
533 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
534 SkipUntil(tok::semi, false, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000535 }
536}
537
538/// ParseReturnStatement
539/// jump-statement:
540/// 'return' expression[opt] ';'
541void Parser::ParseReturnStatement() {
542 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
543 ConsumeToken(); // eat the 'return'.
544
Chris Lattnera0927ce2006-08-12 16:59:03 +0000545 if (Tok.getKind() != tok::semi) {
546 ExprResult R = ParseExpression();
547 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
548 SkipUntil(tok::semi, false, true);
549 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000550}
Chris Lattner0116c472006-08-15 06:03:28 +0000551
552/// ParseAsmStatement - Parse a GNU extended asm statement.
553/// [GNU] asm-statement:
554/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
555///
556/// [GNU] asm-argument:
557/// asm-string-literal
558/// asm-string-literal ':' asm-operands[opt]
559/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
560/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
561/// ':' asm-clobbers
562///
563/// [GNU] asm-clobbers:
564/// asm-string-literal
565/// asm-clobbers ',' asm-string-literal
566///
567void Parser::ParseAsmStatement() {
568 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
569 ConsumeToken();
570
571 DeclSpec DS;
572 SourceLocation Loc = Tok.getLocation();
573 ParseTypeQualifierListOpt(DS);
574
575 // GNU asms accept, but warn, about type-qualifiers other than volatile.
576 if (DS.TypeQualifiers & DeclSpec::TQ_const)
577 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
578 if (DS.TypeQualifiers & DeclSpec::TQ_restrict)
579 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
580
581 // Remember if this was a volatile asm.
582 bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
583
584 if (Tok.getKind() != tok::l_paren) {
585 Diag(Tok, diag::err_expected_lparen_after, "asm");
586 SkipUntil(tok::r_paren);
587 return;
588 }
589 Loc = Tok.getLocation();
590 ConsumeParen();
591
592 ParseAsmStringLiteral();
593
594 // Parse Outputs, if present.
595 ParseAsmOperandsOpt();
596
597 // Parse Inputs, if present.
598 ParseAsmOperandsOpt();
599
600 // Parse the clobbers, if present.
601 if (Tok.getKind() == tok::colon) {
602 ConsumeToken();
603
604 if (Tok.getKind() == tok::string_literal) {
605 // Parse the asm-string list for clobbers.
606 while (1) {
607 ParseAsmStringLiteral();
608
609 if (Tok.getKind() != tok::comma) break;
610 ConsumeToken();
611 }
612 }
613 }
614
615 MatchRHSPunctuation(tok::r_paren, Loc);
616}
617
618/// ParseAsmOperands - Parse the asm-operands production as used by
619/// asm-statement. We also parse a leading ':' token. If the leading colon is
620/// not present, we do not parse anything.
621///
622/// [GNU] asm-operands:
623/// asm-operand
624/// asm-operands ',' asm-operand
625///
626/// [GNU] asm-operand:
627/// asm-string-literal '(' expression ')'
628/// '[' identifier ']' asm-string-literal '(' expression ')'
629///
630void Parser::ParseAsmOperandsOpt() {
631 // Only do anything if this operand is present.
632 if (Tok.getKind() != tok::colon) return;
633 ConsumeToken();
634
635 // 'asm-operands' isn't present?
636 if (Tok.getKind() != tok::string_literal && Tok.getKind() != tok::l_square)
637 return;
638
639 while (1) {
640 // Read the [id] if present.
641 if (Tok.getKind() == tok::l_square) {
642 SourceLocation Loc = Tok.getLocation();
643 ConsumeBracket();
644
645 if (Tok.getKind() != tok::identifier) {
646 Diag(Tok, diag::err_expected_ident);
647 SkipUntil(tok::r_paren);
648 return;
649 }
650 MatchRHSPunctuation(tok::r_square, Loc);
651 }
652
653 ParseAsmStringLiteral();
654
655 if (Tok.getKind() != tok::l_paren) {
656 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
657 SkipUntil(tok::r_paren);
658 return;
659 }
660
661 // Read the parenthesized expression.
662 ParenParseOption ExprTy = SimpleExpr;
663 ExprResult Res = ParseParenExpression(ExprTy);
664 if (Res.isInvalid) {
665 SkipUntil(tok::r_paren);
666 return;
667 }
668
669 // Eat the comma and continue parsing if it exists.
670 if (Tok.getKind() != tok::comma) return;
671 ConsumeToken();
672 }
673}