Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseStmt.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" |
| 17 | #include "clang/Parse/DeclSpec.h" |
| 18 | #include "clang/Parse/Scope.h" |
| 19 | using 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] |
| 40 | /// [GNU] asm-statement |
| 41 | /// [OMP] openmp-construct [TODO] |
| 42 | /// |
| 43 | /// labeled-statement: |
| 44 | /// identifier ':' statement |
| 45 | /// 'case' constant-expression ':' statement |
| 46 | /// 'default' ':' statement |
| 47 | /// |
| 48 | /// selection-statement: |
| 49 | /// if-statement |
| 50 | /// switch-statement |
| 51 | /// |
| 52 | /// iteration-statement: |
| 53 | /// while-statement |
| 54 | /// do-statement |
| 55 | /// for-statement |
| 56 | /// |
| 57 | /// expression-statement: |
| 58 | /// expression[opt] ';' |
| 59 | /// |
| 60 | /// jump-statement: |
| 61 | /// 'goto' identifier ';' |
| 62 | /// 'continue' ';' |
| 63 | /// 'break' ';' |
| 64 | /// 'return' expression[opt] ';' |
| 65 | /// [GNU] 'goto' '*' expression ';' |
| 66 | /// |
| 67 | /// [OBC] objc-throw-statement: [TODO] |
| 68 | /// [OBC] '@' 'throw' expression ';' [TODO] |
| 69 | /// [OBC] '@' 'throw' ';' [TODO] |
| 70 | /// |
| 71 | Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) { |
| 72 | const char *SemiError = 0; |
| 73 | Parser::StmtResult Res; |
| 74 | |
| 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. |
| 78 | switch (Tok.getKind()) { |
| 79 | case tok::identifier: // C99 6.8.1: labeled-statement |
| 80 | // identifier ':' statement |
| 81 | // declaration (if !OnlyStatement) |
| 82 | // expression[opt] ';' |
| 83 | return ParseIdentifierStatement(OnlyStatement); |
| 84 | |
| 85 | default: |
| 86 | if (!OnlyStatement && isDeclarationSpecifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | return Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext)); |
| 88 | } else if (Tok.getKind() == tok::r_brace) { |
| 89 | Diag(Tok, diag::err_expected_statement); |
| 90 | return true; |
| 91 | } else { |
| 92 | // expression[opt] ';' |
| 93 | ExprResult Res = ParseExpression(); |
| 94 | if (Res.isInvalid) { |
| 95 | // If the expression is invalid, skip ahead to the next semicolon. Not |
| 96 | // doing this opens us up to the possibility of infinite loops if |
| 97 | // ParseExpression does not consume any tokens. |
| 98 | SkipUntil(tok::semi); |
| 99 | return true; |
| 100 | } |
| 101 | // Otherwise, eat the semicolon. |
| 102 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
| 103 | return Actions.ParseExprStmt(Res.Val); |
| 104 | } |
| 105 | |
| 106 | case tok::kw_case: // C99 6.8.1: labeled-statement |
| 107 | return ParseCaseStatement(); |
| 108 | case tok::kw_default: // C99 6.8.1: labeled-statement |
| 109 | return ParseDefaultStatement(); |
| 110 | |
| 111 | case tok::l_brace: // C99 6.8.2: compound-statement |
| 112 | return ParseCompoundStatement(); |
| 113 | case tok::semi: // C99 6.8.3p3: expression[opt] ';' |
| 114 | return Actions.ParseNullStmt(ConsumeToken()); |
| 115 | |
| 116 | case tok::kw_if: // C99 6.8.4.1: if-statement |
| 117 | return ParseIfStatement(); |
| 118 | case tok::kw_switch: // C99 6.8.4.2: switch-statement |
| 119 | return ParseSwitchStatement(); |
| 120 | |
| 121 | case tok::kw_while: // C99 6.8.5.1: while-statement |
| 122 | return ParseWhileStatement(); |
| 123 | case tok::kw_do: // C99 6.8.5.2: do-statement |
| 124 | Res = ParseDoStatement(); |
| 125 | SemiError = "do/while loop"; |
| 126 | break; |
| 127 | case tok::kw_for: // C99 6.8.5.3: for-statement |
| 128 | return ParseForStatement(); |
| 129 | |
| 130 | case tok::kw_goto: // C99 6.8.6.1: goto-statement |
| 131 | Res = ParseGotoStatement(); |
| 132 | SemiError = "goto statement"; |
| 133 | break; |
| 134 | case tok::kw_continue: // C99 6.8.6.2: continue-statement |
| 135 | Res = ParseContinueStatement(); |
| 136 | SemiError = "continue statement"; |
| 137 | break; |
| 138 | case tok::kw_break: // C99 6.8.6.3: break-statement |
| 139 | Res = ParseBreakStatement(); |
| 140 | SemiError = "break statement"; |
| 141 | break; |
| 142 | case tok::kw_return: // C99 6.8.6.4: return-statement |
| 143 | Res = ParseReturnStatement(); |
| 144 | SemiError = "return statement"; |
| 145 | break; |
| 146 | |
| 147 | case tok::kw_asm: |
| 148 | Res = ParseAsmStatement(); |
| 149 | SemiError = "asm statement"; |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | // If we reached this code, the statement must end in a semicolon. |
| 154 | if (Tok.getKind() == tok::semi) { |
| 155 | ConsumeToken(); |
| 156 | } else { |
| 157 | Diag(Tok, diag::err_expected_semi_after, SemiError); |
| 158 | SkipUntil(tok::semi); |
| 159 | } |
| 160 | return Res; |
| 161 | } |
| 162 | |
| 163 | /// ParseIdentifierStatement - Because we don't have two-token lookahead, we |
| 164 | /// have a bit of a quandry here. Reading the identifier is necessary to see if |
| 165 | /// there is a ':' after it. If there is, this is a label, regardless of what |
| 166 | /// else the identifier can mean. If not, this is either part of a declaration |
| 167 | /// (if the identifier is a type-name) or part of an expression. |
| 168 | /// |
| 169 | /// labeled-statement: |
| 170 | /// identifier ':' statement |
| 171 | /// [GNU] identifier ':' attributes[opt] statement |
| 172 | /// declaration (if !OnlyStatement) |
| 173 | /// expression[opt] ';' |
| 174 | /// |
| 175 | Parser::StmtResult Parser::ParseIdentifierStatement(bool OnlyStatement) { |
| 176 | assert(Tok.getKind() == tok::identifier && Tok.getIdentifierInfo() && |
| 177 | "Not an identifier!"); |
| 178 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 179 | Token IdentTok = Tok; // Save the whole token. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | ConsumeToken(); // eat the identifier. |
| 181 | |
| 182 | // identifier ':' statement |
| 183 | if (Tok.getKind() == tok::colon) { |
| 184 | SourceLocation ColonLoc = ConsumeToken(); |
| 185 | |
| 186 | // Read label attributes, if present. |
| 187 | DeclTy *AttrList = 0; |
| 188 | if (Tok.getKind() == tok::kw___attribute) |
| 189 | // TODO: save these somewhere. |
| 190 | AttrList = ParseAttributes(); |
| 191 | |
| 192 | StmtResult SubStmt = ParseStatement(); |
| 193 | |
| 194 | // Broken substmt shouldn't prevent the label from being added to the AST. |
| 195 | if (SubStmt.isInvalid) |
| 196 | SubStmt = Actions.ParseNullStmt(ColonLoc); |
| 197 | |
| 198 | return Actions.ParseLabelStmt(IdentTok.getLocation(), |
| 199 | IdentTok.getIdentifierInfo(), |
| 200 | ColonLoc, SubStmt.Val); |
| 201 | } |
| 202 | |
| 203 | // Check to see if this is a declaration. |
| 204 | void *TypeRep; |
| 205 | if (!OnlyStatement && |
| 206 | (TypeRep = Actions.isTypeName(*IdentTok.getIdentifierInfo(), CurScope))) { |
| 207 | // Handle this. Warn/disable if in middle of block and !C99. |
| 208 | DeclSpec DS; |
| 209 | |
| 210 | // Add the typedef name to the start of the decl-specs. |
| 211 | const char *PrevSpec = 0; |
| 212 | int isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, |
| 213 | IdentTok.getLocation(), PrevSpec, |
| 214 | TypeRep); |
| 215 | assert(!isInvalid && "First declspec can't be invalid!"); |
| 216 | |
| 217 | // ParseDeclarationSpecifiers will continue from there. |
| 218 | ParseDeclarationSpecifiers(DS); |
| 219 | |
| 220 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 221 | // declaration-specifiers init-declarator-list[opt] ';' |
| 222 | if (Tok.getKind() == tok::semi) { |
| 223 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 224 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 225 | |
| 226 | ConsumeToken(); |
| 227 | // FIXME: Return this as a type decl. |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | // Parse all the declarators. |
| 232 | Declarator DeclaratorInfo(DS, Declarator::BlockContext); |
| 233 | ParseDeclarator(DeclaratorInfo); |
| 234 | |
| 235 | DeclTy *Decl = ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 236 | return Decl ? Actions.ParseDeclStmt(Decl) : 0; |
| 237 | } |
| 238 | |
| 239 | // Otherwise, this is an expression. Seed it with II and parse it. |
| 240 | ExprResult Res = ParseExpressionWithLeadingIdentifier(IdentTok); |
| 241 | if (Res.isInvalid) { |
| 242 | SkipUntil(tok::semi); |
| 243 | return true; |
| 244 | } else if (Tok.getKind() != tok::semi) { |
| 245 | Diag(Tok, diag::err_expected_semi_after, "expression"); |
| 246 | SkipUntil(tok::semi); |
| 247 | return true; |
| 248 | } else { |
| 249 | ConsumeToken(); |
| 250 | // Convert expr to a stmt. |
| 251 | return Actions.ParseExprStmt(Res.Val); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /// ParseCaseStatement |
| 256 | /// labeled-statement: |
| 257 | /// 'case' constant-expression ':' statement |
| 258 | /// [GNU] 'case' constant-expression '...' constant-expression ':' statement |
| 259 | /// |
| 260 | /// Note that this does not parse the 'statement' at the end. |
| 261 | /// |
| 262 | Parser::StmtResult Parser::ParseCaseStatement() { |
| 263 | assert(Tok.getKind() == tok::kw_case && "Not a case stmt!"); |
| 264 | SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'. |
| 265 | |
| 266 | ExprResult LHS = ParseConstantExpression(); |
| 267 | if (LHS.isInvalid) { |
| 268 | SkipUntil(tok::colon); |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | // GNU case range extension. |
| 273 | SourceLocation DotDotDotLoc; |
| 274 | ExprTy *RHSVal = 0; |
| 275 | if (Tok.getKind() == tok::ellipsis) { |
| 276 | Diag(Tok, diag::ext_gnu_case_range); |
| 277 | DotDotDotLoc = ConsumeToken(); |
| 278 | |
| 279 | ExprResult RHS = ParseConstantExpression(); |
| 280 | if (RHS.isInvalid) { |
| 281 | SkipUntil(tok::colon); |
| 282 | return true; |
| 283 | } |
| 284 | RHSVal = RHS.Val; |
| 285 | } |
| 286 | |
| 287 | if (Tok.getKind() != tok::colon) { |
| 288 | Diag(Tok, diag::err_expected_colon_after, "'case'"); |
| 289 | SkipUntil(tok::colon); |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | SourceLocation ColonLoc = ConsumeToken(); |
| 294 | |
| 295 | // Diagnose the common error "switch (X) { case 4: }", which is not valid. |
| 296 | if (Tok.getKind() == tok::r_brace) { |
| 297 | Diag(Tok, diag::err_label_end_of_compound_statement); |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | StmtResult SubStmt = ParseStatement(); |
| 302 | |
| 303 | // Broken substmt shouldn't prevent the case from being added to the AST. |
| 304 | if (SubStmt.isInvalid) |
| 305 | SubStmt = Actions.ParseNullStmt(ColonLoc); |
| 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | return Actions.ParseCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc, |
| 308 | SubStmt.Val); |
| 309 | } |
| 310 | |
| 311 | /// ParseDefaultStatement |
| 312 | /// labeled-statement: |
| 313 | /// 'default' ':' statement |
| 314 | /// Note that this does not parse the 'statement' at the end. |
| 315 | /// |
| 316 | Parser::StmtResult Parser::ParseDefaultStatement() { |
| 317 | assert(Tok.getKind() == tok::kw_default && "Not a default stmt!"); |
| 318 | SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. |
| 319 | |
| 320 | if (Tok.getKind() != tok::colon) { |
| 321 | Diag(Tok, diag::err_expected_colon_after, "'default'"); |
| 322 | SkipUntil(tok::colon); |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | SourceLocation ColonLoc = ConsumeToken(); |
| 327 | |
| 328 | // Diagnose the common error "switch (X) {... default: }", which is not valid. |
| 329 | if (Tok.getKind() == tok::r_brace) { |
| 330 | Diag(Tok, diag::err_label_end_of_compound_statement); |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | StmtResult SubStmt = ParseStatement(); |
| 335 | if (SubStmt.isInvalid) |
| 336 | return true; |
| 337 | |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 338 | return Actions.ParseDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
| 342 | /// ParseCompoundStatement - Parse a "{}" block. |
| 343 | /// |
| 344 | /// compound-statement: [C99 6.8.2] |
| 345 | /// { block-item-list[opt] } |
| 346 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 347 | /// |
| 348 | /// block-item-list: |
| 349 | /// block-item |
| 350 | /// block-item-list block-item |
| 351 | /// |
| 352 | /// block-item: |
| 353 | /// declaration |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 354 | /// [GNU] '__extension__' declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | /// statement |
| 356 | /// [OMP] openmp-directive [TODO] |
| 357 | /// |
| 358 | /// [GNU] label-declarations: |
| 359 | /// [GNU] label-declaration |
| 360 | /// [GNU] label-declarations label-declaration |
| 361 | /// |
| 362 | /// [GNU] label-declaration: |
| 363 | /// [GNU] '__label__' identifier-list ';' |
| 364 | /// |
| 365 | /// [OMP] openmp-directive: [TODO] |
| 366 | /// [OMP] barrier-directive |
| 367 | /// [OMP] flush-directive |
| 368 | /// |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame^] | 369 | Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!"); |
| 371 | |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 372 | // Enter a scope to hold everything within the compound stmt. Compound |
| 373 | // statements can always hold declarations. |
| 374 | EnterScope(Scope::DeclScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | |
| 376 | // Parse the statements in the body. |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame^] | 377 | StmtResult Body = ParseCompoundStatementBody(isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 378 | |
| 379 | ExitScope(); |
| 380 | return Body; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the |
| 385 | /// ParseCompoundStmt action. This expects the '{' to be the current token, and |
| 386 | /// consume the '}' at the end of the block. It does not manipulate the scope |
| 387 | /// stack. |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame^] | 388 | Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'. |
| 390 | |
| 391 | // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 392 | // only allowed at the start of a compound stmt regardless of the language. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | |
| 394 | llvm::SmallVector<StmtTy*, 32> Stmts; |
| 395 | while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) { |
Chris Lattner | 45a566c | 2007-08-27 01:01:57 +0000 | [diff] [blame] | 396 | StmtResult R; |
| 397 | if (Tok.getKind() != tok::kw___extension__) { |
| 398 | R = ParseStatementOrDeclaration(false); |
| 399 | } else { |
| 400 | // __extension__ can start declarations and it can also be a unary |
| 401 | // operator for expressions. Consume multiple __extension__ markers here |
| 402 | // until we can determine which is which. |
| 403 | SourceLocation ExtLoc = ConsumeToken(); |
| 404 | while (Tok.getKind() == tok::kw___extension__) |
| 405 | ConsumeToken(); |
| 406 | |
| 407 | // If this is the start of a declaration, parse it as such. |
| 408 | if (isDeclarationSpecifier()) { |
| 409 | // FIXME: Save the __extension__ on the decl as a node somehow. |
| 410 | // FIXME: disable extwarns. |
| 411 | R = Actions.ParseDeclStmt(ParseDeclaration(Declarator::BlockContext)); |
| 412 | } else { |
| 413 | // Otherwise this was a unary __extension__ marker. Parse the |
| 414 | // subexpression and add the __extension__ unary op. |
| 415 | // FIXME: disable extwarns. |
| 416 | ExprResult Res = ParseCastExpression(false); |
| 417 | if (Res.isInvalid) { |
| 418 | SkipUntil(tok::semi); |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | // Add the __extension__ node to the AST. |
| 423 | Res = Actions.ParseUnaryOp(ExtLoc, tok::kw___extension__, Res.Val); |
| 424 | if (Res.isInvalid) |
| 425 | continue; |
| 426 | |
| 427 | // Eat the semicolon at the end of stmt and convert the expr into a stmt. |
| 428 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); |
| 429 | R = Actions.ParseExprStmt(Res.Val); |
| 430 | } |
| 431 | } |
| 432 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 433 | if (!R.isInvalid && R.Val) |
| 434 | Stmts.push_back(R.Val); |
| 435 | } |
| 436 | |
| 437 | // We broke out of the while loop because we found a '}' or EOF. |
| 438 | if (Tok.getKind() != tok::r_brace) { |
| 439 | Diag(Tok, diag::err_expected_rbrace); |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | SourceLocation RBraceLoc = ConsumeBrace(); |
| 444 | return Actions.ParseCompoundStmt(LBraceLoc, RBraceLoc, |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame^] | 445 | &Stmts[0], Stmts.size(), isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /// ParseIfStatement |
| 449 | /// if-statement: [C99 6.8.4.1] |
| 450 | /// 'if' '(' expression ')' statement |
| 451 | /// 'if' '(' expression ')' statement 'else' statement |
| 452 | /// |
| 453 | Parser::StmtResult Parser::ParseIfStatement() { |
| 454 | assert(Tok.getKind() == tok::kw_if && "Not an if stmt!"); |
| 455 | SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. |
| 456 | |
| 457 | if (Tok.getKind() != tok::l_paren) { |
| 458 | Diag(Tok, diag::err_expected_lparen_after, "if"); |
| 459 | SkipUntil(tok::semi); |
| 460 | return true; |
| 461 | } |
| 462 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 463 | // C99 6.8.4p3 - In C99, the if statement is a block. This is not |
| 464 | // the case for C90. |
| 465 | if (getLang().C99) |
| 466 | EnterScope(Scope::DeclScope); |
| 467 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | // Parse the condition. |
| 469 | ExprResult CondExp = ParseSimpleParenExpression(); |
| 470 | if (CondExp.isInvalid) { |
| 471 | SkipUntil(tok::semi); |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 472 | if (getLang().C99) |
| 473 | ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 474 | return true; |
| 475 | } |
| 476 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 477 | // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 478 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 479 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 480 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 481 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 482 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 483 | // Read the if condition. |
| 484 | StmtResult CondStmt = ParseStatement(); |
| 485 | |
| 486 | // Broken substmt shouldn't prevent the label from being added to the AST. |
| 487 | if (CondStmt.isInvalid) |
| 488 | CondStmt = Actions.ParseNullStmt(Tok.getLocation()); |
| 489 | |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 490 | // Pop the 'if' scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 491 | if (NeedsInnerScope) ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 492 | |
| 493 | // If it has an else, parse it. |
| 494 | SourceLocation ElseLoc; |
| 495 | StmtResult ElseStmt(false); |
| 496 | if (Tok.getKind() == tok::kw_else) { |
| 497 | ElseLoc = ConsumeToken(); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 499 | // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 500 | // there is no compound stmt. C90 does not have this clause. We only do |
| 501 | // this if the body isn't a compound statement to avoid push/pop in common |
| 502 | // cases. |
| 503 | NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 504 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 505 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | ElseStmt = ParseStatement(); |
Chris Lattner | a36ce71 | 2007-08-22 05:16:28 +0000 | [diff] [blame] | 507 | |
| 508 | // Pop the 'else' scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 509 | if (NeedsInnerScope) ExitScope(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | |
| 511 | if (ElseStmt.isInvalid) |
| 512 | ElseStmt = Actions.ParseNullStmt(ElseLoc); |
| 513 | } |
| 514 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 515 | if (getLang().C99) |
| 516 | ExitScope(); |
| 517 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 518 | return Actions.ParseIfStmt(IfLoc, CondExp.Val, CondStmt.Val, |
| 519 | ElseLoc, ElseStmt.Val); |
| 520 | } |
| 521 | |
| 522 | /// ParseSwitchStatement |
| 523 | /// switch-statement: |
| 524 | /// 'switch' '(' expression ')' statement |
| 525 | Parser::StmtResult Parser::ParseSwitchStatement() { |
| 526 | assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!"); |
| 527 | SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. |
| 528 | |
| 529 | if (Tok.getKind() != tok::l_paren) { |
| 530 | Diag(Tok, diag::err_expected_lparen_after, "switch"); |
| 531 | SkipUntil(tok::semi); |
| 532 | return true; |
| 533 | } |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 534 | |
| 535 | // C99 6.8.4p3 - In C99, the switch statement is a block. This is |
| 536 | // not the case for C90. Start the switch scope. |
| 537 | if (getLang().C99) |
| 538 | EnterScope(Scope::BreakScope|Scope::DeclScope); |
| 539 | else |
| 540 | EnterScope(Scope::BreakScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | |
| 542 | // Parse the condition. |
| 543 | ExprResult Cond = ParseSimpleParenExpression(); |
| 544 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 545 | if (Cond.isInvalid) { |
| 546 | ExitScope(); |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | StmtResult Switch = Actions.StartSwitchStmt(Cond.Val); |
| 551 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 552 | // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 553 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 554 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 555 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 556 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 557 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 558 | // Read the body statement. |
| 559 | StmtResult Body = ParseStatement(); |
| 560 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 561 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 562 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 563 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 564 | if (Body.isInvalid) { |
| 565 | Body = Actions.ParseNullStmt(Tok.getLocation()); |
| 566 | // FIXME: Remove the case statement list from the Switch statement. |
| 567 | } |
| 568 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | ExitScope(); |
| 570 | |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 571 | return Actions.FinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /// ParseWhileStatement |
| 575 | /// while-statement: [C99 6.8.5.1] |
| 576 | /// 'while' '(' expression ')' statement |
| 577 | Parser::StmtResult Parser::ParseWhileStatement() { |
| 578 | assert(Tok.getKind() == tok::kw_while && "Not a while stmt!"); |
| 579 | SourceLocation WhileLoc = Tok.getLocation(); |
| 580 | ConsumeToken(); // eat the 'while'. |
| 581 | |
| 582 | if (Tok.getKind() != tok::l_paren) { |
| 583 | Diag(Tok, diag::err_expected_lparen_after, "while"); |
| 584 | SkipUntil(tok::semi); |
| 585 | return true; |
| 586 | } |
| 587 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 588 | // C99 6.8.5p5 - In C99, the while statement is a block. This is not |
| 589 | // the case for C90. Start the loop scope. |
| 590 | if (getLang().C99) |
| 591 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 592 | else |
| 593 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | |
| 595 | // Parse the condition. |
| 596 | ExprResult Cond = ParseSimpleParenExpression(); |
| 597 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 598 | // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 599 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 600 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 601 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 602 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 603 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | // Read the body statement. |
| 605 | StmtResult Body = ParseStatement(); |
| 606 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 607 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 608 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 609 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 610 | ExitScope(); |
| 611 | |
| 612 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 613 | |
| 614 | return Actions.ParseWhileStmt(WhileLoc, Cond.Val, Body.Val); |
| 615 | } |
| 616 | |
| 617 | /// ParseDoStatement |
| 618 | /// do-statement: [C99 6.8.5.2] |
| 619 | /// 'do' statement 'while' '(' expression ')' ';' |
| 620 | /// Note: this lets the caller parse the end ';'. |
| 621 | Parser::StmtResult Parser::ParseDoStatement() { |
| 622 | assert(Tok.getKind() == tok::kw_do && "Not a do stmt!"); |
| 623 | SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. |
| 624 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 625 | // C99 6.8.5p5 - In C99, the do statement is a block. This is not |
| 626 | // the case for C90. Start the loop scope. |
| 627 | if (getLang().C99) |
| 628 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 629 | else |
| 630 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 632 | // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 633 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 634 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 635 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 636 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 637 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 638 | // Read the body statement. |
| 639 | StmtResult Body = ParseStatement(); |
| 640 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 641 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 642 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 643 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 644 | if (Tok.getKind() != tok::kw_while) { |
| 645 | ExitScope(); |
| 646 | Diag(Tok, diag::err_expected_while); |
| 647 | Diag(DoLoc, diag::err_matching, "do"); |
| 648 | SkipUntil(tok::semi); |
| 649 | return true; |
| 650 | } |
| 651 | SourceLocation WhileLoc = ConsumeToken(); |
| 652 | |
| 653 | if (Tok.getKind() != tok::l_paren) { |
| 654 | ExitScope(); |
| 655 | Diag(Tok, diag::err_expected_lparen_after, "do/while"); |
| 656 | SkipUntil(tok::semi); |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | // Parse the condition. |
| 661 | ExprResult Cond = ParseSimpleParenExpression(); |
| 662 | |
| 663 | ExitScope(); |
| 664 | |
| 665 | if (Cond.isInvalid || Body.isInvalid) return true; |
| 666 | |
| 667 | return Actions.ParseDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val); |
| 668 | } |
| 669 | |
| 670 | /// ParseForStatement |
| 671 | /// for-statement: [C99 6.8.5.3] |
| 672 | /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement |
| 673 | /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement |
| 674 | Parser::StmtResult Parser::ParseForStatement() { |
| 675 | assert(Tok.getKind() == tok::kw_for && "Not a for stmt!"); |
| 676 | SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. |
| 677 | |
| 678 | if (Tok.getKind() != tok::l_paren) { |
| 679 | Diag(Tok, diag::err_expected_lparen_after, "for"); |
| 680 | SkipUntil(tok::semi); |
| 681 | return true; |
| 682 | } |
| 683 | |
Chris Lattner | 2215325 | 2007-08-26 23:08:06 +0000 | [diff] [blame] | 684 | // C99 6.8.5p5 - In C99, the for statement is a block. This is not |
| 685 | // the case for C90. Start the loop scope. |
| 686 | if (getLang().C99) |
| 687 | EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope); |
| 688 | else |
| 689 | EnterScope(Scope::BreakScope | Scope::ContinueScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 690 | |
| 691 | SourceLocation LParenLoc = ConsumeParen(); |
| 692 | ExprResult Value; |
| 693 | |
| 694 | StmtTy *FirstPart = 0; |
| 695 | ExprTy *SecondPart = 0; |
| 696 | StmtTy *ThirdPart = 0; |
| 697 | |
| 698 | // Parse the first part of the for specifier. |
| 699 | if (Tok.getKind() == tok::semi) { // for (; |
| 700 | // no first part, eat the ';'. |
| 701 | ConsumeToken(); |
| 702 | } else if (isDeclarationSpecifier()) { // for (int X = 4; |
| 703 | // Parse declaration, which eats the ';'. |
| 704 | if (!getLang().C99) // Use of C99-style for loops in C90 mode? |
| 705 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
| 706 | DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext); |
| 707 | StmtResult stmtResult = Actions.ParseDeclStmt(aBlockVarDecl); |
| 708 | FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val; |
| 709 | } else { |
| 710 | Value = ParseExpression(); |
| 711 | |
| 712 | // Turn the expression into a stmt. |
| 713 | if (!Value.isInvalid) { |
| 714 | StmtResult R = Actions.ParseExprStmt(Value.Val); |
| 715 | if (!R.isInvalid) |
| 716 | FirstPart = R.Val; |
| 717 | } |
| 718 | |
| 719 | if (Tok.getKind() == tok::semi) { |
| 720 | ConsumeToken(); |
| 721 | } else { |
| 722 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 723 | SkipUntil(tok::semi); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // Parse the second part of the for specifier. |
| 728 | if (Tok.getKind() == tok::semi) { // for (...;; |
| 729 | // no second part. |
| 730 | Value = ExprResult(); |
| 731 | } else { |
| 732 | Value = ParseExpression(); |
| 733 | if (!Value.isInvalid) |
| 734 | SecondPart = Value.Val; |
| 735 | } |
| 736 | |
| 737 | if (Tok.getKind() == tok::semi) { |
| 738 | ConsumeToken(); |
| 739 | } else { |
| 740 | if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for); |
| 741 | SkipUntil(tok::semi); |
| 742 | } |
| 743 | |
| 744 | // Parse the third part of the for specifier. |
| 745 | if (Tok.getKind() == tok::r_paren) { // for (...;...;) |
| 746 | // no third part. |
| 747 | Value = ExprResult(); |
| 748 | } else { |
| 749 | Value = ParseExpression(); |
| 750 | if (!Value.isInvalid) { |
| 751 | // Turn the expression into a stmt. |
| 752 | StmtResult R = Actions.ParseExprStmt(Value.Val); |
| 753 | if (!R.isInvalid) |
| 754 | ThirdPart = R.Val; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // Match the ')'. |
| 759 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 760 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 761 | // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 762 | // there is no compound stmt. C90 does not have this clause. We only do this |
| 763 | // if the body isn't a compound statement to avoid push/pop in common cases. |
| 764 | bool NeedsInnerScope = getLang().C99 && Tok.getKind() != tok::l_brace; |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 765 | if (NeedsInnerScope) EnterScope(Scope::DeclScope); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 767 | // Read the body statement. |
| 768 | StmtResult Body = ParseStatement(); |
| 769 | |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 770 | // Pop the body scope if needed. |
Chris Lattner | 3848440 | 2007-08-22 05:33:11 +0000 | [diff] [blame] | 771 | if (NeedsInnerScope) ExitScope(); |
Chris Lattner | 0ecea03 | 2007-08-22 05:28:50 +0000 | [diff] [blame] | 772 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 773 | // Leave the for-scope. |
| 774 | ExitScope(); |
| 775 | |
| 776 | if (Body.isInvalid) |
| 777 | return Body; |
| 778 | |
| 779 | return Actions.ParseForStmt(ForLoc, LParenLoc, FirstPart, SecondPart, |
| 780 | ThirdPart, RParenLoc, Body.Val); |
| 781 | } |
| 782 | |
| 783 | /// ParseGotoStatement |
| 784 | /// jump-statement: |
| 785 | /// 'goto' identifier ';' |
| 786 | /// [GNU] 'goto' '*' expression ';' |
| 787 | /// |
| 788 | /// Note: this lets the caller parse the end ';'. |
| 789 | /// |
| 790 | Parser::StmtResult Parser::ParseGotoStatement() { |
| 791 | assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!"); |
| 792 | SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. |
| 793 | |
| 794 | StmtResult Res; |
| 795 | if (Tok.getKind() == tok::identifier) { |
| 796 | Res = Actions.ParseGotoStmt(GotoLoc, Tok.getLocation(), |
| 797 | Tok.getIdentifierInfo()); |
| 798 | ConsumeToken(); |
| 799 | } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) { |
| 800 | // GNU indirect goto extension. |
| 801 | Diag(Tok, diag::ext_gnu_indirect_goto); |
| 802 | SourceLocation StarLoc = ConsumeToken(); |
| 803 | ExprResult R = ParseExpression(); |
| 804 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 805 | SkipUntil(tok::semi, false, true); |
| 806 | return true; |
| 807 | } |
| 808 | Res = Actions.ParseIndirectGotoStmt(GotoLoc, StarLoc, R.Val); |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 809 | } else { |
| 810 | Diag(Tok, diag::err_expected_ident); |
| 811 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 812 | } |
Chris Lattner | 95cfb85 | 2007-07-22 04:13:33 +0000 | [diff] [blame] | 813 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 814 | return Res; |
| 815 | } |
| 816 | |
| 817 | /// ParseContinueStatement |
| 818 | /// jump-statement: |
| 819 | /// 'continue' ';' |
| 820 | /// |
| 821 | /// Note: this lets the caller parse the end ';'. |
| 822 | /// |
| 823 | Parser::StmtResult Parser::ParseContinueStatement() { |
| 824 | SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. |
| 825 | return Actions.ParseContinueStmt(ContinueLoc, CurScope); |
| 826 | } |
| 827 | |
| 828 | /// ParseBreakStatement |
| 829 | /// jump-statement: |
| 830 | /// 'break' ';' |
| 831 | /// |
| 832 | /// Note: this lets the caller parse the end ';'. |
| 833 | /// |
| 834 | Parser::StmtResult Parser::ParseBreakStatement() { |
| 835 | SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. |
| 836 | return Actions.ParseBreakStmt(BreakLoc, CurScope); |
| 837 | } |
| 838 | |
| 839 | /// ParseReturnStatement |
| 840 | /// jump-statement: |
| 841 | /// 'return' expression[opt] ';' |
| 842 | Parser::StmtResult Parser::ParseReturnStatement() { |
| 843 | assert(Tok.getKind() == tok::kw_return && "Not a return stmt!"); |
| 844 | SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. |
| 845 | |
| 846 | ExprResult R(0); |
| 847 | if (Tok.getKind() != tok::semi) { |
| 848 | R = ParseExpression(); |
| 849 | if (R.isInvalid) { // Skip to the semicolon, but don't consume it. |
| 850 | SkipUntil(tok::semi, false, true); |
| 851 | return true; |
| 852 | } |
| 853 | } |
| 854 | return Actions.ParseReturnStmt(ReturnLoc, R.Val); |
| 855 | } |
| 856 | |
| 857 | /// ParseAsmStatement - Parse a GNU extended asm statement. |
| 858 | /// [GNU] asm-statement: |
| 859 | /// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' |
| 860 | /// |
| 861 | /// [GNU] asm-argument: |
| 862 | /// asm-string-literal |
| 863 | /// asm-string-literal ':' asm-operands[opt] |
| 864 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 865 | /// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] |
| 866 | /// ':' asm-clobbers |
| 867 | /// |
| 868 | /// [GNU] asm-clobbers: |
| 869 | /// asm-string-literal |
| 870 | /// asm-clobbers ',' asm-string-literal |
| 871 | /// |
| 872 | Parser::StmtResult Parser::ParseAsmStatement() { |
| 873 | assert(Tok.getKind() == tok::kw_asm && "Not an asm stmt"); |
| 874 | ConsumeToken(); |
| 875 | |
| 876 | DeclSpec DS; |
| 877 | SourceLocation Loc = Tok.getLocation(); |
| 878 | ParseTypeQualifierListOpt(DS); |
| 879 | |
| 880 | // GNU asms accept, but warn, about type-qualifiers other than volatile. |
| 881 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 882 | Diag(Loc, diag::w_asm_qualifier_ignored, "const"); |
| 883 | if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) |
| 884 | Diag(Loc, diag::w_asm_qualifier_ignored, "restrict"); |
| 885 | |
| 886 | // Remember if this was a volatile asm. |
| 887 | //bool isVolatile = DS.TypeQualifiers & DeclSpec::TQ_volatile; |
| 888 | |
| 889 | if (Tok.getKind() != tok::l_paren) { |
| 890 | Diag(Tok, diag::err_expected_lparen_after, "asm"); |
| 891 | SkipUntil(tok::r_paren); |
| 892 | return true; |
| 893 | } |
| 894 | Loc = ConsumeParen(); |
| 895 | |
| 896 | ParseAsmStringLiteral(); |
| 897 | |
| 898 | // Parse Outputs, if present. |
| 899 | ParseAsmOperandsOpt(); |
| 900 | |
| 901 | // Parse Inputs, if present. |
| 902 | ParseAsmOperandsOpt(); |
| 903 | |
| 904 | // Parse the clobbers, if present. |
| 905 | if (Tok.getKind() == tok::colon) { |
| 906 | ConsumeToken(); |
| 907 | |
| 908 | if (isTokenStringLiteral()) { |
| 909 | // Parse the asm-string list for clobbers. |
| 910 | while (1) { |
| 911 | ParseAsmStringLiteral(); |
| 912 | |
| 913 | if (Tok.getKind() != tok::comma) break; |
| 914 | ConsumeToken(); |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | MatchRHSPunctuation(tok::r_paren, Loc); |
| 920 | |
| 921 | // FIXME: Implement action for asm parsing. |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | /// ParseAsmOperands - Parse the asm-operands production as used by |
| 926 | /// asm-statement. We also parse a leading ':' token. If the leading colon is |
| 927 | /// not present, we do not parse anything. |
| 928 | /// |
| 929 | /// [GNU] asm-operands: |
| 930 | /// asm-operand |
| 931 | /// asm-operands ',' asm-operand |
| 932 | /// |
| 933 | /// [GNU] asm-operand: |
| 934 | /// asm-string-literal '(' expression ')' |
| 935 | /// '[' identifier ']' asm-string-literal '(' expression ')' |
| 936 | /// |
| 937 | void Parser::ParseAsmOperandsOpt() { |
| 938 | // Only do anything if this operand is present. |
| 939 | if (Tok.getKind() != tok::colon) return; |
| 940 | ConsumeToken(); |
| 941 | |
| 942 | // 'asm-operands' isn't present? |
| 943 | if (!isTokenStringLiteral() && Tok.getKind() != tok::l_square) |
| 944 | return; |
| 945 | |
| 946 | while (1) { |
| 947 | // Read the [id] if present. |
| 948 | if (Tok.getKind() == tok::l_square) { |
| 949 | SourceLocation Loc = ConsumeBracket(); |
| 950 | |
| 951 | if (Tok.getKind() != tok::identifier) { |
| 952 | Diag(Tok, diag::err_expected_ident); |
| 953 | SkipUntil(tok::r_paren); |
| 954 | return; |
| 955 | } |
| 956 | MatchRHSPunctuation(tok::r_square, Loc); |
| 957 | } |
| 958 | |
| 959 | ParseAsmStringLiteral(); |
| 960 | |
| 961 | if (Tok.getKind() != tok::l_paren) { |
| 962 | Diag(Tok, diag::err_expected_lparen_after, "asm operand"); |
| 963 | SkipUntil(tok::r_paren); |
| 964 | return; |
| 965 | } |
| 966 | |
| 967 | // Read the parenthesized expression. |
| 968 | ExprResult Res = ParseSimpleParenExpression(); |
| 969 | if (Res.isInvalid) { |
| 970 | SkipUntil(tok::r_paren); |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | // Eat the comma and continue parsing if it exists. |
| 975 | if (Tok.getKind() != tok::comma) return; |
| 976 | ConsumeToken(); |
| 977 | } |
| 978 | } |