ParseDeclCXX.cpp revision 15faa7fdfb496489dec9470aa5eb699b29ecdacc
1//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// 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 C++ Declaration portions of the Parser interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Parse/Parser.h" 15#include "clang/Basic/Diagnostic.h" 16#include "clang/Parse/DeclSpec.h" 17#include "clang/Parse/Scope.h" 18#include "AstGuard.h" 19using namespace clang; 20 21/// ParseNamespace - We know that the current token is a namespace keyword. This 22/// may either be a top level namespace or a block-level namespace alias. 23/// 24/// namespace-definition: [C++ 7.3: basic.namespace] 25/// named-namespace-definition 26/// unnamed-namespace-definition 27/// 28/// unnamed-namespace-definition: 29/// 'namespace' attributes[opt] '{' namespace-body '}' 30/// 31/// named-namespace-definition: 32/// original-namespace-definition 33/// extension-namespace-definition 34/// 35/// original-namespace-definition: 36/// 'namespace' identifier attributes[opt] '{' namespace-body '}' 37/// 38/// extension-namespace-definition: 39/// 'namespace' original-namespace-name '{' namespace-body '}' 40/// 41/// namespace-alias-definition: [C++ 7.3.2: namespace.alias] 42/// 'namespace' identifier '=' qualified-namespace-specifier ';' 43/// 44Parser::DeclTy *Parser::ParseNamespace(unsigned Context) { 45 assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); 46 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. 47 48 SourceLocation IdentLoc; 49 IdentifierInfo *Ident = 0; 50 51 if (Tok.is(tok::identifier)) { 52 Ident = Tok.getIdentifierInfo(); 53 IdentLoc = ConsumeToken(); // eat the identifier. 54 } 55 56 // Read label attributes, if present. 57 DeclTy *AttrList = 0; 58 if (Tok.is(tok::kw___attribute)) 59 // FIXME: save these somewhere. 60 AttrList = ParseAttributes(); 61 62 if (Tok.is(tok::equal)) { 63 // FIXME: Verify no attributes were present. 64 // FIXME: parse this. 65 } else if (Tok.is(tok::l_brace)) { 66 67 SourceLocation LBrace = ConsumeBrace(); 68 69 // Enter a scope for the namespace. 70 EnterScope(Scope::DeclScope); 71 72 DeclTy *NamespcDecl = 73 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace); 74 75 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) 76 ParseExternalDeclaration(); 77 78 // Leave the namespace scope. 79 ExitScope(); 80 81 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); 82 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace); 83 84 return NamespcDecl; 85 86 } else { 87 Diag(Tok, Ident ? diag::err_expected_lbrace : 88 diag::err_expected_ident_lbrace); 89 } 90 91 return 0; 92} 93 94/// ParseLinkage - We know that the current token is a string_literal 95/// and just before that, that extern was seen. 96/// 97/// linkage-specification: [C++ 7.5p2: dcl.link] 98/// 'extern' string-literal '{' declaration-seq[opt] '}' 99/// 'extern' string-literal declaration 100/// 101Parser::DeclTy *Parser::ParseLinkage(unsigned Context) { 102 assert(Tok.is(tok::string_literal) && "Not a string literal!"); 103 llvm::SmallVector<char, 8> LangBuffer; 104 // LangBuffer is guaranteed to be big enough. 105 LangBuffer.resize(Tok.getLength()); 106 const char *LangBufPtr = &LangBuffer[0]; 107 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr); 108 109 SourceLocation Loc = ConsumeStringToken(); 110 DeclTy *D = 0; 111 SourceLocation LBrace, RBrace; 112 113 if (Tok.isNot(tok::l_brace)) { 114 D = ParseDeclarationOrFunctionDefinition(); 115 } else { 116 LBrace = ConsumeBrace(); 117 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 118 // FIXME capture the decls. 119 D = ParseExternalDeclaration(); 120 } 121 122 RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); 123 } 124 125 if (!D) 126 return 0; 127 128 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D); 129} 130 131/// ParseClassName - Parse a C++ class-name, which names a class. Note 132/// that we only check that the result names a type; semantic analysis 133/// will need to verify that the type names a class. The result is 134/// either a type or NULL, dependending on whether a type name was 135/// found. 136/// 137/// class-name: [C++ 9.1] 138/// identifier 139/// template-id [TODO] 140/// 141Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) { 142 // Parse the class-name. 143 // FIXME: Alternatively, parse a simple-template-id. 144 if (Tok.isNot(tok::identifier)) { 145 Diag(Tok, diag::err_expected_class_name); 146 return 0; 147 } 148 149 // We have an identifier; check whether it is actually a type. 150 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS); 151 if (!Type) { 152 Diag(Tok, diag::err_expected_class_name); 153 return 0; 154 } 155 156 // Consume the identifier. 157 ConsumeToken(); 158 159 return Type; 160} 161 162/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or 163/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which 164/// until we reach the start of a definition or see a token that 165/// cannot start a definition. 166/// 167/// class-specifier: [C++ class] 168/// class-head '{' member-specification[opt] '}' 169/// class-head '{' member-specification[opt] '}' attributes[opt] 170/// class-head: 171/// class-key identifier[opt] base-clause[opt] 172/// class-key nested-name-specifier identifier base-clause[opt] 173/// class-key nested-name-specifier[opt] simple-template-id 174/// base-clause[opt] 175/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] 176/// [GNU] class-key attributes[opt] nested-name-specifier 177/// identifier base-clause[opt] 178/// [GNU] class-key attributes[opt] nested-name-specifier[opt] 179/// simple-template-id base-clause[opt] 180/// class-key: 181/// 'class' 182/// 'struct' 183/// 'union' 184/// 185/// elaborated-type-specifier: [C++ dcl.type.elab] 186/// class-key ::[opt] nested-name-specifier[opt] identifier 187/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] 188/// simple-template-id 189/// 190/// Note that the C++ class-specifier and elaborated-type-specifier, 191/// together, subsume the C99 struct-or-union-specifier: 192/// 193/// struct-or-union-specifier: [C99 6.7.2.1] 194/// struct-or-union identifier[opt] '{' struct-contents '}' 195/// struct-or-union identifier 196/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents 197/// '}' attributes[opt] 198/// [GNU] struct-or-union attributes[opt] identifier 199/// struct-or-union: 200/// 'struct' 201/// 'union' 202void Parser::ParseClassSpecifier(DeclSpec &DS) { 203 assert((Tok.is(tok::kw_class) || 204 Tok.is(tok::kw_struct) || 205 Tok.is(tok::kw_union)) && 206 "Not a class specifier"); 207 DeclSpec::TST TagType = 208 Tok.is(tok::kw_class) ? DeclSpec::TST_class : 209 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct : 210 DeclSpec::TST_union; 211 212 SourceLocation StartLoc = ConsumeToken(); 213 214 AttributeList *Attr = 0; 215 // If attributes exist after tag, parse them. 216 if (Tok.is(tok::kw___attribute)) 217 Attr = ParseAttributes(); 218 219 // Parse the (optional) nested-name-specifier. 220 CXXScopeSpec SS; 221 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) { 222 if (Tok.isNot(tok::identifier)) 223 Diag(Tok, diag::err_expected_ident); 224 } 225 226 // Parse the (optional) class name. 227 // FIXME: Alternatively, parse a simple-template-id. 228 IdentifierInfo *Name = 0; 229 SourceLocation NameLoc; 230 if (Tok.is(tok::identifier)) { 231 Name = Tok.getIdentifierInfo(); 232 NameLoc = ConsumeToken(); 233 } 234 235 // There are three options here. If we have 'struct foo;', then 236 // this is a forward declaration. If we have 'struct foo {...' or 237 // 'struct fo :...' then this is a definition. Otherwise we have 238 // something like 'struct foo xyz', a reference. 239 Action::TagKind TK; 240 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) 241 TK = Action::TK_Definition; 242 else if (Tok.is(tok::semi)) 243 TK = Action::TK_Declaration; 244 else 245 TK = Action::TK_Reference; 246 247 if (!Name && TK != Action::TK_Definition) { 248 // We have a declaration or reference to an anonymous class. 249 Diag(StartLoc, diag::err_anon_type_definition) 250 << DeclSpec::getSpecifierName(TagType); 251 252 // Skip the rest of this declarator, up until the comma or semicolon. 253 SkipUntil(tok::comma, true); 254 return; 255 } 256 257 // Parse the tag portion of this. 258 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name, 259 NameLoc, Attr); 260 261 // Parse the optional base clause (C++ only). 262 if (getLang().CPlusPlus && Tok.is(tok::colon)) { 263 ParseBaseClause(TagDecl); 264 } 265 266 // If there is a body, parse it and inform the actions module. 267 if (Tok.is(tok::l_brace)) 268 if (getLang().CPlusPlus) 269 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl); 270 else 271 ParseStructUnionBody(StartLoc, TagType, TagDecl); 272 else if (TK == Action::TK_Definition) { 273 // FIXME: Complain that we have a base-specifier list but no 274 // definition. 275 Diag(Tok, diag::err_expected_lbrace); 276 } 277 278 const char *PrevSpec = 0; 279 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) 280 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; 281} 282 283/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. 284/// 285/// base-clause : [C++ class.derived] 286/// ':' base-specifier-list 287/// base-specifier-list: 288/// base-specifier '...'[opt] 289/// base-specifier-list ',' base-specifier '...'[opt] 290void Parser::ParseBaseClause(DeclTy *ClassDecl) 291{ 292 assert(Tok.is(tok::colon) && "Not a base clause"); 293 ConsumeToken(); 294 295 // Build up an array of parsed base specifiers. 296 llvm::SmallVector<BaseTy *, 8> BaseInfo; 297 298 while (true) { 299 // Parse a base-specifier. 300 BaseResult Result = ParseBaseSpecifier(ClassDecl); 301 if (Result.isInvalid) { 302 // Skip the rest of this base specifier, up until the comma or 303 // opening brace. 304 SkipUntil(tok::comma, tok::l_brace, true, true); 305 } else { 306 // Add this to our array of base specifiers. 307 BaseInfo.push_back(Result.Val); 308 } 309 310 // If the next token is a comma, consume it and keep reading 311 // base-specifiers. 312 if (Tok.isNot(tok::comma)) break; 313 314 // Consume the comma. 315 ConsumeToken(); 316 } 317 318 // Attach the base specifiers 319 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size()); 320} 321 322/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is 323/// one entry in the base class list of a class specifier, for example: 324/// class foo : public bar, virtual private baz { 325/// 'public bar' and 'virtual private baz' are each base-specifiers. 326/// 327/// base-specifier: [C++ class.derived] 328/// ::[opt] nested-name-specifier[opt] class-name 329/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] 330/// class-name 331/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] 332/// class-name 333Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl) 334{ 335 bool IsVirtual = false; 336 SourceLocation StartLoc = Tok.getLocation(); 337 338 // Parse the 'virtual' keyword. 339 if (Tok.is(tok::kw_virtual)) { 340 ConsumeToken(); 341 IsVirtual = true; 342 } 343 344 // Parse an (optional) access specifier. 345 AccessSpecifier Access = getAccessSpecifierIfPresent(); 346 if (Access) 347 ConsumeToken(); 348 349 // Parse the 'virtual' keyword (again!), in case it came after the 350 // access specifier. 351 if (Tok.is(tok::kw_virtual)) { 352 SourceLocation VirtualLoc = ConsumeToken(); 353 if (IsVirtual) { 354 // Complain about duplicate 'virtual' 355 Diag(VirtualLoc, diag::err_dup_virtual) 356 << SourceRange(VirtualLoc, VirtualLoc); 357 } 358 359 IsVirtual = true; 360 } 361 362 // Parse optional '::' and optional nested-name-specifier. 363 CXXScopeSpec SS; 364 MaybeParseCXXScopeSpecifier(SS); 365 366 // The location of the base class itself. 367 SourceLocation BaseLoc = Tok.getLocation(); 368 369 // Parse the class-name. 370 TypeTy *BaseType = ParseClassName(&SS); 371 if (!BaseType) 372 return true; 373 374 // Find the complete source range for the base-specifier. 375 SourceRange Range(StartLoc, BaseLoc); 376 377 // Notify semantic analysis that we have parsed a complete 378 // base-specifier. 379 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, 380 BaseType, BaseLoc); 381} 382 383/// getAccessSpecifierIfPresent - Determine whether the next token is 384/// a C++ access-specifier. 385/// 386/// access-specifier: [C++ class.derived] 387/// 'private' 388/// 'protected' 389/// 'public' 390AccessSpecifier Parser::getAccessSpecifierIfPresent() const 391{ 392 switch (Tok.getKind()) { 393 default: return AS_none; 394 case tok::kw_private: return AS_private; 395 case tok::kw_protected: return AS_protected; 396 case tok::kw_public: return AS_public; 397 } 398} 399 400/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. 401/// 402/// member-declaration: 403/// decl-specifier-seq[opt] member-declarator-list[opt] ';' 404/// function-definition ';'[opt] 405/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] 406/// using-declaration [TODO] 407/// [C++0x] static_assert-declaration [TODO] 408/// template-declaration [TODO] 409/// 410/// member-declarator-list: 411/// member-declarator 412/// member-declarator-list ',' member-declarator 413/// 414/// member-declarator: 415/// declarator pure-specifier[opt] 416/// declarator constant-initializer[opt] 417/// identifier[opt] ':' constant-expression 418/// 419/// pure-specifier: [TODO] 420/// '= 0' 421/// 422/// constant-initializer: 423/// '=' constant-expression 424/// 425Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) { 426 SourceLocation DSStart = Tok.getLocation(); 427 // decl-specifier-seq: 428 // Parse the common declaration-specifiers piece. 429 DeclSpec DS; 430 ParseDeclarationSpecifiers(DS); 431 432 if (Tok.is(tok::semi)) { 433 ConsumeToken(); 434 // C++ 9.2p7: The member-declarator-list can be omitted only after a 435 // class-specifier or an enum-specifier or in a friend declaration. 436 // FIXME: Friend declarations. 437 switch (DS.getTypeSpecType()) { 438 case DeclSpec::TST_struct: 439 case DeclSpec::TST_union: 440 case DeclSpec::TST_class: 441 case DeclSpec::TST_enum: 442 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 443 default: 444 Diag(DSStart, diag::err_no_declarators); 445 return 0; 446 } 447 } 448 449 Declarator DeclaratorInfo(DS, Declarator::MemberContext); 450 451 if (Tok.isNot(tok::colon)) { 452 // Parse the first declarator. 453 ParseDeclarator(DeclaratorInfo); 454 // Error parsing the declarator? 455 if (!DeclaratorInfo.hasName()) { 456 // If so, skip until the semi-colon or a }. 457 SkipUntil(tok::r_brace, true); 458 if (Tok.is(tok::semi)) 459 ConsumeToken(); 460 return 0; 461 } 462 463 // function-definition: 464 if (Tok.is(tok::l_brace) 465 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) { 466 if (!DeclaratorInfo.isFunctionDeclarator()) { 467 Diag(Tok, diag::err_func_def_no_params); 468 ConsumeBrace(); 469 SkipUntil(tok::r_brace, true); 470 return 0; 471 } 472 473 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 474 Diag(Tok, diag::err_function_declared_typedef); 475 // This recovery skips the entire function body. It would be nice 476 // to simply call ParseCXXInlineMethodDef() below, however Sema 477 // assumes the declarator represents a function, not a typedef. 478 ConsumeBrace(); 479 SkipUntil(tok::r_brace, true); 480 return 0; 481 } 482 483 return ParseCXXInlineMethodDef(AS, DeclaratorInfo); 484 } 485 } 486 487 // member-declarator-list: 488 // member-declarator 489 // member-declarator-list ',' member-declarator 490 491 DeclTy *LastDeclInGroup = 0; 492 OwningExprResult BitfieldSize(Actions); 493 OwningExprResult Init(Actions); 494 495 while (1) { 496 497 // member-declarator: 498 // declarator pure-specifier[opt] 499 // declarator constant-initializer[opt] 500 // identifier[opt] ':' constant-expression 501 502 if (Tok.is(tok::colon)) { 503 ConsumeToken(); 504 BitfieldSize = ParseConstantExpression(); 505 if (BitfieldSize.isInvalid()) 506 SkipUntil(tok::comma, true, true); 507 } 508 509 // pure-specifier: 510 // '= 0' 511 // 512 // constant-initializer: 513 // '=' constant-expression 514 515 if (Tok.is(tok::equal)) { 516 ConsumeToken(); 517 Init = ParseInitializer(); 518 if (Init.isInvalid()) 519 SkipUntil(tok::comma, true, true); 520 } 521 522 // If attributes exist after the declarator, parse them. 523 if (Tok.is(tok::kw___attribute)) 524 DeclaratorInfo.AddAttributes(ParseAttributes()); 525 526 // NOTE: If Sema is the Action module and declarator is an instance field, 527 // this call will *not* return the created decl; LastDeclInGroup will be 528 // returned instead. 529 // See Sema::ActOnCXXMemberDeclarator for details. 530 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS, 531 DeclaratorInfo, 532 BitfieldSize.move(), 533 Init.move(), 534 LastDeclInGroup); 535 536 // If we don't have a comma, it is either the end of the list (a ';') 537 // or an error, bail out. 538 if (Tok.isNot(tok::comma)) 539 break; 540 541 // Consume the comma. 542 ConsumeToken(); 543 544 // Parse the next declarator. 545 DeclaratorInfo.clear(); 546 BitfieldSize = 0; 547 Init = 0; 548 549 // Attributes are only allowed on the second declarator. 550 if (Tok.is(tok::kw___attribute)) 551 DeclaratorInfo.AddAttributes(ParseAttributes()); 552 553 if (Tok.isNot(tok::colon)) 554 ParseDeclarator(DeclaratorInfo); 555 } 556 557 if (Tok.is(tok::semi)) { 558 ConsumeToken(); 559 // Reverse the chain list. 560 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); 561 } 562 563 Diag(Tok, diag::err_expected_semi_decl_list); 564 // Skip to end of block or statement 565 SkipUntil(tok::r_brace, true, true); 566 if (Tok.is(tok::semi)) 567 ConsumeToken(); 568 return 0; 569} 570 571/// ParseCXXMemberSpecification - Parse the class definition. 572/// 573/// member-specification: 574/// member-declaration member-specification[opt] 575/// access-specifier ':' member-specification[opt] 576/// 577void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, 578 unsigned TagType, DeclTy *TagDecl) { 579 assert((TagType == DeclSpec::TST_struct || 580 TagType == DeclSpec::TST_union || 581 TagType == DeclSpec::TST_class) && "Invalid TagType!"); 582 583 SourceLocation LBraceLoc = ConsumeBrace(); 584 585 if (!CurScope->isCXXClassScope() && // Not about to define a nested class. 586 CurScope->isInCXXInlineMethodScope()) { 587 // We will define a local class of an inline method. 588 // Push a new LexedMethodsForTopClass for its inline methods. 589 PushTopClassStack(); 590 } 591 592 // Enter a scope for the class. 593 EnterScope(Scope::CXXClassScope|Scope::DeclScope); 594 595 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc); 596 597 // C++ 11p3: Members of a class defined with the keyword class are private 598 // by default. Members of a class defined with the keywords struct or union 599 // are public by default. 600 AccessSpecifier CurAS; 601 if (TagType == DeclSpec::TST_class) 602 CurAS = AS_private; 603 else 604 CurAS = AS_public; 605 606 // While we still have something to read, read the member-declarations. 607 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 608 // Each iteration of this loop reads one member-declaration. 609 610 // Check for extraneous top-level semicolon. 611 if (Tok.is(tok::semi)) { 612 Diag(Tok, diag::ext_extra_struct_semi); 613 ConsumeToken(); 614 continue; 615 } 616 617 AccessSpecifier AS = getAccessSpecifierIfPresent(); 618 if (AS != AS_none) { 619 // Current token is a C++ access specifier. 620 CurAS = AS; 621 ConsumeToken(); 622 ExpectAndConsume(tok::colon, diag::err_expected_colon); 623 continue; 624 } 625 626 // Parse all the comma separated declarators. 627 ParseCXXClassMemberDeclaration(CurAS); 628 } 629 630 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 631 632 AttributeList *AttrList = 0; 633 // If attributes exist after class contents, parse them. 634 if (Tok.is(tok::kw___attribute)) 635 AttrList = ParseAttributes(); // FIXME: where should I put them? 636 637 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl, 638 LBraceLoc, RBraceLoc); 639 640 // C++ 9.2p2: Within the class member-specification, the class is regarded as 641 // complete within function bodies, default arguments, 642 // exception-specifications, and constructor ctor-initializers (including 643 // such things in nested classes). 644 // 645 // FIXME: Only function bodies are parsed correctly, fix the rest. 646 if (!CurScope->getParent()->isCXXClassScope()) { 647 // We are not inside a nested class. This class and its nested classes 648 // are complete and we can parse the lexed inline method definitions. 649 ParseLexedMethodDefs(); 650 651 // For a local class of inline method, pop the LexedMethodsForTopClass that 652 // was previously pushed. 653 654 assert((CurScope->isInCXXInlineMethodScope() || 655 TopClassStacks.size() == 1) && 656 "MethodLexers not getting popped properly!"); 657 if (CurScope->isInCXXInlineMethodScope()) 658 PopTopClassStack(); 659 } 660 661 // Leave the class scope. 662 ExitScope(); 663 664 Actions.ActOnFinishCXXClassDef(TagDecl); 665} 666 667/// ParseConstructorInitializer - Parse a C++ constructor initializer, 668/// which explicitly initializes the members or base classes of a 669/// class (C++ [class.base.init]). For example, the three initializers 670/// after the ':' in the Derived constructor below: 671/// 672/// @code 673/// class Base { }; 674/// class Derived : Base { 675/// int x; 676/// float f; 677/// public: 678/// Derived(float f) : Base(), x(17), f(f) { } 679/// }; 680/// @endcode 681/// 682/// [C++] ctor-initializer: 683/// ':' mem-initializer-list 684/// 685/// [C++] mem-initializer-list: 686/// mem-initializer 687/// mem-initializer , mem-initializer-list 688void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) { 689 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); 690 691 SourceLocation ColonLoc = ConsumeToken(); 692 693 llvm::SmallVector<MemInitTy*, 4> MemInitializers; 694 695 do { 696 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); 697 if (!MemInit.isInvalid) 698 MemInitializers.push_back(MemInit.Val); 699 700 if (Tok.is(tok::comma)) 701 ConsumeToken(); 702 else if (Tok.is(tok::l_brace)) 703 break; 704 else { 705 // Skip over garbage, until we get to '{'. Don't eat the '{'. 706 SkipUntil(tok::l_brace, true, true); 707 break; 708 } 709 } while (true); 710 711 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, 712 &MemInitializers[0], MemInitializers.size()); 713} 714 715/// ParseMemInitializer - Parse a C++ member initializer, which is 716/// part of a constructor initializer that explicitly initializes one 717/// member or base class (C++ [class.base.init]). See 718/// ParseConstructorInitializer for an example. 719/// 720/// [C++] mem-initializer: 721/// mem-initializer-id '(' expression-list[opt] ')' 722/// 723/// [C++] mem-initializer-id: 724/// '::'[opt] nested-name-specifier[opt] class-name 725/// identifier 726Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) { 727 // FIXME: parse '::'[opt] nested-name-specifier[opt] 728 729 if (Tok.isNot(tok::identifier)) { 730 Diag(Tok, diag::err_expected_member_or_base_name); 731 return true; 732 } 733 734 // Get the identifier. This may be a member name or a class name, 735 // but we'll let the semantic analysis determine which it is. 736 IdentifierInfo *II = Tok.getIdentifierInfo(); 737 SourceLocation IdLoc = ConsumeToken(); 738 739 // Parse the '('. 740 if (Tok.isNot(tok::l_paren)) { 741 Diag(Tok, diag::err_expected_lparen); 742 return true; 743 } 744 SourceLocation LParenLoc = ConsumeParen(); 745 746 // Parse the optional expression-list. 747 ExprVector ArgExprs(Actions); 748 CommaLocsTy CommaLocs; 749 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { 750 SkipUntil(tok::r_paren); 751 return true; 752 } 753 754 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 755 756 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc, 757 LParenLoc, ArgExprs.take(), 758 ArgExprs.size(), &CommaLocs[0], RParenLoc); 759} 760 761/// ParseExceptionSpecification - Parse a C++ exception-specification 762/// (C++ [except.spec]). 763/// 764/// exception-specification: 765/// 'throw' '(' type-id-list [opt] ')' 766/// [MS] 'throw' '(' '...' ')' 767/// 768/// type-id-list: 769/// type-id 770/// type-id-list ',' type-id 771/// 772bool Parser::ParseExceptionSpecification() { 773 assert(Tok.is(tok::kw_throw) && "expected throw"); 774 775 SourceLocation ThrowLoc = ConsumeToken(); 776 777 if (!Tok.is(tok::l_paren)) { 778 return Diag(Tok, diag::err_expected_lparen_after) << "throw"; 779 } 780 SourceLocation LParenLoc = ConsumeParen(); 781 782 // Parse throw(...), a Microsoft extension that means "this function 783 // can throw anything". 784 if (Tok.is(tok::ellipsis)) { 785 SourceLocation EllipsisLoc = ConsumeToken(); 786 if (!getLang().Microsoft) 787 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); 788 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 789 return false; 790 } 791 792 // Parse the sequence of type-ids. 793 while (Tok.isNot(tok::r_paren)) { 794 ParseTypeName(); 795 if (Tok.is(tok::comma)) 796 ConsumeToken(); 797 else 798 break; 799 } 800 801 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 802 return false; 803} 804