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