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