SemaCXXScopeSpec.cpp revision de0a95b8662e0267a5bf81801e87477d67d74c17
1//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// 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 C++ semantic analysis for scope specifiers. 11// 12//===----------------------------------------------------------------------===// 13 14#include "Sema.h" 15#include "Lookup.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/DeclTemplate.h" 18#include "clang/AST/ExprCXX.h" 19#include "clang/AST/NestedNameSpecifier.h" 20#include "clang/Basic/PartialDiagnostic.h" 21#include "clang/Parse/DeclSpec.h" 22#include "llvm/ADT/STLExtras.h" 23#include "llvm/Support/raw_ostream.h" 24using namespace clang; 25 26/// \brief Find the current instantiation that associated with the given type. 27static CXXRecordDecl *getCurrentInstantiationOf(QualType T) { 28 if (T.isNull()) 29 return 0; 30 31 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 32 if (isa<RecordType>(Ty)) 33 return cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 34 else if (isa<InjectedClassNameType>(Ty)) 35 return cast<InjectedClassNameType>(Ty)->getDecl(); 36 else 37 return 0; 38} 39 40/// \brief Compute the DeclContext that is associated with the given type. 41/// 42/// \param T the type for which we are attempting to find a DeclContext. 43/// 44/// \returns the declaration context represented by the type T, 45/// or NULL if the declaration context cannot be computed (e.g., because it is 46/// dependent and not the current instantiation). 47DeclContext *Sema::computeDeclContext(QualType T) { 48 if (const TagType *Tag = T->getAs<TagType>()) 49 return Tag->getDecl(); 50 51 return ::getCurrentInstantiationOf(T); 52} 53 54/// \brief Compute the DeclContext that is associated with the given 55/// scope specifier. 56/// 57/// \param SS the C++ scope specifier as it appears in the source 58/// 59/// \param EnteringContext when true, we will be entering the context of 60/// this scope specifier, so we can retrieve the declaration context of a 61/// class template or class template partial specialization even if it is 62/// not the current instantiation. 63/// 64/// \returns the declaration context represented by the scope specifier @p SS, 65/// or NULL if the declaration context cannot be computed (e.g., because it is 66/// dependent and not the current instantiation). 67DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, 68 bool EnteringContext) { 69 if (!SS.isSet() || SS.isInvalid()) 70 return 0; 71 72 NestedNameSpecifier *NNS 73 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 74 if (NNS->isDependent()) { 75 // If this nested-name-specifier refers to the current 76 // instantiation, return its DeclContext. 77 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 78 return Record; 79 80 if (EnteringContext) { 81 const Type *NNSType = NNS->getAsType(); 82 if (!NNSType) { 83 // do nothing, fall out 84 } else if (const TemplateSpecializationType *SpecType 85 = NNSType->getAs<TemplateSpecializationType>()) { 86 // We are entering the context of the nested name specifier, so try to 87 // match the nested name specifier to either a primary class template 88 // or a class template partial specialization. 89 if (ClassTemplateDecl *ClassTemplate 90 = dyn_cast_or_null<ClassTemplateDecl>( 91 SpecType->getTemplateName().getAsTemplateDecl())) { 92 QualType ContextType 93 = Context.getCanonicalType(QualType(SpecType, 0)); 94 95 // If the type of the nested name specifier is the same as the 96 // injected class name of the named class template, we're entering 97 // into that class template definition. 98 QualType Injected 99 = ClassTemplate->getInjectedClassNameSpecialization(Context); 100 if (Context.hasSameType(Injected, ContextType)) 101 return ClassTemplate->getTemplatedDecl(); 102 103 // If the type of the nested name specifier is the same as the 104 // type of one of the class template's class template partial 105 // specializations, we're entering into the definition of that 106 // class template partial specialization. 107 if (ClassTemplatePartialSpecializationDecl *PartialSpec 108 = ClassTemplate->findPartialSpecialization(ContextType)) 109 return PartialSpec; 110 } 111 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { 112 // The nested name specifier refers to a member of a class template. 113 return RecordT->getDecl(); 114 } 115 } 116 117 return 0; 118 } 119 120 switch (NNS->getKind()) { 121 case NestedNameSpecifier::Identifier: 122 assert(false && "Dependent nested-name-specifier has no DeclContext"); 123 break; 124 125 case NestedNameSpecifier::Namespace: 126 return NNS->getAsNamespace(); 127 128 case NestedNameSpecifier::TypeSpec: 129 case NestedNameSpecifier::TypeSpecWithTemplate: { 130 const TagType *Tag = NNS->getAsType()->getAs<TagType>(); 131 assert(Tag && "Non-tag type in nested-name-specifier"); 132 return Tag->getDecl(); 133 } break; 134 135 case NestedNameSpecifier::Global: 136 return Context.getTranslationUnitDecl(); 137 } 138 139 // Required to silence a GCC warning. 140 return 0; 141} 142 143bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 144 if (!SS.isSet() || SS.isInvalid()) 145 return false; 146 147 NestedNameSpecifier *NNS 148 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 149 return NNS->isDependent(); 150} 151 152// \brief Determine whether this C++ scope specifier refers to an 153// unknown specialization, i.e., a dependent type that is not the 154// current instantiation. 155bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { 156 if (!isDependentScopeSpecifier(SS)) 157 return false; 158 159 NestedNameSpecifier *NNS 160 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 161 return getCurrentInstantiationOf(NNS) == 0; 162} 163 164/// \brief If the given nested name specifier refers to the current 165/// instantiation, return the declaration that corresponds to that 166/// current instantiation (C++0x [temp.dep.type]p1). 167/// 168/// \param NNS a dependent nested name specifier. 169CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 170 assert(getLangOptions().CPlusPlus && "Only callable in C++"); 171 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 172 173 if (!NNS->getAsType()) 174 return 0; 175 176 QualType T = QualType(NNS->getAsType(), 0); 177 return ::getCurrentInstantiationOf(T); 178} 179 180/// \brief Require that the context specified by SS be complete. 181/// 182/// If SS refers to a type, this routine checks whether the type is 183/// complete enough (or can be made complete enough) for name lookup 184/// into the DeclContext. A type that is not yet completed can be 185/// considered "complete enough" if it is a class/struct/union/enum 186/// that is currently being defined. Or, if we have a type that names 187/// a class template specialization that is not a complete type, we 188/// will attempt to instantiate that class template. 189bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, 190 DeclContext *DC) { 191 assert(DC != 0 && "given null context"); 192 193 if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 194 // If this is a dependent type, then we consider it complete. 195 if (Tag->isDependentContext()) 196 return false; 197 198 // If we're currently defining this type, then lookup into the 199 // type is okay: don't complain that it isn't complete yet. 200 const TagType *TagT = Context.getTypeDeclType(Tag)->getAs<TagType>(); 201 if (TagT && TagT->isBeingDefined()) 202 return false; 203 204 // The type must be complete. 205 if (RequireCompleteType(SS.getRange().getBegin(), 206 Context.getTypeDeclType(Tag), 207 PDiag(diag::err_incomplete_nested_name_spec) 208 << SS.getRange())) { 209 SS.setScopeRep(0); // Mark the ScopeSpec invalid. 210 return true; 211 } 212 } 213 214 return false; 215} 216 217/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the 218/// global scope ('::'). 219Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, 220 SourceLocation CCLoc) { 221 return NestedNameSpecifier::GlobalSpecifier(Context); 222} 223 224/// \brief Determines whether the given declaration is an valid acceptable 225/// result for name lookup of a nested-name-specifier. 226bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) { 227 if (!SD) 228 return false; 229 230 // Namespace and namespace aliases are fine. 231 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD)) 232 return true; 233 234 if (!isa<TypeDecl>(SD)) 235 return false; 236 237 // Determine whether we have a class (or, in C++0x, an enum) or 238 // a typedef thereof. If so, build the nested-name-specifier. 239 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 240 if (T->isDependentType()) 241 return true; 242 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { 243 if (TD->getUnderlyingType()->isRecordType() || 244 (Context.getLangOptions().CPlusPlus0x && 245 TD->getUnderlyingType()->isEnumeralType())) 246 return true; 247 } else if (isa<RecordDecl>(SD) || 248 (Context.getLangOptions().CPlusPlus0x && isa<EnumDecl>(SD))) 249 return true; 250 251 return false; 252} 253 254/// \brief If the given nested-name-specifier begins with a bare identifier 255/// (e.g., Base::), perform name lookup for that identifier as a 256/// nested-name-specifier within the given scope, and return the result of that 257/// name lookup. 258NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { 259 if (!S || !NNS) 260 return 0; 261 262 while (NNS->getPrefix()) 263 NNS = NNS->getPrefix(); 264 265 if (NNS->getKind() != NestedNameSpecifier::Identifier) 266 return 0; 267 268 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), 269 LookupNestedNameSpecifierName); 270 LookupName(Found, S); 271 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); 272 273 if (!Found.isSingleResult()) 274 return 0; 275 276 NamedDecl *Result = Found.getFoundDecl(); 277 if (isAcceptableNestedNameSpecifier(Result)) 278 return Result; 279 280 return 0; 281} 282 283bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, 284 SourceLocation IdLoc, 285 IdentifierInfo &II, 286 TypeTy *ObjectTypePtr) { 287 QualType ObjectType = GetTypeFromParser(ObjectTypePtr); 288 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); 289 290 // Determine where to perform name lookup 291 DeclContext *LookupCtx = 0; 292 bool isDependent = false; 293 if (!ObjectType.isNull()) { 294 // This nested-name-specifier occurs in a member access expression, e.g., 295 // x->B::f, and we are looking into the type of the object. 296 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 297 LookupCtx = computeDeclContext(ObjectType); 298 isDependent = ObjectType->isDependentType(); 299 } else if (SS.isSet()) { 300 // This nested-name-specifier occurs after another nested-name-specifier, 301 // so long into the context associated with the prior nested-name-specifier. 302 LookupCtx = computeDeclContext(SS, false); 303 isDependent = isDependentScopeSpecifier(SS); 304 Found.setContextRange(SS.getRange()); 305 } 306 307 if (LookupCtx) { 308 // Perform "qualified" name lookup into the declaration context we 309 // computed, which is either the type of the base of a member access 310 // expression or the declaration context associated with a prior 311 // nested-name-specifier. 312 313 // The declaration context must be complete. 314 if (!LookupCtx->isDependentContext() && 315 RequireCompleteDeclContext(SS, LookupCtx)) 316 return false; 317 318 LookupQualifiedName(Found, LookupCtx); 319 } else if (isDependent) { 320 return false; 321 } else { 322 LookupName(Found, S); 323 } 324 Found.suppressDiagnostics(); 325 326 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 327 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); 328 329 return false; 330} 331 332/// \brief Build a new nested-name-specifier for "identifier::", as described 333/// by ActOnCXXNestedNameSpecifier. 334/// 335/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in 336/// that it contains an extra parameter \p ScopeLookupResult, which provides 337/// the result of name lookup within the scope of the nested-name-specifier 338/// that was computed at template definition time. 339/// 340/// If ErrorRecoveryLookup is true, then this call is used to improve error 341/// recovery. This means that it should not emit diagnostics, it should 342/// just return null on failure. It also means it should only return a valid 343/// scope if it *knows* that the result is correct. It should not return in a 344/// dependent context, for example. 345Sema::CXXScopeTy *Sema::BuildCXXNestedNameSpecifier(Scope *S, 346 CXXScopeSpec &SS, 347 SourceLocation IdLoc, 348 SourceLocation CCLoc, 349 IdentifierInfo &II, 350 QualType ObjectType, 351 NamedDecl *ScopeLookupResult, 352 bool EnteringContext, 353 bool ErrorRecoveryLookup) { 354 NestedNameSpecifier *Prefix 355 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 356 357 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); 358 359 // Determine where to perform name lookup 360 DeclContext *LookupCtx = 0; 361 bool isDependent = false; 362 if (!ObjectType.isNull()) { 363 // This nested-name-specifier occurs in a member access expression, e.g., 364 // x->B::f, and we are looking into the type of the object. 365 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 366 LookupCtx = computeDeclContext(ObjectType); 367 isDependent = ObjectType->isDependentType(); 368 } else if (SS.isSet()) { 369 // This nested-name-specifier occurs after another nested-name-specifier, 370 // so long into the context associated with the prior nested-name-specifier. 371 LookupCtx = computeDeclContext(SS, EnteringContext); 372 isDependent = isDependentScopeSpecifier(SS); 373 Found.setContextRange(SS.getRange()); 374 } 375 376 377 bool ObjectTypeSearchedInScope = false; 378 if (LookupCtx) { 379 // Perform "qualified" name lookup into the declaration context we 380 // computed, which is either the type of the base of a member access 381 // expression or the declaration context associated with a prior 382 // nested-name-specifier. 383 384 // The declaration context must be complete. 385 if (!LookupCtx->isDependentContext() && 386 RequireCompleteDeclContext(SS, LookupCtx)) 387 return 0; 388 389 LookupQualifiedName(Found, LookupCtx); 390 391 if (!ObjectType.isNull() && Found.empty()) { 392 // C++ [basic.lookup.classref]p4: 393 // If the id-expression in a class member access is a qualified-id of 394 // the form 395 // 396 // class-name-or-namespace-name::... 397 // 398 // the class-name-or-namespace-name following the . or -> operator is 399 // looked up both in the context of the entire postfix-expression and in 400 // the scope of the class of the object expression. If the name is found 401 // only in the scope of the class of the object expression, the name 402 // shall refer to a class-name. If the name is found only in the 403 // context of the entire postfix-expression, the name shall refer to a 404 // class-name or namespace-name. [...] 405 // 406 // Qualified name lookup into a class will not find a namespace-name, 407 // so we do not need to diagnoste that case specifically. However, 408 // this qualified name lookup may find nothing. In that case, perform 409 // unqualified name lookup in the given scope (if available) or 410 // reconstruct the result from when name lookup was performed at template 411 // definition time. 412 if (S) 413 LookupName(Found, S); 414 else if (ScopeLookupResult) 415 Found.addDecl(ScopeLookupResult); 416 417 ObjectTypeSearchedInScope = true; 418 } 419 } else if (isDependent) { 420 // Don't speculate if we're just trying to improve error recovery. 421 if (ErrorRecoveryLookup) 422 return 0; 423 424 // We were not able to compute the declaration context for a dependent 425 // base object type or prior nested-name-specifier, so this 426 // nested-name-specifier refers to an unknown specialization. Just build 427 // a dependent nested-name-specifier. 428 if (!Prefix) 429 return NestedNameSpecifier::Create(Context, &II); 430 431 return NestedNameSpecifier::Create(Context, Prefix, &II); 432 } else { 433 // Perform unqualified name lookup in the current scope. 434 LookupName(Found, S); 435 } 436 437 // FIXME: Deal with ambiguities cleanly. 438 439 if (Found.empty() && !ErrorRecoveryLookup) { 440 // We haven't found anything, and we're not recovering from a 441 // different kind of error, so look for typos. 442 DeclarationName Name = Found.getLookupName(); 443 if (CorrectTypo(Found, S, &SS, LookupCtx, EnteringContext, 444 CTC_NoKeywords) && 445 Found.isSingleResult() && 446 isAcceptableNestedNameSpecifier(Found.getAsSingle<NamedDecl>())) { 447 if (LookupCtx) 448 Diag(Found.getNameLoc(), diag::err_no_member_suggest) 449 << Name << LookupCtx << Found.getLookupName() << SS.getRange() 450 << FixItHint::CreateReplacement(Found.getNameLoc(), 451 Found.getLookupName().getAsString()); 452 else 453 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest) 454 << Name << Found.getLookupName() 455 << FixItHint::CreateReplacement(Found.getNameLoc(), 456 Found.getLookupName().getAsString()); 457 458 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 459 Diag(ND->getLocation(), diag::note_previous_decl) 460 << ND->getDeclName(); 461 } else { 462 Found.clear(); 463 Found.setLookupName(&II); 464 } 465 } 466 467 NamedDecl *SD = Found.getAsSingle<NamedDecl>(); 468 if (isAcceptableNestedNameSpecifier(SD)) { 469 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) { 470 // C++ [basic.lookup.classref]p4: 471 // [...] If the name is found in both contexts, the 472 // class-name-or-namespace-name shall refer to the same entity. 473 // 474 // We already found the name in the scope of the object. Now, look 475 // into the current scope (the scope of the postfix-expression) to 476 // see if we can find the same name there. As above, if there is no 477 // scope, reconstruct the result from the template instantiation itself. 478 NamedDecl *OuterDecl; 479 if (S) { 480 LookupResult FoundOuter(*this, &II, IdLoc, LookupNestedNameSpecifierName); 481 LookupName(FoundOuter, S); 482 OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); 483 } else 484 OuterDecl = ScopeLookupResult; 485 486 if (isAcceptableNestedNameSpecifier(OuterDecl) && 487 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && 488 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || 489 !Context.hasSameType( 490 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), 491 Context.getTypeDeclType(cast<TypeDecl>(SD))))) { 492 if (ErrorRecoveryLookup) 493 return 0; 494 495 Diag(IdLoc, diag::err_nested_name_member_ref_lookup_ambiguous) 496 << &II; 497 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) 498 << ObjectType; 499 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); 500 501 // Fall through so that we'll pick the name we found in the object 502 // type, since that's probably what the user wanted anyway. 503 } 504 } 505 506 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) 507 return NestedNameSpecifier::Create(Context, Prefix, Namespace); 508 509 // FIXME: It would be nice to maintain the namespace alias name, then 510 // see through that alias when resolving the nested-name-specifier down to 511 // a declaration context. 512 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) 513 return NestedNameSpecifier::Create(Context, Prefix, 514 515 Alias->getNamespace()); 516 517 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 518 return NestedNameSpecifier::Create(Context, Prefix, false, 519 T.getTypePtr()); 520 } 521 522 // Otherwise, we have an error case. If we don't want diagnostics, just 523 // return an error now. 524 if (ErrorRecoveryLookup) 525 return 0; 526 527 // If we didn't find anything during our lookup, try again with 528 // ordinary name lookup, which can help us produce better error 529 // messages. 530 if (Found.empty()) { 531 Found.clear(LookupOrdinaryName); 532 LookupName(Found, S); 533 } 534 535 unsigned DiagID; 536 if (!Found.empty()) 537 DiagID = diag::err_expected_class_or_namespace; 538 else if (SS.isSet()) { 539 Diag(IdLoc, diag::err_no_member) << &II << LookupCtx << SS.getRange(); 540 return 0; 541 } else 542 DiagID = diag::err_undeclared_var_use; 543 544 if (SS.isSet()) 545 Diag(IdLoc, DiagID) << &II << SS.getRange(); 546 else 547 Diag(IdLoc, DiagID) << &II; 548 549 return 0; 550} 551 552/// ActOnCXXNestedNameSpecifier - Called during parsing of a 553/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now 554/// we want to resolve "bar::". 'SS' is empty or the previously parsed 555/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar', 556/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'. 557/// Returns a CXXScopeTy* object representing the C++ scope. 558Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, 559 CXXScopeSpec &SS, 560 SourceLocation IdLoc, 561 SourceLocation CCLoc, 562 IdentifierInfo &II, 563 TypeTy *ObjectTypePtr, 564 bool EnteringContext) { 565 return BuildCXXNestedNameSpecifier(S, SS, IdLoc, CCLoc, II, 566 QualType::getFromOpaquePtr(ObjectTypePtr), 567 /*ScopeLookupResult=*/0, EnteringContext, 568 false); 569} 570 571/// IsInvalidUnlessNestedName - This method is used for error recovery 572/// purposes to determine whether the specified identifier is only valid as 573/// a nested name specifier, for example a namespace name. It is 574/// conservatively correct to always return false from this method. 575/// 576/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. 577bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, 578 IdentifierInfo &II, TypeTy *ObjectType, 579 bool EnteringContext) { 580 return BuildCXXNestedNameSpecifier(S, SS, SourceLocation(), SourceLocation(), 581 II, QualType::getFromOpaquePtr(ObjectType), 582 /*ScopeLookupResult=*/0, EnteringContext, 583 true); 584} 585 586Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, 587 const CXXScopeSpec &SS, 588 TypeTy *Ty, 589 SourceRange TypeRange, 590 SourceLocation CCLoc) { 591 NestedNameSpecifier *Prefix 592 = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); 593 QualType T = GetTypeFromParser(Ty); 594 return NestedNameSpecifier::Create(Context, Prefix, /*FIXME:*/false, 595 T.getTypePtr()); 596} 597 598bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 599 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 600 601 NestedNameSpecifier *Qualifier = 602 static_cast<NestedNameSpecifier*>(SS.getScopeRep()); 603 604 // There are only two places a well-formed program may qualify a 605 // declarator: first, when defining a namespace or class member 606 // out-of-line, and second, when naming an explicitly-qualified 607 // friend function. The latter case is governed by 608 // C++03 [basic.lookup.unqual]p10: 609 // In a friend declaration naming a member function, a name used 610 // in the function declarator and not part of a template-argument 611 // in a template-id is first looked up in the scope of the member 612 // function's class. If it is not found, or if the name is part of 613 // a template-argument in a template-id, the look up is as 614 // described for unqualified names in the definition of the class 615 // granting friendship. 616 // i.e. we don't push a scope unless it's a class member. 617 618 switch (Qualifier->getKind()) { 619 case NestedNameSpecifier::Global: 620 case NestedNameSpecifier::Namespace: 621 // These are always namespace scopes. We never want to enter a 622 // namespace scope from anything but a file context. 623 return CurContext->getLookupContext()->isFileContext(); 624 625 case NestedNameSpecifier::Identifier: 626 case NestedNameSpecifier::TypeSpec: 627 case NestedNameSpecifier::TypeSpecWithTemplate: 628 // These are never namespace scopes. 629 return true; 630 } 631 632 // Silence bogus warning. 633 return false; 634} 635 636/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 637/// scope or nested-name-specifier) is parsed, part of a declarator-id. 638/// After this method is called, according to [C++ 3.4.3p3], names should be 639/// looked up in the declarator-id's scope, until the declarator is parsed and 640/// ActOnCXXExitDeclaratorScope is called. 641/// The 'SS' should be a non-empty valid CXXScopeSpec. 642bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { 643 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 644 645 if (SS.isInvalid()) return true; 646 647 DeclContext *DC = computeDeclContext(SS, true); 648 if (!DC) return true; 649 650 // Before we enter a declarator's context, we need to make sure that 651 // it is a complete declaration context. 652 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) 653 return true; 654 655 EnterDeclaratorContext(S, DC); 656 657 // Rebuild the nested name specifier for the new scope. 658 if (DC->isDependentContext()) 659 RebuildNestedNameSpecifierInCurrentInstantiation(SS); 660 661 return false; 662} 663 664/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 665/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 666/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 667/// Used to indicate that names should revert to being looked up in the 668/// defining scope. 669void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 670 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 671 if (SS.isInvalid()) 672 return; 673 assert(!SS.isInvalid() && computeDeclContext(SS, true) && 674 "exiting declarator scope we never really entered"); 675 ExitDeclaratorContext(S); 676} 677