SemaCXXScopeSpec.cpp revision 360784
150276Speter//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// 2184989Srafan// 350276Speter// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 450276Speter// See https://llvm.org/LICENSE.txt for license information. 550276Speter// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 650276Speter// 750276Speter//===----------------------------------------------------------------------===// 850276Speter// 950276Speter// This file implements C++ semantic analysis for scope specifiers. 1050276Speter// 1150276Speter//===----------------------------------------------------------------------===// 1250276Speter 1350276Speter#include "TypeLocBuilder.h" 1450276Speter#include "clang/AST/ASTContext.h" 1550276Speter#include "clang/AST/DeclTemplate.h" 1650276Speter#include "clang/AST/ExprCXX.h" 1750276Speter#include "clang/AST/NestedNameSpecifier.h" 1850276Speter#include "clang/Basic/PartialDiagnostic.h" 1950276Speter#include "clang/Sema/DeclSpec.h" 2050276Speter#include "clang/Sema/Lookup.h" 2150276Speter#include "clang/Sema/SemaInternal.h" 2250276Speter#include "clang/Sema/Template.h" 2350276Speter#include "llvm/ADT/STLExtras.h" 2450276Speterusing namespace clang; 2550276Speter 2650276Speter/// Find the current instantiation that associated with the given type. 2750276Speterstatic CXXRecordDecl *getCurrentInstantiationOf(QualType T, 2850276Speter DeclContext *CurContext) { 2950276Speter if (T.isNull()) 30166124Srafan return nullptr; 3150276Speter 3250276Speter const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 3350276Speter if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 3450276Speter CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 3550276Speter if (!Record->isDependentContext() || 3650276Speter Record->isCurrentInstantiation(CurContext)) 3750276Speter return Record; 3850276Speter 3950276Speter return nullptr; 40184989Srafan } else if (isa<InjectedClassNameType>(Ty)) 4150276Speter return cast<InjectedClassNameType>(Ty)->getDecl(); 4250276Speter else 4350276Speter return nullptr; 4450276Speter} 4550276Speter 4650276Speter/// Compute the DeclContext that is associated with the given type. 4750276Speter/// 4850276Speter/// \param T the type for which we are attempting to find a DeclContext. 4950276Speter/// 5050276Speter/// \returns the declaration context represented by the type T, 5150276Speter/// or NULL if the declaration context cannot be computed (e.g., because it is 5250276Speter/// dependent and not the current instantiation). 5350276SpeterDeclContext *Sema::computeDeclContext(QualType T) { 5450276Speter if (!T->isDependentType()) 5550276Speter if (const TagType *Tag = T->getAs<TagType>()) 5650276Speter return Tag->getDecl(); 5750276Speter 5850276Speter return ::getCurrentInstantiationOf(T, CurContext); 5950276Speter} 6050276Speter 6150276Speter/// Compute the DeclContext that is associated with the given 6250276Speter/// scope specifier. 6350276Speter/// 6450276Speter/// \param SS the C++ scope specifier as it appears in the source 65166124Srafan/// 66166124Srafan/// \param EnteringContext when true, we will be entering the context of 67166124Srafan/// this scope specifier, so we can retrieve the declaration context of a 68166124Srafan/// class template or class template partial specialization even if it is 69166124Srafan/// not the current instantiation. 70166124Srafan/// 7150276Speter/// \returns the declaration context represented by the scope specifier @p SS, 72166124Srafan/// or NULL if the declaration context cannot be computed (e.g., because it is 73166124Srafan/// dependent and not the current instantiation). 7450276SpeterDeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, 75166124Srafan bool EnteringContext) { 7650276Speter if (!SS.isSet() || SS.isInvalid()) 77184989Srafan return nullptr; 78166124Srafan 7950276Speter NestedNameSpecifier *NNS = SS.getScopeRep(); 8050276Speter if (NNS->isDependent()) { 8150276Speter // If this nested-name-specifier refers to the current 8250276Speter // instantiation, return its DeclContext. 8350276Speter if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 84166124Srafan return Record; 85166124Srafan 86166124Srafan if (EnteringContext) { 8750276Speter const Type *NNSType = NNS->getAsType(); 8850276Speter if (!NNSType) { 89166124Srafan return nullptr; 9050276Speter } 9150276Speter 9250276Speter // Look through type alias templates, per C++0x [temp.dep.type]p1. 9350276Speter NNSType = Context.getCanonicalType(NNSType); 9450276Speter if (const TemplateSpecializationType *SpecType 9550276Speter = NNSType->getAs<TemplateSpecializationType>()) { 9650276Speter // We are entering the context of the nested name specifier, so try to 9750276Speter // match the nested name specifier to either a primary class template 9850276Speter // or a class template partial specialization. 9950276Speter if (ClassTemplateDecl *ClassTemplate 10050276Speter = dyn_cast_or_null<ClassTemplateDecl>( 10150276Speter SpecType->getTemplateName().getAsTemplateDecl())) { 10250276Speter QualType ContextType 10350276Speter = Context.getCanonicalType(QualType(SpecType, 0)); 10450276Speter 10550276Speter // If the type of the nested name specifier is the same as the 10650276Speter // injected class name of the named class template, we're entering 10750276Speter // into that class template definition. 10850276Speter QualType Injected 10950276Speter = ClassTemplate->getInjectedClassNameSpecialization(); 11050276Speter if (Context.hasSameType(Injected, ContextType)) 11150276Speter return ClassTemplate->getTemplatedDecl(); 11250276Speter 11350276Speter // If the type of the nested name specifier is the same as the 11450276Speter // type of one of the class template's class template partial 11550276Speter // specializations, we're entering into the definition of that 11650276Speter // class template partial specialization. 11776726Speter if (ClassTemplatePartialSpecializationDecl *PartialSpec 11876726Speter = ClassTemplate->findPartialSpecialization(ContextType)) { 119166124Srafan // A declaration of the partial specialization must be visible. 12050276Speter // We can always recover here, because this only happens when we're 12150276Speter // entering the context, and that can't happen in a SFINAE context. 122166124Srafan assert(!isSFINAEContext() && 12350276Speter "partial specialization scope specifier in SFINAE context?"); 124166124Srafan if (!hasVisibleDeclaration(PartialSpec)) 125166124Srafan diagnoseMissingImport(SS.getLastQualifierNameLoc(), PartialSpec, 126166124Srafan MissingImportKind::PartialSpecialization, 12750276Speter /*Recover*/true); 12850276Speter return PartialSpec; 129166124Srafan } 13050276Speter } 13150276Speter } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { 132166124Srafan // The nested name specifier refers to a member of a class template. 133166124Srafan return RecordT->getDecl(); 13450276Speter } 13550276Speter } 136166124Srafan 13750276Speter return nullptr; 138166124Srafan } 139166124Srafan 140166124Srafan switch (NNS->getKind()) { 141166124Srafan case NestedNameSpecifier::Identifier: 14250276Speter llvm_unreachable("Dependent nested-name-specifier has no DeclContext"); 143166124Srafan 14450276Speter case NestedNameSpecifier::Namespace: 14550276Speter return NNS->getAsNamespace(); 14650276Speter 14750276Speter case NestedNameSpecifier::NamespaceAlias: 148166124Srafan return NNS->getAsNamespaceAlias()->getNamespace(); 149166124Srafan 150166124Srafan case NestedNameSpecifier::TypeSpec: 151166124Srafan case NestedNameSpecifier::TypeSpecWithTemplate: { 152166124Srafan const TagType *Tag = NNS->getAsType()->getAs<TagType>(); 153166124Srafan assert(Tag && "Non-tag type in nested-name-specifier"); 154166124Srafan return Tag->getDecl(); 155166124Srafan } 156166124Srafan 157166124Srafan case NestedNameSpecifier::Global: 158166124Srafan return Context.getTranslationUnitDecl(); 159166124Srafan 160166124Srafan case NestedNameSpecifier::Super: 161166124Srafan return NNS->getAsRecordDecl(); 162166124Srafan } 16350276Speter 164166124Srafan llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 165166124Srafan} 166166124Srafan 167166124Srafanbool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 168166124Srafan if (!SS.isSet() || SS.isInvalid()) 16950276Speter return false; 17050276Speter 17150276Speter return SS.getScopeRep()->isDependent(); 172166124Srafan} 17350276Speter 17450276Speter/// If the given nested name specifier refers to the current 17550276Speter/// instantiation, return the declaration that corresponds to that 17650276Speter/// current instantiation (C++0x [temp.dep.type]p1). 17750276Speter/// 178166124Srafan/// \param NNS a dependent nested name specifier. 179166124SrafanCXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 180166124Srafan assert(getLangOpts().CPlusPlus && "Only callable in C++"); 181166124Srafan assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 182166124Srafan 183166124Srafan if (!NNS->getAsType()) 184166124Srafan return nullptr; 185166124Srafan 18650276Speter QualType T = QualType(NNS->getAsType(), 0); 18750276Speter return ::getCurrentInstantiationOf(T, CurContext); 18850276Speter} 189166124Srafan 19050276Speter/// Require that the context specified by SS be complete. 19150276Speter/// 19250276Speter/// If SS refers to a type, this routine checks whether the type is 19350276Speter/// complete enough (or can be made complete enough) for name lookup 19450276Speter/// into the DeclContext. A type that is not yet completed can be 19550276Speter/// considered "complete enough" if it is a class/struct/union/enum 19650276Speter/// that is currently being defined. Or, if we have a type that names 19750276Speter/// a class template specialization that is not a complete type, we 19850276Speter/// will attempt to instantiate that class template. 19950276Speterbool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, 20050276Speter DeclContext *DC) { 20150276Speter assert(DC && "given null context"); 20250276Speter 20350276Speter TagDecl *tag = dyn_cast<TagDecl>(DC); 20450276Speter 20550276Speter // If this is a dependent type, then we consider it complete. 20650276Speter // FIXME: This is wrong; we should require a (visible) definition to 20750276Speter // exist in this case too. 20850276Speter if (!tag || tag->isDependentContext()) 20950276Speter return false; 21076726Speter 211166124Srafan // Grab the tag definition, if there is one. 21250276Speter QualType type = Context.getTypeDeclType(tag); 21350276Speter tag = type->getAsTagDecl(); 21450276Speter 21550276Speter // If we're currently defining this type, then lookup into the 21650276Speter // type is okay: don't complain that it isn't complete yet. 21750276Speter if (tag->isBeingDefined()) 21850276Speter return false; 21950276Speter 22050276Speter SourceLocation loc = SS.getLastQualifierNameLoc(); 22150276Speter if (loc.isInvalid()) loc = SS.getRange().getBegin(); 22250276Speter 223166124Srafan // The type must be complete. 224166124Srafan if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec, 22550276Speter SS.getRange())) { 22650276Speter SS.SetInvalid(SS.getRange()); 22750276Speter return true; 228166124Srafan } 22950276Speter 230166124Srafan // Fixed enum types are complete, but they aren't valid as scopes 23150276Speter // until we see a definition, so awkwardly pull out this special 23250276Speter // case. 23350276Speter auto *EnumD = dyn_cast<EnumDecl>(tag); 23450276Speter if (!EnumD) 235166124Srafan return false; 236166124Srafan if (EnumD->isCompleteDefinition()) { 23750276Speter // If we know about the definition but it is not visible, complain. 238166124Srafan NamedDecl *SuggestedDef = nullptr; 239166124Srafan if (!hasVisibleDefinition(EnumD, &SuggestedDef, 240166124Srafan /*OnlyNeedComplete*/false)) { 241166124Srafan // If the user is going to see an error here, recover by making the 242166124Srafan // definition visible. 243166124Srafan bool TreatAsComplete = !isSFINAEContext(); 244166124Srafan diagnoseMissingImport(loc, SuggestedDef, MissingImportKind::Definition, 245166124Srafan /*Recover*/TreatAsComplete); 24650276Speter return !TreatAsComplete; 247166124Srafan } 248166124Srafan return false; 249166124Srafan } 25050276Speter 251166124Srafan // Try to instantiate the definition, if this is a specialization of an 252166124Srafan // enumeration temploid. 25350276Speter if (EnumDecl *Pattern = EnumD->getInstantiatedFromMemberEnum()) { 254166124Srafan MemberSpecializationInfo *MSI = EnumD->getMemberSpecializationInfo(); 25550276Speter if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) { 256166124Srafan if (InstantiateEnum(loc, EnumD, Pattern, 257166124Srafan getTemplateInstantiationArgs(EnumD), 25850276Speter TSK_ImplicitInstantiation)) { 259166124Srafan SS.SetInvalid(SS.getRange()); 26050276Speter return true; 261166124Srafan } 262166124Srafan return false; 26350276Speter } 264166124Srafan } 26550276Speter 266166124Srafan Diag(loc, diag::err_incomplete_nested_name_spec) 267166124Srafan << type << SS.getRange(); 26850276Speter SS.SetInvalid(SS.getRange()); 269166124Srafan return true; 27050276Speter} 27150276Speter 272166124Srafanbool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc, 273166124Srafan CXXScopeSpec &SS) { 274166124Srafan SS.MakeGlobal(Context, CCLoc); 275166124Srafan return false; 276166124Srafan} 277166124Srafan 278166124Srafanbool Sema::ActOnSuperScopeSpecifier(SourceLocation SuperLoc, 27950276Speter SourceLocation ColonColonLoc, 280166124Srafan CXXScopeSpec &SS) { 28150276Speter CXXRecordDecl *RD = nullptr; 28250276Speter for (Scope *S = getCurScope(); S; S = S->getParent()) { 283166124Srafan if (S->isFunctionScope()) { 284166124Srafan if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(S->getEntity())) 285166124Srafan RD = MD->getParent(); 286166124Srafan break; 287166124Srafan } 288166124Srafan if (S->isClassScope()) { 289166124Srafan RD = cast<CXXRecordDecl>(S->getEntity()); 290166124Srafan break; 291166124Srafan } 29250276Speter } 293166124Srafan 294166124Srafan if (!RD) { 29550276Speter Diag(SuperLoc, diag::err_invalid_super_scope); 296166124Srafan return true; 29750276Speter } else if (RD->isLambda()) { 29850276Speter Diag(SuperLoc, diag::err_super_in_lambda_unsupported); 299166124Srafan return true; 300166124Srafan } else if (RD->getNumBases() == 0) { 301166124Srafan Diag(SuperLoc, diag::err_no_base_classes) << RD->getName(); 302166124Srafan return true; 303166124Srafan } 304166124Srafan 305166124Srafan SS.MakeSuper(Context, RD, SuperLoc, ColonColonLoc); 306184989Srafan return false; 307166124Srafan} 308166124Srafan 309166124Srafan/// Determines whether the given declaration is an valid acceptable 31050276Speter/// result for name lookup of a nested-name-specifier. 311166124Srafan/// \param SD Declaration checked for nested-name-specifier. 31250276Speter/// \param IsExtension If not null and the declaration is accepted as an 31350276Speter/// extension, the pointed variable is assigned true. 314166124Srafanbool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD, 315166124Srafan bool *IsExtension) { 316166124Srafan if (!SD) 317166124Srafan return false; 318166124Srafan 319184989Srafan SD = SD->getUnderlyingDecl(); 320166124Srafan 321166124Srafan // Namespace and namespace aliases are fine. 322166124Srafan if (isa<NamespaceDecl>(SD)) 32350276Speter return true; 324166124Srafan 32550276Speter if (!isa<TypeDecl>(SD)) 326166124Srafan return false; 327166124Srafan 32850276Speter // Determine whether we have a class (or, in C++11, an enum) or 329166124Srafan // a typedef thereof. If so, build the nested-name-specifier. 33050276Speter QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 331166124Srafan if (T->isDependentType()) 332166124Srafan return true; 33350276Speter if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { 334166124Srafan if (TD->getUnderlyingType()->isRecordType()) 33550276Speter return true; 336166124Srafan if (TD->getUnderlyingType()->isEnumeralType()) { 337166124Srafan if (Context.getLangOpts().CPlusPlus11) 338166124Srafan return true; 339166124Srafan if (IsExtension) 340166124Srafan *IsExtension = true; 341166124Srafan } 342166124Srafan } else if (isa<RecordDecl>(SD)) { 343166124Srafan return true; 344166124Srafan } else if (isa<EnumDecl>(SD)) { 345166124Srafan if (Context.getLangOpts().CPlusPlus11) 34650276Speter return true; 347166124Srafan if (IsExtension) 34850276Speter *IsExtension = true; 349166124Srafan } 350166124Srafan 351166124Srafan return false; 352166124Srafan} 353166124Srafan 354166124Srafan/// If the given nested-name-specifier begins with a bare identifier 355166124Srafan/// (e.g., Base::), perform name lookup for that identifier as a 356166124Srafan/// nested-name-specifier within the given scope, and return the result of that 357166124Srafan/// name lookup. 358166124SrafanNamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { 35950276Speter if (!S || !NNS) 360166124Srafan return nullptr; 36150276Speter 362166124Srafan while (NNS->getPrefix()) 363166124Srafan NNS = NNS->getPrefix(); 364166124Srafan 365166124Srafan if (NNS->getKind() != NestedNameSpecifier::Identifier) 366166124Srafan return nullptr; 367166124Srafan 368166124Srafan LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), 369166124Srafan LookupNestedNameSpecifierName); 370166124Srafan LookupName(Found, S); 371166124Srafan assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); 372166124Srafan 373166124Srafan if (!Found.isSingleResult()) 374166124Srafan return nullptr; 375166124Srafan 376166124Srafan NamedDecl *Result = Found.getFoundDecl(); 377166124Srafan if (isAcceptableNestedNameSpecifier(Result)) 37850276Speter return Result; 379166124Srafan 38050276Speter return nullptr; 381166124Srafan} 382166124Srafan 38350276Speterbool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, 384166124Srafan NestedNameSpecInfo &IdInfo) { 38550276Speter QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType); 386166124Srafan LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc, 387166124Srafan LookupNestedNameSpecifierName); 388166124Srafan 389166124Srafan // Determine where to perform name lookup 390166124Srafan DeclContext *LookupCtx = nullptr; 391166124Srafan bool isDependent = false; 392166124Srafan if (!ObjectType.isNull()) { 393166124Srafan // This nested-name-specifier occurs in a member access expression, e.g., 394166124Srafan // x->B::f, and we are looking into the type of the object. 39550276Speter assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 396166124Srafan LookupCtx = computeDeclContext(ObjectType); 39750276Speter isDependent = ObjectType->isDependentType(); 398166124Srafan } else if (SS.isSet()) { 399166124Srafan // This nested-name-specifier occurs after another nested-name-specifier, 400166124Srafan // so long into the context associated with the prior nested-name-specifier. 401166124Srafan LookupCtx = computeDeclContext(SS, false); 402166124Srafan isDependent = isDependentScopeSpecifier(SS); 403166124Srafan Found.setContextRange(SS.getRange()); 404166124Srafan } 405166124Srafan 406166124Srafan if (LookupCtx) { 407166124Srafan // Perform "qualified" name lookup into the declaration context we 408166124Srafan // computed, which is either the type of the base of a member access 409166124Srafan // expression or the declaration context associated with a prior 410166124Srafan // nested-name-specifier. 411166124Srafan 412166124Srafan // The declaration context must be complete. 413166124Srafan if (!LookupCtx->isDependentContext() && 41450276Speter RequireCompleteDeclContext(SS, LookupCtx)) 415166124Srafan return false; 41650276Speter 417166124Srafan LookupQualifiedName(Found, LookupCtx); 418166124Srafan } else if (isDependent) { 419166124Srafan return false; 420166124Srafan } else { 421166124Srafan LookupName(Found, S); 422166124Srafan } 423166124Srafan Found.suppressDiagnostics(); 424166124Srafan 425166124Srafan return Found.getAsSingle<NamespaceDecl>(); 426166124Srafan} 427166124Srafan 428166124Srafannamespace { 429166124Srafan 430166124Srafan// Callback to only accept typo corrections that can be a valid C++ member 431166124Srafan// intializer: either a non-static field member or a base class. 432166124Srafanclass NestedNameSpecifierValidatorCCC final 43350276Speter : public CorrectionCandidateCallback { 434166124Srafanpublic: 43550276Speter explicit NestedNameSpecifierValidatorCCC(Sema &SRef) 436166124Srafan : SRef(SRef) {} 437166124Srafan 438166124Srafan bool ValidateCandidate(const TypoCorrection &candidate) override { 439166124Srafan return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl()); 440166124Srafan } 441166124Srafan 442166124Srafan std::unique_ptr<CorrectionCandidateCallback> clone() override { 443166124Srafan return std::make_unique<NestedNameSpecifierValidatorCCC>(*this); 44450276Speter } 445166124Srafan 446166124Srafan private: 447166124Srafan Sema &SRef; 448166124Srafan}; 44950276Speter 450166124Srafan} 451166124Srafan 452166124Srafan/// Build a new nested-name-specifier for "identifier::", as described 453166124Srafan/// by ActOnCXXNestedNameSpecifier. 454166124Srafan/// 455166124Srafan/// \param S Scope in which the nested-name-specifier occurs. 456166124Srafan/// \param IdInfo Parser information about an identifier in the 457166124Srafan/// nested-name-spec. 458166124Srafan/// \param EnteringContext If true, enter the context specified by the 459166124Srafan/// nested-name-specifier. 46050276Speter/// \param SS Optional nested name specifier preceding the identifier. 461166124Srafan/// \param ScopeLookupResult Provides the result of name lookup within the 462166124Srafan/// scope of the nested-name-specifier that was computed at template 463166124Srafan/// definition time. 464166124Srafan/// \param ErrorRecoveryLookup Specifies if the method is called to improve 465166124Srafan/// error recovery and what kind of recovery is performed. 466166124Srafan/// \param IsCorrectedToColon If not null, suggestion of replace '::' -> ':' 467166124Srafan/// are allowed. The bool value pointed by this parameter is set to 468166124Srafan/// 'true' if the identifier is treated as if it was followed by ':', 469166124Srafan/// not '::'. 470166124Srafan/// \param OnlyNamespace If true, only considers namespaces in lookup. 471166124Srafan/// 472166124Srafan/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in 473166124Srafan/// that it contains an extra parameter \p ScopeLookupResult, which provides 474166124Srafan/// the result of name lookup within the scope of the nested-name-specifier 475166124Srafan/// that was computed at template definition time. 476166124Srafan/// 477166124Srafan/// If ErrorRecoveryLookup is true, then this call is used to improve error 478166124Srafan/// recovery. This means that it should not emit diagnostics, it should 479166124Srafan/// just return true on failure. It also means it should only return a valid 480166124Srafan/// scope if it *knows* that the result is correct. It should not return in a 481166124Srafan/// dependent context, for example. Nor will it extend \p SS with the scope 482166124Srafan/// specifier. 483166124Srafanbool Sema::BuildCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo, 484166124Srafan bool EnteringContext, CXXScopeSpec &SS, 485166124Srafan NamedDecl *ScopeLookupResult, 486166124Srafan bool ErrorRecoveryLookup, 487166124Srafan bool *IsCorrectedToColon, 488166124Srafan bool OnlyNamespace) { 489166124Srafan if (IdInfo.Identifier->isEditorPlaceholder()) 490166124Srafan return true; 491166124Srafan LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc, 492166124Srafan OnlyNamespace ? LookupNamespaceName 493166124Srafan : LookupNestedNameSpecifierName); 494166124Srafan QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType); 495166124Srafan 496166124Srafan // Determine where to perform name lookup 497166124Srafan DeclContext *LookupCtx = nullptr; 498166124Srafan bool isDependent = false; 499166124Srafan if (IsCorrectedToColon) 500166124Srafan *IsCorrectedToColon = false; 501166124Srafan if (!ObjectType.isNull()) { 502166124Srafan // This nested-name-specifier occurs in a member access expression, e.g., 503166124Srafan // x->B::f, and we are looking into the type of the object. 504166124Srafan assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 505166124Srafan LookupCtx = computeDeclContext(ObjectType); 506166124Srafan isDependent = ObjectType->isDependentType(); 507166124Srafan } else if (SS.isSet()) { 508166124Srafan // This nested-name-specifier occurs after another nested-name-specifier, 509166124Srafan // so look into the context associated with the prior nested-name-specifier. 510166124Srafan LookupCtx = computeDeclContext(SS, EnteringContext); 511166124Srafan isDependent = isDependentScopeSpecifier(SS); 512166124Srafan Found.setContextRange(SS.getRange()); 513166124Srafan } 51450276Speter 51550276Speter bool ObjectTypeSearchedInScope = false; 51650276Speter if (LookupCtx) { 51750276Speter // Perform "qualified" name lookup into the declaration context we 518166124Srafan // computed, which is either the type of the base of a member access 519166124Srafan // expression or the declaration context associated with a prior 520166124Srafan // nested-name-specifier. 521166124Srafan 522166124Srafan // The declaration context must be complete. 523166124Srafan if (!LookupCtx->isDependentContext() && 524166124Srafan RequireCompleteDeclContext(SS, LookupCtx)) 525166124Srafan return true; 526166124Srafan 527166124Srafan LookupQualifiedName(Found, LookupCtx); 528166124Srafan 529166124Srafan if (!ObjectType.isNull() && Found.empty()) { 530166124Srafan // C++ [basic.lookup.classref]p4: 531166124Srafan // If the id-expression in a class member access is a qualified-id of 532166124Srafan // the form 533166124Srafan // 53450276Speter // class-name-or-namespace-name::... 535166124Srafan // 536166124Srafan // the class-name-or-namespace-name following the . or -> operator is 537166124Srafan // looked up both in the context of the entire postfix-expression and in 53850276Speter // the scope of the class of the object expression. If the name is found 539166124Srafan // only in the scope of the class of the object expression, the name 54050276Speter // shall refer to a class-name. If the name is found only in the 541166124Srafan // context of the entire postfix-expression, the name shall refer to a 542166124Srafan // class-name or namespace-name. [...] 543166124Srafan // 544166124Srafan // Qualified name lookup into a class will not find a namespace-name, 545166124Srafan // so we do not need to diagnose that case specifically. However, 546166124Srafan // this qualified name lookup may find nothing. In that case, perform 54750276Speter // unqualified name lookup in the given scope (if available) or 548166124Srafan // reconstruct the result from when name lookup was performed at template 54950276Speter // definition time. 55050276Speter if (S) 55150276Speter LookupName(Found, S); 55250276Speter else if (ScopeLookupResult) 55350276Speter Found.addDecl(ScopeLookupResult); 55450276Speter 55550276Speter ObjectTypeSearchedInScope = true; 556 } 557 } else if (!isDependent) { 558 // Perform unqualified name lookup in the current scope. 559 LookupName(Found, S); 560 } 561 562 if (Found.isAmbiguous()) 563 return true; 564 565 // If we performed lookup into a dependent context and did not find anything, 566 // that's fine: just build a dependent nested-name-specifier. 567 if (Found.empty() && isDependent && 568 !(LookupCtx && LookupCtx->isRecord() && 569 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || 570 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) { 571 // Don't speculate if we're just trying to improve error recovery. 572 if (ErrorRecoveryLookup) 573 return true; 574 575 // We were not able to compute the declaration context for a dependent 576 // base object type or prior nested-name-specifier, so this 577 // nested-name-specifier refers to an unknown specialization. Just build 578 // a dependent nested-name-specifier. 579 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, IdInfo.CCLoc); 580 return false; 581 } 582 583 if (Found.empty() && !ErrorRecoveryLookup) { 584 // If identifier is not found as class-name-or-namespace-name, but is found 585 // as other entity, don't look for typos. 586 LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName); 587 if (LookupCtx) 588 LookupQualifiedName(R, LookupCtx); 589 else if (S && !isDependent) 590 LookupName(R, S); 591 if (!R.empty()) { 592 // Don't diagnose problems with this speculative lookup. 593 R.suppressDiagnostics(); 594 // The identifier is found in ordinary lookup. If correction to colon is 595 // allowed, suggest replacement to ':'. 596 if (IsCorrectedToColon) { 597 *IsCorrectedToColon = true; 598 Diag(IdInfo.CCLoc, diag::err_nested_name_spec_is_not_class) 599 << IdInfo.Identifier << getLangOpts().CPlusPlus 600 << FixItHint::CreateReplacement(IdInfo.CCLoc, ":"); 601 if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) 602 Diag(ND->getLocation(), diag::note_declared_at); 603 return true; 604 } 605 // Replacement '::' -> ':' is not allowed, just issue respective error. 606 Diag(R.getNameLoc(), OnlyNamespace 607 ? unsigned(diag::err_expected_namespace_name) 608 : unsigned(diag::err_expected_class_or_namespace)) 609 << IdInfo.Identifier << getLangOpts().CPlusPlus; 610 if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) 611 Diag(ND->getLocation(), diag::note_entity_declared_at) 612 << IdInfo.Identifier; 613 return true; 614 } 615 } 616 617 if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) { 618 // We haven't found anything, and we're not recovering from a 619 // different kind of error, so look for typos. 620 DeclarationName Name = Found.getLookupName(); 621 Found.clear(); 622 NestedNameSpecifierValidatorCCC CCC(*this); 623 if (TypoCorrection Corrected = CorrectTypo( 624 Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, CCC, 625 CTK_ErrorRecovery, LookupCtx, EnteringContext)) { 626 if (LookupCtx) { 627 bool DroppedSpecifier = 628 Corrected.WillReplaceSpecifier() && 629 Name.getAsString() == Corrected.getAsString(getLangOpts()); 630 if (DroppedSpecifier) 631 SS.clear(); 632 diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) 633 << Name << LookupCtx << DroppedSpecifier 634 << SS.getRange()); 635 } else 636 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 637 << Name); 638 639 if (Corrected.getCorrectionSpecifier()) 640 SS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), 641 SourceRange(Found.getNameLoc())); 642 643 if (NamedDecl *ND = Corrected.getFoundDecl()) 644 Found.addDecl(ND); 645 Found.setLookupName(Corrected.getCorrection()); 646 } else { 647 Found.setLookupName(IdInfo.Identifier); 648 } 649 } 650 651 NamedDecl *SD = 652 Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr; 653 bool IsExtension = false; 654 bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension); 655 if (!AcceptSpec && IsExtension) { 656 AcceptSpec = true; 657 Diag(IdInfo.IdentifierLoc, diag::ext_nested_name_spec_is_enum); 658 } 659 if (AcceptSpec) { 660 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope && 661 !getLangOpts().CPlusPlus11) { 662 // C++03 [basic.lookup.classref]p4: 663 // [...] If the name is found in both contexts, the 664 // class-name-or-namespace-name shall refer to the same entity. 665 // 666 // We already found the name in the scope of the object. Now, look 667 // into the current scope (the scope of the postfix-expression) to 668 // see if we can find the same name there. As above, if there is no 669 // scope, reconstruct the result from the template instantiation itself. 670 // 671 // Note that C++11 does *not* perform this redundant lookup. 672 NamedDecl *OuterDecl; 673 if (S) { 674 LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc, 675 LookupNestedNameSpecifierName); 676 LookupName(FoundOuter, S); 677 OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); 678 } else 679 OuterDecl = ScopeLookupResult; 680 681 if (isAcceptableNestedNameSpecifier(OuterDecl) && 682 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && 683 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || 684 !Context.hasSameType( 685 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), 686 Context.getTypeDeclType(cast<TypeDecl>(SD))))) { 687 if (ErrorRecoveryLookup) 688 return true; 689 690 Diag(IdInfo.IdentifierLoc, 691 diag::err_nested_name_member_ref_lookup_ambiguous) 692 << IdInfo.Identifier; 693 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) 694 << ObjectType; 695 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); 696 697 // Fall through so that we'll pick the name we found in the object 698 // type, since that's probably what the user wanted anyway. 699 } 700 } 701 702 if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD)) 703 MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); 704 705 // If we're just performing this lookup for error-recovery purposes, 706 // don't extend the nested-name-specifier. Just return now. 707 if (ErrorRecoveryLookup) 708 return false; 709 710 // The use of a nested name specifier may trigger deprecation warnings. 711 DiagnoseUseOfDecl(SD, IdInfo.CCLoc); 712 713 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) { 714 SS.Extend(Context, Namespace, IdInfo.IdentifierLoc, IdInfo.CCLoc); 715 return false; 716 } 717 718 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) { 719 SS.Extend(Context, Alias, IdInfo.IdentifierLoc, IdInfo.CCLoc); 720 return false; 721 } 722 723 QualType T = 724 Context.getTypeDeclType(cast<TypeDecl>(SD->getUnderlyingDecl())); 725 TypeLocBuilder TLB; 726 if (isa<InjectedClassNameType>(T)) { 727 InjectedClassNameTypeLoc InjectedTL 728 = TLB.push<InjectedClassNameTypeLoc>(T); 729 InjectedTL.setNameLoc(IdInfo.IdentifierLoc); 730 } else if (isa<RecordType>(T)) { 731 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T); 732 RecordTL.setNameLoc(IdInfo.IdentifierLoc); 733 } else if (isa<TypedefType>(T)) { 734 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T); 735 TypedefTL.setNameLoc(IdInfo.IdentifierLoc); 736 } else if (isa<EnumType>(T)) { 737 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T); 738 EnumTL.setNameLoc(IdInfo.IdentifierLoc); 739 } else if (isa<TemplateTypeParmType>(T)) { 740 TemplateTypeParmTypeLoc TemplateTypeTL 741 = TLB.push<TemplateTypeParmTypeLoc>(T); 742 TemplateTypeTL.setNameLoc(IdInfo.IdentifierLoc); 743 } else if (isa<UnresolvedUsingType>(T)) { 744 UnresolvedUsingTypeLoc UnresolvedTL 745 = TLB.push<UnresolvedUsingTypeLoc>(T); 746 UnresolvedTL.setNameLoc(IdInfo.IdentifierLoc); 747 } else if (isa<SubstTemplateTypeParmType>(T)) { 748 SubstTemplateTypeParmTypeLoc TL 749 = TLB.push<SubstTemplateTypeParmTypeLoc>(T); 750 TL.setNameLoc(IdInfo.IdentifierLoc); 751 } else if (isa<SubstTemplateTypeParmPackType>(T)) { 752 SubstTemplateTypeParmPackTypeLoc TL 753 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T); 754 TL.setNameLoc(IdInfo.IdentifierLoc); 755 } else { 756 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier"); 757 } 758 759 if (T->isEnumeralType()) 760 Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec); 761 762 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 763 IdInfo.CCLoc); 764 return false; 765 } 766 767 // Otherwise, we have an error case. If we don't want diagnostics, just 768 // return an error now. 769 if (ErrorRecoveryLookup) 770 return true; 771 772 // If we didn't find anything during our lookup, try again with 773 // ordinary name lookup, which can help us produce better error 774 // messages. 775 if (Found.empty()) { 776 Found.clear(LookupOrdinaryName); 777 LookupName(Found, S); 778 } 779 780 // In Microsoft mode, if we are within a templated function and we can't 781 // resolve Identifier, then extend the SS with Identifier. This will have 782 // the effect of resolving Identifier during template instantiation. 783 // The goal is to be able to resolve a function call whose 784 // nested-name-specifier is located inside a dependent base class. 785 // Example: 786 // 787 // class C { 788 // public: 789 // static void foo2() { } 790 // }; 791 // template <class T> class A { public: typedef C D; }; 792 // 793 // template <class T> class B : public A<T> { 794 // public: 795 // void foo() { D::foo2(); } 796 // }; 797 if (getLangOpts().MSVCCompat) { 798 DeclContext *DC = LookupCtx ? LookupCtx : CurContext; 799 if (DC->isDependentContext() && DC->isFunctionOrMethod()) { 800 CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent()); 801 if (ContainingClass && ContainingClass->hasAnyDependentBases()) { 802 Diag(IdInfo.IdentifierLoc, 803 diag::ext_undeclared_unqual_id_with_dependent_base) 804 << IdInfo.Identifier << ContainingClass; 805 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, 806 IdInfo.CCLoc); 807 return false; 808 } 809 } 810 } 811 812 if (!Found.empty()) { 813 if (TypeDecl *TD = Found.getAsSingle<TypeDecl>()) 814 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace) 815 << Context.getTypeDeclType(TD) << getLangOpts().CPlusPlus; 816 else { 817 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace) 818 << IdInfo.Identifier << getLangOpts().CPlusPlus; 819 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 820 Diag(ND->getLocation(), diag::note_entity_declared_at) 821 << IdInfo.Identifier; 822 } 823 } else if (SS.isSet()) 824 Diag(IdInfo.IdentifierLoc, diag::err_no_member) << IdInfo.Identifier 825 << LookupCtx << SS.getRange(); 826 else 827 Diag(IdInfo.IdentifierLoc, diag::err_undeclared_var_use) 828 << IdInfo.Identifier; 829 830 return true; 831} 832 833bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo, 834 bool EnteringContext, CXXScopeSpec &SS, 835 bool ErrorRecoveryLookup, 836 bool *IsCorrectedToColon, 837 bool OnlyNamespace) { 838 if (SS.isInvalid()) 839 return true; 840 841 return BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS, 842 /*ScopeLookupResult=*/nullptr, false, 843 IsCorrectedToColon, OnlyNamespace); 844} 845 846bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS, 847 const DeclSpec &DS, 848 SourceLocation ColonColonLoc) { 849 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) 850 return true; 851 852 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype); 853 854 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); 855 if (T.isNull()) 856 return true; 857 858 if (!T->isDependentType() && !T->getAs<TagType>()) { 859 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace) 860 << T << getLangOpts().CPlusPlus; 861 return true; 862 } 863 864 TypeLocBuilder TLB; 865 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); 866 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); 867 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 868 ColonColonLoc); 869 return false; 870} 871 872/// IsInvalidUnlessNestedName - This method is used for error recovery 873/// purposes to determine whether the specified identifier is only valid as 874/// a nested name specifier, for example a namespace name. It is 875/// conservatively correct to always return false from this method. 876/// 877/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. 878bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, 879 NestedNameSpecInfo &IdInfo, 880 bool EnteringContext) { 881 if (SS.isInvalid()) 882 return false; 883 884 return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS, 885 /*ScopeLookupResult=*/nullptr, true); 886} 887 888bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 889 CXXScopeSpec &SS, 890 SourceLocation TemplateKWLoc, 891 TemplateTy OpaqueTemplate, 892 SourceLocation TemplateNameLoc, 893 SourceLocation LAngleLoc, 894 ASTTemplateArgsPtr TemplateArgsIn, 895 SourceLocation RAngleLoc, 896 SourceLocation CCLoc, 897 bool EnteringContext) { 898 if (SS.isInvalid()) 899 return true; 900 901 TemplateName Template = OpaqueTemplate.get(); 902 903 // Translate the parser's template argument list in our AST format. 904 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 905 translateTemplateArguments(TemplateArgsIn, TemplateArgs); 906 907 DependentTemplateName *DTN = Template.getAsDependentTemplateName(); 908 if (DTN && DTN->isIdentifier()) { 909 // Handle a dependent template specialization for which we cannot resolve 910 // the template name. 911 assert(DTN->getQualifier() == SS.getScopeRep()); 912 QualType T = Context.getDependentTemplateSpecializationType(ETK_None, 913 DTN->getQualifier(), 914 DTN->getIdentifier(), 915 TemplateArgs); 916 917 // Create source-location information for this type. 918 TypeLocBuilder Builder; 919 DependentTemplateSpecializationTypeLoc SpecTL 920 = Builder.push<DependentTemplateSpecializationTypeLoc>(T); 921 SpecTL.setElaboratedKeywordLoc(SourceLocation()); 922 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); 923 SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 924 SpecTL.setTemplateNameLoc(TemplateNameLoc); 925 SpecTL.setLAngleLoc(LAngleLoc); 926 SpecTL.setRAngleLoc(RAngleLoc); 927 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 928 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 929 930 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 931 CCLoc); 932 return false; 933 } 934 935 // If we assumed an undeclared identifier was a template name, try to 936 // typo-correct it now. 937 if (Template.getAsAssumedTemplateName() && 938 resolveAssumedTemplateNameAsType(S, Template, TemplateNameLoc)) 939 return true; 940 941 TemplateDecl *TD = Template.getAsTemplateDecl(); 942 if (Template.getAsOverloadedTemplate() || DTN || 943 isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) { 944 SourceRange R(TemplateNameLoc, RAngleLoc); 945 if (SS.getRange().isValid()) 946 R.setBegin(SS.getRange().getBegin()); 947 948 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier) 949 << (TD && isa<VarTemplateDecl>(TD)) << Template << R; 950 NoteAllFoundTemplates(Template); 951 return true; 952 } 953 954 // We were able to resolve the template name to an actual template. 955 // Build an appropriate nested-name-specifier. 956 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); 957 if (T.isNull()) 958 return true; 959 960 // Alias template specializations can produce types which are not valid 961 // nested name specifiers. 962 if (!T->isDependentType() && !T->getAs<TagType>()) { 963 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; 964 NoteAllFoundTemplates(Template); 965 return true; 966 } 967 968 // Provide source-location information for the template specialization type. 969 TypeLocBuilder Builder; 970 TemplateSpecializationTypeLoc SpecTL 971 = Builder.push<TemplateSpecializationTypeLoc>(T); 972 SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 973 SpecTL.setTemplateNameLoc(TemplateNameLoc); 974 SpecTL.setLAngleLoc(LAngleLoc); 975 SpecTL.setRAngleLoc(RAngleLoc); 976 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 977 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 978 979 980 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 981 CCLoc); 982 return false; 983} 984 985namespace { 986 /// A structure that stores a nested-name-specifier annotation, 987 /// including both the nested-name-specifier 988 struct NestedNameSpecifierAnnotation { 989 NestedNameSpecifier *NNS; 990 }; 991} 992 993void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) { 994 if (SS.isEmpty() || SS.isInvalid()) 995 return nullptr; 996 997 void *Mem = Context.Allocate( 998 (sizeof(NestedNameSpecifierAnnotation) + SS.location_size()), 999 alignof(NestedNameSpecifierAnnotation)); 1000 NestedNameSpecifierAnnotation *Annotation 1001 = new (Mem) NestedNameSpecifierAnnotation; 1002 Annotation->NNS = SS.getScopeRep(); 1003 memcpy(Annotation + 1, SS.location_data(), SS.location_size()); 1004 return Annotation; 1005} 1006 1007void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr, 1008 SourceRange AnnotationRange, 1009 CXXScopeSpec &SS) { 1010 if (!AnnotationPtr) { 1011 SS.SetInvalid(AnnotationRange); 1012 return; 1013 } 1014 1015 NestedNameSpecifierAnnotation *Annotation 1016 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr); 1017 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1)); 1018} 1019 1020bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 1021 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 1022 1023 // Don't enter a declarator context when the current context is an Objective-C 1024 // declaration. 1025 if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext)) 1026 return false; 1027 1028 NestedNameSpecifier *Qualifier = SS.getScopeRep(); 1029 1030 // There are only two places a well-formed program may qualify a 1031 // declarator: first, when defining a namespace or class member 1032 // out-of-line, and second, when naming an explicitly-qualified 1033 // friend function. The latter case is governed by 1034 // C++03 [basic.lookup.unqual]p10: 1035 // In a friend declaration naming a member function, a name used 1036 // in the function declarator and not part of a template-argument 1037 // in a template-id is first looked up in the scope of the member 1038 // function's class. If it is not found, or if the name is part of 1039 // a template-argument in a template-id, the look up is as 1040 // described for unqualified names in the definition of the class 1041 // granting friendship. 1042 // i.e. we don't push a scope unless it's a class member. 1043 1044 switch (Qualifier->getKind()) { 1045 case NestedNameSpecifier::Global: 1046 case NestedNameSpecifier::Namespace: 1047 case NestedNameSpecifier::NamespaceAlias: 1048 // These are always namespace scopes. We never want to enter a 1049 // namespace scope from anything but a file context. 1050 return CurContext->getRedeclContext()->isFileContext(); 1051 1052 case NestedNameSpecifier::Identifier: 1053 case NestedNameSpecifier::TypeSpec: 1054 case NestedNameSpecifier::TypeSpecWithTemplate: 1055 case NestedNameSpecifier::Super: 1056 // These are never namespace scopes. 1057 return true; 1058 } 1059 1060 llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 1061} 1062 1063/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global 1064/// scope or nested-name-specifier) is parsed, part of a declarator-id. 1065/// After this method is called, according to [C++ 3.4.3p3], names should be 1066/// looked up in the declarator-id's scope, until the declarator is parsed and 1067/// ActOnCXXExitDeclaratorScope is called. 1068/// The 'SS' should be a non-empty valid CXXScopeSpec. 1069bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { 1070 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 1071 1072 if (SS.isInvalid()) return true; 1073 1074 DeclContext *DC = computeDeclContext(SS, true); 1075 if (!DC) return true; 1076 1077 // Before we enter a declarator's context, we need to make sure that 1078 // it is a complete declaration context. 1079 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) 1080 return true; 1081 1082 EnterDeclaratorContext(S, DC); 1083 1084 // Rebuild the nested name specifier for the new scope. 1085 if (DC->isDependentContext()) 1086 RebuildNestedNameSpecifierInCurrentInstantiation(SS); 1087 1088 return false; 1089} 1090 1091/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously 1092/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same 1093/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. 1094/// Used to indicate that names should revert to being looked up in the 1095/// defining scope. 1096void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 1097 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 1098 if (SS.isInvalid()) 1099 return; 1100 assert(!SS.isInvalid() && computeDeclContext(SS, true) && 1101 "exiting declarator scope we never really entered"); 1102 ExitDeclaratorContext(S); 1103} 1104