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