blob: b676d46a43703c9f4c3805b4e2e727a1ec279fbf [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 Lattnere550a4e2006-08-24 06:37:51 +0000359 ParseSimpleParenExpression();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000360
361 // Read the if condition.
362 ParseStatement();
363
364 // If it has an else, parse it.
365 if (Tok.getKind() == tok::kw_else) {
366 ConsumeToken();
367 ParseStatement();
368 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000369}
370
Chris Lattner9075bd72006-08-10 04:59:57 +0000371/// ParseSwitchStatement
372/// switch-statement:
373/// 'switch' '(' expression ')' statement
374void Parser::ParseSwitchStatement() {
375 assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
376 ConsumeToken(); // eat the 'switch'.
377
378 if (Tok.getKind() != tok::l_paren) {
379 Diag(Tok, diag::err_expected_lparen_after, "switch");
380 SkipUntil(tok::semi);
381 return;
382 }
383
384 // Parse the condition.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000385 ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000386
387 // Read the body statement.
388 ParseStatement();
389}
390
391/// ParseWhileStatement
392/// while-statement: [C99 6.8.5.1]
393/// 'while' '(' expression ')' statement
394void Parser::ParseWhileStatement() {
395 assert(Tok.getKind() == tok::kw_while && "Not a while stmt!");
396 ConsumeToken(); // eat the 'while'.
397
398 if (Tok.getKind() != tok::l_paren) {
399 Diag(Tok, diag::err_expected_lparen_after, "while");
400 SkipUntil(tok::semi);
401 return;
402 }
403
404 // Parse the condition.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000405 ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000406
407 // Read the body statement.
408 ParseStatement();
409}
410
411/// ParseDoStatement
412/// do-statement: [C99 6.8.5.2]
413/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +0000414/// Note: this lets the caller parse the end ';'.
Chris Lattner9075bd72006-08-10 04:59:57 +0000415void Parser::ParseDoStatement() {
416 assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
417 SourceLocation DoLoc = Tok.getLocation();
418 ConsumeToken(); // eat the 'do'.
419
420 // Read the body statement.
421 ParseStatement();
422
423 if (Tok.getKind() != tok::kw_while) {
424 Diag(Tok, diag::err_expected_while);
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000425 Diag(DoLoc, diag::err_matching, "do");
Chris Lattner9075bd72006-08-10 04:59:57 +0000426 SkipUntil(tok::semi);
427 return;
428 }
429 ConsumeToken();
430
431 if (Tok.getKind() != tok::l_paren) {
432 Diag(Tok, diag::err_expected_lparen_after, "do/while");
433 SkipUntil(tok::semi);
434 return;
435 }
436
437 // Parse the condition.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000438 ParseSimpleParenExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000439}
440
441/// ParseForStatement
442/// for-statement: [C99 6.8.5.3]
443/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
444/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
445void Parser::ParseForStatement() {
446 assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
447 SourceLocation ForLoc = Tok.getLocation();
448 ConsumeToken(); // eat the 'for'.
449
450 if (Tok.getKind() != tok::l_paren) {
451 Diag(Tok, diag::err_expected_lparen_after, "for");
452 SkipUntil(tok::semi);
453 return;
454 }
455
456 SourceLocation LParenLoc = Tok.getLocation();
457 ConsumeParen();
458
Chris Lattner89c50c62006-08-11 06:41:18 +0000459 ExprResult Value;
460
Chris Lattner9075bd72006-08-10 04:59:57 +0000461 // Parse the first part of the for specifier.
462 if (Tok.getKind() == tok::semi) { // for (;
Chris Lattner53361ac2006-08-10 05:19:57 +0000463 // no first part, eat the ';'.
464 ConsumeToken();
Chris Lattner9075bd72006-08-10 04:59:57 +0000465 } else if (isDeclarationSpecifier()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +0000466 // Parse declaration, which eats the ';'.
Chris Lattnerab1803652006-08-10 05:22:36 +0000467 if (!getLang().C99) // Use of C99-style for loops in C90 mode?
468 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Chris Lattner53361ac2006-08-10 05:19:57 +0000469 ParseDeclaration(Declarator::ForContext);
Chris Lattner9075bd72006-08-10 04:59:57 +0000470 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000471 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000472
Chris Lattner53361ac2006-08-10 05:19:57 +0000473 if (Tok.getKind() == tok::semi) {
474 ConsumeToken();
475 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000476 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner53361ac2006-08-10 05:19:57 +0000477 SkipUntil(tok::semi);
478 }
Chris Lattner9075bd72006-08-10 04:59:57 +0000479 }
480
481 // Parse the second part of the for specifier.
482 if (Tok.getKind() == tok::semi) { // for (...;;
483 // no second part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000484 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000485 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000486 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000487 }
488
489 if (Tok.getKind() == tok::semi) {
490 ConsumeToken();
491 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000492 if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
Chris Lattner9075bd72006-08-10 04:59:57 +0000493 SkipUntil(tok::semi);
494 }
495
496 // Parse the third part of the for specifier.
497 if (Tok.getKind() == tok::r_paren) { // for (...;...;)
498 // no third part.
Chris Lattner89c50c62006-08-11 06:41:18 +0000499 Value = ExprResult();
Chris Lattner9075bd72006-08-10 04:59:57 +0000500 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000501 Value = ParseExpression();
Chris Lattner9075bd72006-08-10 04:59:57 +0000502 }
503
Chris Lattner4564bc12006-08-10 23:14:52 +0000504 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000505 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000506
507 // Read the body statement.
508 ParseStatement();
509}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000510
Chris Lattner503fadc2006-08-10 05:45:44 +0000511/// ParseGotoStatement
512/// jump-statement:
513/// 'goto' identifier ';'
514/// [GNU] 'goto' '*' expression ';'
515///
516/// Note: this lets the caller parse the end ';'.
517///
518void Parser::ParseGotoStatement() {
519 assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
520 ConsumeToken(); // eat the 'goto'.
521
522 if (Tok.getKind() == tok::identifier) {
523 ConsumeToken();
524 } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
525 // GNU indirect goto extension.
526 Diag(Tok, diag::ext_gnu_indirect_goto);
527 ConsumeToken();
Chris Lattnera0927ce2006-08-12 16:59:03 +0000528 ExprResult R = ParseExpression();
529 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
530 SkipUntil(tok::semi, false, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000531 }
532}
533
534/// ParseReturnStatement
535/// jump-statement:
536/// 'return' expression[opt] ';'
537void Parser::ParseReturnStatement() {
538 assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
539 ConsumeToken(); // eat the 'return'.
540
Chris Lattnera0927ce2006-08-12 16:59:03 +0000541 if (Tok.getKind() != tok::semi) {
542 ExprResult R = ParseExpression();
543 if (R.isInvalid) // Skip to the semicolon, but don't consume it.
544 SkipUntil(tok::semi, false, true);
545 }
Chris Lattner503fadc2006-08-10 05:45:44 +0000546}
Chris Lattner0116c472006-08-15 06:03:28 +0000547
548/// ParseAsmStatement - Parse a GNU extended asm statement.
549/// [GNU] asm-statement:
550/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
551///
552/// [GNU] asm-argument:
553/// asm-string-literal
554/// asm-string-literal ':' asm-operands[opt]
555/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
556/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
557/// ':' asm-clobbers
558///
559/// [GNU] asm-clobbers:
560/// asm-string-literal
561/// asm-clobbers ',' asm-string-literal
562///
563void Parser::ParseAsmStatement() {
564 assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt");
565 ConsumeToken();
566
567 DeclSpec DS;
568 SourceLocation Loc = Tok.getLocation();
569 ParseTypeQualifierListOpt(DS);
570
571 // GNU asms accept, but warn, about type-qualifiers other than volatile.
572 if (DS.TypeQualifiers & DeclSpec::TQ_const)
573 Diag(Loc, diag::w_asm_qualifier_ignored, "const");
574 if (DS.TypeQualifiers & DeclSpec::TQ_restrict)
575 Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
576
577 // Remember if this was a volatile asm.
578 bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile;
579
580 if (Tok.getKind() != tok::l_paren) {
581 Diag(Tok, diag::err_expected_lparen_after, "asm");
582 SkipUntil(tok::r_paren);
583 return;
584 }
585 Loc = Tok.getLocation();
586 ConsumeParen();
587
588 ParseAsmStringLiteral();
589
590 // Parse Outputs, if present.
591 ParseAsmOperandsOpt();
592
593 // Parse Inputs, if present.
594 ParseAsmOperandsOpt();
595
596 // Parse the clobbers, if present.
597 if (Tok.getKind() == tok::colon) {
598 ConsumeToken();
599
600 if (Tok.getKind() == tok::string_literal) {
601 // Parse the asm-string list for clobbers.
602 while (1) {
603 ParseAsmStringLiteral();
604
605 if (Tok.getKind() != tok::comma) break;
606 ConsumeToken();
607 }
608 }
609 }
610
611 MatchRHSPunctuation(tok::r_paren, Loc);
612}
613
614/// ParseAsmOperands - Parse the asm-operands production as used by
615/// asm-statement. We also parse a leading ':' token. If the leading colon is
616/// not present, we do not parse anything.
617///
618/// [GNU] asm-operands:
619/// asm-operand
620/// asm-operands ',' asm-operand
621///
622/// [GNU] asm-operand:
623/// asm-string-literal '(' expression ')'
624/// '[' identifier ']' asm-string-literal '(' expression ')'
625///
626void Parser::ParseAsmOperandsOpt() {
627 // Only do anything if this operand is present.
628 if (Tok.getKind() != tok::colon) return;
629 ConsumeToken();
630
631 // 'asm-operands' isn't present?
632 if (Tok.getKind() != tok::string_literal && Tok.getKind() != tok::l_square)
633 return;
634
635 while (1) {
636 // Read the [id] if present.
637 if (Tok.getKind() == tok::l_square) {
638 SourceLocation Loc = Tok.getLocation();
639 ConsumeBracket();
640
641 if (Tok.getKind() != tok::identifier) {
642 Diag(Tok, diag::err_expected_ident);
643 SkipUntil(tok::r_paren);
644 return;
645 }
646 MatchRHSPunctuation(tok::r_square, Loc);
647 }
648
649 ParseAsmStringLiteral();
650
651 if (Tok.getKind() != tok::l_paren) {
652 Diag(Tok, diag::err_expected_lparen_after, "asm operand");
653 SkipUntil(tok::r_paren);
654 return;
655 }
656
657 // Read the parenthesized expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000658 ExprResult Res = ParseSimpleParenExpression();
Chris Lattner0116c472006-08-15 06:03:28 +0000659 if (Res.isInvalid) {
660 SkipUntil(tok::r_paren);
661 return;
662 }
663
664 // Eat the comma and continue parsing if it exists.
665 if (Tok.getKind() != tok::comma) return;
666 ConsumeToken();
667 }
668}