ParseStmt.cpp revision cd1477562e7cf03279850885583d615e1f631dd4
1//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 "ExtensionRAIIObject.h" 17#include "AstGuard.h" 18#include "clang/Parse/DeclSpec.h" 19#include "clang/Parse/Scope.h" 20#include "clang/Basic/Diagnostic.h" 21#include "clang/Basic/PrettyStackTrace.h" 22#include "clang/Basic/SourceManager.h" 23using namespace clang; 24 25//===----------------------------------------------------------------------===// 26// C99 6.8: Statements and Blocks. 27//===----------------------------------------------------------------------===// 28 29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. 30/// StatementOrDeclaration: 31/// statement 32/// declaration 33/// 34/// statement: 35/// labeled-statement 36/// compound-statement 37/// expression-statement 38/// selection-statement 39/// iteration-statement 40/// jump-statement 41/// [C++] declaration-statement 42/// [C++] try-block 43/// [OBC] objc-throw-statement 44/// [OBC] objc-try-catch-statement 45/// [OBC] objc-synchronized-statement 46/// [GNU] asm-statement 47/// [OMP] openmp-construct [TODO] 48/// 49/// labeled-statement: 50/// identifier ':' statement 51/// 'case' constant-expression ':' statement 52/// 'default' ':' statement 53/// 54/// selection-statement: 55/// if-statement 56/// switch-statement 57/// 58/// iteration-statement: 59/// while-statement 60/// do-statement 61/// for-statement 62/// 63/// expression-statement: 64/// expression[opt] ';' 65/// 66/// jump-statement: 67/// 'goto' identifier ';' 68/// 'continue' ';' 69/// 'break' ';' 70/// 'return' expression[opt] ';' 71/// [GNU] 'goto' '*' expression ';' 72/// 73/// [OBC] objc-throw-statement: 74/// [OBC] '@' 'throw' expression ';' 75/// [OBC] '@' 'throw' ';' 76/// 77Parser::OwningStmtResult 78Parser::ParseStatementOrDeclaration(bool OnlyStatement) { 79 const char *SemiError = 0; 80 OwningStmtResult Res(Actions); 81 82 // Cases in this switch statement should fall through if the parser expects 83 // the token to end in a semicolon (in which case SemiError should be set), 84 // or they directly 'return;' if not. 85 tok::TokenKind Kind = Tok.getKind(); 86 SourceLocation AtLoc; 87 switch (Kind) { 88 case tok::at: // May be a @try or @throw statement 89 { 90 AtLoc = ConsumeToken(); // consume @ 91 return ParseObjCAtStatement(AtLoc); 92 } 93 94 case tok::identifier: 95 if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement 96 // identifier ':' statement 97 return ParseLabeledStatement(); 98 } 99 // PASS THROUGH. 100 101 default: { 102 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) { 103 SourceLocation DeclStart = Tok.getLocation(); 104 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext); 105 // FIXME: Pass in the right location for the end of the declstmt. 106 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart); 107 } 108 109 if (Tok.is(tok::r_brace)) { 110 Diag(Tok, diag::err_expected_statement); 111 return StmtError(); 112 } 113 114 // expression[opt] ';' 115 OwningExprResult Expr(ParseExpression()); 116 if (Expr.isInvalid()) { 117 // If the expression is invalid, skip ahead to the next semicolon. Not 118 // doing this opens us up to the possibility of infinite loops if 119 // ParseExpression does not consume any tokens. 120 SkipUntil(tok::semi); 121 return StmtError(); 122 } 123 // Otherwise, eat the semicolon. 124 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); 125 return Actions.ActOnExprStmt(move(Expr)); 126 } 127 128 case tok::kw_case: // C99 6.8.1: labeled-statement 129 return ParseCaseStatement(); 130 case tok::kw_default: // C99 6.8.1: labeled-statement 131 return ParseDefaultStatement(); 132 133 case tok::l_brace: // C99 6.8.2: compound-statement 134 return ParseCompoundStatement(); 135 case tok::semi: // C99 6.8.3p3: expression[opt] ';' 136 return Actions.ActOnNullStmt(ConsumeToken()); 137 138 case tok::kw_if: // C99 6.8.4.1: if-statement 139 return ParseIfStatement(); 140 case tok::kw_switch: // C99 6.8.4.2: switch-statement 141 return ParseSwitchStatement(); 142 143 case tok::kw_while: // C99 6.8.5.1: while-statement 144 return ParseWhileStatement(); 145 case tok::kw_do: // C99 6.8.5.2: do-statement 146 Res = ParseDoStatement(); 147 SemiError = "do/while loop"; 148 break; 149 case tok::kw_for: // C99 6.8.5.3: for-statement 150 return ParseForStatement(); 151 152 case tok::kw_goto: // C99 6.8.6.1: goto-statement 153 Res = ParseGotoStatement(); 154 SemiError = "goto statement"; 155 break; 156 case tok::kw_continue: // C99 6.8.6.2: continue-statement 157 Res = ParseContinueStatement(); 158 SemiError = "continue statement"; 159 break; 160 case tok::kw_break: // C99 6.8.6.3: break-statement 161 Res = ParseBreakStatement(); 162 SemiError = "break statement"; 163 break; 164 case tok::kw_return: // C99 6.8.6.4: return-statement 165 Res = ParseReturnStatement(); 166 SemiError = "return statement"; 167 break; 168 169 case tok::kw_asm: { 170 bool msAsm = false; 171 Res = ParseAsmStatement(msAsm); 172 if (msAsm) return move(Res); 173 SemiError = "asm statement"; 174 break; 175 } 176 177 case tok::kw_try: // C++ 15: try-block 178 return ParseCXXTryBlock(); 179 } 180 181 // If we reached this code, the statement must end in a semicolon. 182 if (Tok.is(tok::semi)) { 183 ConsumeToken(); 184 } else if (!Res.isInvalid()) { 185 Diag(Tok, diag::err_expected_semi_after) << SemiError; 186 // Skip until we see a } or ;, but don't eat it. 187 SkipUntil(tok::r_brace, true, true); 188 } 189 return move(Res); 190} 191 192/// ParseLabeledStatement - We have an identifier and a ':' after it. 193/// 194/// labeled-statement: 195/// identifier ':' statement 196/// [GNU] identifier ':' attributes[opt] statement 197/// 198Parser::OwningStmtResult Parser::ParseLabeledStatement() { 199 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && 200 "Not an identifier!"); 201 202 Token IdentTok = Tok; // Save the whole token. 203 ConsumeToken(); // eat the identifier. 204 205 assert(Tok.is(tok::colon) && "Not a label!"); 206 207 // identifier ':' statement 208 SourceLocation ColonLoc = ConsumeToken(); 209 210 // Read label attributes, if present. 211 Action::AttrTy *AttrList = 0; 212 if (Tok.is(tok::kw___attribute)) 213 // TODO: save these somewhere. 214 AttrList = ParseAttributes(); 215 216 OwningStmtResult SubStmt(ParseStatement()); 217 218 // Broken substmt shouldn't prevent the label from being added to the AST. 219 if (SubStmt.isInvalid()) 220 SubStmt = Actions.ActOnNullStmt(ColonLoc); 221 222 return Actions.ActOnLabelStmt(IdentTok.getLocation(), 223 IdentTok.getIdentifierInfo(), 224 ColonLoc, move(SubStmt)); 225} 226 227/// ParseCaseStatement 228/// labeled-statement: 229/// 'case' constant-expression ':' statement 230/// [GNU] 'case' constant-expression '...' constant-expression ':' statement 231/// 232Parser::OwningStmtResult Parser::ParseCaseStatement() { 233 assert(Tok.is(tok::kw_case) && "Not a case stmt!"); 234 235 // It is very very common for code to contain many case statements recursively 236 // nested, as in (but usually without indentation): 237 // case 1: 238 // case 2: 239 // case 3: 240 // case 4: 241 // case 5: etc. 242 // 243 // Parsing this naively works, but is both inefficient and can cause us to run 244 // out of stack space in our recursive descent parser. As a special case, 245 // flatten this recursion into an iterative loop. This is complex and gross, 246 // but all the grossness is constrained to ParseCaseStatement (and some 247 // wierdness in the actions), so this is just local grossness :). 248 249 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the 250 // example above. 251 OwningStmtResult TopLevelCase(Actions, true); 252 253 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which 254 // gets updated each time a new case is parsed, and whose body is unset so 255 // far. When parsing 'case 4', this is the 'case 3' node. 256 StmtTy *DeepestParsedCaseStmt = 0; 257 258 // While we have case statements, eat and stack them. 259 do { 260 SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'. 261 262 OwningExprResult LHS(ParseConstantExpression()); 263 if (LHS.isInvalid()) { 264 SkipUntil(tok::colon); 265 return StmtError(); 266 } 267 268 // GNU case range extension. 269 SourceLocation DotDotDotLoc; 270 OwningExprResult RHS(Actions); 271 if (Tok.is(tok::ellipsis)) { 272 Diag(Tok, diag::ext_gnu_case_range); 273 DotDotDotLoc = ConsumeToken(); 274 275 RHS = ParseConstantExpression(); 276 if (RHS.isInvalid()) { 277 SkipUntil(tok::colon); 278 return StmtError(); 279 } 280 } 281 282 if (Tok.isNot(tok::colon)) { 283 Diag(Tok, diag::err_expected_colon_after) << "'case'"; 284 SkipUntil(tok::colon); 285 return StmtError(); 286 } 287 288 SourceLocation ColonLoc = ConsumeToken(); 289 290 OwningStmtResult Case = 291 Actions.ActOnCaseStmt(CaseLoc, move(LHS), DotDotDotLoc, 292 move(RHS), ColonLoc); 293 294 // If we had a sema error parsing this case, then just ignore it and 295 // continue parsing the sub-stmt. 296 if (Case.isInvalid()) { 297 if (TopLevelCase.isInvalid()) // No parsed case stmts. 298 return ParseStatement(); 299 // Otherwise, just don't add it as a nested case. 300 } else { 301 // If this is the first case statement we parsed, it becomes TopLevelCase. 302 // Otherwise we link it into the current chain. 303 StmtTy *NextDeepest = Case.get(); 304 if (TopLevelCase.isInvalid()) 305 TopLevelCase = move(Case); 306 else 307 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(Case)); 308 DeepestParsedCaseStmt = NextDeepest; 309 } 310 311 // Handle all case statements. 312 } while (Tok.is(tok::kw_case)); 313 314 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!"); 315 316 // If we found a non-case statement, start by parsing it. 317 OwningStmtResult SubStmt(Actions); 318 319 if (Tok.isNot(tok::r_brace)) { 320 SubStmt = ParseStatement(); 321 } else { 322 // Nicely diagnose the common error "switch (X) { case 4: }", which is 323 // not valid. 324 // FIXME: add insertion hint. 325 Diag(Tok, diag::err_label_end_of_compound_statement); 326 SubStmt = true; 327 } 328 329 // Broken sub-stmt shouldn't prevent forming the case statement properly. 330 if (SubStmt.isInvalid()) 331 SubStmt = Actions.ActOnNullStmt(SourceLocation()); 332 333 // Install the body into the most deeply-nested case. 334 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, move(SubStmt)); 335 336 // Return the top level parsed statement tree. 337 return move(TopLevelCase); 338} 339 340/// ParseDefaultStatement 341/// labeled-statement: 342/// 'default' ':' statement 343/// Note that this does not parse the 'statement' at the end. 344/// 345Parser::OwningStmtResult Parser::ParseDefaultStatement() { 346 assert(Tok.is(tok::kw_default) && "Not a default stmt!"); 347 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'. 348 349 if (Tok.isNot(tok::colon)) { 350 Diag(Tok, diag::err_expected_colon_after) << "'default'"; 351 SkipUntil(tok::colon); 352 return StmtError(); 353 } 354 355 SourceLocation ColonLoc = ConsumeToken(); 356 357 // Diagnose the common error "switch (X) {... default: }", which is not valid. 358 if (Tok.is(tok::r_brace)) { 359 Diag(Tok, diag::err_label_end_of_compound_statement); 360 return StmtError(); 361 } 362 363 OwningStmtResult SubStmt(ParseStatement()); 364 if (SubStmt.isInvalid()) 365 return StmtError(); 366 367 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, 368 move(SubStmt), CurScope); 369} 370 371 372/// ParseCompoundStatement - Parse a "{}" block. 373/// 374/// compound-statement: [C99 6.8.2] 375/// { block-item-list[opt] } 376/// [GNU] { label-declarations block-item-list } [TODO] 377/// 378/// block-item-list: 379/// block-item 380/// block-item-list block-item 381/// 382/// block-item: 383/// declaration 384/// [GNU] '__extension__' declaration 385/// statement 386/// [OMP] openmp-directive [TODO] 387/// 388/// [GNU] label-declarations: 389/// [GNU] label-declaration 390/// [GNU] label-declarations label-declaration 391/// 392/// [GNU] label-declaration: 393/// [GNU] '__label__' identifier-list ';' 394/// 395/// [OMP] openmp-directive: [TODO] 396/// [OMP] barrier-directive 397/// [OMP] flush-directive 398/// 399Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { 400 assert(Tok.is(tok::l_brace) && "Not a compount stmt!"); 401 402 // Enter a scope to hold everything within the compound stmt. Compound 403 // statements can always hold declarations. 404 ParseScope CompoundScope(this, Scope::DeclScope); 405 406 // Parse the statements in the body. 407 return ParseCompoundStatementBody(isStmtExpr); 408} 409 410 411/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the 412/// ActOnCompoundStmt action. This expects the '{' to be the current token, and 413/// consume the '}' at the end of the block. It does not manipulate the scope 414/// stack. 415Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { 416 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), 417 Tok.getLocation(), 418 "in compound statement ('{}')"); 419 420 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'. 421 422 // TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are 423 // only allowed at the start of a compound stmt regardless of the language. 424 425 typedef StmtVector StmtsTy; 426 StmtsTy Stmts(Actions); 427 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 428 OwningStmtResult R(Actions); 429 if (Tok.isNot(tok::kw___extension__)) { 430 R = ParseStatementOrDeclaration(false); 431 } else { 432 // __extension__ can start declarations and it can also be a unary 433 // operator for expressions. Consume multiple __extension__ markers here 434 // until we can determine which is which. 435 // FIXME: This loses extension expressions in the AST! 436 SourceLocation ExtLoc = ConsumeToken(); 437 while (Tok.is(tok::kw___extension__)) 438 ConsumeToken(); 439 440 // __extension__ silences extension warnings in the subexpression. 441 ExtensionRAIIObject O(Diags); // Use RAII to do this. 442 443 // If this is the start of a declaration, parse it as such. 444 if (isDeclarationStatement()) { 445 // FIXME: Save the __extension__ on the decl as a node somehow. 446 SourceLocation DeclStart = Tok.getLocation(); 447 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext); 448 // FIXME: Pass in the right location for the end of the declstmt. 449 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart); 450 } else { 451 // Otherwise this was a unary __extension__ marker. 452 OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc)); 453 454 if (Res.isInvalid()) { 455 SkipUntil(tok::semi); 456 continue; 457 } 458 459 // Eat the semicolon at the end of stmt and convert the expr into a 460 // statement. 461 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); 462 R = Actions.ActOnExprStmt(move(Res)); 463 } 464 } 465 466 if (R.isUsable()) 467 Stmts.push_back(R.release()); 468 } 469 470 // We broke out of the while loop because we found a '}' or EOF. 471 if (Tok.isNot(tok::r_brace)) { 472 Diag(Tok, diag::err_expected_rbrace); 473 return StmtError(); 474 } 475 476 SourceLocation RBraceLoc = ConsumeBrace(); 477 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts), 478 isStmtExpr); 479} 480 481/// ParseParenExprOrCondition: 482/// [C ] '(' expression ')' 483/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true] 484/// 485/// This function parses and performs error recovery on the specified condition 486/// or expression (depending on whether we're in C++ or C mode). This function 487/// goes out of its way to recover well. It returns true if there was a parser 488/// error (the right paren couldn't be found), which indicates that the caller 489/// should try to recover harder. It returns false if the condition is 490/// successfully parsed. Note that a successful parse can still have semantic 491/// errors in the condition. 492bool Parser::ParseParenExprOrCondition(OwningExprResult &CondExp, 493 bool OnlyAllowCondition) { 494 SourceLocation LParenLoc = ConsumeParen(); 495 496 if (getLang().CPlusPlus) 497 CondExp = ParseCXXCondition(); 498 else 499 CondExp = ParseExpression(); 500 501 // If the parser was confused by the condition and we don't have a ')', try to 502 // recover by skipping ahead to a semi and bailing out. If condexp is 503 // semantically invalid but we have well formed code, keep going. 504 if (CondExp.isInvalid() && Tok.isNot(tok::r_paren)) { 505 SkipUntil(tok::semi); 506 // Skipping may have stopped if it found the containing ')'. If so, we can 507 // continue parsing the if statement. 508 if (Tok.isNot(tok::r_paren)) 509 return true; 510 } 511 512 // Otherwise the condition is valid or the rparen is present. 513 MatchRHSPunctuation(tok::r_paren, LParenLoc); 514 return false; 515} 516 517 518/// ParseIfStatement 519/// if-statement: [C99 6.8.4.1] 520/// 'if' '(' expression ')' statement 521/// 'if' '(' expression ')' statement 'else' statement 522/// [C++] 'if' '(' condition ')' statement 523/// [C++] 'if' '(' condition ')' statement 'else' statement 524/// 525Parser::OwningStmtResult Parser::ParseIfStatement() { 526 assert(Tok.is(tok::kw_if) && "Not an if stmt!"); 527 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'. 528 529 if (Tok.isNot(tok::l_paren)) { 530 Diag(Tok, diag::err_expected_lparen_after) << "if"; 531 SkipUntil(tok::semi); 532 return StmtError(); 533 } 534 535 bool C99orCXX = getLang().C99 || getLang().CPlusPlus; 536 537 // C99 6.8.4p3 - In C99, the if statement is a block. This is not 538 // the case for C90. 539 // 540 // C++ 6.4p3: 541 // A name introduced by a declaration in a condition is in scope from its 542 // point of declaration until the end of the substatements controlled by the 543 // condition. 544 // C++ 3.3.2p4: 545 // Names declared in the for-init-statement, and in the condition of if, 546 // while, for, and switch statements are local to the if, while, for, or 547 // switch statement (including the controlled statement). 548 // 549 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX); 550 551 // Parse the condition. 552 OwningExprResult CondExp(Actions); 553 if (ParseParenExprOrCondition(CondExp)) 554 return StmtError(); 555 556 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if 557 // there is no compound stmt. C90 does not have this clause. We only do this 558 // if the body isn't a compound statement to avoid push/pop in common cases. 559 // 560 // C++ 6.4p1: 561 // The substatement in a selection-statement (each substatement, in the else 562 // form of the if statement) implicitly defines a local scope. 563 // 564 // For C++ we create a scope for the condition and a new scope for 565 // substatements because: 566 // -When the 'then' scope exits, we want the condition declaration to still be 567 // active for the 'else' scope too. 568 // -Sema will detect name clashes by considering declarations of a 569 // 'ControlScope' as part of its direct subscope. 570 // -If we wanted the condition and substatement to be in the same scope, we 571 // would have to notify ParseStatement not to create a new scope. It's 572 // simpler to let it create a new scope. 573 // 574 ParseScope InnerScope(this, Scope::DeclScope, 575 C99orCXX && Tok.isNot(tok::l_brace)); 576 577 // Read the 'then' stmt. 578 SourceLocation ThenStmtLoc = Tok.getLocation(); 579 OwningStmtResult ThenStmt(ParseStatement()); 580 581 // Pop the 'if' scope if needed. 582 InnerScope.Exit(); 583 584 // If it has an else, parse it. 585 SourceLocation ElseLoc; 586 SourceLocation ElseStmtLoc; 587 OwningStmtResult ElseStmt(Actions); 588 589 if (Tok.is(tok::kw_else)) { 590 ElseLoc = ConsumeToken(); 591 592 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if 593 // there is no compound stmt. C90 does not have this clause. We only do 594 // this if the body isn't a compound statement to avoid push/pop in common 595 // cases. 596 // 597 // C++ 6.4p1: 598 // The substatement in a selection-statement (each substatement, in the else 599 // form of the if statement) implicitly defines a local scope. 600 // 601 ParseScope InnerScope(this, Scope::DeclScope, 602 C99orCXX && Tok.isNot(tok::l_brace)); 603 604 bool WithinElse = CurScope->isWithinElse(); 605 CurScope->setWithinElse(true); 606 ElseStmtLoc = Tok.getLocation(); 607 ElseStmt = ParseStatement(); 608 CurScope->setWithinElse(WithinElse); 609 610 // Pop the 'else' scope if needed. 611 InnerScope.Exit(); 612 } 613 614 IfScope.Exit(); 615 616 // If the condition was invalid, discard the if statement. We could recover 617 // better by replacing it with a valid expr, but don't do that yet. 618 if (CondExp.isInvalid()) 619 return StmtError(); 620 621 // If the then or else stmt is invalid and the other is valid (and present), 622 // make turn the invalid one into a null stmt to avoid dropping the other 623 // part. If both are invalid, return error. 624 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) || 625 (ThenStmt.isInvalid() && ElseStmt.get() == 0) || 626 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) { 627 // Both invalid, or one is invalid and other is non-present: return error. 628 return StmtError(); 629 } 630 631 // Now if either are invalid, replace with a ';'. 632 if (ThenStmt.isInvalid()) 633 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); 634 if (ElseStmt.isInvalid()) 635 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); 636 637 return Actions.ActOnIfStmt(IfLoc, move(CondExp), move(ThenStmt), 638 ElseLoc, move(ElseStmt)); 639} 640 641/// ParseSwitchStatement 642/// switch-statement: 643/// 'switch' '(' expression ')' statement 644/// [C++] 'switch' '(' condition ')' statement 645Parser::OwningStmtResult Parser::ParseSwitchStatement() { 646 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); 647 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'. 648 649 if (Tok.isNot(tok::l_paren)) { 650 Diag(Tok, diag::err_expected_lparen_after) << "switch"; 651 SkipUntil(tok::semi); 652 return StmtError(); 653 } 654 655 bool C99orCXX = getLang().C99 || getLang().CPlusPlus; 656 657 // C99 6.8.4p3 - In C99, the switch statement is a block. This is 658 // not the case for C90. Start the switch scope. 659 // 660 // C++ 6.4p3: 661 // A name introduced by a declaration in a condition is in scope from its 662 // point of declaration until the end of the substatements controlled by the 663 // condition. 664 // C++ 3.3.2p4: 665 // Names declared in the for-init-statement, and in the condition of if, 666 // while, for, and switch statements are local to the if, while, for, or 667 // switch statement (including the controlled statement). 668 // 669 unsigned ScopeFlags = Scope::BreakScope; 670 if (C99orCXX) 671 ScopeFlags |= Scope::DeclScope | Scope::ControlScope; 672 ParseScope SwitchScope(this, ScopeFlags); 673 674 // Parse the condition. 675 OwningExprResult Cond(Actions); 676 if (ParseParenExprOrCondition(Cond)) 677 return StmtError(); 678 679 OwningStmtResult Switch(Actions); 680 if (!Cond.isInvalid()) 681 Switch = Actions.ActOnStartOfSwitchStmt(move(Cond)); 682 683 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if 684 // there is no compound stmt. C90 does not have this clause. We only do this 685 // if the body isn't a compound statement to avoid push/pop in common cases. 686 // 687 // C++ 6.4p1: 688 // The substatement in a selection-statement (each substatement, in the else 689 // form of the if statement) implicitly defines a local scope. 690 // 691 // See comments in ParseIfStatement for why we create a scope for the 692 // condition and a new scope for substatement in C++. 693 // 694 ParseScope InnerScope(this, Scope::DeclScope, 695 C99orCXX && Tok.isNot(tok::l_brace)); 696 697 // Read the body statement. 698 OwningStmtResult Body(ParseStatement()); 699 700 // Pop the body scope if needed. 701 InnerScope.Exit(); 702 703 if (Body.isInvalid()) { 704 Body = Actions.ActOnNullStmt(Tok.getLocation()); 705 // FIXME: Remove the case statement list from the Switch statement. 706 } 707 708 SwitchScope.Exit(); 709 710 if (Cond.isInvalid()) 711 return StmtError(); 712 713 return Actions.ActOnFinishSwitchStmt(SwitchLoc, move(Switch), move(Body)); 714} 715 716/// ParseWhileStatement 717/// while-statement: [C99 6.8.5.1] 718/// 'while' '(' expression ')' statement 719/// [C++] 'while' '(' condition ')' statement 720Parser::OwningStmtResult Parser::ParseWhileStatement() { 721 assert(Tok.is(tok::kw_while) && "Not a while stmt!"); 722 SourceLocation WhileLoc = Tok.getLocation(); 723 ConsumeToken(); // eat the 'while'. 724 725 if (Tok.isNot(tok::l_paren)) { 726 Diag(Tok, diag::err_expected_lparen_after) << "while"; 727 SkipUntil(tok::semi); 728 return StmtError(); 729 } 730 731 bool C99orCXX = getLang().C99 || getLang().CPlusPlus; 732 733 // C99 6.8.5p5 - In C99, the while statement is a block. This is not 734 // the case for C90. Start the loop scope. 735 // 736 // C++ 6.4p3: 737 // A name introduced by a declaration in a condition is in scope from its 738 // point of declaration until the end of the substatements controlled by the 739 // condition. 740 // C++ 3.3.2p4: 741 // Names declared in the for-init-statement, and in the condition of if, 742 // while, for, and switch statements are local to the if, while, for, or 743 // switch statement (including the controlled statement). 744 // 745 unsigned ScopeFlags; 746 if (C99orCXX) 747 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | 748 Scope::DeclScope | Scope::ControlScope; 749 else 750 ScopeFlags = Scope::BreakScope | Scope::ContinueScope; 751 ParseScope WhileScope(this, ScopeFlags); 752 753 // Parse the condition. 754 OwningExprResult Cond(Actions); 755 if (ParseParenExprOrCondition(Cond)) 756 return StmtError(); 757 758 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if 759 // there is no compound stmt. C90 does not have this clause. We only do this 760 // if the body isn't a compound statement to avoid push/pop in common cases. 761 // 762 // C++ 6.5p2: 763 // The substatement in an iteration-statement implicitly defines a local scope 764 // which is entered and exited each time through the loop. 765 // 766 // See comments in ParseIfStatement for why we create a scope for the 767 // condition and a new scope for substatement in C++. 768 // 769 ParseScope InnerScope(this, Scope::DeclScope, 770 C99orCXX && Tok.isNot(tok::l_brace)); 771 772 // Read the body statement. 773 OwningStmtResult Body(ParseStatement()); 774 775 // Pop the body scope if needed. 776 InnerScope.Exit(); 777 WhileScope.Exit(); 778 779 if (Cond.isInvalid() || Body.isInvalid()) 780 return StmtError(); 781 782 return Actions.ActOnWhileStmt(WhileLoc, move(Cond), move(Body)); 783} 784 785/// ParseDoStatement 786/// do-statement: [C99 6.8.5.2] 787/// 'do' statement 'while' '(' expression ')' ';' 788/// Note: this lets the caller parse the end ';'. 789Parser::OwningStmtResult Parser::ParseDoStatement() { 790 assert(Tok.is(tok::kw_do) && "Not a do stmt!"); 791 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'. 792 793 // C99 6.8.5p5 - In C99, the do statement is a block. This is not 794 // the case for C90. Start the loop scope. 795 unsigned ScopeFlags; 796 if (getLang().C99) 797 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope; 798 else 799 ScopeFlags = Scope::BreakScope | Scope::ContinueScope; 800 801 ParseScope DoScope(this, ScopeFlags); 802 803 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if 804 // there is no compound stmt. C90 does not have this clause. We only do this 805 // if the body isn't a compound statement to avoid push/pop in common cases. 806 // 807 // C++ 6.5p2: 808 // The substatement in an iteration-statement implicitly defines a local scope 809 // which is entered and exited each time through the loop. 810 // 811 ParseScope InnerScope(this, Scope::DeclScope, 812 (getLang().C99 || getLang().CPlusPlus) && 813 Tok.isNot(tok::l_brace)); 814 815 // Read the body statement. 816 OwningStmtResult Body(ParseStatement()); 817 818 // Pop the body scope if needed. 819 InnerScope.Exit(); 820 821 if (Tok.isNot(tok::kw_while)) { 822 if (!Body.isInvalid()) { 823 Diag(Tok, diag::err_expected_while); 824 Diag(DoLoc, diag::note_matching) << "do"; 825 SkipUntil(tok::semi, false, true); 826 } 827 return StmtError(); 828 } 829 SourceLocation WhileLoc = ConsumeToken(); 830 831 if (Tok.isNot(tok::l_paren)) { 832 Diag(Tok, diag::err_expected_lparen_after) << "do/while"; 833 SkipUntil(tok::semi, false, true); 834 return StmtError(); 835 } 836 837 // Parse the parenthesized condition. 838 OwningExprResult Cond(Actions); 839 ParseParenExprOrCondition(Cond, true); 840 841 DoScope.Exit(); 842 843 if (Cond.isInvalid() || Body.isInvalid()) 844 return StmtError(); 845 846 return Actions.ActOnDoStmt(DoLoc, move(Body), WhileLoc, move(Cond)); 847} 848 849/// ParseForStatement 850/// for-statement: [C99 6.8.5.3] 851/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement 852/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement 853/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')' 854/// [C++] statement 855/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement 856/// [OBJC2] 'for' '(' expr 'in' expr ')' statement 857/// 858/// [C++] for-init-statement: 859/// [C++] expression-statement 860/// [C++] simple-declaration 861/// 862Parser::OwningStmtResult Parser::ParseForStatement() { 863 assert(Tok.is(tok::kw_for) && "Not a for stmt!"); 864 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'. 865 866 if (Tok.isNot(tok::l_paren)) { 867 Diag(Tok, diag::err_expected_lparen_after) << "for"; 868 SkipUntil(tok::semi); 869 return StmtError(); 870 } 871 872 bool C99orCXX = getLang().C99 || getLang().CPlusPlus; 873 874 // C99 6.8.5p5 - In C99, the for statement is a block. This is not 875 // the case for C90. Start the loop scope. 876 // 877 // C++ 6.4p3: 878 // A name introduced by a declaration in a condition is in scope from its 879 // point of declaration until the end of the substatements controlled by the 880 // condition. 881 // C++ 3.3.2p4: 882 // Names declared in the for-init-statement, and in the condition of if, 883 // while, for, and switch statements are local to the if, while, for, or 884 // switch statement (including the controlled statement). 885 // C++ 6.5.3p1: 886 // Names declared in the for-init-statement are in the same declarative-region 887 // as those declared in the condition. 888 // 889 unsigned ScopeFlags; 890 if (C99orCXX) 891 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | 892 Scope::DeclScope | Scope::ControlScope; 893 else 894 ScopeFlags = Scope::BreakScope | Scope::ContinueScope; 895 896 ParseScope ForScope(this, ScopeFlags); 897 898 SourceLocation LParenLoc = ConsumeParen(); 899 OwningExprResult Value(Actions); 900 901 bool ForEach = false; 902 OwningStmtResult FirstPart(Actions); 903 OwningExprResult SecondPart(Actions), ThirdPart(Actions); 904 905 // Parse the first part of the for specifier. 906 if (Tok.is(tok::semi)) { // for (; 907 // no first part, eat the ';'. 908 ConsumeToken(); 909 } else if (isSimpleDeclaration()) { // for (int X = 4; 910 // Parse declaration, which eats the ';'. 911 if (!C99orCXX) // Use of C99-style for loops in C90 mode? 912 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); 913 914 SourceLocation DeclStart = Tok.getLocation(); 915 DeclGroupPtrTy DG = ParseSimpleDeclaration(Declarator::ForContext, false); 916 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); 917 918 if (Tok.is(tok::semi)) { // for (int x = 4; 919 ConsumeToken(); 920 } else if ((ForEach = isTokIdentifier_in())) { 921 // ObjC: for (id x in expr) 922 ConsumeToken(); // consume 'in' 923 SecondPart = ParseExpression(); 924 } else { 925 Diag(Tok, diag::err_expected_semi_for); 926 SkipUntil(tok::semi); 927 } 928 } else { 929 Value = ParseExpression(); 930 931 // Turn the expression into a stmt. 932 if (!Value.isInvalid()) 933 FirstPart = Actions.ActOnExprStmt(move(Value)); 934 935 if (Tok.is(tok::semi)) { 936 ConsumeToken(); 937 } else if ((ForEach = isTokIdentifier_in())) { 938 ConsumeToken(); // consume 'in' 939 SecondPart = ParseExpression(); 940 } else { 941 if (!Value.isInvalid()) Diag(Tok, diag::err_expected_semi_for); 942 SkipUntil(tok::semi); 943 } 944 } 945 if (!ForEach) { 946 assert(!SecondPart.get() && "Shouldn't have a second expression yet."); 947 // Parse the second part of the for specifier. 948 if (Tok.is(tok::semi)) { // for (...;; 949 // no second part. 950 } else { 951 SecondPart =getLang().CPlusPlus ? ParseCXXCondition() : ParseExpression(); 952 } 953 954 if (Tok.is(tok::semi)) { 955 ConsumeToken(); 956 } else { 957 if (!SecondPart.isInvalid()) Diag(Tok, diag::err_expected_semi_for); 958 SkipUntil(tok::semi); 959 } 960 961 // Parse the third part of the for specifier. 962 if (Tok.is(tok::r_paren)) { // for (...;...;) 963 // no third part. 964 } else { 965 ThirdPart = ParseExpression(); 966 } 967 } 968 // Match the ')'. 969 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 970 971 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if 972 // there is no compound stmt. C90 does not have this clause. We only do this 973 // if the body isn't a compound statement to avoid push/pop in common cases. 974 // 975 // C++ 6.5p2: 976 // The substatement in an iteration-statement implicitly defines a local scope 977 // which is entered and exited each time through the loop. 978 // 979 // See comments in ParseIfStatement for why we create a scope for 980 // for-init-statement/condition and a new scope for substatement in C++. 981 // 982 ParseScope InnerScope(this, Scope::DeclScope, 983 C99orCXX && Tok.isNot(tok::l_brace)); 984 985 // Read the body statement. 986 OwningStmtResult Body(ParseStatement()); 987 988 // Pop the body scope if needed. 989 InnerScope.Exit(); 990 991 // Leave the for-scope. 992 ForScope.Exit(); 993 994 if (Body.isInvalid()) 995 return StmtError(); 996 997 if (!ForEach) 998 return Actions.ActOnForStmt(ForLoc, LParenLoc, move(FirstPart), 999 move(SecondPart), move(ThirdPart), 1000 RParenLoc, move(Body)); 1001 1002 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, 1003 move(FirstPart), 1004 move(SecondPart), 1005 RParenLoc, move(Body)); 1006} 1007 1008/// ParseGotoStatement 1009/// jump-statement: 1010/// 'goto' identifier ';' 1011/// [GNU] 'goto' '*' expression ';' 1012/// 1013/// Note: this lets the caller parse the end ';'. 1014/// 1015Parser::OwningStmtResult Parser::ParseGotoStatement() { 1016 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); 1017 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'. 1018 1019 OwningStmtResult Res(Actions); 1020 if (Tok.is(tok::identifier)) { 1021 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), 1022 Tok.getIdentifierInfo()); 1023 ConsumeToken(); 1024 } else if (Tok.is(tok::star) && !getLang().NoExtensions) { 1025 // GNU indirect goto extension. 1026 Diag(Tok, diag::ext_gnu_indirect_goto); 1027 SourceLocation StarLoc = ConsumeToken(); 1028 OwningExprResult R(ParseExpression()); 1029 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. 1030 SkipUntil(tok::semi, false, true); 1031 return StmtError(); 1032 } 1033 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, move(R)); 1034 } else { 1035 Diag(Tok, diag::err_expected_ident); 1036 return StmtError(); 1037 } 1038 1039 return move(Res); 1040} 1041 1042/// ParseContinueStatement 1043/// jump-statement: 1044/// 'continue' ';' 1045/// 1046/// Note: this lets the caller parse the end ';'. 1047/// 1048Parser::OwningStmtResult Parser::ParseContinueStatement() { 1049 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'. 1050 return Actions.ActOnContinueStmt(ContinueLoc, CurScope); 1051} 1052 1053/// ParseBreakStatement 1054/// jump-statement: 1055/// 'break' ';' 1056/// 1057/// Note: this lets the caller parse the end ';'. 1058/// 1059Parser::OwningStmtResult Parser::ParseBreakStatement() { 1060 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'. 1061 return Actions.ActOnBreakStmt(BreakLoc, CurScope); 1062} 1063 1064/// ParseReturnStatement 1065/// jump-statement: 1066/// 'return' expression[opt] ';' 1067Parser::OwningStmtResult Parser::ParseReturnStatement() { 1068 assert(Tok.is(tok::kw_return) && "Not a return stmt!"); 1069 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'. 1070 1071 OwningExprResult R(Actions); 1072 if (Tok.isNot(tok::semi)) { 1073 R = ParseExpression(); 1074 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it. 1075 SkipUntil(tok::semi, false, true); 1076 return StmtError(); 1077 } 1078 } 1079 return Actions.ActOnReturnStmt(ReturnLoc, move(R)); 1080} 1081 1082/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this 1083/// routine is called to skip/ignore tokens that comprise the MS asm statement. 1084Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() { 1085 if (Tok.is(tok::l_brace)) { 1086 unsigned short savedBraceCount = BraceCount; 1087 do { 1088 ConsumeAnyToken(); 1089 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof)); 1090 } else { 1091 // From the MS website: If used without braces, the __asm keyword means 1092 // that the rest of the line is an assembly-language statement. 1093 SourceManager &SrcMgr = PP.getSourceManager(); 1094 SourceLocation TokLoc = Tok.getLocation(); 1095 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc); 1096 do { 1097 ConsumeAnyToken(); 1098 TokLoc = Tok.getLocation(); 1099 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) && 1100 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) && 1101 Tok.isNot(tok::eof)); 1102 } 1103 return Actions.ActOnNullStmt(Tok.getLocation()); 1104} 1105 1106/// ParseAsmStatement - Parse a GNU extended asm statement. 1107/// asm-statement: 1108/// gnu-asm-statement 1109/// ms-asm-statement 1110/// 1111/// [GNU] gnu-asm-statement: 1112/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';' 1113/// 1114/// [GNU] asm-argument: 1115/// asm-string-literal 1116/// asm-string-literal ':' asm-operands[opt] 1117/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] 1118/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt] 1119/// ':' asm-clobbers 1120/// 1121/// [GNU] asm-clobbers: 1122/// asm-string-literal 1123/// asm-clobbers ',' asm-string-literal 1124/// 1125/// [MS] ms-asm-statement: 1126/// '__asm' assembly-instruction ';'[opt] 1127/// '__asm' '{' assembly-instruction-list '}' ';'[opt] 1128/// 1129/// [MS] assembly-instruction-list: 1130/// assembly-instruction ';'[opt] 1131/// assembly-instruction-list ';' assembly-instruction ';'[opt] 1132/// 1133Parser::OwningStmtResult Parser::ParseAsmStatement(bool &msAsm) { 1134 assert(Tok.is(tok::kw_asm) && "Not an asm stmt"); 1135 SourceLocation AsmLoc = ConsumeToken(); 1136 1137 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) { 1138 msAsm = true; 1139 return FuzzyParseMicrosoftAsmStatement(); 1140 } 1141 DeclSpec DS; 1142 SourceLocation Loc = Tok.getLocation(); 1143 ParseTypeQualifierListOpt(DS); 1144 1145 // GNU asms accept, but warn, about type-qualifiers other than volatile. 1146 if (DS.getTypeQualifiers() & DeclSpec::TQ_const) 1147 Diag(Loc, diag::w_asm_qualifier_ignored) << "const"; 1148 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict) 1149 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict"; 1150 1151 // Remember if this was a volatile asm. 1152 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile; 1153 bool isSimple = false; 1154 if (Tok.isNot(tok::l_paren)) { 1155 Diag(Tok, diag::err_expected_lparen_after) << "asm"; 1156 SkipUntil(tok::r_paren); 1157 return StmtError(); 1158 } 1159 Loc = ConsumeParen(); 1160 1161 OwningExprResult AsmString(ParseAsmStringLiteral()); 1162 if (AsmString.isInvalid()) 1163 return StmtError(); 1164 1165 llvm::SmallVector<std::string, 4> Names; 1166 ExprVector Constraints(Actions); 1167 ExprVector Exprs(Actions); 1168 ExprVector Clobbers(Actions); 1169 1170 unsigned NumInputs = 0, NumOutputs = 0; 1171 1172 SourceLocation RParenLoc; 1173 if (Tok.is(tok::r_paren)) { 1174 // We have a simple asm expression 1175 isSimple = true; 1176 1177 RParenLoc = ConsumeParen(); 1178 } else { 1179 // Parse Outputs, if present. 1180 if (ParseAsmOperandsOpt(Names, Constraints, Exprs)) 1181 return StmtError(); 1182 1183 NumOutputs = Names.size(); 1184 1185 // Parse Inputs, if present. 1186 if (ParseAsmOperandsOpt(Names, Constraints, Exprs)) 1187 return StmtError(); 1188 1189 assert(Names.size() == Constraints.size() && 1190 Constraints.size() == Exprs.size() 1191 && "Input operand size mismatch!"); 1192 1193 NumInputs = Names.size() - NumOutputs; 1194 1195 // Parse the clobbers, if present. 1196 if (Tok.is(tok::colon)) { 1197 ConsumeToken(); 1198 1199 // Parse the asm-string list for clobbers. 1200 while (1) { 1201 OwningExprResult Clobber(ParseAsmStringLiteral()); 1202 1203 if (Clobber.isInvalid()) 1204 break; 1205 1206 Clobbers.push_back(Clobber.release()); 1207 1208 if (Tok.isNot(tok::comma)) break; 1209 ConsumeToken(); 1210 } 1211 } 1212 1213 RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc); 1214 } 1215 1216 return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile, 1217 NumOutputs, NumInputs, &Names[0], 1218 move_arg(Constraints), move_arg(Exprs), 1219 move(AsmString), move_arg(Clobbers), 1220 RParenLoc); 1221} 1222 1223/// ParseAsmOperands - Parse the asm-operands production as used by 1224/// asm-statement. We also parse a leading ':' token. If the leading colon is 1225/// not present, we do not parse anything. 1226/// 1227/// [GNU] asm-operands: 1228/// asm-operand 1229/// asm-operands ',' asm-operand 1230/// 1231/// [GNU] asm-operand: 1232/// asm-string-literal '(' expression ')' 1233/// '[' identifier ']' asm-string-literal '(' expression ')' 1234/// 1235bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names, 1236 llvm::SmallVectorImpl<ExprTy*> &Constraints, 1237 llvm::SmallVectorImpl<ExprTy*> &Exprs) { 1238 // Only do anything if this operand is present. 1239 if (Tok.isNot(tok::colon)) return false; 1240 ConsumeToken(); 1241 1242 // 'asm-operands' isn't present? 1243 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square)) 1244 return false; 1245 1246 while (1) { 1247 // Read the [id] if present. 1248 if (Tok.is(tok::l_square)) { 1249 SourceLocation Loc = ConsumeBracket(); 1250 1251 if (Tok.isNot(tok::identifier)) { 1252 Diag(Tok, diag::err_expected_ident); 1253 SkipUntil(tok::r_paren); 1254 return true; 1255 } 1256 1257 IdentifierInfo *II = Tok.getIdentifierInfo(); 1258 ConsumeToken(); 1259 1260 Names.push_back(std::string(II->getName(), II->getLength())); 1261 MatchRHSPunctuation(tok::r_square, Loc); 1262 } else 1263 Names.push_back(std::string()); 1264 1265 OwningExprResult Constraint(ParseAsmStringLiteral()); 1266 if (Constraint.isInvalid()) { 1267 SkipUntil(tok::r_paren); 1268 return true; 1269 } 1270 Constraints.push_back(Constraint.release()); 1271 1272 if (Tok.isNot(tok::l_paren)) { 1273 Diag(Tok, diag::err_expected_lparen_after) << "asm operand"; 1274 SkipUntil(tok::r_paren); 1275 return true; 1276 } 1277 1278 // Read the parenthesized expression. 1279 OwningExprResult Res(ParseSimpleParenExpression()); 1280 if (Res.isInvalid()) { 1281 SkipUntil(tok::r_paren); 1282 return true; 1283 } 1284 Exprs.push_back(Res.release()); 1285 // Eat the comma and continue parsing if it exists. 1286 if (Tok.isNot(tok::comma)) return false; 1287 ConsumeToken(); 1288 } 1289 1290 return true; 1291} 1292 1293Parser::DeclPtrTy Parser::ParseFunctionStatementBody(DeclPtrTy Decl) { 1294 assert(Tok.is(tok::l_brace)); 1295 SourceLocation LBraceLoc = Tok.getLocation(); 1296 1297 PrettyStackTraceActionsDecl CrashInfo(Decl, LBraceLoc, Actions, 1298 PP.getSourceManager(), 1299 "parsing function body"); 1300 1301 // Do not enter a scope for the brace, as the arguments are in the same scope 1302 // (the function body) as the body itself. Instead, just read the statement 1303 // list and put it into a CompoundStmt for safe keeping. 1304 OwningStmtResult FnBody(ParseCompoundStatementBody()); 1305 1306 // If the function body could not be parsed, make a bogus compoundstmt. 1307 if (FnBody.isInvalid()) 1308 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, 1309 MultiStmtArg(Actions), false); 1310 1311 return Actions.ActOnFinishFunctionBody(Decl, move(FnBody)); 1312} 1313 1314/// ParseCXXTryBlock - Parse a C++ try-block. 1315/// 1316/// try-block: 1317/// 'try' compound-statement handler-seq 1318/// 1319/// handler-seq: 1320/// handler handler-seq[opt] 1321/// 1322Parser::OwningStmtResult Parser::ParseCXXTryBlock() { 1323 assert(Tok.is(tok::kw_try) && "Expected 'try'"); 1324 1325 SourceLocation TryLoc = ConsumeToken(); 1326 if (Tok.isNot(tok::l_brace)) 1327 return StmtError(Diag(Tok, diag::err_expected_lbrace)); 1328 OwningStmtResult TryBlock(ParseCompoundStatement()); 1329 if (TryBlock.isInvalid()) 1330 return move(TryBlock); 1331 1332 StmtVector Handlers(Actions); 1333 if (Tok.isNot(tok::kw_catch)) 1334 return StmtError(Diag(Tok, diag::err_expected_catch)); 1335 while (Tok.is(tok::kw_catch)) { 1336 OwningStmtResult Handler(ParseCXXCatchBlock()); 1337 if (!Handler.isInvalid()) 1338 Handlers.push_back(Handler.release()); 1339 } 1340 // Don't bother creating the full statement if we don't have any usable 1341 // handlers. 1342 if (Handlers.empty()) 1343 return StmtError(); 1344 1345 return Actions.ActOnCXXTryBlock(TryLoc, move(TryBlock), move_arg(Handlers)); 1346} 1347 1348/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard 1349/// 1350/// handler: 1351/// 'catch' '(' exception-declaration ')' compound-statement 1352/// 1353/// exception-declaration: 1354/// type-specifier-seq declarator 1355/// type-specifier-seq abstract-declarator 1356/// type-specifier-seq 1357/// '...' 1358/// 1359Parser::OwningStmtResult Parser::ParseCXXCatchBlock() { 1360 assert(Tok.is(tok::kw_catch) && "Expected 'catch'"); 1361 1362 SourceLocation CatchLoc = ConsumeToken(); 1363 1364 SourceLocation LParenLoc = Tok.getLocation(); 1365 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) 1366 return StmtError(); 1367 1368 // C++ 3.3.2p3: 1369 // The name in a catch exception-declaration is local to the handler and 1370 // shall not be redeclared in the outermost block of the handler. 1371 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope); 1372 1373 // exception-declaration is equivalent to '...' or a parameter-declaration 1374 // without default arguments. 1375 DeclPtrTy ExceptionDecl; 1376 if (Tok.isNot(tok::ellipsis)) { 1377 DeclSpec DS; 1378 if (ParseCXXTypeSpecifierSeq(DS)) 1379 return StmtError(); 1380 Declarator ExDecl(DS, Declarator::CXXCatchContext); 1381 ParseDeclarator(ExDecl); 1382 ExceptionDecl = Actions.ActOnExceptionDeclarator(CurScope, ExDecl); 1383 } else 1384 ConsumeToken(); 1385 1386 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid()) 1387 return StmtError(); 1388 1389 if (Tok.isNot(tok::l_brace)) 1390 return StmtError(Diag(Tok, diag::err_expected_lbrace)); 1391 1392 OwningStmtResult Block(ParseCompoundStatement()); 1393 if (Block.isInvalid()) 1394 return move(Block); 1395 1396 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, move(Block)); 1397} 1398