10b57cec5SDimitry Andric //===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements C++ semantic analysis for scope specifiers. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "TypeLocBuilder.h" 140b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 150b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h" 160b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 170b57cec5SDimitry Andric #include "clang/AST/NestedNameSpecifier.h" 180b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 190b57cec5SDimitry Andric #include "clang/Sema/DeclSpec.h" 200b57cec5SDimitry Andric #include "clang/Sema/Lookup.h" 210b57cec5SDimitry Andric #include "clang/Sema/SemaInternal.h" 220b57cec5SDimitry Andric #include "clang/Sema/Template.h" 230b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 240b57cec5SDimitry Andric using namespace clang; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric /// Find the current instantiation that associated with the given type. 270b57cec5SDimitry Andric static CXXRecordDecl *getCurrentInstantiationOf(QualType T, 280b57cec5SDimitry Andric DeclContext *CurContext) { 290b57cec5SDimitry Andric if (T.isNull()) 300b57cec5SDimitry Andric return nullptr; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); 330b57cec5SDimitry Andric if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 340b57cec5SDimitry Andric CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 350b57cec5SDimitry Andric if (!Record->isDependentContext() || 360b57cec5SDimitry Andric Record->isCurrentInstantiation(CurContext)) 370b57cec5SDimitry Andric return Record; 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric return nullptr; 400b57cec5SDimitry Andric } else if (isa<InjectedClassNameType>(Ty)) 410b57cec5SDimitry Andric return cast<InjectedClassNameType>(Ty)->getDecl(); 420b57cec5SDimitry Andric else 430b57cec5SDimitry Andric return nullptr; 440b57cec5SDimitry Andric } 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric DeclContext *Sema::computeDeclContext(QualType T) { 470b57cec5SDimitry Andric if (!T->isDependentType()) 480b57cec5SDimitry Andric if (const TagType *Tag = T->getAs<TagType>()) 490b57cec5SDimitry Andric return Tag->getDecl(); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric return ::getCurrentInstantiationOf(T, CurContext); 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, 550b57cec5SDimitry Andric bool EnteringContext) { 560b57cec5SDimitry Andric if (!SS.isSet() || SS.isInvalid()) 570b57cec5SDimitry Andric return nullptr; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric NestedNameSpecifier *NNS = SS.getScopeRep(); 600b57cec5SDimitry Andric if (NNS->isDependent()) { 610b57cec5SDimitry Andric // If this nested-name-specifier refers to the current 620b57cec5SDimitry Andric // instantiation, return its DeclContext. 630b57cec5SDimitry Andric if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) 640b57cec5SDimitry Andric return Record; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric if (EnteringContext) { 670b57cec5SDimitry Andric const Type *NNSType = NNS->getAsType(); 680b57cec5SDimitry Andric if (!NNSType) { 690b57cec5SDimitry Andric return nullptr; 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric // Look through type alias templates, per C++0x [temp.dep.type]p1. 730b57cec5SDimitry Andric NNSType = Context.getCanonicalType(NNSType); 740b57cec5SDimitry Andric if (const TemplateSpecializationType *SpecType 750b57cec5SDimitry Andric = NNSType->getAs<TemplateSpecializationType>()) { 760b57cec5SDimitry Andric // We are entering the context of the nested name specifier, so try to 770b57cec5SDimitry Andric // match the nested name specifier to either a primary class template 780b57cec5SDimitry Andric // or a class template partial specialization. 790b57cec5SDimitry Andric if (ClassTemplateDecl *ClassTemplate 800b57cec5SDimitry Andric = dyn_cast_or_null<ClassTemplateDecl>( 810b57cec5SDimitry Andric SpecType->getTemplateName().getAsTemplateDecl())) { 8206c3fb27SDimitry Andric QualType ContextType = 8306c3fb27SDimitry Andric Context.getCanonicalType(QualType(SpecType, 0)); 8406c3fb27SDimitry Andric 8506c3fb27SDimitry Andric // FIXME: The fallback on the search of partial 8606c3fb27SDimitry Andric // specialization using ContextType should be eventually removed since 8706c3fb27SDimitry Andric // it doesn't handle the case of constrained template parameters 8806c3fb27SDimitry Andric // correctly. Currently removing this fallback would change the 8906c3fb27SDimitry Andric // diagnostic output for invalid code in a number of tests. 9006c3fb27SDimitry Andric ClassTemplatePartialSpecializationDecl *PartialSpec = nullptr; 9106c3fb27SDimitry Andric ArrayRef<TemplateParameterList *> TemplateParamLists = 9206c3fb27SDimitry Andric SS.getTemplateParamLists(); 9306c3fb27SDimitry Andric if (!TemplateParamLists.empty()) { 9406c3fb27SDimitry Andric unsigned Depth = ClassTemplate->getTemplateParameters()->getDepth(); 9506c3fb27SDimitry Andric auto L = find_if(TemplateParamLists, 9606c3fb27SDimitry Andric [Depth](TemplateParameterList *TPL) { 9706c3fb27SDimitry Andric return TPL->getDepth() == Depth; 9806c3fb27SDimitry Andric }); 9906c3fb27SDimitry Andric if (L != TemplateParamLists.end()) { 10006c3fb27SDimitry Andric void *Pos = nullptr; 10106c3fb27SDimitry Andric PartialSpec = ClassTemplate->findPartialSpecialization( 10206c3fb27SDimitry Andric SpecType->template_arguments(), *L, Pos); 10306c3fb27SDimitry Andric } 10406c3fb27SDimitry Andric } else { 10506c3fb27SDimitry Andric PartialSpec = ClassTemplate->findPartialSpecialization(ContextType); 10606c3fb27SDimitry Andric } 10706c3fb27SDimitry Andric 10806c3fb27SDimitry Andric if (PartialSpec) { 10906c3fb27SDimitry Andric // A declaration of the partial specialization must be visible. 11006c3fb27SDimitry Andric // We can always recover here, because this only happens when we're 11106c3fb27SDimitry Andric // entering the context, and that can't happen in a SFINAE context. 11206c3fb27SDimitry Andric assert(!isSFINAEContext() && "partial specialization scope " 11306c3fb27SDimitry Andric "specifier in SFINAE context?"); 11406c3fb27SDimitry Andric if (PartialSpec->hasDefinition() && 11506c3fb27SDimitry Andric !hasReachableDefinition(PartialSpec)) 11606c3fb27SDimitry Andric diagnoseMissingImport(SS.getLastQualifierNameLoc(), PartialSpec, 11706c3fb27SDimitry Andric MissingImportKind::PartialSpecialization, 11806c3fb27SDimitry Andric true); 11906c3fb27SDimitry Andric return PartialSpec; 12006c3fb27SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric // If the type of the nested name specifier is the same as the 1230b57cec5SDimitry Andric // injected class name of the named class template, we're entering 1240b57cec5SDimitry Andric // into that class template definition. 12506c3fb27SDimitry Andric QualType Injected = 12606c3fb27SDimitry Andric ClassTemplate->getInjectedClassNameSpecialization(); 1270b57cec5SDimitry Andric if (Context.hasSameType(Injected, ContextType)) 1280b57cec5SDimitry Andric return ClassTemplate->getTemplatedDecl(); 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { 1310b57cec5SDimitry Andric // The nested name specifier refers to a member of a class template. 1320b57cec5SDimitry Andric return RecordT->getDecl(); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric return nullptr; 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric switch (NNS->getKind()) { 1400b57cec5SDimitry Andric case NestedNameSpecifier::Identifier: 1410b57cec5SDimitry Andric llvm_unreachable("Dependent nested-name-specifier has no DeclContext"); 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric case NestedNameSpecifier::Namespace: 1440b57cec5SDimitry Andric return NNS->getAsNamespace(); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric case NestedNameSpecifier::NamespaceAlias: 1470b57cec5SDimitry Andric return NNS->getAsNamespaceAlias()->getNamespace(); 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric case NestedNameSpecifier::TypeSpec: 1500b57cec5SDimitry Andric case NestedNameSpecifier::TypeSpecWithTemplate: { 1510b57cec5SDimitry Andric const TagType *Tag = NNS->getAsType()->getAs<TagType>(); 1520b57cec5SDimitry Andric assert(Tag && "Non-tag type in nested-name-specifier"); 1530b57cec5SDimitry Andric return Tag->getDecl(); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric case NestedNameSpecifier::Global: 1570b57cec5SDimitry Andric return Context.getTranslationUnitDecl(); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric case NestedNameSpecifier::Super: 1600b57cec5SDimitry Andric return NNS->getAsRecordDecl(); 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { 1670b57cec5SDimitry Andric if (!SS.isSet() || SS.isInvalid()) 1680b57cec5SDimitry Andric return false; 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric return SS.getScopeRep()->isDependent(); 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { 1740b57cec5SDimitry Andric assert(getLangOpts().CPlusPlus && "Only callable in C++"); 1750b57cec5SDimitry Andric assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric if (!NNS->getAsType()) 1780b57cec5SDimitry Andric return nullptr; 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric QualType T = QualType(NNS->getAsType(), 0); 1810b57cec5SDimitry Andric return ::getCurrentInstantiationOf(T, CurContext); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric /// Require that the context specified by SS be complete. 1850b57cec5SDimitry Andric /// 1860b57cec5SDimitry Andric /// If SS refers to a type, this routine checks whether the type is 1870b57cec5SDimitry Andric /// complete enough (or can be made complete enough) for name lookup 1880b57cec5SDimitry Andric /// into the DeclContext. A type that is not yet completed can be 1890b57cec5SDimitry Andric /// considered "complete enough" if it is a class/struct/union/enum 1900b57cec5SDimitry Andric /// that is currently being defined. Or, if we have a type that names 1910b57cec5SDimitry Andric /// a class template specialization that is not a complete type, we 1920b57cec5SDimitry Andric /// will attempt to instantiate that class template. 1930b57cec5SDimitry Andric bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, 1940b57cec5SDimitry Andric DeclContext *DC) { 1950b57cec5SDimitry Andric assert(DC && "given null context"); 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric TagDecl *tag = dyn_cast<TagDecl>(DC); 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // If this is a dependent type, then we consider it complete. 2000b57cec5SDimitry Andric // FIXME: This is wrong; we should require a (visible) definition to 2010b57cec5SDimitry Andric // exist in this case too. 2020b57cec5SDimitry Andric if (!tag || tag->isDependentContext()) 2030b57cec5SDimitry Andric return false; 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // Grab the tag definition, if there is one. 2060b57cec5SDimitry Andric QualType type = Context.getTypeDeclType(tag); 2070b57cec5SDimitry Andric tag = type->getAsTagDecl(); 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // If we're currently defining this type, then lookup into the 2100b57cec5SDimitry Andric // type is okay: don't complain that it isn't complete yet. 2110b57cec5SDimitry Andric if (tag->isBeingDefined()) 2120b57cec5SDimitry Andric return false; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric SourceLocation loc = SS.getLastQualifierNameLoc(); 2150b57cec5SDimitry Andric if (loc.isInvalid()) loc = SS.getRange().getBegin(); 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // The type must be complete. 2180b57cec5SDimitry Andric if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec, 2190b57cec5SDimitry Andric SS.getRange())) { 2200b57cec5SDimitry Andric SS.SetInvalid(SS.getRange()); 2210b57cec5SDimitry Andric return true; 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 224fe6060f1SDimitry Andric if (auto *EnumD = dyn_cast<EnumDecl>(tag)) 225fe6060f1SDimitry Andric // Fixed enum types and scoped enum instantiations are complete, but they 226fe6060f1SDimitry Andric // aren't valid as scopes until we see or instantiate their definition. 227fe6060f1SDimitry Andric return RequireCompleteEnumDecl(EnumD, loc, &SS); 228fe6060f1SDimitry Andric 2290b57cec5SDimitry Andric return false; 230fe6060f1SDimitry Andric } 231fe6060f1SDimitry Andric 232fe6060f1SDimitry Andric /// Require that the EnumDecl is completed with its enumerators defined or 233fe6060f1SDimitry Andric /// instantiated. SS, if provided, is the ScopeRef parsed. 234fe6060f1SDimitry Andric /// 235fe6060f1SDimitry Andric bool Sema::RequireCompleteEnumDecl(EnumDecl *EnumD, SourceLocation L, 236fe6060f1SDimitry Andric CXXScopeSpec *SS) { 2370b57cec5SDimitry Andric if (EnumD->isCompleteDefinition()) { 2380b57cec5SDimitry Andric // If we know about the definition but it is not visible, complain. 2390b57cec5SDimitry Andric NamedDecl *SuggestedDef = nullptr; 24081ad6265SDimitry Andric if (!hasReachableDefinition(EnumD, &SuggestedDef, 2410b57cec5SDimitry Andric /*OnlyNeedComplete*/ false)) { 2420b57cec5SDimitry Andric // If the user is going to see an error here, recover by making the 2430b57cec5SDimitry Andric // definition visible. 2440b57cec5SDimitry Andric bool TreatAsComplete = !isSFINAEContext(); 245fe6060f1SDimitry Andric diagnoseMissingImport(L, SuggestedDef, MissingImportKind::Definition, 2460b57cec5SDimitry Andric /*Recover*/ TreatAsComplete); 2470b57cec5SDimitry Andric return !TreatAsComplete; 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric return false; 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric // Try to instantiate the definition, if this is a specialization of an 2530b57cec5SDimitry Andric // enumeration temploid. 2540b57cec5SDimitry Andric if (EnumDecl *Pattern = EnumD->getInstantiatedFromMemberEnum()) { 2550b57cec5SDimitry Andric MemberSpecializationInfo *MSI = EnumD->getMemberSpecializationInfo(); 2560b57cec5SDimitry Andric if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) { 257fe6060f1SDimitry Andric if (InstantiateEnum(L, EnumD, Pattern, 2580b57cec5SDimitry Andric getTemplateInstantiationArgs(EnumD), 2590b57cec5SDimitry Andric TSK_ImplicitInstantiation)) { 260fe6060f1SDimitry Andric if (SS) 261fe6060f1SDimitry Andric SS->SetInvalid(SS->getRange()); 2620b57cec5SDimitry Andric return true; 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric return false; 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 268fe6060f1SDimitry Andric if (SS) { 269fe6060f1SDimitry Andric Diag(L, diag::err_incomplete_nested_name_spec) 270fe6060f1SDimitry Andric << QualType(EnumD->getTypeForDecl(), 0) << SS->getRange(); 271fe6060f1SDimitry Andric SS->SetInvalid(SS->getRange()); 272fe6060f1SDimitry Andric } else { 273fe6060f1SDimitry Andric Diag(L, diag::err_incomplete_enum) << QualType(EnumD->getTypeForDecl(), 0); 274fe6060f1SDimitry Andric Diag(EnumD->getLocation(), diag::note_declared_at); 275fe6060f1SDimitry Andric } 276fe6060f1SDimitry Andric 2770b57cec5SDimitry Andric return true; 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric bool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc, 2810b57cec5SDimitry Andric CXXScopeSpec &SS) { 2820b57cec5SDimitry Andric SS.MakeGlobal(Context, CCLoc); 2830b57cec5SDimitry Andric return false; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric bool Sema::ActOnSuperScopeSpecifier(SourceLocation SuperLoc, 2870b57cec5SDimitry Andric SourceLocation ColonColonLoc, 2880b57cec5SDimitry Andric CXXScopeSpec &SS) { 28906c3fb27SDimitry Andric if (getCurLambda()) { 29006c3fb27SDimitry Andric Diag(SuperLoc, diag::err_super_in_lambda_unsupported); 29106c3fb27SDimitry Andric return true; 29206c3fb27SDimitry Andric } 29306c3fb27SDimitry Andric 2940b57cec5SDimitry Andric CXXRecordDecl *RD = nullptr; 2950b57cec5SDimitry Andric for (Scope *S = getCurScope(); S; S = S->getParent()) { 2960b57cec5SDimitry Andric if (S->isFunctionScope()) { 2970b57cec5SDimitry Andric if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(S->getEntity())) 2980b57cec5SDimitry Andric RD = MD->getParent(); 2990b57cec5SDimitry Andric break; 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric if (S->isClassScope()) { 3020b57cec5SDimitry Andric RD = cast<CXXRecordDecl>(S->getEntity()); 3030b57cec5SDimitry Andric break; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric if (!RD) { 3080b57cec5SDimitry Andric Diag(SuperLoc, diag::err_invalid_super_scope); 3090b57cec5SDimitry Andric return true; 3100b57cec5SDimitry Andric } else if (RD->getNumBases() == 0) { 3110b57cec5SDimitry Andric Diag(SuperLoc, diag::err_no_base_classes) << RD->getName(); 3120b57cec5SDimitry Andric return true; 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric SS.MakeSuper(Context, RD, SuperLoc, ColonColonLoc); 3160b57cec5SDimitry Andric return false; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD, 3200b57cec5SDimitry Andric bool *IsExtension) { 3210b57cec5SDimitry Andric if (!SD) 3220b57cec5SDimitry Andric return false; 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric SD = SD->getUnderlyingDecl(); 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric // Namespace and namespace aliases are fine. 3270b57cec5SDimitry Andric if (isa<NamespaceDecl>(SD)) 3280b57cec5SDimitry Andric return true; 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric if (!isa<TypeDecl>(SD)) 3310b57cec5SDimitry Andric return false; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric // Determine whether we have a class (or, in C++11, an enum) or 3340b57cec5SDimitry Andric // a typedef thereof. If so, build the nested-name-specifier. 3350b57cec5SDimitry Andric QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); 3360b57cec5SDimitry Andric if (T->isDependentType()) 3370b57cec5SDimitry Andric return true; 3380b57cec5SDimitry Andric if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { 3390b57cec5SDimitry Andric if (TD->getUnderlyingType()->isRecordType()) 3400b57cec5SDimitry Andric return true; 3410b57cec5SDimitry Andric if (TD->getUnderlyingType()->isEnumeralType()) { 3420b57cec5SDimitry Andric if (Context.getLangOpts().CPlusPlus11) 3430b57cec5SDimitry Andric return true; 3440b57cec5SDimitry Andric if (IsExtension) 3450b57cec5SDimitry Andric *IsExtension = true; 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric } else if (isa<RecordDecl>(SD)) { 3480b57cec5SDimitry Andric return true; 3490b57cec5SDimitry Andric } else if (isa<EnumDecl>(SD)) { 3500b57cec5SDimitry Andric if (Context.getLangOpts().CPlusPlus11) 3510b57cec5SDimitry Andric return true; 3520b57cec5SDimitry Andric if (IsExtension) 3530b57cec5SDimitry Andric *IsExtension = true; 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric return false; 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { 3600b57cec5SDimitry Andric if (!S || !NNS) 3610b57cec5SDimitry Andric return nullptr; 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric while (NNS->getPrefix()) 3640b57cec5SDimitry Andric NNS = NNS->getPrefix(); 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric if (NNS->getKind() != NestedNameSpecifier::Identifier) 3670b57cec5SDimitry Andric return nullptr; 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), 3700b57cec5SDimitry Andric LookupNestedNameSpecifierName); 3710b57cec5SDimitry Andric LookupName(Found, S); 3720b57cec5SDimitry Andric assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric if (!Found.isSingleResult()) 3750b57cec5SDimitry Andric return nullptr; 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric NamedDecl *Result = Found.getFoundDecl(); 3780b57cec5SDimitry Andric if (isAcceptableNestedNameSpecifier(Result)) 3790b57cec5SDimitry Andric return Result; 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric return nullptr; 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric namespace { 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric // Callback to only accept typo corrections that can be a valid C++ member 387349cc55cSDimitry Andric // initializer: either a non-static field member or a base class. 3880b57cec5SDimitry Andric class NestedNameSpecifierValidatorCCC final 3890b57cec5SDimitry Andric : public CorrectionCandidateCallback { 3900b57cec5SDimitry Andric public: 3910b57cec5SDimitry Andric explicit NestedNameSpecifierValidatorCCC(Sema &SRef) 3920b57cec5SDimitry Andric : SRef(SRef) {} 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric bool ValidateCandidate(const TypoCorrection &candidate) override { 3950b57cec5SDimitry Andric return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl()); 3960b57cec5SDimitry Andric } 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric std::unique_ptr<CorrectionCandidateCallback> clone() override { 399a7dea167SDimitry Andric return std::make_unique<NestedNameSpecifierValidatorCCC>(*this); 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric private: 4030b57cec5SDimitry Andric Sema &SRef; 4040b57cec5SDimitry Andric }; 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric bool Sema::BuildCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo, 4090b57cec5SDimitry Andric bool EnteringContext, CXXScopeSpec &SS, 4100b57cec5SDimitry Andric NamedDecl *ScopeLookupResult, 4110b57cec5SDimitry Andric bool ErrorRecoveryLookup, 4120b57cec5SDimitry Andric bool *IsCorrectedToColon, 4130b57cec5SDimitry Andric bool OnlyNamespace) { 4140b57cec5SDimitry Andric if (IdInfo.Identifier->isEditorPlaceholder()) 4150b57cec5SDimitry Andric return true; 4160b57cec5SDimitry Andric LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc, 4170b57cec5SDimitry Andric OnlyNamespace ? LookupNamespaceName 4180b57cec5SDimitry Andric : LookupNestedNameSpecifierName); 4190b57cec5SDimitry Andric QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType); 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Determine where to perform name lookup 4220b57cec5SDimitry Andric DeclContext *LookupCtx = nullptr; 4230b57cec5SDimitry Andric bool isDependent = false; 4240b57cec5SDimitry Andric if (IsCorrectedToColon) 4250b57cec5SDimitry Andric *IsCorrectedToColon = false; 4260b57cec5SDimitry Andric if (!ObjectType.isNull()) { 4270b57cec5SDimitry Andric // This nested-name-specifier occurs in a member access expression, e.g., 4280b57cec5SDimitry Andric // x->B::f, and we are looking into the type of the object. 4290b57cec5SDimitry Andric assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); 4300b57cec5SDimitry Andric LookupCtx = computeDeclContext(ObjectType); 4310b57cec5SDimitry Andric isDependent = ObjectType->isDependentType(); 4320b57cec5SDimitry Andric } else if (SS.isSet()) { 4330b57cec5SDimitry Andric // This nested-name-specifier occurs after another nested-name-specifier, 4340b57cec5SDimitry Andric // so look into the context associated with the prior nested-name-specifier. 4350b57cec5SDimitry Andric LookupCtx = computeDeclContext(SS, EnteringContext); 4360b57cec5SDimitry Andric isDependent = isDependentScopeSpecifier(SS); 4370b57cec5SDimitry Andric Found.setContextRange(SS.getRange()); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric bool ObjectTypeSearchedInScope = false; 4410b57cec5SDimitry Andric if (LookupCtx) { 4420b57cec5SDimitry Andric // Perform "qualified" name lookup into the declaration context we 4430b57cec5SDimitry Andric // computed, which is either the type of the base of a member access 4440b57cec5SDimitry Andric // expression or the declaration context associated with a prior 4450b57cec5SDimitry Andric // nested-name-specifier. 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric // The declaration context must be complete. 4480b57cec5SDimitry Andric if (!LookupCtx->isDependentContext() && 4490b57cec5SDimitry Andric RequireCompleteDeclContext(SS, LookupCtx)) 4500b57cec5SDimitry Andric return true; 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric LookupQualifiedName(Found, LookupCtx); 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric if (!ObjectType.isNull() && Found.empty()) { 4550b57cec5SDimitry Andric // C++ [basic.lookup.classref]p4: 4560b57cec5SDimitry Andric // If the id-expression in a class member access is a qualified-id of 4570b57cec5SDimitry Andric // the form 4580b57cec5SDimitry Andric // 4590b57cec5SDimitry Andric // class-name-or-namespace-name::... 4600b57cec5SDimitry Andric // 4610b57cec5SDimitry Andric // the class-name-or-namespace-name following the . or -> operator is 4620b57cec5SDimitry Andric // looked up both in the context of the entire postfix-expression and in 4630b57cec5SDimitry Andric // the scope of the class of the object expression. If the name is found 4640b57cec5SDimitry Andric // only in the scope of the class of the object expression, the name 4650b57cec5SDimitry Andric // shall refer to a class-name. If the name is found only in the 4660b57cec5SDimitry Andric // context of the entire postfix-expression, the name shall refer to a 4670b57cec5SDimitry Andric // class-name or namespace-name. [...] 4680b57cec5SDimitry Andric // 4690b57cec5SDimitry Andric // Qualified name lookup into a class will not find a namespace-name, 4700b57cec5SDimitry Andric // so we do not need to diagnose that case specifically. However, 4710b57cec5SDimitry Andric // this qualified name lookup may find nothing. In that case, perform 4720b57cec5SDimitry Andric // unqualified name lookup in the given scope (if available) or 4730b57cec5SDimitry Andric // reconstruct the result from when name lookup was performed at template 4740b57cec5SDimitry Andric // definition time. 4750b57cec5SDimitry Andric if (S) 4760b57cec5SDimitry Andric LookupName(Found, S); 4770b57cec5SDimitry Andric else if (ScopeLookupResult) 4780b57cec5SDimitry Andric Found.addDecl(ScopeLookupResult); 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric ObjectTypeSearchedInScope = true; 4810b57cec5SDimitry Andric } 4820b57cec5SDimitry Andric } else if (!isDependent) { 4830b57cec5SDimitry Andric // Perform unqualified name lookup in the current scope. 4840b57cec5SDimitry Andric LookupName(Found, S); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric if (Found.isAmbiguous()) 4880b57cec5SDimitry Andric return true; 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric // If we performed lookup into a dependent context and did not find anything, 4910b57cec5SDimitry Andric // that's fine: just build a dependent nested-name-specifier. 4920b57cec5SDimitry Andric if (Found.empty() && isDependent && 4930b57cec5SDimitry Andric !(LookupCtx && LookupCtx->isRecord() && 4940b57cec5SDimitry Andric (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || 4950b57cec5SDimitry Andric !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) { 4960b57cec5SDimitry Andric // Don't speculate if we're just trying to improve error recovery. 4970b57cec5SDimitry Andric if (ErrorRecoveryLookup) 4980b57cec5SDimitry Andric return true; 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric // We were not able to compute the declaration context for a dependent 5010b57cec5SDimitry Andric // base object type or prior nested-name-specifier, so this 5020b57cec5SDimitry Andric // nested-name-specifier refers to an unknown specialization. Just build 5030b57cec5SDimitry Andric // a dependent nested-name-specifier. 5040b57cec5SDimitry Andric SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, IdInfo.CCLoc); 5050b57cec5SDimitry Andric return false; 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric if (Found.empty() && !ErrorRecoveryLookup) { 5090b57cec5SDimitry Andric // If identifier is not found as class-name-or-namespace-name, but is found 5100b57cec5SDimitry Andric // as other entity, don't look for typos. 5110b57cec5SDimitry Andric LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName); 5120b57cec5SDimitry Andric if (LookupCtx) 5130b57cec5SDimitry Andric LookupQualifiedName(R, LookupCtx); 5140b57cec5SDimitry Andric else if (S && !isDependent) 5150b57cec5SDimitry Andric LookupName(R, S); 5160b57cec5SDimitry Andric if (!R.empty()) { 5170b57cec5SDimitry Andric // Don't diagnose problems with this speculative lookup. 5180b57cec5SDimitry Andric R.suppressDiagnostics(); 5190b57cec5SDimitry Andric // The identifier is found in ordinary lookup. If correction to colon is 5200b57cec5SDimitry Andric // allowed, suggest replacement to ':'. 5210b57cec5SDimitry Andric if (IsCorrectedToColon) { 5220b57cec5SDimitry Andric *IsCorrectedToColon = true; 5230b57cec5SDimitry Andric Diag(IdInfo.CCLoc, diag::err_nested_name_spec_is_not_class) 5240b57cec5SDimitry Andric << IdInfo.Identifier << getLangOpts().CPlusPlus 5250b57cec5SDimitry Andric << FixItHint::CreateReplacement(IdInfo.CCLoc, ":"); 5260b57cec5SDimitry Andric if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) 5270b57cec5SDimitry Andric Diag(ND->getLocation(), diag::note_declared_at); 5280b57cec5SDimitry Andric return true; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric // Replacement '::' -> ':' is not allowed, just issue respective error. 5310b57cec5SDimitry Andric Diag(R.getNameLoc(), OnlyNamespace 5320b57cec5SDimitry Andric ? unsigned(diag::err_expected_namespace_name) 5330b57cec5SDimitry Andric : unsigned(diag::err_expected_class_or_namespace)) 5340b57cec5SDimitry Andric << IdInfo.Identifier << getLangOpts().CPlusPlus; 5350b57cec5SDimitry Andric if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) 5360b57cec5SDimitry Andric Diag(ND->getLocation(), diag::note_entity_declared_at) 5370b57cec5SDimitry Andric << IdInfo.Identifier; 5380b57cec5SDimitry Andric return true; 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric } 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) { 5430b57cec5SDimitry Andric // We haven't found anything, and we're not recovering from a 5440b57cec5SDimitry Andric // different kind of error, so look for typos. 5450b57cec5SDimitry Andric DeclarationName Name = Found.getLookupName(); 5460b57cec5SDimitry Andric Found.clear(); 5470b57cec5SDimitry Andric NestedNameSpecifierValidatorCCC CCC(*this); 5480b57cec5SDimitry Andric if (TypoCorrection Corrected = CorrectTypo( 5490b57cec5SDimitry Andric Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, CCC, 5500b57cec5SDimitry Andric CTK_ErrorRecovery, LookupCtx, EnteringContext)) { 5510b57cec5SDimitry Andric if (LookupCtx) { 5520b57cec5SDimitry Andric bool DroppedSpecifier = 5530b57cec5SDimitry Andric Corrected.WillReplaceSpecifier() && 5540b57cec5SDimitry Andric Name.getAsString() == Corrected.getAsString(getLangOpts()); 5550b57cec5SDimitry Andric if (DroppedSpecifier) 5560b57cec5SDimitry Andric SS.clear(); 5570b57cec5SDimitry Andric diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) 5580b57cec5SDimitry Andric << Name << LookupCtx << DroppedSpecifier 5590b57cec5SDimitry Andric << SS.getRange()); 5600b57cec5SDimitry Andric } else 5610b57cec5SDimitry Andric diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 5620b57cec5SDimitry Andric << Name); 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric if (Corrected.getCorrectionSpecifier()) 5650b57cec5SDimitry Andric SS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(), 5660b57cec5SDimitry Andric SourceRange(Found.getNameLoc())); 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric if (NamedDecl *ND = Corrected.getFoundDecl()) 5690b57cec5SDimitry Andric Found.addDecl(ND); 5700b57cec5SDimitry Andric Found.setLookupName(Corrected.getCorrection()); 5710b57cec5SDimitry Andric } else { 5720b57cec5SDimitry Andric Found.setLookupName(IdInfo.Identifier); 5730b57cec5SDimitry Andric } 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric NamedDecl *SD = 5770b57cec5SDimitry Andric Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr; 5780b57cec5SDimitry Andric bool IsExtension = false; 5790b57cec5SDimitry Andric bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension); 5800b57cec5SDimitry Andric if (!AcceptSpec && IsExtension) { 5810b57cec5SDimitry Andric AcceptSpec = true; 5820b57cec5SDimitry Andric Diag(IdInfo.IdentifierLoc, diag::ext_nested_name_spec_is_enum); 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric if (AcceptSpec) { 5850b57cec5SDimitry Andric if (!ObjectType.isNull() && !ObjectTypeSearchedInScope && 5860b57cec5SDimitry Andric !getLangOpts().CPlusPlus11) { 5870b57cec5SDimitry Andric // C++03 [basic.lookup.classref]p4: 5880b57cec5SDimitry Andric // [...] If the name is found in both contexts, the 5890b57cec5SDimitry Andric // class-name-or-namespace-name shall refer to the same entity. 5900b57cec5SDimitry Andric // 5910b57cec5SDimitry Andric // We already found the name in the scope of the object. Now, look 5920b57cec5SDimitry Andric // into the current scope (the scope of the postfix-expression) to 5930b57cec5SDimitry Andric // see if we can find the same name there. As above, if there is no 5940b57cec5SDimitry Andric // scope, reconstruct the result from the template instantiation itself. 5950b57cec5SDimitry Andric // 5960b57cec5SDimitry Andric // Note that C++11 does *not* perform this redundant lookup. 5970b57cec5SDimitry Andric NamedDecl *OuterDecl; 5980b57cec5SDimitry Andric if (S) { 5990b57cec5SDimitry Andric LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc, 6000b57cec5SDimitry Andric LookupNestedNameSpecifierName); 6010b57cec5SDimitry Andric LookupName(FoundOuter, S); 6020b57cec5SDimitry Andric OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); 6030b57cec5SDimitry Andric } else 6040b57cec5SDimitry Andric OuterDecl = ScopeLookupResult; 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric if (isAcceptableNestedNameSpecifier(OuterDecl) && 6070b57cec5SDimitry Andric OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && 6080b57cec5SDimitry Andric (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || 6090b57cec5SDimitry Andric !Context.hasSameType( 6100b57cec5SDimitry Andric Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), 6110b57cec5SDimitry Andric Context.getTypeDeclType(cast<TypeDecl>(SD))))) { 6120b57cec5SDimitry Andric if (ErrorRecoveryLookup) 6130b57cec5SDimitry Andric return true; 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric Diag(IdInfo.IdentifierLoc, 6160b57cec5SDimitry Andric diag::err_nested_name_member_ref_lookup_ambiguous) 6170b57cec5SDimitry Andric << IdInfo.Identifier; 6180b57cec5SDimitry Andric Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) 6190b57cec5SDimitry Andric << ObjectType; 6200b57cec5SDimitry Andric Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric // Fall through so that we'll pick the name we found in the object 6230b57cec5SDimitry Andric // type, since that's probably what the user wanted anyway. 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric } 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD)) 6280b57cec5SDimitry Andric MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric // If we're just performing this lookup for error-recovery purposes, 6310b57cec5SDimitry Andric // don't extend the nested-name-specifier. Just return now. 6320b57cec5SDimitry Andric if (ErrorRecoveryLookup) 6330b57cec5SDimitry Andric return false; 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric // The use of a nested name specifier may trigger deprecation warnings. 6360b57cec5SDimitry Andric DiagnoseUseOfDecl(SD, IdInfo.CCLoc); 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) { 6390b57cec5SDimitry Andric SS.Extend(Context, Namespace, IdInfo.IdentifierLoc, IdInfo.CCLoc); 6400b57cec5SDimitry Andric return false; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) { 6440b57cec5SDimitry Andric SS.Extend(Context, Alias, IdInfo.IdentifierLoc, IdInfo.CCLoc); 6450b57cec5SDimitry Andric return false; 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric QualType T = 6490b57cec5SDimitry Andric Context.getTypeDeclType(cast<TypeDecl>(SD->getUnderlyingDecl())); 6500eae32dcSDimitry Andric 6510eae32dcSDimitry Andric if (T->isEnumeralType()) 6520eae32dcSDimitry Andric Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec); 6530eae32dcSDimitry Andric 6540b57cec5SDimitry Andric TypeLocBuilder TLB; 6550eae32dcSDimitry Andric if (const auto *USD = dyn_cast<UsingShadowDecl>(SD)) { 6560eae32dcSDimitry Andric T = Context.getUsingType(USD, T); 6570eae32dcSDimitry Andric TLB.pushTypeSpec(T).setNameLoc(IdInfo.IdentifierLoc); 6580eae32dcSDimitry Andric } else if (isa<InjectedClassNameType>(T)) { 6590b57cec5SDimitry Andric InjectedClassNameTypeLoc InjectedTL 6600b57cec5SDimitry Andric = TLB.push<InjectedClassNameTypeLoc>(T); 6610b57cec5SDimitry Andric InjectedTL.setNameLoc(IdInfo.IdentifierLoc); 6620b57cec5SDimitry Andric } else if (isa<RecordType>(T)) { 6630b57cec5SDimitry Andric RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T); 6640b57cec5SDimitry Andric RecordTL.setNameLoc(IdInfo.IdentifierLoc); 6650b57cec5SDimitry Andric } else if (isa<TypedefType>(T)) { 6660b57cec5SDimitry Andric TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T); 6670b57cec5SDimitry Andric TypedefTL.setNameLoc(IdInfo.IdentifierLoc); 6680b57cec5SDimitry Andric } else if (isa<EnumType>(T)) { 6690b57cec5SDimitry Andric EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T); 6700b57cec5SDimitry Andric EnumTL.setNameLoc(IdInfo.IdentifierLoc); 6710b57cec5SDimitry Andric } else if (isa<TemplateTypeParmType>(T)) { 6720b57cec5SDimitry Andric TemplateTypeParmTypeLoc TemplateTypeTL 6730b57cec5SDimitry Andric = TLB.push<TemplateTypeParmTypeLoc>(T); 6740b57cec5SDimitry Andric TemplateTypeTL.setNameLoc(IdInfo.IdentifierLoc); 6750b57cec5SDimitry Andric } else if (isa<UnresolvedUsingType>(T)) { 6760b57cec5SDimitry Andric UnresolvedUsingTypeLoc UnresolvedTL 6770b57cec5SDimitry Andric = TLB.push<UnresolvedUsingTypeLoc>(T); 6780b57cec5SDimitry Andric UnresolvedTL.setNameLoc(IdInfo.IdentifierLoc); 6790b57cec5SDimitry Andric } else if (isa<SubstTemplateTypeParmType>(T)) { 6800b57cec5SDimitry Andric SubstTemplateTypeParmTypeLoc TL 6810b57cec5SDimitry Andric = TLB.push<SubstTemplateTypeParmTypeLoc>(T); 6820b57cec5SDimitry Andric TL.setNameLoc(IdInfo.IdentifierLoc); 6830b57cec5SDimitry Andric } else if (isa<SubstTemplateTypeParmPackType>(T)) { 6840b57cec5SDimitry Andric SubstTemplateTypeParmPackTypeLoc TL 6850b57cec5SDimitry Andric = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T); 6860b57cec5SDimitry Andric TL.setNameLoc(IdInfo.IdentifierLoc); 6870b57cec5SDimitry Andric } else { 6880b57cec5SDimitry Andric llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier"); 6890b57cec5SDimitry Andric } 6900b57cec5SDimitry Andric 6910b57cec5SDimitry Andric SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 6920b57cec5SDimitry Andric IdInfo.CCLoc); 6930b57cec5SDimitry Andric return false; 6940b57cec5SDimitry Andric } 6950b57cec5SDimitry Andric 6960b57cec5SDimitry Andric // Otherwise, we have an error case. If we don't want diagnostics, just 6970b57cec5SDimitry Andric // return an error now. 6980b57cec5SDimitry Andric if (ErrorRecoveryLookup) 6990b57cec5SDimitry Andric return true; 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric // If we didn't find anything during our lookup, try again with 7020b57cec5SDimitry Andric // ordinary name lookup, which can help us produce better error 7030b57cec5SDimitry Andric // messages. 7040b57cec5SDimitry Andric if (Found.empty()) { 7050b57cec5SDimitry Andric Found.clear(LookupOrdinaryName); 7060b57cec5SDimitry Andric LookupName(Found, S); 7070b57cec5SDimitry Andric } 7080b57cec5SDimitry Andric 7090b57cec5SDimitry Andric // In Microsoft mode, if we are within a templated function and we can't 7100b57cec5SDimitry Andric // resolve Identifier, then extend the SS with Identifier. This will have 7110b57cec5SDimitry Andric // the effect of resolving Identifier during template instantiation. 7120b57cec5SDimitry Andric // The goal is to be able to resolve a function call whose 7130b57cec5SDimitry Andric // nested-name-specifier is located inside a dependent base class. 7140b57cec5SDimitry Andric // Example: 7150b57cec5SDimitry Andric // 7160b57cec5SDimitry Andric // class C { 7170b57cec5SDimitry Andric // public: 7180b57cec5SDimitry Andric // static void foo2() { } 7190b57cec5SDimitry Andric // }; 7200b57cec5SDimitry Andric // template <class T> class A { public: typedef C D; }; 7210b57cec5SDimitry Andric // 7220b57cec5SDimitry Andric // template <class T> class B : public A<T> { 7230b57cec5SDimitry Andric // public: 7240b57cec5SDimitry Andric // void foo() { D::foo2(); } 7250b57cec5SDimitry Andric // }; 7260b57cec5SDimitry Andric if (getLangOpts().MSVCCompat) { 7270b57cec5SDimitry Andric DeclContext *DC = LookupCtx ? LookupCtx : CurContext; 7280b57cec5SDimitry Andric if (DC->isDependentContext() && DC->isFunctionOrMethod()) { 7290b57cec5SDimitry Andric CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent()); 7300b57cec5SDimitry Andric if (ContainingClass && ContainingClass->hasAnyDependentBases()) { 7310b57cec5SDimitry Andric Diag(IdInfo.IdentifierLoc, 7320b57cec5SDimitry Andric diag::ext_undeclared_unqual_id_with_dependent_base) 7330b57cec5SDimitry Andric << IdInfo.Identifier << ContainingClass; 734*0fca6ea1SDimitry Andric // Fake up a nested-name-specifier that starts with the 735*0fca6ea1SDimitry Andric // injected-class-name of the enclosing class. 736*0fca6ea1SDimitry Andric QualType T = Context.getTypeDeclType(ContainingClass); 737*0fca6ea1SDimitry Andric TypeLocBuilder TLB; 738*0fca6ea1SDimitry Andric TLB.pushTrivial(Context, T, IdInfo.IdentifierLoc); 739*0fca6ea1SDimitry Andric SS.Extend(Context, /*TemplateKWLoc=*/SourceLocation(), 740*0fca6ea1SDimitry Andric TLB.getTypeLocInContext(Context, T), IdInfo.IdentifierLoc); 741*0fca6ea1SDimitry Andric // Add the identifier to form a dependent name. 7420b57cec5SDimitry Andric SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, 7430b57cec5SDimitry Andric IdInfo.CCLoc); 7440b57cec5SDimitry Andric return false; 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric if (!Found.empty()) { 75081ad6265SDimitry Andric if (TypeDecl *TD = Found.getAsSingle<TypeDecl>()) { 7510b57cec5SDimitry Andric Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace) 7520b57cec5SDimitry Andric << Context.getTypeDeclType(TD) << getLangOpts().CPlusPlus; 75381ad6265SDimitry Andric } else if (Found.getAsSingle<TemplateDecl>()) { 75481ad6265SDimitry Andric ParsedType SuggestedType; 75581ad6265SDimitry Andric DiagnoseUnknownTypeName(IdInfo.Identifier, IdInfo.IdentifierLoc, S, &SS, 75681ad6265SDimitry Andric SuggestedType); 75781ad6265SDimitry Andric } else { 7580b57cec5SDimitry Andric Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace) 7590b57cec5SDimitry Andric << IdInfo.Identifier << getLangOpts().CPlusPlus; 7600b57cec5SDimitry Andric if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) 7610b57cec5SDimitry Andric Diag(ND->getLocation(), diag::note_entity_declared_at) 7620b57cec5SDimitry Andric << IdInfo.Identifier; 7630b57cec5SDimitry Andric } 7640b57cec5SDimitry Andric } else if (SS.isSet()) 7650b57cec5SDimitry Andric Diag(IdInfo.IdentifierLoc, diag::err_no_member) << IdInfo.Identifier 7660b57cec5SDimitry Andric << LookupCtx << SS.getRange(); 7670b57cec5SDimitry Andric else 7680b57cec5SDimitry Andric Diag(IdInfo.IdentifierLoc, diag::err_undeclared_var_use) 7690b57cec5SDimitry Andric << IdInfo.Identifier; 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric return true; 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo, 7750b57cec5SDimitry Andric bool EnteringContext, CXXScopeSpec &SS, 7760b57cec5SDimitry Andric bool *IsCorrectedToColon, 7770b57cec5SDimitry Andric bool OnlyNamespace) { 7780b57cec5SDimitry Andric if (SS.isInvalid()) 7790b57cec5SDimitry Andric return true; 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric return BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS, 7820b57cec5SDimitry Andric /*ScopeLookupResult=*/nullptr, false, 7830b57cec5SDimitry Andric IsCorrectedToColon, OnlyNamespace); 7840b57cec5SDimitry Andric } 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS, 7870b57cec5SDimitry Andric const DeclSpec &DS, 7880b57cec5SDimitry Andric SourceLocation ColonColonLoc) { 7890b57cec5SDimitry Andric if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) 7900b57cec5SDimitry Andric return true; 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric assert(DS.getTypeSpecType() == DeclSpec::TST_decltype); 7930b57cec5SDimitry Andric 794349cc55cSDimitry Andric QualType T = BuildDecltypeType(DS.getRepAsExpr()); 7950b57cec5SDimitry Andric if (T.isNull()) 7960b57cec5SDimitry Andric return true; 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric if (!T->isDependentType() && !T->getAs<TagType>()) { 7990b57cec5SDimitry Andric Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace) 8000b57cec5SDimitry Andric << T << getLangOpts().CPlusPlus; 8010b57cec5SDimitry Andric return true; 8020b57cec5SDimitry Andric } 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric TypeLocBuilder TLB; 8050b57cec5SDimitry Andric DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); 80604eeddc0SDimitry Andric DecltypeTL.setDecltypeLoc(DS.getTypeSpecTypeLoc()); 80704eeddc0SDimitry Andric DecltypeTL.setRParenLoc(DS.getTypeofParensRange().getEnd()); 8080b57cec5SDimitry Andric SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), 8090b57cec5SDimitry Andric ColonColonLoc); 8100b57cec5SDimitry Andric return false; 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric 813*0fca6ea1SDimitry Andric bool Sema::ActOnCXXNestedNameSpecifierIndexedPack(CXXScopeSpec &SS, 814*0fca6ea1SDimitry Andric const DeclSpec &DS, 815*0fca6ea1SDimitry Andric SourceLocation ColonColonLoc, 816*0fca6ea1SDimitry Andric QualType Type) { 817*0fca6ea1SDimitry Andric if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) 818*0fca6ea1SDimitry Andric return true; 819*0fca6ea1SDimitry Andric 820*0fca6ea1SDimitry Andric assert(DS.getTypeSpecType() == DeclSpec::TST_typename_pack_indexing); 821*0fca6ea1SDimitry Andric 822*0fca6ea1SDimitry Andric if (Type.isNull()) 823*0fca6ea1SDimitry Andric return true; 824*0fca6ea1SDimitry Andric 825*0fca6ea1SDimitry Andric TypeLocBuilder TLB; 826*0fca6ea1SDimitry Andric TLB.pushTrivial(getASTContext(), 827*0fca6ea1SDimitry Andric cast<PackIndexingType>(Type.getTypePtr())->getPattern(), 828*0fca6ea1SDimitry Andric DS.getBeginLoc()); 829*0fca6ea1SDimitry Andric PackIndexingTypeLoc PIT = TLB.push<PackIndexingTypeLoc>(Type); 830*0fca6ea1SDimitry Andric PIT.setEllipsisLoc(DS.getEllipsisLoc()); 831*0fca6ea1SDimitry Andric SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, Type), 832*0fca6ea1SDimitry Andric ColonColonLoc); 833*0fca6ea1SDimitry Andric return false; 834*0fca6ea1SDimitry Andric } 835*0fca6ea1SDimitry Andric 8360b57cec5SDimitry Andric bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, 8370b57cec5SDimitry Andric NestedNameSpecInfo &IdInfo, 8380b57cec5SDimitry Andric bool EnteringContext) { 8390b57cec5SDimitry Andric if (SS.isInvalid()) 8400b57cec5SDimitry Andric return false; 8410b57cec5SDimitry Andric 8420b57cec5SDimitry Andric return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS, 8430b57cec5SDimitry Andric /*ScopeLookupResult=*/nullptr, true); 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, 8470b57cec5SDimitry Andric CXXScopeSpec &SS, 8480b57cec5SDimitry Andric SourceLocation TemplateKWLoc, 8490b57cec5SDimitry Andric TemplateTy OpaqueTemplate, 8500b57cec5SDimitry Andric SourceLocation TemplateNameLoc, 8510b57cec5SDimitry Andric SourceLocation LAngleLoc, 8520b57cec5SDimitry Andric ASTTemplateArgsPtr TemplateArgsIn, 8530b57cec5SDimitry Andric SourceLocation RAngleLoc, 8540b57cec5SDimitry Andric SourceLocation CCLoc, 8550b57cec5SDimitry Andric bool EnteringContext) { 8560b57cec5SDimitry Andric if (SS.isInvalid()) 8570b57cec5SDimitry Andric return true; 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric TemplateName Template = OpaqueTemplate.get(); 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric // Translate the parser's template argument list in our AST format. 8620b57cec5SDimitry Andric TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); 8630b57cec5SDimitry Andric translateTemplateArguments(TemplateArgsIn, TemplateArgs); 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric DependentTemplateName *DTN = Template.getAsDependentTemplateName(); 8660b57cec5SDimitry Andric if (DTN && DTN->isIdentifier()) { 8670b57cec5SDimitry Andric // Handle a dependent template specialization for which we cannot resolve 8680b57cec5SDimitry Andric // the template name. 8690b57cec5SDimitry Andric assert(DTN->getQualifier() == SS.getScopeRep()); 870bdd1243dSDimitry Andric QualType T = Context.getDependentTemplateSpecializationType( 8715f757f3fSDimitry Andric ElaboratedTypeKeyword::None, DTN->getQualifier(), DTN->getIdentifier(), 872bdd1243dSDimitry Andric TemplateArgs.arguments()); 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric // Create source-location information for this type. 8750b57cec5SDimitry Andric TypeLocBuilder Builder; 8760b57cec5SDimitry Andric DependentTemplateSpecializationTypeLoc SpecTL 8770b57cec5SDimitry Andric = Builder.push<DependentTemplateSpecializationTypeLoc>(T); 8780b57cec5SDimitry Andric SpecTL.setElaboratedKeywordLoc(SourceLocation()); 8790b57cec5SDimitry Andric SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); 8800b57cec5SDimitry Andric SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 8810b57cec5SDimitry Andric SpecTL.setTemplateNameLoc(TemplateNameLoc); 8820b57cec5SDimitry Andric SpecTL.setLAngleLoc(LAngleLoc); 8830b57cec5SDimitry Andric SpecTL.setRAngleLoc(RAngleLoc); 8840b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 8850b57cec5SDimitry Andric SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 8880b57cec5SDimitry Andric CCLoc); 8890b57cec5SDimitry Andric return false; 8900b57cec5SDimitry Andric } 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric // If we assumed an undeclared identifier was a template name, try to 8930b57cec5SDimitry Andric // typo-correct it now. 8940b57cec5SDimitry Andric if (Template.getAsAssumedTemplateName() && 8950b57cec5SDimitry Andric resolveAssumedTemplateNameAsType(S, Template, TemplateNameLoc)) 8960b57cec5SDimitry Andric return true; 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric TemplateDecl *TD = Template.getAsTemplateDecl(); 8990b57cec5SDimitry Andric if (Template.getAsOverloadedTemplate() || DTN || 9000b57cec5SDimitry Andric isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) { 9010b57cec5SDimitry Andric SourceRange R(TemplateNameLoc, RAngleLoc); 9020b57cec5SDimitry Andric if (SS.getRange().isValid()) 9030b57cec5SDimitry Andric R.setBegin(SS.getRange().getBegin()); 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier) 906*0fca6ea1SDimitry Andric << isa_and_nonnull<VarTemplateDecl>(TD) << Template << R; 9070b57cec5SDimitry Andric NoteAllFoundTemplates(Template); 9080b57cec5SDimitry Andric return true; 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric // We were able to resolve the template name to an actual template. 9120b57cec5SDimitry Andric // Build an appropriate nested-name-specifier. 9130b57cec5SDimitry Andric QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); 9140b57cec5SDimitry Andric if (T.isNull()) 9150b57cec5SDimitry Andric return true; 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric // Alias template specializations can produce types which are not valid 9180b57cec5SDimitry Andric // nested name specifiers. 9190b57cec5SDimitry Andric if (!T->isDependentType() && !T->getAs<TagType>()) { 9200b57cec5SDimitry Andric Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; 9210b57cec5SDimitry Andric NoteAllFoundTemplates(Template); 9220b57cec5SDimitry Andric return true; 9230b57cec5SDimitry Andric } 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andric // Provide source-location information for the template specialization type. 9260b57cec5SDimitry Andric TypeLocBuilder Builder; 9270b57cec5SDimitry Andric TemplateSpecializationTypeLoc SpecTL 9280b57cec5SDimitry Andric = Builder.push<TemplateSpecializationTypeLoc>(T); 9290b57cec5SDimitry Andric SpecTL.setTemplateKeywordLoc(TemplateKWLoc); 9300b57cec5SDimitry Andric SpecTL.setTemplateNameLoc(TemplateNameLoc); 9310b57cec5SDimitry Andric SpecTL.setLAngleLoc(LAngleLoc); 9320b57cec5SDimitry Andric SpecTL.setRAngleLoc(RAngleLoc); 9330b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 9340b57cec5SDimitry Andric SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric 9370b57cec5SDimitry Andric SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), 9380b57cec5SDimitry Andric CCLoc); 9390b57cec5SDimitry Andric return false; 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric namespace { 9430b57cec5SDimitry Andric /// A structure that stores a nested-name-specifier annotation, 9440b57cec5SDimitry Andric /// including both the nested-name-specifier 9450b57cec5SDimitry Andric struct NestedNameSpecifierAnnotation { 9460b57cec5SDimitry Andric NestedNameSpecifier *NNS; 9470b57cec5SDimitry Andric }; 9480b57cec5SDimitry Andric } 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) { 9510b57cec5SDimitry Andric if (SS.isEmpty() || SS.isInvalid()) 9520b57cec5SDimitry Andric return nullptr; 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric void *Mem = Context.Allocate( 9550b57cec5SDimitry Andric (sizeof(NestedNameSpecifierAnnotation) + SS.location_size()), 9560b57cec5SDimitry Andric alignof(NestedNameSpecifierAnnotation)); 9570b57cec5SDimitry Andric NestedNameSpecifierAnnotation *Annotation 9580b57cec5SDimitry Andric = new (Mem) NestedNameSpecifierAnnotation; 9590b57cec5SDimitry Andric Annotation->NNS = SS.getScopeRep(); 9600b57cec5SDimitry Andric memcpy(Annotation + 1, SS.location_data(), SS.location_size()); 9610b57cec5SDimitry Andric return Annotation; 9620b57cec5SDimitry Andric } 9630b57cec5SDimitry Andric 9640b57cec5SDimitry Andric void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr, 9650b57cec5SDimitry Andric SourceRange AnnotationRange, 9660b57cec5SDimitry Andric CXXScopeSpec &SS) { 9670b57cec5SDimitry Andric if (!AnnotationPtr) { 9680b57cec5SDimitry Andric SS.SetInvalid(AnnotationRange); 9690b57cec5SDimitry Andric return; 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric NestedNameSpecifierAnnotation *Annotation 9730b57cec5SDimitry Andric = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr); 9740b57cec5SDimitry Andric SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1)); 9750b57cec5SDimitry Andric } 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 9780b57cec5SDimitry Andric assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 9790b57cec5SDimitry Andric 9800b57cec5SDimitry Andric // Don't enter a declarator context when the current context is an Objective-C 9810b57cec5SDimitry Andric // declaration. 9820b57cec5SDimitry Andric if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext)) 9830b57cec5SDimitry Andric return false; 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric NestedNameSpecifier *Qualifier = SS.getScopeRep(); 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric // There are only two places a well-formed program may qualify a 9880b57cec5SDimitry Andric // declarator: first, when defining a namespace or class member 9890b57cec5SDimitry Andric // out-of-line, and second, when naming an explicitly-qualified 9900b57cec5SDimitry Andric // friend function. The latter case is governed by 9910b57cec5SDimitry Andric // C++03 [basic.lookup.unqual]p10: 9920b57cec5SDimitry Andric // In a friend declaration naming a member function, a name used 9930b57cec5SDimitry Andric // in the function declarator and not part of a template-argument 9940b57cec5SDimitry Andric // in a template-id is first looked up in the scope of the member 9950b57cec5SDimitry Andric // function's class. If it is not found, or if the name is part of 9960b57cec5SDimitry Andric // a template-argument in a template-id, the look up is as 9970b57cec5SDimitry Andric // described for unqualified names in the definition of the class 9980b57cec5SDimitry Andric // granting friendship. 9990b57cec5SDimitry Andric // i.e. we don't push a scope unless it's a class member. 10000b57cec5SDimitry Andric 10010b57cec5SDimitry Andric switch (Qualifier->getKind()) { 10020b57cec5SDimitry Andric case NestedNameSpecifier::Global: 10030b57cec5SDimitry Andric case NestedNameSpecifier::Namespace: 10040b57cec5SDimitry Andric case NestedNameSpecifier::NamespaceAlias: 10050b57cec5SDimitry Andric // These are always namespace scopes. We never want to enter a 10060b57cec5SDimitry Andric // namespace scope from anything but a file context. 10070b57cec5SDimitry Andric return CurContext->getRedeclContext()->isFileContext(); 10080b57cec5SDimitry Andric 10090b57cec5SDimitry Andric case NestedNameSpecifier::Identifier: 10100b57cec5SDimitry Andric case NestedNameSpecifier::TypeSpec: 10110b57cec5SDimitry Andric case NestedNameSpecifier::TypeSpecWithTemplate: 10120b57cec5SDimitry Andric case NestedNameSpecifier::Super: 10130b57cec5SDimitry Andric // These are never namespace scopes. 10140b57cec5SDimitry Andric return true; 10150b57cec5SDimitry Andric } 10160b57cec5SDimitry Andric 10170b57cec5SDimitry Andric llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); 10180b57cec5SDimitry Andric } 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andric bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { 10210b57cec5SDimitry Andric assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric if (SS.isInvalid()) return true; 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric DeclContext *DC = computeDeclContext(SS, true); 10260b57cec5SDimitry Andric if (!DC) return true; 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric // Before we enter a declarator's context, we need to make sure that 10290b57cec5SDimitry Andric // it is a complete declaration context. 10300b57cec5SDimitry Andric if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) 10310b57cec5SDimitry Andric return true; 10320b57cec5SDimitry Andric 10330b57cec5SDimitry Andric EnterDeclaratorContext(S, DC); 10340b57cec5SDimitry Andric 10350b57cec5SDimitry Andric // Rebuild the nested name specifier for the new scope. 10360b57cec5SDimitry Andric if (DC->isDependentContext()) 10370b57cec5SDimitry Andric RebuildNestedNameSpecifierInCurrentInstantiation(SS); 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric return false; 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { 10430b57cec5SDimitry Andric assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); 10440b57cec5SDimitry Andric if (SS.isInvalid()) 10450b57cec5SDimitry Andric return; 10460b57cec5SDimitry Andric assert(!SS.isInvalid() && computeDeclContext(SS, true) && 10470b57cec5SDimitry Andric "exiting declarator scope we never really entered"); 10480b57cec5SDimitry Andric ExitDeclaratorContext(S); 10490b57cec5SDimitry Andric } 1050